UNPKG

@hyper-fetch/core

Version:

Cache, Queue and Persist your requests no matter if you are online or offline!

199 lines 11.3 kB
import { AdapterInstance, ResponseType } from '../adapter'; import { HttpAdapterType } from '../http-adapter'; import { ClientErrorType, ClientInstance, ClientMode, ClientOptionsType, RequestGenericType, RequestInterceptorType, ResponseInterceptorType } from '.'; import { Cache } from '../cache'; import { Dispatcher } from '../dispatcher'; import { PluginInstance, PluginMethodParameters, PluginMethods } from '../plugin'; import { RequestInstance, RequestJSON, RequestOptionsType, Request } from '../request'; import { LogLevel, AppManager, LoggerManager, RequestManager } from '../managers'; import { EmptyTypes, TypeWithDefaults, ExtractAdapterMethodType, ExtractAdapterOptionsType, ExtractAdapterQueryParamsType, ExtractAdapterEndpointType, ExtractUnionAdapter, HydrateDataType, HydrationOptions, ExtractAdapterDefaultQueryParamsType } from '../types'; /** * **Client** is a class that allows you to configure the connection with the server and then use it to create * requests. It allows you to set global defaults for the requests configuration, query params configuration. * It is also orchestrator for all of the HyperFetch modules like Cache, Dispatcher, AppManager, LoggerManager, * RequestManager and more. */ export declare class Client<GlobalErrorType extends ClientErrorType = Error, Adapter extends AdapterInstance = HttpAdapterType> { options: ClientOptionsType<Client<GlobalErrorType, Adapter>>; readonly url: string; readonly mode: ClientMode; debug: boolean; unstable_onErrorCallbacks: ResponseInterceptorType<ClientInstance>[]; unstable_onSuccessCallbacks: ResponseInterceptorType<ClientInstance>[]; unstable_onResponseCallbacks: ResponseInterceptorType<ClientInstance>[]; unstable_onAuthCallbacks: RequestInterceptorType[]; unstable_onRequestCallbacks: RequestInterceptorType[]; loggerManager: LoggerManager; requestManager: RequestManager; appManager: AppManager; adapter: Adapter; cache: Cache<Adapter>; fetchDispatcher: Dispatcher<Adapter>; submitDispatcher: Dispatcher<Adapter>; isMockerEnabled: boolean; plugins: PluginInstance[]; /** @internal */ unstable_abortKeyMapper: (request: RequestInstance) => string; /** @internal */ unstable_cacheKeyMapper: (request: RequestInstance) => string; /** @internal */ unstable_queryKeyMapper: (request: RequestInstance) => string; /** @internal */ unstable_requestIdMapper: (request: RequestInstance) => string; logger: import('../managers').LoggerMethods; constructor(options: ClientOptionsType<Client<GlobalErrorType, Adapter>>); /** * This method enables the logger usage and display the logs in console */ setDebug: (enabled: boolean) => Client<GlobalErrorType, Adapter>; /** * Set the logger severity of the messages displayed to the console */ setLogLevel: (severity: LogLevel) => Client<GlobalErrorType, Adapter>; /** * Set the new logger instance to the Client */ setLogger: (callback: (Client: ClientInstance) => LoggerManager) => Client<GlobalErrorType, Adapter>; /** * Set globally if mocking should be enabled or disabled for all client requests. * @param isMockerEnabled */ setEnableGlobalMocking: (isMockerEnabled: boolean) => this; /** * Set custom http adapter to handle graphql, rest, firebase or others */ setAdapter: <NewAdapter extends AdapterInstance>(adapter: NewAdapter) => Client<GlobalErrorType, NewAdapter>; /** * Method of manipulating requests before sending the request. We can for example add custom header with token to the request which request had the auth set to true. */ onAuth: (callback: RequestInterceptorType) => Client<GlobalErrorType, Adapter>; /** * Method for removing listeners on auth. * */ removeOnAuthInterceptors: (callbacks: RequestInterceptorType[]) => Client<GlobalErrorType, Adapter>; /** * Method for intercepting error responses. It can be used for example to refresh tokens. */ onError: <ErrorType = null>(callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>) => Client<GlobalErrorType, Adapter>; /** * Method for removing listeners on error. * */ removeOnErrorInterceptors: (callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[]) => Client<GlobalErrorType, Adapter>; /** * Method for intercepting success responses. */ onSuccess: <ErrorType = null>(callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>) => Client<GlobalErrorType, Adapter>; /** * Method for removing listeners on success. * */ removeOnSuccessInterceptors: (callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[]) => Client<GlobalErrorType, Adapter>; /** * Method of manipulating requests before sending the request. */ onRequest: (callback: RequestInterceptorType) => Client<GlobalErrorType, Adapter>; /** * Method for removing listeners on request. * */ removeOnRequestInterceptors: (callbacks: RequestInterceptorType[]) => Client<GlobalErrorType, Adapter>; /** * Method for intercepting any responses. */ onResponse: <ErrorType = null>(callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>) => Client<GlobalErrorType, Adapter>; /** * Method for removing response interceptors. * */ removeOnResponseInterceptors: (callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[]) => Client<GlobalErrorType, Adapter>; /** * Add persistent plugins which trigger on the request lifecycle */ addPlugin: (plugin: PluginInstance) => this; /** * Remove plugins from Client */ removePlugin: (plugin: PluginInstance) => this; triggerPlugins: <Key extends keyof PluginMethods<Client>>(key: Key, data: PluginMethodParameters<Key, Client>) => this; /** * Key setters */ /** Set a custom mapper that generates the abort key used to cancel in-flight requests. */ setAbortKeyMapper: (callback: (request: RequestInstance) => string) => this; /** Set a custom mapper that generates the cache key used to store and retrieve cached responses. */ setCacheKeyMapper: (callback: (request: RequestInstance) => string) => this; /** Set a custom mapper that generates the query key used to identify requests in the dispatcher queue. */ setQueryKeyMapper: (callback: (request: RequestInstance) => string) => this; /** Set a custom mapper that generates the unique request ID for deduplication and tracking. */ setRequestIdMapper: (callback: (request: RequestInstance) => string) => this; /** * Helper used by http adapter to apply the modifications on response error * @private */ unstable_modifyAuth: (request: RequestInstance) => Promise<RequestInstance>; /** * Private helper to run async pre-request processing * @private */ unstable_modifyRequest: (request: RequestInstance) => Promise<RequestInstance>; /** * Private helper to run async on-error response processing * @private */ unstable_modifyErrorResponse: (response: ResponseType<any, GlobalErrorType, Adapter>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType, AdapterInstance>>; /** * Private helper to run async on-success response processing * @private */ unstable_modifySuccessResponse: (response: ResponseType<any, GlobalErrorType, Adapter>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType, AdapterInstance>>; /** * Private helper to run async response processing * @private */ unstable_modifyResponse: (response: ResponseType<any, GlobalErrorType, Adapter>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType, AdapterInstance>>; /** * Reconstruct a Request class instance from its serialized JSON form. * Useful when dispatcher storage serializes queue data (e.g. MMKV, AsyncStorage) * and the deserialized request loses its class identity. */ fromJSON: <RequestProperties extends RequestGenericType<ExtractAdapterQueryParamsType<Adapter>> = {}>(json: RequestJSON<RequestInstance>) => Request<TypeWithDefaults<RequestProperties, "response", undefined>, TypeWithDefaults<RequestProperties, "payload", undefined>, TypeWithDefaults<RequestProperties, "queryParams", ExtractAdapterDefaultQueryParamsType<Adapter>, never>, TypeWithDefaults<RequestProperties, "error", GlobalErrorType>, TypeWithDefaults<RequestProperties, "endpoint", string> extends string ? TypeWithDefaults<RequestProperties, "endpoint", string> : string, Client<GlobalErrorType, Adapter>>; /** * Clears the Client instance and remove all listeners on it's dependencies */ clear: () => void; /** * Hydrate your SSR cache data * @param hydrationData * @param options */ hydrate: (hydrationData: (HydrateDataType | EmptyTypes)[], options?: Partial<HydrationOptions> | ((item: HydrateDataType) => Partial<HydrationOptions>)) => void; /** * Create requests based on the Client setup * * @template Response Your response */ createRequest: <RequestProperties extends RequestGenericType<ExtractAdapterQueryParamsType<Adapter>> = {}>( /** * `createRequest` must be initialized twice(currying). * * ✅ Good: * ```ts * const request = createRequest<RequestProperties>()(params) * ``` * ⛔ Bad: * ```ts * const request = createRequest<RequestProperties>(params) * ``` * * We are using currying to achieve auto generated types for the endpoint string. * * This solution will be removed once https://github.com/microsoft/TypeScript/issues/10571 get resolved. */ _USE_DOUBLE_INITIALIZATION?: never) => <EndpointType extends ExtractAdapterEndpointType<Adapter>, AdapterOptions extends ExtractAdapterOptionsType<Adapter>, MethodType extends ExtractAdapterMethodType<Adapter>>(params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Request<TypeWithDefaults<RequestProperties, "response", undefined>, TypeWithDefaults<RequestProperties, "payload", undefined>, TypeWithDefaults<RequestProperties, "queryParams", ExtractAdapterDefaultQueryParamsType<Adapter>, never>, TypeWithDefaults<RequestProperties, "error", GlobalErrorType>, TypeWithDefaults<RequestProperties, "endpoint", EndpointType> extends string ? TypeWithDefaults<RequestProperties, "endpoint", EndpointType> : any, Client<GlobalErrorType, ExtractUnionAdapter<Adapter, { method: MethodType; options: AdapterOptions; queryParams: TypeWithDefaults<RequestProperties, "queryParams", ExtractAdapterDefaultQueryParamsType<Adapter>, never>; }> extends EmptyTypes ? Adapter : ExtractUnionAdapter<Adapter, { method: MethodType; options: AdapterOptions; queryParams: TypeWithDefaults<RequestProperties, "queryParams", ExtractAdapterDefaultQueryParamsType<Adapter>, never>; }>>, false, false, false, undefined>; } //# sourceMappingURL=client.d.ts.map