alovajs/alova

[Bug]: Uniapp的适配器与tokenAuthentication所导出两个方法会产生类型报错

Closed this issue · 0 comments

这是否是一个 Bug?

  • 我已经确认我要报告的是一个 Bug

这个问题是否已经存在?

  • 我已经确认这个 Issue 没有被报告过

Alova 版本

2.21.0

前端框架

Vue

问题描述

这是beforeRequest钩子的

Type 'AlovaBeforeRequest<StatesHook<any, any>, GlobalFetchRequestAdapter>' is not assignable to type '(method: Method<any, any, any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>) => void | Promise<...>'.
  Types of parameters 'method' and 'method' are incompatible.
    Type 'Method<any, any, any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>' is not assignable to type 'Method<any, any, any, any, FetchRequestInit, Response, Headers>'.
      The types of 'context.options.requestAdapter' are incompatible between these types.
        Type 'AlovaRequestAdapter<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>' is not assignable to type 'AlovaRequestAdapter<any, any, FetchRequestInit, Response, Headers>'.
          Type 'RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData' is not assignable to type 'Response'.
            Type 'RequestSuccessCallbackResult' is not assignable to type 'Response'.ts(2322)
index.d.ts(331, 3): The expected type comes from property 'beforeRequest' which is declared here on type 'AlovaOptions<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>'

这是reponded的

Type 'AlovaResponded<StatesHook<any, any>, GlobalFetchRequestAdapter>' is not assignable to type 'ResponsedHandler<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any> | ResponsedHandlerRecord<...> | undefined'.
  Type 'ResponsedHandler<any, any, FetchRequestInit, Response, Headers>' is not assignable to type 'ResponsedHandler<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any> | ResponsedHandlerRecord<...> | undefined'.
    Type 'ResponsedHandler<any, any, FetchRequestInit, Response, Headers>' is not assignable to type 'ResponsedHandler<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>'.
      Type 'RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData' is not assignable to type 'Response'.
        Type 'RequestSuccessCallbackResult' is not assignable to type 'Response'.ts(2322)
index.d.ts(344, 3): The expected type comes from property 'responded' which is declared here on type 'AlovaOptions<any, any, FetchRequestInit, RequestSuccessCallbackResult | UploadFileSuccessCallbackResult | DownloadSuccessData, any>'

期望的表现

无类型报错

复现链接

No response

复现步骤

const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication({
  refreshTokenOnSuccess: {
    // 响应时触发,可获取到response和method,并返回boolean表示token是否过期
    // 当服务端返回401时,表示token过期
    isExpired: (response, method) => {
      return response.status === 401;
    },

    // 当token过期时触发,在此函数中触发刷新token
    handler: async (response, method) => {
      try {
        const { token, refresh_token } = await refreshToken();
        localStorage.setItem('token', token);
        localStorage.setItem('refresh_token', refresh_token);
      } catch (error) {
        // token刷新失败,跳转回登录页
        location.href = '/login';
        // 并抛出错误
        throw error;
      }
    }
  }
});

export default createAlova({
  baseURL: '/api',
  timeout: 50000,
  ...AdapterUniapp({
    mockRequest: mockAdapter,
  }),
  // 该钩子类型报错
  beforeRequest: onAuthRequired((method) => {}),
  // 该钩子类型报错
  responded: onResponseRefreshToken({
    onSuccess: ()=>{},
    onError:()=>{},
    onComplete:()=>{}
  })
}

系统信息

"alova": "^2.21.0",
"@alova/adapter-uniapp": "^1.2.2",
"@alova/scene-vue": "^1.6.1",
"@alova/mock": "^1.5.1",
"@dcloudio/uni-app": "3.0.0-alpha-4010920240606001",

补充说明

No response