UNPKG

@selfcommunity/api-services

Version:
118 lines (117 loc) 3.64 kB
import { AxiosInstance, AxiosResponse } from 'axios'; /** * List of all Http methods */ export declare type HttpMethod = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK'; /** * AxiosResponseHeaders interface */ export declare type AxiosResponseHeaders = Record<string, string> & { 'set-cookie'?: string[]; }; /** * General HttpResponse */ export declare type HttpResponse<T = unknown, D = any> = AxiosResponse<T, D>; /** * Interface for the ApiClient */ export interface ApiClientInterface { request<TRequest, TResponse>(config?: any): Promise<HttpResponse<TResponse>>; post<TRequest, TResponse>(path: string, object: TRequest, config?: any): Promise<HttpResponse<TResponse>>; patch<TRequest, TResponse>(path: string, object: TRequest): Promise<HttpResponse<TResponse>>; put<TRequest, TResponse>(path: string, object: TRequest): Promise<HttpResponse<TResponse>>; get<TResponse>(path: string): Promise<HttpResponse<TResponse>>; } /** * Create an abstraction wrapping of axios * Create a sort of interface between axios and the rest of application * This makes it easy for instance, to swap axios out for another package, * should we choose to do so in the future, without it breaking our app. */ export declare class ApiClient implements ApiClientInterface { static DEFAULT_TIMEOUT: number; protected readonly client: AxiosInstance; protected createClient(config?: any): AxiosInstance; constructor(config?: any); /** * Get client instance */ getClientInstance(): AxiosInstance; /** * Change configuration * @param config */ config(config: any): void; /** * Set default header * @param name * @param value * @param methods */ setDefaultHeader: ({ name, value, methods }: { name: string; value: string; methods?: string[]; }) => void; /** * Delete default header * @param name * @param methods */ deleteDefaultHeader: ({ name, methods }: { name: string; methods?: string[]; }) => void; /** * setSupportWithCredentials * Disable/enable withCredentials * Bypass cookie if disabled * @param enable */ setSupportWithCredentials(enable: boolean): void; /** * setBasePortal * Set base path of all http requests * @param portal */ setBasePortal(baseUrl: string): void; /** * setAuthorizeToken * Set authorization header for all http requests * @param token */ setAuthorizeToken(token?: string): void; /** * request wrapper * @param config */ request<TRequest, TResponse>(config: any): Promise<HttpResponse<TResponse>>; /** * get wrapper * @param path * @param config */ get<TResponse>(path: string, config?: any): Promise<HttpResponse<TResponse>>; /** * post wrapper * @param path * @param payload * @param config */ post<TRequest, TResponse>(path: string, payload: TRequest, config?: any): Promise<HttpResponse<TResponse>>; /** * patch wrapper * @param path * @param object */ patch<TRequest, TResponse>(path: string, payload: TRequest): Promise<HttpResponse<TResponse>>; /** * put wrapper * @param path * @param payload */ put<TRequest, TResponse>(path: string, payload: TRequest): Promise<HttpResponse<TResponse>>; } declare const client: ApiClient; export default client;