import { AlovaGenerics, AlovaOptions, AlovaRequestAdapter, Method, StatesHook } from 'alova'; import adapterFetch from 'alova/fetch'; /** * Unifiedly obtain the type of AlovaRequestAdapter */ export type AlovaRequestAdapterUnified< RA extends | AlovaRequestAdapter | ((...args: any[]) => AlovaRequestAdapter) = AlovaRequestAdapter > = RA extends AlovaRequestAdapter ? RA : ReturnType; // transform types StateHook and AlovaRequestAdapter to an AlovaGenerics export type StateHookAdapter2AG, RA extends AlovaRequestAdapter> = Parameters[1] extends Method> ? AlovaGenerics ? SE : unknown> : never; export type AlovaResponded, RA extends AlovaRequestAdapter> = NonNullable< AlovaOptions>['responded'] >; export type MetaMatches = Record; export type ResponseInterceptHandler, RESULT = Promise> = ( response: ReturnType['response']> extends Promise ? RE : never, method: Parameters[1] ) => RESULT; export type ResponseErrorInterceptHandler, RESULT = Promise> = ( error: any, method: Parameters[1] ) => RESULT; export type ResponseAuthorizationInterceptor> = | ResponseInterceptHandler> | { metaMatches?: MetaMatches; handler: ResponseInterceptHandler>; }; export type RequestHandler, RESULT = Promise> = ( method: Parameters[1] ) => RESULT; export interface TokenAuthenticationOptions> { /** * Ignore intercepted methods */ visitorMeta?: MetaMatches; /** * Login request interceptor */ login?: ResponseAuthorizationInterceptor; /** * Logout request interceptor */ logout?: ResponseAuthorizationInterceptor; /** * Assign token callback function, requests for login ID and visitor ID will not trigger this function * @param method method instance */ assignToken?: (method: Method) => void | Promise; } export interface ClientTokenAuthenticationOptions> extends TokenAuthenticationOptions { /** * Determine whether the token has expired in the interceptor before the request, and refresh the token */ refreshToken?: { /** * Determine whether the token has expired */ isExpired: RequestHandler>; /** * Refresh token */ handler: RequestHandler; /** * Customize the method meta that matches the refresh token */ metaMatches?: MetaMatches; }; } export type AlovaBeforeRequest, RA extends AlovaRequestAdapter> = ( method: Method> ) => void | Promise; type BeforeRequestType, RA extends AlovaRequestAdapter> = ( originalBeforeRequest?: AlovaBeforeRequest ) => AlovaBeforeRequest; type ResponseType, RA extends AlovaRequestAdapter> = ( originalResponded?: AlovaResponded ) => AlovaResponded; export interface TokenAuthenticationResult, RA extends AlovaRequestAdapter> { onAuthRequired: BeforeRequestType; onResponseRefreshToken: ResponseType; waitingList: { method: Method>; resolve: () => void; }[]; } export interface ServerTokenAuthenticationOptions> extends TokenAuthenticationOptions { /** * Determine whether the token has expired in the request success interceptor and refresh the token */ refreshTokenOnSuccess?: { /** * Determine whether the token has expired */ isExpired: ResponseInterceptHandler>; /** * Refresh token */ handler: ResponseInterceptHandler; /** * Customize the method meta that matches the refresh token */ metaMatches?: MetaMatches; }; /** * Determine whether the token has expired in the request failure interceptor and refresh the token */ refreshTokenOnError?: { /** * Determine whether the token has expired */ isExpired: ResponseErrorInterceptHandler>; /** * Refresh token */ handler: ResponseErrorInterceptHandler; /** * Customize the method meta that matches the refresh token */ metaMatches?: MetaMatches; }; } /** * Create a client-side token authentication interceptor * @example * ```js * const { onAuthRequired, onResponseRefreshToken } = createClientTokenAuthentication(\/* ... *\/); * const alova = createAlova({ * // ... * beforeRequest: onAuthRequired(method => { * // ... * }), * responded: onResponseRefreshToken({ * onSuccess(response, method) { * // ... * }, * onError(error, method) { * // ... * }, * }) * }); * ``` * @param options Configuration parameters * @returns token authentication interceptor function */ export declare function createClientTokenAuthentication< SH extends StatesHook, RA extends | AlovaRequestAdapter | ((...args: any[]) => AlovaRequestAdapter) = typeof adapterFetch >( options: ClientTokenAuthenticationOptions> ): TokenAuthenticationResult>; /** * Create a server-side token authentication interceptor * @example * ```js * const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthentication(\/* ... *\/); * const alova = createAlova({ * // ... * beforeRequest: onAuthRequired(method => { * // ... * }), * responded: onResponseRefreshToken({ * onSuccess(response, method) { * // ... * }, * onError(error, method) { * // ... * }, * }) * }); * ``` * @param options Configuration parameters * @returns token authentication interceptor function */ export declare function createServerTokenAuthentication< SH extends StatesHook, RA extends | AlovaRequestAdapter | ((...args: any[]) => AlovaRequestAdapter) = typeof adapterFetch >( options: ServerTokenAuthenticationOptions> ): TokenAuthenticationResult>;