|
| 1 | +import { createAlova } from 'alova'; |
| 2 | +import type { AlovaDefaultCacheAdapter, AlovaGenerics, AlovaGlobalCacheAdapter, AlovaRequestAdapter } from 'alova'; |
| 3 | +import VueHook from 'alova/vue'; |
| 4 | +import type { VueHookType } from 'alova/vue'; |
| 5 | +import adapterFetch from 'alova/fetch'; |
| 6 | +import { createServerTokenAuthentication } from 'alova/client'; |
| 7 | +import type { FetchRequestInit } from 'alova/fetch'; |
| 8 | +import { BACKEND_ERROR_CODE } from './constant'; |
| 9 | +import type { CustomAlovaConfig, RequestOptions } from './type'; |
| 10 | + |
| 11 | +export const createAlovaRequest = < |
| 12 | + RequestConfig = FetchRequestInit, |
| 13 | + ResponseType = Response, |
| 14 | + ResponseHeader = Headers, |
| 15 | + L1Cache extends AlovaGlobalCacheAdapter = AlovaDefaultCacheAdapter, |
| 16 | + L2Cache extends AlovaGlobalCacheAdapter = AlovaDefaultCacheAdapter |
| 17 | +>( |
| 18 | + customConfig: CustomAlovaConfig< |
| 19 | + AlovaGenerics<any, any, RequestConfig, ResponseType, ResponseHeader, L1Cache, L2Cache, any> |
| 20 | + >, |
| 21 | + options: RequestOptions<AlovaGenerics<any, any, RequestConfig, ResponseType, ResponseHeader, L1Cache, L2Cache, any>> |
| 22 | +) => { |
| 23 | + const { tokenRefresher } = options; |
| 24 | + const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication< |
| 25 | + VueHookType, |
| 26 | + AlovaRequestAdapter<RequestConfig, ResponseType, ResponseHeader> |
| 27 | + >({ |
| 28 | + refreshTokenOnSuccess: { |
| 29 | + isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false, |
| 30 | + handler: async (response, method) => tokenRefresher?.handler(response, method) |
| 31 | + }, |
| 32 | + refreshTokenOnError: { |
| 33 | + isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false, |
| 34 | + handler: async (response, method) => tokenRefresher?.handler(response, method) |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + const instance = createAlova({ |
| 39 | + ...customConfig, |
| 40 | + timeout: customConfig.timeout ?? 10 * 1000, |
| 41 | + requestAdapter: (customConfig.requestAdapter as any) ?? adapterFetch(), |
| 42 | + statesHook: VueHook, |
| 43 | + beforeRequest: onAuthRequired(options.onRequest as any), |
| 44 | + responded: onResponseRefreshToken({ |
| 45 | + onSuccess: async (response, method) => { |
| 46 | + // check if http status is success |
| 47 | + let error: any = null; |
| 48 | + let transformedData: any = null; |
| 49 | + try { |
| 50 | + if (await options.isBackendSuccess(response)) { |
| 51 | + transformedData = await options.transformBackendResponse(response); |
| 52 | + } else { |
| 53 | + error = new Error('the backend request error'); |
| 54 | + error.code = BACKEND_ERROR_CODE; |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + error = err; |
| 58 | + } |
| 59 | + |
| 60 | + if (error) { |
| 61 | + await options.onError?.(error, response, method); |
| 62 | + throw error; |
| 63 | + } |
| 64 | + |
| 65 | + return transformedData; |
| 66 | + }, |
| 67 | + onComplete: options.onComplete, |
| 68 | + onError: (error, method) => options.onError?.(error, null, method) |
| 69 | + }) |
| 70 | + }); |
| 71 | + |
| 72 | + return instance; |
| 73 | +}; |
| 74 | + |
| 75 | +export { BACKEND_ERROR_CODE }; |
| 76 | +export type * from './type'; |
| 77 | +export type * from 'alova'; |
0 commit comments