mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-29 06:36:41 +08:00
feat: add subpackage @sa/alova
This commit is contained in:
parent
e7799d2db1
commit
b46fd98af2
@ -49,6 +49,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@better-scroll/core": "2.5.1",
|
"@better-scroll/core": "2.5.1",
|
||||||
"@iconify/vue": "4.1.2",
|
"@iconify/vue": "4.1.2",
|
||||||
|
"@sa/alova": "workspace:*",
|
||||||
"@sa/axios": "workspace:*",
|
"@sa/axios": "workspace:*",
|
||||||
"@sa/color": "workspace:*",
|
"@sa/color": "workspace:*",
|
||||||
"@sa/hooks": "workspace:*",
|
"@sa/hooks": "workspace:*",
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
"name": "@sa/alova",
|
"name": "@sa/alova",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./src/index.ts"
|
".": "./src/index.ts",
|
||||||
|
"./client": "./src/client.ts"
|
||||||
},
|
},
|
||||||
"typesVersions": {
|
"typesVersions": {
|
||||||
"*": {
|
"*": {
|
||||||
@ -11,6 +12,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sa/utils": "workspace:*",
|
"@sa/utils": "workspace:*",
|
||||||
"alova": "^3.0.16"
|
"alova": "^3.0.19"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
packages/alova/src/client.ts
Normal file
1
packages/alova/src/client.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from 'alova/client';
|
@ -1,5 +1,2 @@
|
|||||||
/** request id key */
|
|
||||||
export const REQUEST_ID_KEY = 'X-Request-Id';
|
|
||||||
|
|
||||||
/** the backend error code key */
|
/** the backend error code key */
|
||||||
export const BACKEND_ERROR_CODE = 'BACKEND_ERROR';
|
export const BACKEND_ERROR_CODE = 'BACKEND_ERROR';
|
||||||
|
@ -1,86 +1,76 @@
|
|||||||
|
import type { AlovaDefaultCacheAdapter, AlovaGenerics, AlovaGlobalCacheAdapter, AlovaRequestAdapter } from 'alova';
|
||||||
import { createAlova } from 'alova';
|
import { createAlova } from 'alova';
|
||||||
import VueHook from 'alova/vue';
|
import VueHook from 'alova/vue';
|
||||||
|
import type { FetchRequestInit } from 'alova/fetch';
|
||||||
import adapterFetch from 'alova/fetch';
|
import adapterFetch from 'alova/fetch';
|
||||||
import { createServerTokenAuthentication } from 'alova/client';
|
import { createServerTokenAuthentication } from 'alova/client';
|
||||||
import { BACKEND_ERROR_CODE, REQUEST_ID_KEY } from './constant';
|
import { BACKEND_ERROR_CODE } from './constant';
|
||||||
import { isJSON } from './shared';
|
|
||||||
import type { CustomAlovaConfig, RequestOptions } from './type';
|
import type { CustomAlovaConfig, RequestOptions } from './type';
|
||||||
|
|
||||||
export const createAlovaRequest = (customConfig: CustomAlovaConfig<any>, options: RequestOptions<any>) => {
|
export const createAlovaRequest = <
|
||||||
const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication({
|
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<
|
||||||
|
typeof VueHook,
|
||||||
|
AlovaRequestAdapter<RequestConfig, ResponseType, ResponseHeader>
|
||||||
|
>({
|
||||||
refreshTokenOnSuccess: {
|
refreshTokenOnSuccess: {
|
||||||
isExpired: async response => {
|
isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false,
|
||||||
const contentType = response.headers.get('Content-Type');
|
handler: async (response, method) => tokenRefresher?.handler(response, method)
|
||||||
if (isJSON(contentType ?? '')) {
|
|
||||||
const resp = response.clone();
|
|
||||||
const data = await resp.json();
|
|
||||||
const responseCode = String(data.code);
|
|
||||||
if (customConfig.expiredTokenCodes.includes(responseCode)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
handler: async () => {
|
|
||||||
if (options.refreshTokenHandler) {
|
|
||||||
await options.refreshTokenHandler();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
refreshTokenOnError: {
|
refreshTokenOnError: {
|
||||||
isExpired: async response => {
|
isExpired: (response, method) => tokenRefresher?.isExpired(response, method) || false,
|
||||||
const contentType = response.headers.get('Content-Type');
|
handler: async (response, method) => tokenRefresher?.handler(response, method)
|
||||||
if (isJSON(contentType ?? '')) {
|
|
||||||
const resp = response.clone();
|
|
||||||
const data = await resp.json();
|
|
||||||
const responseCode = String(data.code);
|
|
||||||
if (customConfig.expiredTokenCodes.includes(responseCode)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
handler: async () => {
|
|
||||||
if (options.refreshTokenHandler) {
|
|
||||||
await options.refreshTokenHandler();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const instance = createAlova({
|
const instance = createAlova({
|
||||||
...customConfig,
|
...customConfig,
|
||||||
timeout: customConfig.timeout ?? 10 * 1000,
|
timeout: customConfig.timeout ?? 10 * 1000,
|
||||||
requestAdapter: customConfig.requestAdapter ?? adapterFetch(),
|
requestAdapter: (customConfig.requestAdapter as any) ?? adapterFetch(),
|
||||||
statesHook: VueHook,
|
statesHook: VueHook,
|
||||||
beforeRequest: onAuthRequired(options.onRequest),
|
beforeRequest: onAuthRequired(options.onRequest as any),
|
||||||
responded: onResponseRefreshToken({
|
responded: onResponseRefreshToken({
|
||||||
onSuccess: async resp => {
|
onSuccess: async (response, method) => {
|
||||||
// check if http status is success
|
// check if http status is success
|
||||||
if (resp.ok || resp.status === 304) {
|
let error: any = null;
|
||||||
if (
|
let transformedData: any = null;
|
||||||
!isJSON(resp.headers.get('Content-Type') ?? '') ||
|
try {
|
||||||
(options.isBackendSuccess && (await options.isBackendSuccess(resp)))
|
if (await options.isBackendSuccess(response)) {
|
||||||
) {
|
transformedData = await options.transformBackendResponse(response);
|
||||||
return resp;
|
} else {
|
||||||
|
error = new Error('the backend request error');
|
||||||
|
error.code = BACKEND_ERROR_CODE;
|
||||||
}
|
}
|
||||||
if (options.onBackendFail) {
|
} catch (err) {
|
||||||
const fail = await options.onBackendFail(resp);
|
error = err;
|
||||||
if (fail) {
|
|
||||||
return fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return options.transformBackendResponse ? await options.transformBackendResponse(resp) : resp;
|
|
||||||
}
|
}
|
||||||
throw new Error(resp.statusText);
|
|
||||||
|
if (error) {
|
||||||
|
await options.onError?.(error, response, method);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return transformedData;
|
||||||
},
|
},
|
||||||
onComplete: options.onComplete,
|
onComplete: options.onComplete,
|
||||||
onError: options.onError
|
onError: (error, method) => options.onError?.(error, null, method)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { BACKEND_ERROR_CODE, REQUEST_ID_KEY };
|
export { BACKEND_ERROR_CODE };
|
||||||
export type * from './type';
|
export type * from './type';
|
||||||
|
export type * from 'alova';
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
export function isJSON(contentType: string) {
|
|
||||||
return contentType.includes('application/json');
|
|
||||||
}
|
|
@ -1,18 +1,9 @@
|
|||||||
import type {
|
import type { AlovaGenerics, AlovaOptions, AlovaRequestAdapter, Method, ResponseCompleteHandler } from 'alova';
|
||||||
AlovaGenerics,
|
|
||||||
AlovaOptions,
|
|
||||||
AlovaRequestAdapter,
|
|
||||||
Method,
|
|
||||||
ResponseCompleteHandler,
|
|
||||||
ResponseErrorHandler
|
|
||||||
} from 'alova';
|
|
||||||
|
|
||||||
export type CustomAlovaConfig<AG extends AlovaGenerics> = Omit<
|
export type CustomAlovaConfig<AG extends AlovaGenerics> = Omit<
|
||||||
AlovaOptions<AG>,
|
AlovaOptions<AG>,
|
||||||
'statesHook' | 'beforeRequest' | 'responded' | 'requestAdapter'
|
'statesHook' | 'beforeRequest' | 'responded' | 'requestAdapter'
|
||||||
> & {
|
> & {
|
||||||
/** expired token codes */
|
|
||||||
expiredTokenCodes: string[];
|
|
||||||
/** request adapter. all request of alova will be sent by it. */
|
/** request adapter. all request of alova will be sent by it. */
|
||||||
requestAdapter?: AlovaRequestAdapter<AG['RequestConfig'], AG['Response'], AG['ResponseHeader']>;
|
requestAdapter?: AlovaRequestAdapter<AG['RequestConfig'], AG['Response'], AG['ResponseHeader']>;
|
||||||
};
|
};
|
||||||
@ -25,26 +16,23 @@ export interface RequestOptions<AG extends AlovaGenerics> {
|
|||||||
*
|
*
|
||||||
* @param method alova Method Instance
|
* @param method alova Method Instance
|
||||||
*/
|
*/
|
||||||
onRequest?: (method: Method<AG>) => void | Promise<void>;
|
onRequest?: AlovaOptions<AG>['beforeRequest'];
|
||||||
/**
|
/**
|
||||||
* The hook to check backend response is success or not
|
* The hook to check backend response is success or not
|
||||||
*
|
*
|
||||||
* @param response Axios response
|
* @param response alova response
|
||||||
*/
|
*/
|
||||||
isBackendSuccess?: (response: Response) => Promise<boolean>;
|
isBackendSuccess: (response: AG['Response']) => Promise<boolean>;
|
||||||
|
|
||||||
/** The hook to refresh token */
|
/** The config to refresh token */
|
||||||
refreshTokenHandler?: () => Promise<void>;
|
tokenRefresher?: {
|
||||||
/**
|
/** detect the token is expired */
|
||||||
* The hook after backend request fail
|
isExpired(response: AG['Response'], Method: Method<AG>): Promise<boolean> | boolean;
|
||||||
*
|
/** refhresh token handler */
|
||||||
* For example: You can handle the expired token in this hook
|
handler(response: AG['Response'], Method: Method<AG>): Promise<void>;
|
||||||
*
|
};
|
||||||
* @param response Axios response
|
|
||||||
* @param instance Axios instance
|
|
||||||
*/
|
|
||||||
onBackendFail?: (response: Response) => Promise<Response | null> | Promise<void>;
|
|
||||||
|
|
||||||
|
/** The hook after backend request complete */
|
||||||
onComplete?: ResponseCompleteHandler<AG>;
|
onComplete?: ResponseCompleteHandler<AG>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,11 +42,11 @@ export interface RequestOptions<AG extends AlovaGenerics> {
|
|||||||
*
|
*
|
||||||
* @param error
|
* @param error
|
||||||
*/
|
*/
|
||||||
onError?: ResponseErrorHandler<AG>;
|
onError?: (error: any, response: AG['Response'] | null, methodInstance: Method<AG>) => any | Promise<any>;
|
||||||
/**
|
/**
|
||||||
* transform backend response when the responseType is json
|
* transform backend response when the responseType is json
|
||||||
*
|
*
|
||||||
* @param response Axios response
|
* @param response alova response
|
||||||
*/
|
*/
|
||||||
transformBackendResponse?: (response: AG['Response']) => any | Promise<any>;
|
transformBackendResponse: (response: AG['Response']) => any;
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,9 @@ importers:
|
|||||||
'@iconify/vue':
|
'@iconify/vue':
|
||||||
specifier: 4.1.2
|
specifier: 4.1.2
|
||||||
version: 4.1.2(vue@3.5.3(typescript@5.5.4))
|
version: 4.1.2(vue@3.5.3(typescript@5.5.4))
|
||||||
|
'@sa/alova':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:packages/alova
|
||||||
'@sa/axios':
|
'@sa/axios':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:packages/axios
|
version: link:packages/axios
|
||||||
@ -160,6 +163,15 @@ importers:
|
|||||||
specifier: 2.1.6
|
specifier: 2.1.6
|
||||||
version: 2.1.6(typescript@5.5.4)
|
version: 2.1.6(typescript@5.5.4)
|
||||||
|
|
||||||
|
packages/alova:
|
||||||
|
dependencies:
|
||||||
|
'@sa/utils':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../utils
|
||||||
|
alova:
|
||||||
|
specifier: ^3.0.19
|
||||||
|
version: 3.0.20
|
||||||
|
|
||||||
packages/axios:
|
packages/axios:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@sa/utils':
|
'@sa/utils':
|
||||||
@ -275,6 +287,9 @@ importers:
|
|||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
|
'@alova/shared@1.0.5':
|
||||||
|
resolution: {integrity: sha512-/a2Qm+xebQJ1OlIgpslK+UL1J7yhkt1/Mqdq58a22+fSVdANukmUcF4j4w1DF3lxZ04SrqP+2oJprJ8UOvM+9Q==}
|
||||||
|
|
||||||
'@ampproject/remapping@2.3.0':
|
'@ampproject/remapping@2.3.0':
|
||||||
resolution: {integrity: sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==}
|
resolution: {integrity: sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==}
|
||||||
engines: {node: '>=6.0.0'}
|
engines: {node: '>=6.0.0'}
|
||||||
@ -1300,6 +1315,10 @@ packages:
|
|||||||
ajv@6.12.6:
|
ajv@6.12.6:
|
||||||
resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==}
|
resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==}
|
||||||
|
|
||||||
|
alova@3.0.20:
|
||||||
|
resolution: {integrity: sha512-jNrHzktwFXDItoD15LQeptIlYBThmnAoQPSpi/BNW9vqw2h1EMEDXmVIoStKhwg4M6GwnN0oQiTTsbd3y1a4Xw==}
|
||||||
|
engines: {node: '>= 18.0.0'}
|
||||||
|
|
||||||
ansi-colors@4.1.3:
|
ansi-colors@4.1.3:
|
||||||
resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==}
|
resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==}
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
@ -3329,6 +3348,9 @@ packages:
|
|||||||
queue-microtask@1.2.3:
|
queue-microtask@1.2.3:
|
||||||
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
|
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
|
||||||
|
|
||||||
|
rate-limiter-flexible@5.0.3:
|
||||||
|
resolution: {integrity: sha512-lWx2y8NBVlTOLPyqs+6y7dxfEpT6YFqKy3MzWbCy95sTTOhOuxufP2QvRyOHpfXpB9OUJPbVLybw3z3AVAS5fA==}
|
||||||
|
|
||||||
rc9@2.1.2:
|
rc9@2.1.2:
|
||||||
resolution: {integrity: sha512-btXCnMmRIBINM2LDZoEmOogIZU7Qe7zn4BpomSKZ/ykbLObuBdvG+mFq11DL6fjH1DRwHhrlgtYWG96bJiC7Cg==}
|
resolution: {integrity: sha512-btXCnMmRIBINM2LDZoEmOogIZU7Qe7zn4BpomSKZ/ykbLObuBdvG+mFq11DL6fjH1DRwHhrlgtYWG96bJiC7Cg==}
|
||||||
|
|
||||||
@ -4143,6 +4165,8 @@ packages:
|
|||||||
|
|
||||||
snapshots:
|
snapshots:
|
||||||
|
|
||||||
|
'@alova/shared@1.0.5': {}
|
||||||
|
|
||||||
'@ampproject/remapping@2.3.0':
|
'@ampproject/remapping@2.3.0':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@jridgewell/gen-mapping': 0.3.5
|
'@jridgewell/gen-mapping': 0.3.5
|
||||||
@ -5210,6 +5234,11 @@ snapshots:
|
|||||||
json-schema-traverse: 0.4.1
|
json-schema-traverse: 0.4.1
|
||||||
uri-js: 4.4.1
|
uri-js: 4.4.1
|
||||||
|
|
||||||
|
alova@3.0.20:
|
||||||
|
dependencies:
|
||||||
|
'@alova/shared': 1.0.5
|
||||||
|
rate-limiter-flexible: 5.0.3
|
||||||
|
|
||||||
ansi-colors@4.1.3: {}
|
ansi-colors@4.1.3: {}
|
||||||
|
|
||||||
ansi-escapes@7.0.0:
|
ansi-escapes@7.0.0:
|
||||||
@ -7460,6 +7489,8 @@ snapshots:
|
|||||||
|
|
||||||
queue-microtask@1.2.3: {}
|
queue-microtask@1.2.3: {}
|
||||||
|
|
||||||
|
rate-limiter-flexible@5.0.3: {}
|
||||||
|
|
||||||
rc9@2.1.2:
|
rc9@2.1.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
defu: 6.1.4
|
defu: 6.1.4
|
||||||
|
Loading…
Reference in New Issue
Block a user