UNPKG

5.92 kBTypeScriptView Raw
1import type { BaseQueryApi, BaseQueryFn } from './baseQueryTypes';
2import type { MaybePromise, Override } from './tsHelpers';
3export declare type ResponseHandler = 'content-type' | 'json' | 'text' | ((response: Response) => Promise<any>);
4declare type CustomRequestInit = Override<RequestInit, {
5 headers?: Headers | string[][] | Record<string, string | undefined> | undefined;
6}>;
7export interface FetchArgs extends CustomRequestInit {
8 url: string;
9 params?: Record<string, any>;
10 body?: any;
11 responseHandler?: ResponseHandler;
12 validateStatus?: (response: Response, body: any) => boolean;
13 /**
14 * A number in milliseconds that represents that maximum time a request can take before timing out.
15 */
16 timeout?: number;
17}
18export declare type FetchBaseQueryError = {
19 /**
20 * * `number`:
21 * HTTP status code
22 */
23 status: number;
24 data: unknown;
25} | {
26 /**
27 * * `"FETCH_ERROR"`:
28 * An error that occurred during execution of `fetch` or the `fetchFn` callback option
29 **/
30 status: 'FETCH_ERROR';
31 data?: undefined;
32 error: string;
33} | {
34 /**
35 * * `"PARSING_ERROR"`:
36 * An error happened during parsing.
37 * Most likely a non-JSON-response was returned with the default `responseHandler` "JSON",
38 * or an error occurred while executing a custom `responseHandler`.
39 **/
40 status: 'PARSING_ERROR';
41 originalStatus: number;
42 data: string;
43 error: string;
44} | {
45 /**
46 * * `"TIMEOUT_ERROR"`:
47 * Request timed out
48 **/
49 status: 'TIMEOUT_ERROR';
50 data?: undefined;
51 error: string;
52} | {
53 /**
54 * * `"CUSTOM_ERROR"`:
55 * A custom error type that you can return from your `queryFn` where another error might not make sense.
56 **/
57 status: 'CUSTOM_ERROR';
58 data?: unknown;
59 error: string;
60};
61export declare type FetchBaseQueryArgs = {
62 baseUrl?: string;
63 prepareHeaders?: (headers: Headers, api: Pick<BaseQueryApi, 'getState' | 'extra' | 'endpoint' | 'type' | 'forced'>) => MaybePromise<Headers | void>;
64 fetchFn?: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;
65 paramsSerializer?: (params: Record<string, any>) => string;
66 /**
67 * By default, we only check for 'application/json' and 'application/vnd.api+json' as the content-types for json. If you need to support another format, you can pass
68 * in a predicate function for your given api to get the same automatic stringifying behavior
69 * @example
70 * ```ts
71 * const isJsonContentType = (headers: Headers) => ["application/vnd.api+json", "application/json", "application/vnd.hal+json"].includes(headers.get("content-type")?.trim());
72 * ```
73 */
74 isJsonContentType?: (headers: Headers) => boolean;
75 /**
76 * Defaults to `application/json`;
77 */
78 jsonContentType?: string;
79 /**
80 * Custom replacer function used when calling `JSON.stringify()`;
81 */
82 jsonReplacer?: (this: any, key: string, value: any) => any;
83} & RequestInit & Pick<FetchArgs, 'responseHandler' | 'validateStatus' | 'timeout'>;
84export declare type FetchBaseQueryMeta = {
85 request: Request;
86 response?: Response;
87};
88/**
89 * This is a very small wrapper around fetch that aims to simplify requests.
90 *
91 * @example
92 * ```ts
93 * const baseQuery = fetchBaseQuery({
94 * baseUrl: 'https://api.your-really-great-app.com/v1/',
95 * prepareHeaders: (headers, { getState }) => {
96 * const token = (getState() as RootState).auth.token;
97 * // If we have a token set in state, let's assume that we should be passing it.
98 * if (token) {
99 * headers.set('authorization', `Bearer ${token}`);
100 * }
101 * return headers;
102 * },
103 * })
104 * ```
105 *
106 * @param {string} baseUrl
107 * The base URL for an API service.
108 * Typically in the format of https://example.com/
109 *
110 * @param {(headers: Headers, api: { getState: () => unknown; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }) => Headers} prepareHeaders
111 * An optional function that can be used to inject headers on requests.
112 * Provides a Headers object, as well as most of the `BaseQueryApi` (`dispatch` is not available).
113 * Useful for setting authentication or headers that need to be set conditionally.
114 *
115 * @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
116 *
117 * @param {(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>} fetchFn
118 * Accepts a custom `fetch` function if you do not want to use the default on the window.
119 * Useful in SSR environments if you need to use a library such as `isomorphic-fetch` or `cross-fetch`
120 *
121 * @param {(params: Record<string, unknown>) => string} paramsSerializer
122 * An optional function that can be used to stringify querystring parameters.
123 *
124 * @param {(headers: Headers) => boolean} isJsonContentType
125 * An optional predicate function to determine if `JSON.stringify()` should be called on the `body` arg of `FetchArgs`
126 *
127 * @param {string} jsonContentType Used when automatically setting the content-type header for a request with a jsonifiable body that does not have an explicit content-type header. Defaults to `application/json`.
128 *
129 * @param {(this: any, key: string, value: any) => any} jsonReplacer Custom replacer function used when calling `JSON.stringify()`.
130 *
131 * @param {number} timeout
132 * A number in milliseconds that represents the maximum time a request can take before timing out.
133 */
134export declare function fetchBaseQuery({ baseUrl, prepareHeaders, fetchFn, paramsSerializer, isJsonContentType, jsonContentType, jsonReplacer, timeout: defaultTimeout, validateStatus: globalValidateStatus, ...baseFetchOptions }?: FetchBaseQueryArgs): BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>;
135export {};