import type { AnyAction, ThunkDispatch } from '@reduxjs/toolkit'; import type { RootState } from './core/apiState'; import type { BaseQueryExtraOptions, BaseQueryFn, BaseQueryResult, BaseQueryArg, BaseQueryApi, QueryReturnValue, BaseQueryError, BaseQueryMeta } from './baseQueryTypes'; import type { HasRequiredProps, MaybePromise, OmitFromUnion, CastAny } from './tsHelpers'; import type { NEVER } from './fakeBaseQuery'; declare const resultType: unique symbol; declare const baseQuery: unique symbol; interface EndpointDefinitionWithQuery { /** * `query` can be a function that returns either a `string` or an `object` which is passed to your `baseQuery`. If you are using [fetchBaseQuery](./fetchBaseQuery), this can return either a `string` or an `object` of properties in `FetchArgs`. If you use your own custom [`baseQuery`](../../rtk-query/usage/customizing-queries), you can customize this behavior to your liking. * * @example * * ```ts * // codeblock-meta title="query example" * * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' * interface Post { * id: number * name: string * } * type PostsResponse = Post[] * * const api = createApi({ * baseQuery: fetchBaseQuery({ baseUrl: '/' }), * endpoints: (build) => ({ * getPosts: build.query({ * // highlight-start * query: () => 'posts', * // highlight-end * }) * }) * }) * ``` */ query(arg: QueryArg): BaseQueryArg; queryFn?: never; /** * A function to manipulate the data returned by a query or mutation. */ transformResponse?(baseQueryReturnValue: BaseQueryResult, meta: BaseQueryMeta): ResultType | Promise; } interface EndpointDefinitionWithQueryFn { /** * Can be used in place of `query` as an inline function that bypasses `baseQuery` completely for the endpoint. * * @example * ```ts * // codeblock-meta title="Basic queryFn example" * * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' * interface Post { * id: number * name: string * } * type PostsResponse = Post[] * * const api = createApi({ * baseQuery: fetchBaseQuery({ baseUrl: '/' }), * endpoints: (build) => ({ * getPosts: build.query({ * query: () => 'posts', * }), * flipCoin: build.query<'heads' | 'tails', void>({ * // highlight-start * queryFn(arg, queryApi, extraOptions, baseQuery) { * const randomVal = Math.random() * if (randomVal < 0.45) { * return { data: 'heads' } * } * if (randomVal < 0.9) { * return { data: 'tails' } * } * return { error: { status: 500, statusText: 'Internal Server Error', data: "Coin landed on it's edge!" } } * } * // highlight-end * }) * }) * }) * ``` */ queryFn(arg: QueryArg, api: BaseQueryApi, extraOptions: BaseQueryExtraOptions, baseQuery: (arg: Parameters[0]) => ReturnType): MaybePromise>>; query?: never; transformResponse?: never; } export declare type BaseEndpointDefinition = (([CastAny, {}>] extends [NEVER] ? never : EndpointDefinitionWithQuery) | EndpointDefinitionWithQueryFn) & { [resultType]?: ResultType; [baseQuery]?: BaseQuery; } & HasRequiredProps, { extraOptions: BaseQueryExtraOptions; }, { extraOptions?: BaseQueryExtraOptions; }>; export declare enum DefinitionType { query = "query", mutation = "mutation" } export declare type GetResultDescriptionFn = (result: ResultType | undefined, error: ErrorType | undefined, arg: QueryArg) => ReadonlyArray>; export declare type FullTagDescription = { type: TagType; id?: number | string; }; export declare type TagDescription = TagType | FullTagDescription; export declare type ResultDescription = ReadonlyArray> | GetResultDescriptionFn; /** @deprecated please use `onQueryStarted` instead */ export interface QueryApi { /** @deprecated please use `onQueryStarted` instead */ dispatch: ThunkDispatch; /** @deprecated please use `onQueryStarted` instead */ getState(): RootState; /** @deprecated please use `onQueryStarted` instead */ extra: unknown; /** @deprecated please use `onQueryStarted` instead */ requestId: string; /** @deprecated please use `onQueryStarted` instead */ context: Context; } export interface QueryExtraOptions { type: DefinitionType.query; /** * Used by `query` endpoints. Determines which 'tag' is attached to the cached data returned by the query. * Expects an array of tag type strings, an array of objects of tag types with ids, or a function that returns such an array. * 1. `['Post']` - equivalent to `2` * 2. `[{ type: 'Post' }]` - equivalent to `1` * 3. `[{ type: 'Post', id: 1 }]` * 4. `(result, error, arg) => ['Post']` - equivalent to `5` * 5. `(result, error, arg) => [{ type: 'Post' }]` - equivalent to `4` * 6. `(result, error, arg) => [{ type: 'Post', id: 1 }]` * * @example * * ```ts * // codeblock-meta title="providesTags example" * * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' * interface Post { * id: number * name: string * } * type PostsResponse = Post[] * * const api = createApi({ * baseQuery: fetchBaseQuery({ baseUrl: '/' }), * tagTypes: ['Posts'], * endpoints: (build) => ({ * getPosts: build.query({ * query: () => 'posts', * // highlight-start * providesTags: (result) => * result * ? [ * ...result.map(({ id }) => ({ type: 'Posts' as const, id })), * { type: 'Posts', id: 'LIST' }, * ] * : [{ type: 'Posts', id: 'LIST' }], * // highlight-end * }) * }) * }) * ``` */ providesTags?: ResultDescription>; /** * Not to be used. A query should not invalidate tags in the cache. */ invalidatesTags?: never; } export declare type QueryDefinition = BaseEndpointDefinition & QueryExtraOptions; export interface MutationExtraOptions { type: DefinitionType.mutation; /** * Used by `mutation` endpoints. Determines which cached data should be either re-fetched or removed from the cache. * Expects the same shapes as `providesTags`. * * @example * * ```ts * // codeblock-meta title="invalidatesTags example" * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' * interface Post { * id: number * name: string * } * type PostsResponse = Post[] * * const api = createApi({ * baseQuery: fetchBaseQuery({ baseUrl: '/' }), * tagTypes: ['Posts'], * endpoints: (build) => ({ * getPosts: build.query({ * query: () => 'posts', * providesTags: (result) => * result * ? [ * ...result.map(({ id }) => ({ type: 'Posts' as const, id })), * { type: 'Posts', id: 'LIST' }, * ] * : [{ type: 'Posts', id: 'LIST' }], * }), * addPost: build.mutation>({ * query(body) { * return { * url: `posts`, * method: 'POST', * body, * } * }, * // highlight-start * invalidatesTags: [{ type: 'Posts', id: 'LIST' }], * // highlight-end * }), * }) * }) * ``` */ invalidatesTags?: ResultDescription>; /** * Not to be used. A mutation should not provide tags to the cache. */ providesTags?: never; } export declare type MutationDefinition = BaseEndpointDefinition & MutationExtraOptions; export declare type EndpointDefinition = QueryDefinition | MutationDefinition; export declare type EndpointDefinitions = Record>; export declare function isQueryDefinition(e: EndpointDefinition): e is QueryDefinition; export declare function isMutationDefinition(e: EndpointDefinition): e is MutationDefinition; export declare type EndpointBuilder = { /** * An endpoint definition that retrieves data, and may provide tags to the cache. * * @example * ```js * // codeblock-meta title="Example of all query endpoint options" * const api = createApi({ * baseQuery, * endpoints: (build) => ({ * getPost: build.query({ * query: (id) => ({ url: `post/${id}` }), * // Pick out data and prevent nested properties in a hook or selector * transformResponse: (response) => response.data, * // `result` is the server response * providesTags: (result, error, id) => [{ type: 'Post', id }], * // trigger side effects or optimistic updates * onQueryStarted(id, { dispatch, getState, extra, requestId, queryFulfilled, getCacheEntry, updateCachedData }) {}, * // handle subscriptions etc * onCacheEntryAdded(id, { dispatch, getState, extra, requestId, cacheEntryRemoved, cacheDataLoaded, getCacheEntry, updateCachedData }) {}, * }), * }), *}); *``` */ query(definition: OmitFromUnion, 'type'>): QueryDefinition; /** * An endpoint definition that alters data on the server or will possibly invalidate the cache. * * @example * ```js * // codeblock-meta title="Example of all mutation endpoint options" * const api = createApi({ * baseQuery, * endpoints: (build) => ({ * updatePost: build.mutation({ * query: ({ id, ...patch }) => ({ url: `post/${id}`, method: 'PATCH', body: patch }), * // Pick out data and prevent nested properties in a hook or selector * transformResponse: (response) => response.data, * // `result` is the server response * invalidatesTags: (result, error, id) => [{ type: 'Post', id }], * // trigger side effects or optimistic updates * onQueryStarted(id, { dispatch, getState, extra, requestId, queryFulfilled, getCacheEntry }) {}, * // handle subscriptions etc * onCacheEntryAdded(id, { dispatch, getState, extra, requestId, cacheEntryRemoved, cacheDataLoaded, getCacheEntry }) {}, * }), * }), * }); * ``` */ mutation(definition: OmitFromUnion, 'type'>): MutationDefinition; }; export declare type AssertTagTypes = >(t: T) => T; export declare function calculateProvidedBy(description: ResultDescription | undefined, result: ResultType | undefined, error: ErrorType | undefined, queryArg: QueryArg, assertTagTypes: AssertTagTypes): readonly FullTagDescription[]; export declare type QueryArgFrom> = D extends BaseEndpointDefinition ? QA : unknown; export declare type ResultTypeFrom> = D extends BaseEndpointDefinition ? RT : unknown; export declare type ReducerPathFrom> = D extends EndpointDefinition ? RP : unknown; export declare type TagTypesFrom> = D extends EndpointDefinition ? RP : unknown; export declare type ReplaceTagTypes = { [K in keyof Definitions]: Definitions[K] extends QueryDefinition ? QueryDefinition : Definitions[K] extends MutationDefinition ? MutationDefinition : never; }; export {};