-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): merge remote project v1.3.8
- Loading branch information
1 parent
8b95b33
commit e4a4e59
Showing
39 changed files
with
1,775 additions
and
1,489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@sa/alova", | ||
"version": "1.3.8", | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./fetch": "./src/fetch.ts", | ||
"./client": "./src/client.ts", | ||
"./mock": "./src/mock.ts" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": ["./src/*"] | ||
} | ||
}, | ||
"dependencies": { | ||
"@alova/mock": "2.0.8", | ||
"@sa/utils": "workspace:*", | ||
"alova": "3.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from 'alova/client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/** the backend error code key */ | ||
export const BACKEND_ERROR_CODE = 'BACKEND_ERROR'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import adapterFetch from 'alova/fetch'; | ||
export default adapterFetch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { createAlova } from 'alova'; | ||
import type { AlovaDefaultCacheAdapter, AlovaGenerics, AlovaGlobalCacheAdapter, AlovaRequestAdapter } from 'alova'; | ||
import VueHook from 'alova/vue'; | ||
import type { VueHookType } from 'alova/vue'; | ||
import adapterFetch from 'alova/fetch'; | ||
import { createServerTokenAuthentication } from 'alova/client'; | ||
import type { FetchRequestInit } from 'alova/fetch'; | ||
import { BACKEND_ERROR_CODE } from './constant'; | ||
import type { CustomAlovaConfig, RequestOptions } from './type'; | ||
|
||
export const createAlovaRequest = < | ||
RequestConfig = FetchRequestInit, | ||
ResponseType = Response, | ||
ResponseHeader = Headers, | ||
L1Cache extends AlovaGlobalCacheAdapter = AlovaDefaultCacheAdapter, | ||
L2Cache extends AlovaGlobalCacheAdapter = AlovaDefaultCacheAdapter | ||
>( | ||
customConfig: CustomAlovaConfig<AlovaGenerics<any, any, RequestConfig, ResponseType, ResponseHeader, L1Cache, L2Cache, any>>, | ||
options: RequestOptions<AlovaGenerics<any, any, RequestConfig, ResponseType, ResponseHeader, L1Cache, L2Cache, any>> | ||
) => { | ||
const { tokenRefresher } = options; | ||
const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication< | ||
VueHookType, | ||
AlovaRequestAdapter<RequestConfig, ResponseType, ResponseHeader> | ||
>({ | ||
refreshTokenOnSuccess: { | ||
isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false, | ||
handler: async (response, method) => tokenRefresher?.handler(response, method) | ||
}, | ||
refreshTokenOnError: { | ||
isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false, | ||
handler: async (response, method) => tokenRefresher?.handler(response, method) | ||
} | ||
}); | ||
|
||
const instance = createAlova({ | ||
...customConfig, | ||
timeout: customConfig.timeout ?? 10 * 1000, | ||
requestAdapter: (customConfig.requestAdapter as any) ?? adapterFetch(), | ||
statesHook: VueHook, | ||
beforeRequest: onAuthRequired(options.onRequest as any), | ||
responded: onResponseRefreshToken({ | ||
onSuccess: async (response, method) => { | ||
// check if http status is success | ||
let error: any = null; | ||
let transformedData: any = null; | ||
try { | ||
if (await options.isBackendSuccess(response)) { | ||
transformedData = await options.transformBackendResponse(response); | ||
} else { | ||
error = new Error('the backend request error'); | ||
error.code = BACKEND_ERROR_CODE; | ||
} | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
if (error) { | ||
await options.onError?.(error, response, method); | ||
throw error; | ||
} | ||
|
||
return transformedData; | ||
}, | ||
onComplete: options.onComplete, | ||
onError: (error, method) => options.onError?.(error, null, method) | ||
}) | ||
}); | ||
|
||
return instance; | ||
}; | ||
|
||
export { BACKEND_ERROR_CODE }; | ||
export type * from './type'; | ||
export type * from 'alova'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '@alova/mock'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { AlovaGenerics, AlovaOptions, AlovaRequestAdapter, Method, ResponseCompleteHandler } from 'alova'; | ||
|
||
export type CustomAlovaConfig<AG extends AlovaGenerics> = Omit<AlovaOptions<AG>, 'statesHook' | 'beforeRequest' | 'responded' | 'requestAdapter'> & { | ||
/** request adapter. all request of alova will be sent by it. */ | ||
requestAdapter?: AlovaRequestAdapter<AG['RequestConfig'], AG['Response'], AG['ResponseHeader']>; | ||
}; | ||
|
||
export interface RequestOptions<AG extends AlovaGenerics> { | ||
/** | ||
* The hook before request | ||
* | ||
* For example: You can add header token in this hook | ||
* | ||
* @param method alova Method Instance | ||
*/ | ||
onRequest?: AlovaOptions<AG>['beforeRequest']; | ||
/** | ||
* The hook to check backend response is success or not | ||
* | ||
* @param response alova response | ||
*/ | ||
isBackendSuccess: (response: AG['Response']) => Promise<boolean>; | ||
|
||
/** The config to refresh token */ | ||
tokenRefresher?: { | ||
/** detect the token is expired */ | ||
isExpired(response: AG['Response'], Method: Method<AG>): Promise<boolean> | boolean; | ||
/** refresh token handler */ | ||
handler(response: AG['Response'], Method: Method<AG>): Promise<void>; | ||
}; | ||
|
||
/** The hook after backend request complete */ | ||
onComplete?: ResponseCompleteHandler<AG>; | ||
|
||
/** | ||
* The hook to handle error | ||
* | ||
* For example: You can show error message in this hook | ||
* | ||
* @param error | ||
*/ | ||
onError?: (error: any, response: AG['Response'] | null, methodInstance: Method<AG>) => any | Promise<any>; | ||
/** | ||
* transform backend response when the responseType is json | ||
* | ||
* @param response alova response | ||
*/ | ||
transformBackendResponse: (response: AG['Response']) => any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"jsx": "preserve", | ||
"lib": ["DOM", "ESNext"], | ||
"baseUrl": ".", | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"types": ["node"], | ||
"strict": true, | ||
"strictNullChecks": true, | ||
"noUnusedLocals": true, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,6 @@ | |
"qs": "6.13.0" | ||
}, | ||
"devDependencies": { | ||
"@types/qs": "6.9.15" | ||
"@types/qs": "6.9.16" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ | |
} | ||
}, | ||
"dependencies": { | ||
"ofetch": "1.3.4" | ||
"ofetch": "1.4.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.