UNPKG

hapic

Version:

A http api client based on axios.

130 lines (129 loc) 4.12 kB
import { ResponseType } from './constants'; import type { HookErrorFn, HookFn, HookReqFn, HookResFn } from './hook'; import type { RequestBaseOptions, RequestOptions } from './request'; import type { Response, ResponseData } from './response'; import { HookManager, HookName } from './hook'; import type { AuthorizationHeader } from './header'; export declare class Client { readonly '@instanceof': symbol; defaults: RequestBaseOptions; protected headers: Headers; protected hookManager: HookManager; constructor(input?: RequestBaseOptions); /** * Return base url * * @return string */ getBaseURL(): string | undefined; /** * Overwrite existing base url. * * @param url */ setBaseURL(url: string): this; /** * Set a header for all upcoming requests. * * @param key * @param value */ setHeader(key: string, value: any): this; /** * Get a header for all upcoming requests. * * @param key */ getHeader(key: string): string | null; /** * Unset a specific for all upcoming requests. * * @param key */ unsetHeader(key: string): this; /** * Unset all defined headers for the upcoming requests. */ unsetHeaders(): this; /** * Set an authorization header (basic, api-key, bearer). * * @param options */ setAuthorizationHeader(options: AuthorizationHeader): this; /** * Get authorization header. */ getAuthorizationHeader(): string | undefined; /** * Unset an authorization header. */ unsetAuthorizationHeader(): this; /** * Make a custom request. * * @param config */ request<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(config: RequestOptions<RT>): Promise<R>; /** * Request a resource with the GET method. * * @param url * @param config */ get<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, config?: RequestBaseOptions<RT>): Promise<R>; /** * Delete a resource with the DELETE method. * * @param url * @param config */ delete<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, config?: RequestBaseOptions<RT>): Promise<R>; /** * Make a verification resource with the HEAD method. * * @param url * @param config */ head<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, config?: RequestBaseOptions<RT>): Promise<R>; /** * Create a resource with the POST method. * * @param url * @param body * @param config */ post<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, body?: any, config?: RequestBaseOptions<RT>): Promise<R>; /** * Update a resource with the PUT method. * * @param url * @param body * @param config */ put<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, body?: any, config?: RequestBaseOptions<RT>): Promise<R>; /** * Update a resource with the PATCH method. * * @param url * @param body * @param config */ patch<T = any, RT extends `${ResponseType}` = `${ResponseType.JSON}`, R = Response<ResponseData<RT, T>>>(url: string, body?: any, config?: RequestBaseOptions<RT>): Promise<R>; /** * Register a hook fn. * * @param name * @param fn */ on(name: `${HookName.REQUEST}`, fn: HookReqFn): number; on(name: `${HookName.RESPONSE}`, fn: HookResFn): number; on(name: `${HookName.RESPONSE_ERROR}` | `${HookName.REQUEST_ERROR}`, fn: HookErrorFn): number; /** * Remove single or specific hook fn(s). * * @param name * @param fn */ off(name: `${HookName}`, fn?: HookFn | number): this; }