UNPKG

api-registry

Version:

Centralized HTTP API client for the browser and Node.js based on Fetch API

81 lines (69 loc) 3.34 kB
declare class JsonResponse<TResult> extends Response { json(): Promise<TResult>; } type RequestInterceptor = (request: Request, next: () => Promise<Response>) => Promise<Response>; type RequestOptions = RequestInit | (() => Promise<RequestInit>); type PrimitiveValue = string | number | boolean | null; type SimpleObject<T> = { [K in keyof T]: PrimitiveValue; }; declare class JsonEndpointBase { protected readonly _api: JsonApi; protected readonly _path: string; protected readonly _options: RequestOptions[]; protected readonly _interceptors: RequestInterceptor[]; protected _ttl: number; constructor(input: string | JsonEndpointBase, api: JsonApi); protected send(data?: Record<string, unknown>, init?: RequestOptions): Promise<Response>; private getFullUrl; private buildRequest; protected sendAndParse<T>(data?: Record<string, unknown>, init?: RequestOptions): Promise<T>; private reduceOptions; private fetchWithInterceptors; } declare class JsonEndpoint<TResult = void> extends JsonEndpointBase { build(): (requestOptions?: RequestOptions) => Promise<JsonResponse<TResult>>; buildWithParse(): (requestOptions?: RequestOptions) => Promise<TResult>; receives<T extends SimpleObject<T>>(): JsonEndpointWithData<TResult, T>; returns<TNewResult>(): JsonEndpoint<TNewResult>; withOptions(endpointOptions: RequestOptions): JsonEndpoint<TResult>; intercept(interceptor: RequestInterceptor): JsonEndpoint<TResult>; withCache(ttl: number): JsonEndpoint<TResult>; } declare class JsonEndpointWithData<TResult, T extends SimpleObject<T>> extends JsonEndpointBase { build(): (data: T, requestOptions?: RequestOptions) => Promise<JsonResponse<TResult>>; buildWithParse(): (data: T, requestOptions?: RequestOptions) => Promise<TResult>; returns<TNewResult>(): JsonEndpointWithData<TNewResult, T>; withOptions(endpointOptions: RequestOptions): JsonEndpointWithData<TResult, T>; intercept(interceptor: RequestInterceptor): JsonEndpointWithData<TResult, T>; withCache(ttl: number): JsonEndpointWithData<TResult, T>; } type BaseUrl = string | (() => Promise<string>); declare class JsonApi { private readonly _name; private readonly _options; private readonly _interceptors; private _baseURL?; constructor(name: string); get name(): string; getBaseUrl(): Promise<string | undefined>; get baseURL(): BaseUrl | undefined; set baseURL(value: BaseUrl | undefined); get options(): RequestOptions[]; get interceptors(): RequestInterceptor[]; endpoint(url: string, method?: string): JsonEndpoint; withOptions(apiOptions: RequestOptions): JsonApi; intercept(interceptor: RequestInterceptor): JsonApi; } declare class JsonApiRegistry$1 { private readonly _apis; constructor(); api(name: string, baseURL?: BaseUrl, allowChangingBaseURL?: boolean): JsonApi; } declare class JsonResponseError extends Error { readonly response: Response; readonly cause?: Error; constructor(response: Response, message?: string, cause?: Error); } declare const JsonApiRegistry: JsonApiRegistry$1; export { JsonApi, JsonApiRegistry, JsonEndpoint, JsonResponse, JsonResponseError, type RequestInterceptor, type RequestOptions };