@appello/services
Version:
Services package with api / graphql
33 lines (32 loc) • 1.88 kB
TypeScript
import { AnyObject, Nillable, Nullable, Unidentifiable } from '@appello/common';
import { AxiosError, AxiosInstance, AxiosPromise, AxiosRequestConfig, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
export interface ApiBaseTokens {
accessToken: string;
refreshToken: string;
}
export interface ApiServiceConfig<T extends AnyObject = ApiBaseTokens> {
url: string;
getAccessToken: () => Promise<Nillable<string>> | Nillable<string>;
getRefreshToken: () => Promise<Nillable<string>> | Nillable<string>;
onTokenRefreshSuccess: (tokens: T) => void;
onTokenRefreshError: (error?: unknown) => void;
refreshTokenUrl?: string;
refreshTokens?: (instance: AxiosInstance, error: AxiosError) => Promise<Unidentifiable<Nullable<T>>>;
getAccessTokenFromRefreshRequest?: (data: AnyObject) => string;
axiosConfig?: CreateAxiosDefaults;
}
export interface ApiServiceReturn {
axios: AxiosInstance;
get<Res = unknown, Req = unknown>(url: string, query?: Req, config?: AxiosRequestConfig): AxiosPromise<Res>;
delete<Res = unknown, Req = unknown>(url: string, query?: Req, config?: AxiosRequestConfig): AxiosPromise<Res>;
post<Res = unknown, Req = unknown>(url: string, data?: Req, config?: AxiosRequestConfig): AxiosPromise<Res>;
put<Res = unknown, Req = unknown>(url: string, data?: Req, config?: AxiosRequestConfig): AxiosPromise<Res>;
patch<Res = unknown, Req = unknown>(url: string, data?: Req, config?: AxiosRequestConfig): AxiosPromise<Res>;
request<T = unknown>(config: AxiosRequestConfig): AxiosPromise<T>;
setBaseUrl(url: string): void;
setAxiosConfig(config: AxiosRequestConfig): void;
clone: <U extends AnyObject = ApiBaseTokens>(config: ApiServiceConfig<U>) => ApiServiceReturn;
}
export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
refreshRetry?: boolean;
}