@kode-frontend/session-interceptor
Version:
Session interceptor for handling jwt token update flow
54 lines (48 loc) • 2.04 kB
TypeScript
import { AxiosInstance, InternalAxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';
type Tokens = Record<string, string>;
type ErrorData = {
code: string;
status: number;
};
type BusinessError = {
code: string;
message: string;
};
type THeader = {
key: string;
value: string;
};
type THeadersGetterArg = {
instance: AxiosInstance;
config: InternalAxiosRequestConfig;
};
type TStartHeadersInterceptorArg = {
getHeaders: (arg: THeadersGetterArg) => THeader[];
};
declare const startHeadersInterceptor: ({ getHeaders, }: TStartHeadersInterceptorArg) => (instances: AxiosInstance[]) => {
ejectAll: () => void;
};
type TStorageGetter = (key: string) => string | null;
type TStorageSetter = (key: string, value: string) => void;
type TStorage = {
storageGetter: TStorageGetter;
storageSetter: TStorageSetter;
};
type SessionInterceptorArg<T> = {
invalidAccessTokenErrors: ErrorData[];
invalidRefreshTokenErrors: ErrorData[];
storage: TStorage;
tokensGetter: () => Promise<T>;
onGotNewTokens?: (tokens: T) => void;
onInvalidRefreshResponse: () => void;
onRefreshError?: () => void;
onUnhandledError?: (e: AxiosError) => void;
/** If specified then `invalidAccessTokenErrors` will be ignored */
checkAccessTokenInvalid?: (response: AxiosResponse<any, any>) => boolean;
/** If specified then `invalidRefreshTokenErrors` will be ignored */
checkRefreshTokenInvalid?: (response: AxiosResponse<any, any>) => boolean;
};
declare const startSessionInterceptor: <T extends Tokens>({ invalidAccessTokenErrors, invalidRefreshTokenErrors, storage, tokensGetter, onGotNewTokens, onInvalidRefreshResponse, onRefreshError, onUnhandledError, checkRefreshTokenInvalid, checkAccessTokenInvalid, }: SessionInterceptorArg<T>) => (instances: AxiosInstance[]) => {
ejectAll: () => void;
};
export { type BusinessError, type ErrorData, type THeader, type THeadersGetterArg, type TStorage, type Tokens, startHeadersInterceptor, startSessionInterceptor };