1 | import type { BaseQueryApi, BaseQueryFn } from './baseQueryTypes';
|
2 | import type { MaybePromise, Override } from './tsHelpers';
|
3 | export declare type ResponseHandler = 'json' | 'text' | ((response: Response) => Promise<any>);
|
4 | declare type CustomRequestInit = Override<RequestInit, {
|
5 | headers?: Headers | string[][] | Record<string, string | undefined> | undefined;
|
6 | }>;
|
7 | export 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 | export 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 occurred 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 occurred 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 `queryFn` where another error might not make sense.
|
44 | **/
|
45 | status: 'CUSTOM_ERROR';
|
46 | data?: unknown;
|
47 | error: string;
|
48 | };
|
49 | export declare type FetchBaseQueryArgs = {
|
50 | baseUrl?: string;
|
51 | prepareHeaders?: (headers: Headers, api: Pick<BaseQueryApi, 'getState' | 'extra' | 'endpoint' | 'type' | 'forced'>) => MaybePromise<Headers>;
|
52 | fetchFn?: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;
|
53 | paramsSerializer?: (params: Record<string, any>) => string;
|
54 | } & RequestInit;
|
55 | export declare type FetchBaseQueryMeta = {
|
56 | request: Request;
|
57 | response?: Response;
|
58 | };
|
59 | /**
|
60 | * This is a very small wrapper around fetch that aims to simplify requests.
|
61 | *
|
62 | * @example
|
63 | * ```ts
|
64 | * const baseQuery = fetchBaseQuery({
|
65 | * baseUrl: 'https://api.your-really-great-app.com/v1/',
|
66 | * prepareHeaders: (headers, { getState }) => {
|
67 | * const token = (getState() as RootState).auth.token;
|
68 | * // If we have a token set in state, let's assume that we should be passing it.
|
69 | * if (token) {
|
70 | * headers.set('authorization', `Bearer ${token}`);
|
71 | * }
|
72 | * return headers;
|
73 | * },
|
74 | * })
|
75 | * ```
|
76 | *
|
77 | * @param {string} baseUrl
|
78 | * The base URL for an API service.
|
79 | * Typically in the format of https://example.com/
|
80 | *
|
81 | * @param {(headers: Headers, api: { getState: () => unknown; extra: unknown; endpoint: string; type: 'query' | 'mutation'; forced: boolean; }) => Headers} prepareHeaders
|
82 | * An optional function that can be used to inject headers on requests.
|
83 | * Provides a Headers object, as well as most of the `BaseQueryApi` (`dispatch` is not available).
|
84 | * Useful for setting authentication or headers that need to be set conditionally.
|
85 | *
|
86 | * @link https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
87 | *
|
88 | * @param {(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>} fetchFn
|
89 | * Accepts a custom `fetch` function if you do not want to use the default on the window.
|
90 | * Useful in SSR environments if you need to use a library such as `isomorphic-fetch` or `cross-fetch`
|
91 | *
|
92 | * @param {(params: Record<string, unknown>) => string} paramsSerializer
|
93 | * An optional function that can be used to stringify querystring parameters.
|
94 | */
|
95 | export declare function fetchBaseQuery({ baseUrl, prepareHeaders, fetchFn, paramsSerializer, ...baseFetchOptions }?: FetchBaseQueryArgs): BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>;
|
96 | export {};
|