UNPKG

3.51 kBTypeScriptView Raw
1import type { BaseQueryFn } from './baseQueryTypes';
2import type { MaybePromise, Override } from './tsHelpers';
3export declare type ResponseHandler = '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}
14export declare type FetchBaseQueryError = {
15 /**
16 * * `number`:
17 * HTTP status code
18 */
19 status: number;
20 data: unknown;
21} | {
22 /**
23 * * `"FETCH_ERROR"`:
24 * An error that occured during execution of `fetch` or the `fetchFn` callback option
25 **/
26 status: 'FETCH_ERROR';
27 data?: undefined;
28 error: string;
29} | {
30 /**
31 * * `"PARSING_ERROR"`:
32 * An error happened during parsing.
33 * Most likely a non-JSON-response was returned with the default `responseHandler` "JSON",
34 * or an error occured while executing a custom `responseHandler`.
35 **/
36 status: 'PARSING_ERROR';
37 originalStatus: number;
38 data: string;
39 error: string;
40} | {
41 /**
42 * * `"CUSTOM_ERROR"`:
43 * A custom error type that you can return from your `fetchFn` where another error might not make sense.
44 **/
45 status: 'CUSTOM_ERROR';
46 data?: unknown;
47 error: string;
48};
49export declare type FetchBaseQueryArgs = {
50 baseUrl?: string;
51 prepareHeaders?: (headers: Headers, api: {
52 getState: () => unknown;
53 }) => MaybePromise<Headers>;
54 fetchFn?: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;
55} & RequestInit;
56export declare type FetchBaseQueryMeta = {
57 request: Request;
58 response?: Response;
59};
60/**
61 * This is a very small wrapper around fetch that aims to simplify requests.
62 *
63 * @example
64 * ```ts
65 * const baseQuery = fetchBaseQuery({
66 * baseUrl: 'https://api.your-really-great-app.com/v1/',
67 * prepareHeaders: (headers, { getState }) => {
68 * const token = (getState() as RootState).auth.token;
69 * // If we have a token set in state, let's assume that we should be passing it.
70 * if (token) {
71 * headers.set('authorization', `Bearer ${token}`);
72 * }
73 * return headers;
74 * },
75 * })
76 * ```
77 *
78 * @param {string} baseUrl
79 * The base URL for an API service.
80 * Typically in the format of http://example.com/
81 *
82 * @param {(headers: Headers, api: { getState: () => unknown }) => Headers} prepareHeaders
83 * An optional function that can be used to inject headers on requests.
84 * Provides a Headers object, as well as the `getState` function from the
85 * redux store. Can be useful for authentication.
86 *
87 * @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
88 *
89 * @param {(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>} fetchFn
90 * Accepts a custom `fetch` function if you do not want to use the default on the window.
91 * Useful in SSR environments if you need to use a library such as `isomorphic-fetch` or `cross-fetch`
92 *
93 */
94export declare function fetchBaseQuery({ baseUrl, prepareHeaders, fetchFn, ...baseFetchOptions }?: FetchBaseQueryArgs): BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>;
95export {};