@hyper-fetch/core
Version:
Cache, Queue and Persist your requests no matter if you are online or offline!
144 lines • 9.83 kB
TypeScript
import { EmptyTypes, ExtendRequest } from '../types';
import { EndpointMapper, AdapterFetcherType, AdapterPayloadMappingType, HeaderMappingType, QueryParamsMapper, QueryParamsType, RequestResponseType } from './adapter.types';
import { RequestInstance, RequestOptionsType } from '../request';
import { Client, ClientInstance } from '../client';
import { LoggerMethods } from '../managers';
import { getErrorMessage } from './adapter.utils';
export type DefaultMapperType = <V, C>(value: V, config: C) => V;
export declare const defaultMapper: DefaultMapperType;
/**
* Base adapter class responsible for executing HTTP requests. Adapters handle endpoint resolution,
* header/payload/query-params mapping, and the actual fetch call. Extend or configure this class
* to integrate custom transport layers (e.g., axios, graphql, firebase).
*/
export declare class Adapter<AdapterOptions, MethodType extends string, StatusType extends number | string, Extra extends Record<string, any>, QueryParams = QueryParamsType | string | EmptyTypes, DefaultQueryParams = undefined, EndpointType = string, EndpointMapperType extends EndpointMapper<EndpointType> | DefaultMapperType = DefaultMapperType, QueryParamsMapperType extends QueryParamsMapper<QueryParams> | DefaultMapperType = DefaultMapperType, HeaderMapperType extends HeaderMappingType | DefaultMapperType = DefaultMapperType, PayloadMapperType extends AdapterPayloadMappingType | DefaultMapperType = DefaultMapperType> {
options: {
name: string;
defaultMethod: MethodType;
defaultExtra: Extra;
systemErrorStatus: StatusType;
systemErrorExtra: Extra;
defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>;
};
/** Fetching function */
unstable_fetcher: AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>;
/**
* ********************
* Defaults
* ********************
*/
name: string;
defaultMethod: MethodType;
defaultExtra: Extra;
systemErrorStatus: StatusType;
systemErrorExtra: Extra;
defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>;
logger: LoggerMethods;
initialized: boolean;
client: ClientInstance;
unstable_onInitializeCallback?: (options: {
client: ClientInstance;
}) => void;
unstable_queryParamsMapperConfig: Parameters<QueryParamsMapperType>[1];
unstable_headerMapperConfig: Parameters<HeaderMapperType>[1];
unstable_payloadMapperConfig: Parameters<PayloadMapperType>[1];
unstable_endpointMapperConfig: Parameters<EndpointMapperType>[1];
constructor(options: {
name: string;
defaultMethod: MethodType;
defaultExtra: Extra;
systemErrorStatus: StatusType;
systemErrorExtra: Extra;
defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>;
});
/** Initialize the adapter with a client reference and set up logging. Called automatically when the client is created. */
initialize: (client: ClientInstance) => this;
/** Register a callback invoked once the adapter is initialized with a client. */
onInitialize: (callback: (options: {
client: ClientInstance;
}) => void) => this;
/**
* ********************
* Options Setters
* ********************
*/
unstable_internalErrorMapping: (error: ReturnType<typeof getErrorMessage>) => any;
/** Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats. */
unstable_headerMapper: HeaderMapperType;
/** Method to get request data and transform them to the required format. It handles FormData and JSON by default. */
unstable_payloadMapper: PayloadMapperType;
/** Method to get the endpoint for the adapter request. */
unstable_endpointMapper: EndpointMapperType;
/** Method to get request data and transform them to the required format. */
unstable_queryParamsMapper: QueryParamsMapperType;
/** Get default adapter options for the request. */
unstable_getAdapterDefaults?: (request: ExtendRequest<RequestInstance, {
client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>;
}>) => AdapterOptions;
/** Get default request options for the request. */
unstable_getRequestDefaults?: (options: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Partial<RequestOptionsType<EndpointType, AdapterOptions, MethodType>>;
/**
* Get formatted endpoint name of the request.
* Helpful in displaying long endpoints like in case of graphql schemas etc.
*/
unstable_devtoolsEndpointGetter: (endpoint: string) => string;
/**
* ********************
* Methods
* ********************
*/
/** Set the default HTTP method used when a request does not specify one. */
setDefaultMethod: (method: MethodType) => this;
/** Set the default extra metadata attached to every request response (e.g., headers, status). */
setDefaultExtra: (extra: Extra) => this;
/** Set a function that formats the endpoint string for display in devtools. */
setDevtoolsEndpointGetter: (callback: (endpoint: string) => string) => this;
/**
* This method allows to configure global defaults for the request configuration like method, auth, deduplication etc.
*/
setRequestDefaults: (callback: typeof this.unstable_getRequestDefaults) => this;
/**
* Set the adapter default options added to every sent request
*/
setAdapterDefaults: (callback: (request: ExtendRequest<RequestInstance, {
client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>;
}>) => AdapterOptions) => this;
/** Set a custom mapping for internal adapter errors before they are returned in the response. */
setInternalErrorMapping: (callback: (error: ReturnType<typeof getErrorMessage>) => any) => this;
/**
* Set the custom header mapping function
*/
setHeaderMapper: <NewMapper extends HeaderMappingType>(headerMapper: NewMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, NewMapper, PayloadMapperType>;
/**
* Set the request payload mapping function which gets triggered before request is send
*/
setPayloadMapper: <NewMapper extends AdapterPayloadMappingType>(payloadMapper: NewMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, NewMapper>;
/**
* Set the endpoint mapping function which transforms the endpoint string before the request is sent
*/
setEndpointMapper: <NewEndpointMapper extends EndpointMapper<EndpointType>>(endpointMapper: NewEndpointMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, NewEndpointMapper, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
/**
* Set the query params mapping function which get triggered before request get sent
*/
setQueryParamsMapper: <NewQueryParamsMapper extends QueryParamsMapper<QueryParams>>(queryParamsMapper: NewQueryParamsMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, NewQueryParamsMapper, HeaderMapperType, PayloadMapperType>;
/** Set the configuration object passed as the second argument to the query params mapper. */
setQueryParamsMapperConfig: <NewQueryParamsMapperConfig extends Parameters<QueryParamsMapperType>[1]>(config: NewQueryParamsMapperConfig) => this;
/** Set the configuration object passed as the second argument to the header mapper. */
setHeaderMapperConfig: <NewHeaderMapperConfig extends Parameters<HeaderMapperType>[1]>(config: NewHeaderMapperConfig) => this;
/** Set the configuration object passed as the second argument to the endpoint mapper. */
setEndpointMapperConfig: <NewEndpointMapperConfig extends Parameters<EndpointMapperType>[1]>(config: NewEndpointMapperConfig) => this;
/** Set the configuration object passed as the second argument to the payload mapper. */
setPayloadMapperConfig: <NewPayloadMapperConfig extends Parameters<PayloadMapperType>[1]>(config: NewPayloadMapperConfig) => this;
/**
* ********************
* Fetching
* ********************
*/
/** Set the fetcher function that performs the actual network request. Must be called before any request can be executed. */
setFetcher(fetcher: AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>): this;
/** Execute a request through the adapter pipeline: maps endpoint/headers/payload, runs plugins, then calls the fetcher. */
fetch(request: ExtendRequest<RequestInstance, {
client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>;
}>, requestId: string): Promise<RequestResponseType<RequestInstance>>;
}
//# sourceMappingURL=adapter.d.ts.map