endpointDefinitions.d.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. import type { SerializeQueryArgs } from './defaultSerializeQueryArgs';
  2. import type { QuerySubState, RootState } from './core/apiState';
  3. import type { BaseQueryExtraOptions, BaseQueryFn, BaseQueryResult, BaseQueryArg, BaseQueryApi, QueryReturnValue, BaseQueryError, BaseQueryMeta } from './baseQueryTypes';
  4. import type { HasRequiredProps, MaybePromise, OmitFromUnion, CastAny, NonUndefined, UnwrapPromise } from './tsHelpers';
  5. import type { NEVER } from './fakeBaseQuery';
  6. import type { Api } from '@reduxjs/toolkit/query';
  7. declare const resultType: unique symbol;
  8. declare const baseQuery: unique symbol;
  9. interface EndpointDefinitionWithQuery<QueryArg, BaseQuery extends BaseQueryFn, ResultType> {
  10. /**
  11. * `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.
  12. *
  13. * @example
  14. *
  15. * ```ts
  16. * // codeblock-meta title="query example"
  17. *
  18. * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
  19. * interface Post {
  20. * id: number
  21. * name: string
  22. * }
  23. * type PostsResponse = Post[]
  24. *
  25. * const api = createApi({
  26. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  27. * tagTypes: ['Post'],
  28. * endpoints: (build) => ({
  29. * getPosts: build.query<PostsResponse, void>({
  30. * // highlight-start
  31. * query: () => 'posts',
  32. * // highlight-end
  33. * }),
  34. * addPost: build.mutation<Post, Partial<Post>>({
  35. * // highlight-start
  36. * query: (body) => ({
  37. * url: `posts`,
  38. * method: 'POST',
  39. * body,
  40. * }),
  41. * // highlight-end
  42. * invalidatesTags: [{ type: 'Post', id: 'LIST' }],
  43. * }),
  44. * })
  45. * })
  46. * ```
  47. */
  48. query(arg: QueryArg): BaseQueryArg<BaseQuery>;
  49. queryFn?: never;
  50. /**
  51. * A function to manipulate the data returned by a query or mutation.
  52. */
  53. transformResponse?(baseQueryReturnValue: BaseQueryResult<BaseQuery>, meta: BaseQueryMeta<BaseQuery>, arg: QueryArg): ResultType | Promise<ResultType>;
  54. /**
  55. * A function to manipulate the data returned by a failed query or mutation.
  56. */
  57. transformErrorResponse?(baseQueryReturnValue: BaseQueryError<BaseQuery>, meta: BaseQueryMeta<BaseQuery>, arg: QueryArg): unknown;
  58. /**
  59. * Defaults to `true`.
  60. *
  61. * Most apps should leave this setting on. The only time it can be a performance issue
  62. * is if an API returns extremely large amounts of data (e.g. 10,000 rows per request) and
  63. * you're unable to paginate it.
  64. *
  65. * For details of how this works, please see the below. When it is set to `false`,
  66. * every request will cause subscribed components to rerender, even when the data has not changed.
  67. *
  68. * @see https://redux-toolkit.js.org/api/other-exports#copywithstructuralsharing
  69. */
  70. structuralSharing?: boolean;
  71. }
  72. interface EndpointDefinitionWithQueryFn<QueryArg, BaseQuery extends BaseQueryFn, ResultType> {
  73. /**
  74. * Can be used in place of `query` as an inline function that bypasses `baseQuery` completely for the endpoint.
  75. *
  76. * @example
  77. * ```ts
  78. * // codeblock-meta title="Basic queryFn example"
  79. *
  80. * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
  81. * interface Post {
  82. * id: number
  83. * name: string
  84. * }
  85. * type PostsResponse = Post[]
  86. *
  87. * const api = createApi({
  88. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  89. * endpoints: (build) => ({
  90. * getPosts: build.query<PostsResponse, void>({
  91. * query: () => 'posts',
  92. * }),
  93. * flipCoin: build.query<'heads' | 'tails', void>({
  94. * // highlight-start
  95. * queryFn(arg, queryApi, extraOptions, baseQuery) {
  96. * const randomVal = Math.random()
  97. * if (randomVal < 0.45) {
  98. * return { data: 'heads' }
  99. * }
  100. * if (randomVal < 0.9) {
  101. * return { data: 'tails' }
  102. * }
  103. * return { error: { status: 500, statusText: 'Internal Server Error', data: "Coin landed on it's edge!" } }
  104. * }
  105. * // highlight-end
  106. * })
  107. * })
  108. * })
  109. * ```
  110. */
  111. queryFn(arg: QueryArg, api: BaseQueryApi, extraOptions: BaseQueryExtraOptions<BaseQuery>, baseQuery: (arg: Parameters<BaseQuery>[0]) => ReturnType<BaseQuery>): MaybePromise<QueryReturnValue<ResultType, BaseQueryError<BaseQuery>>>;
  112. query?: never;
  113. transformResponse?: never;
  114. transformErrorResponse?: never;
  115. /**
  116. * Defaults to `true`.
  117. *
  118. * Most apps should leave this setting on. The only time it can be a performance issue
  119. * is if an API returns extremely large amounts of data (e.g. 10,000 rows per request) and
  120. * you're unable to paginate it.
  121. *
  122. * For details of how this works, please see the below. When it is set to `false`,
  123. * every request will cause subscribed components to rerender, even when the data has not changed.
  124. *
  125. * @see https://redux-toolkit.js.org/api/other-exports#copywithstructuralsharing
  126. */
  127. structuralSharing?: boolean;
  128. }
  129. export interface BaseEndpointTypes<QueryArg, BaseQuery extends BaseQueryFn, ResultType> {
  130. QueryArg: QueryArg;
  131. BaseQuery: BaseQuery;
  132. ResultType: ResultType;
  133. }
  134. export type BaseEndpointDefinition<QueryArg, BaseQuery extends BaseQueryFn, ResultType> = (([CastAny<BaseQueryResult<BaseQuery>, {}>] extends [NEVER] ? never : EndpointDefinitionWithQuery<QueryArg, BaseQuery, ResultType>) | EndpointDefinitionWithQueryFn<QueryArg, BaseQuery, ResultType>) & {
  135. [resultType]?: ResultType;
  136. [baseQuery]?: BaseQuery;
  137. } & HasRequiredProps<BaseQueryExtraOptions<BaseQuery>, {
  138. extraOptions: BaseQueryExtraOptions<BaseQuery>;
  139. }, {
  140. extraOptions?: BaseQueryExtraOptions<BaseQuery>;
  141. }>;
  142. export declare enum DefinitionType {
  143. query = "query",
  144. mutation = "mutation"
  145. }
  146. export type GetResultDescriptionFn<TagTypes extends string, ResultType, QueryArg, ErrorType, MetaType> = (result: ResultType | undefined, error: ErrorType | undefined, arg: QueryArg, meta: MetaType) => ReadonlyArray<TagDescription<TagTypes>>;
  147. export type FullTagDescription<TagType> = {
  148. type: TagType;
  149. id?: number | string;
  150. };
  151. export type TagDescription<TagType> = TagType | FullTagDescription<TagType>;
  152. export type ResultDescription<TagTypes extends string, ResultType, QueryArg, ErrorType, MetaType> = ReadonlyArray<TagDescription<TagTypes>> | GetResultDescriptionFn<TagTypes, ResultType, QueryArg, ErrorType, MetaType>;
  153. export interface QueryTypes<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string> extends BaseEndpointTypes<QueryArg, BaseQuery, ResultType> {
  154. /**
  155. * The endpoint definition type. To be used with some internal generic types.
  156. * @example
  157. * ```ts
  158. * const useMyWrappedHook: UseQuery<typeof api.endpoints.query.Types.QueryDefinition> = ...
  159. * ```
  160. */
  161. QueryDefinition: QueryDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  162. TagTypes: TagTypes;
  163. ReducerPath: ReducerPath;
  164. }
  165. export interface QueryExtraOptions<TagTypes extends string, ResultType, QueryArg, BaseQuery extends BaseQueryFn, ReducerPath extends string = string> {
  166. type: DefinitionType.query;
  167. /**
  168. * Used by `query` endpoints. Determines which 'tag' is attached to the cached data returned by the query.
  169. * Expects an array of tag type strings, an array of objects of tag types with ids, or a function that returns such an array.
  170. * 1. `['Post']` - equivalent to `2`
  171. * 2. `[{ type: 'Post' }]` - equivalent to `1`
  172. * 3. `[{ type: 'Post', id: 1 }]`
  173. * 4. `(result, error, arg) => ['Post']` - equivalent to `5`
  174. * 5. `(result, error, arg) => [{ type: 'Post' }]` - equivalent to `4`
  175. * 6. `(result, error, arg) => [{ type: 'Post', id: 1 }]`
  176. *
  177. * @example
  178. *
  179. * ```ts
  180. * // codeblock-meta title="providesTags example"
  181. *
  182. * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
  183. * interface Post {
  184. * id: number
  185. * name: string
  186. * }
  187. * type PostsResponse = Post[]
  188. *
  189. * const api = createApi({
  190. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  191. * tagTypes: ['Posts'],
  192. * endpoints: (build) => ({
  193. * getPosts: build.query<PostsResponse, void>({
  194. * query: () => 'posts',
  195. * // highlight-start
  196. * providesTags: (result) =>
  197. * result
  198. * ? [
  199. * ...result.map(({ id }) => ({ type: 'Posts' as const, id })),
  200. * { type: 'Posts', id: 'LIST' },
  201. * ]
  202. * : [{ type: 'Posts', id: 'LIST' }],
  203. * // highlight-end
  204. * })
  205. * })
  206. * })
  207. * ```
  208. */
  209. providesTags?: ResultDescription<TagTypes, ResultType, QueryArg, BaseQueryError<BaseQuery>, BaseQueryMeta<BaseQuery>>;
  210. /**
  211. * Not to be used. A query should not invalidate tags in the cache.
  212. */
  213. invalidatesTags?: never;
  214. /**
  215. * Can be provided to return a custom cache key value based on the query arguments.
  216. *
  217. * This is primarily intended for cases where a non-serializable value is passed as part of the query arg object and should be excluded from the cache key. It may also be used for cases where an endpoint should only have a single cache entry, such as an infinite loading / pagination implementation.
  218. *
  219. * Unlike the `createApi` version which can _only_ return a string, this per-endpoint option can also return an an object, number, or boolean. If it returns a string, that value will be used as the cache key directly. If it returns an object / number / boolean, that value will be passed to the built-in `defaultSerializeQueryArgs`. This simplifies the use case of stripping out args you don't want included in the cache key.
  220. *
  221. *
  222. * @example
  223. *
  224. * ```ts
  225. * // codeblock-meta title="serializeQueryArgs : exclude value"
  226. *
  227. * import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
  228. * interface Post {
  229. * id: number
  230. * name: string
  231. * }
  232. *
  233. * interface MyApiClient {
  234. * fetchPost: (id: string) => Promise<Post>
  235. * }
  236. *
  237. * createApi({
  238. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  239. * endpoints: (build) => ({
  240. * // Example: an endpoint with an API client passed in as an argument,
  241. * // but only the item ID should be used as the cache key
  242. * getPost: build.query<Post, { id: string; client: MyApiClient }>({
  243. * queryFn: async ({ id, client }) => {
  244. * const post = await client.fetchPost(id)
  245. * return { data: post }
  246. * },
  247. * // highlight-start
  248. * serializeQueryArgs: ({ queryArgs, endpointDefinition, endpointName }) => {
  249. * const { id } = queryArgs
  250. * // This can return a string, an object, a number, or a boolean.
  251. * // If it returns an object, number or boolean, that value
  252. * // will be serialized automatically via `defaultSerializeQueryArgs`
  253. * return { id } // omit `client` from the cache key
  254. *
  255. * // Alternately, you can use `defaultSerializeQueryArgs` yourself:
  256. * // return defaultSerializeQueryArgs({
  257. * // endpointName,
  258. * // queryArgs: { id },
  259. * // endpointDefinition
  260. * // })
  261. * // Or create and return a string yourself:
  262. * // return `getPost(${id})`
  263. * },
  264. * // highlight-end
  265. * }),
  266. * }),
  267. *})
  268. * ```
  269. */
  270. serializeQueryArgs?: SerializeQueryArgs<QueryArg, string | number | boolean | Record<any, any>>;
  271. /**
  272. * Can be provided to merge an incoming response value into the current cache data.
  273. * If supplied, no automatic structural sharing will be applied - it's up to
  274. * you to update the cache appropriately.
  275. *
  276. * Since RTKQ normally replaces cache entries with the new response, you will usually
  277. * need to use this with the `serializeQueryArgs` or `forceRefetch` options to keep
  278. * an existing cache entry so that it can be updated.
  279. *
  280. * Since this is wrapped with Immer, you may either mutate the `currentCacheValue` directly,
  281. * or return a new value, but _not_ both at once.
  282. *
  283. * Will only be called if the existing `currentCacheData` is _not_ `undefined` - on first response,
  284. * the cache entry will just save the response data directly.
  285. *
  286. * Useful if you don't want a new request to completely override the current cache value,
  287. * maybe because you have manually updated it from another source and don't want those
  288. * updates to get lost.
  289. *
  290. *
  291. * @example
  292. *
  293. * ```ts
  294. * // codeblock-meta title="merge: pagination"
  295. *
  296. * import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
  297. * interface Post {
  298. * id: number
  299. * name: string
  300. * }
  301. *
  302. * createApi({
  303. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  304. * endpoints: (build) => ({
  305. * listItems: build.query<string[], number>({
  306. * query: (pageNumber) => `/listItems?page=${pageNumber}`,
  307. * // Only have one cache entry because the arg always maps to one string
  308. * serializeQueryArgs: ({ endpointName }) => {
  309. * return endpointName
  310. * },
  311. * // Always merge incoming data to the cache entry
  312. * merge: (currentCache, newItems) => {
  313. * currentCache.push(...newItems)
  314. * },
  315. * // Refetch when the page arg changes
  316. * forceRefetch({ currentArg, previousArg }) {
  317. * return currentArg !== previousArg
  318. * },
  319. * }),
  320. * }),
  321. *})
  322. * ```
  323. */
  324. merge?(currentCacheData: ResultType, responseData: ResultType, otherArgs: {
  325. arg: QueryArg;
  326. baseQueryMeta: BaseQueryMeta<BaseQuery>;
  327. requestId: string;
  328. fulfilledTimeStamp: number;
  329. }): ResultType | void;
  330. /**
  331. * Check to see if the endpoint should force a refetch in cases where it normally wouldn't.
  332. * This is primarily useful for "infinite scroll" / pagination use cases where
  333. * RTKQ is keeping a single cache entry that is added to over time, in combination
  334. * with `serializeQueryArgs` returning a fixed cache key and a `merge` callback
  335. * set to add incoming data to the cache entry each time.
  336. *
  337. * @example
  338. *
  339. * ```ts
  340. * // codeblock-meta title="forceRefresh: pagination"
  341. *
  342. * import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
  343. * interface Post {
  344. * id: number
  345. * name: string
  346. * }
  347. *
  348. * createApi({
  349. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  350. * endpoints: (build) => ({
  351. * listItems: build.query<string[], number>({
  352. * query: (pageNumber) => `/listItems?page=${pageNumber}`,
  353. * // Only have one cache entry because the arg always maps to one string
  354. * serializeQueryArgs: ({ endpointName }) => {
  355. * return endpointName
  356. * },
  357. * // Always merge incoming data to the cache entry
  358. * merge: (currentCache, newItems) => {
  359. * currentCache.push(...newItems)
  360. * },
  361. * // Refetch when the page arg changes
  362. * forceRefetch({ currentArg, previousArg }) {
  363. * return currentArg !== previousArg
  364. * },
  365. * }),
  366. * }),
  367. *})
  368. * ```
  369. */
  370. forceRefetch?(params: {
  371. currentArg: QueryArg | undefined;
  372. previousArg: QueryArg | undefined;
  373. state: RootState<any, any, string>;
  374. endpointState?: QuerySubState<any>;
  375. }): boolean;
  376. /**
  377. * All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
  378. */
  379. Types?: QueryTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  380. }
  381. export type QueryDefinition<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType> & QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>;
  382. export interface MutationTypes<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string> extends BaseEndpointTypes<QueryArg, BaseQuery, ResultType> {
  383. /**
  384. * The endpoint definition type. To be used with some internal generic types.
  385. * @example
  386. * ```ts
  387. * const useMyWrappedHook: UseMutation<typeof api.endpoints.query.Types.MutationDefinition> = ...
  388. * ```
  389. */
  390. MutationDefinition: MutationDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  391. TagTypes: TagTypes;
  392. ReducerPath: ReducerPath;
  393. }
  394. export interface MutationExtraOptions<TagTypes extends string, ResultType, QueryArg, BaseQuery extends BaseQueryFn, ReducerPath extends string = string> {
  395. type: DefinitionType.mutation;
  396. /**
  397. * Used by `mutation` endpoints. Determines which cached data should be either re-fetched or removed from the cache.
  398. * Expects the same shapes as `providesTags`.
  399. *
  400. * @example
  401. *
  402. * ```ts
  403. * // codeblock-meta title="invalidatesTags example"
  404. * import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
  405. * interface Post {
  406. * id: number
  407. * name: string
  408. * }
  409. * type PostsResponse = Post[]
  410. *
  411. * const api = createApi({
  412. * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  413. * tagTypes: ['Posts'],
  414. * endpoints: (build) => ({
  415. * getPosts: build.query<PostsResponse, void>({
  416. * query: () => 'posts',
  417. * providesTags: (result) =>
  418. * result
  419. * ? [
  420. * ...result.map(({ id }) => ({ type: 'Posts' as const, id })),
  421. * { type: 'Posts', id: 'LIST' },
  422. * ]
  423. * : [{ type: 'Posts', id: 'LIST' }],
  424. * }),
  425. * addPost: build.mutation<Post, Partial<Post>>({
  426. * query(body) {
  427. * return {
  428. * url: `posts`,
  429. * method: 'POST',
  430. * body,
  431. * }
  432. * },
  433. * // highlight-start
  434. * invalidatesTags: [{ type: 'Posts', id: 'LIST' }],
  435. * // highlight-end
  436. * }),
  437. * })
  438. * })
  439. * ```
  440. */
  441. invalidatesTags?: ResultDescription<TagTypes, ResultType, QueryArg, BaseQueryError<BaseQuery>, BaseQueryMeta<BaseQuery>>;
  442. /**
  443. * Not to be used. A mutation should not provide tags to the cache.
  444. */
  445. providesTags?: never;
  446. /**
  447. * All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
  448. */
  449. Types?: MutationTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  450. }
  451. export type MutationDefinition<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType> & MutationExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>;
  452. export type EndpointDefinition<QueryArg, BaseQuery extends BaseQueryFn, TagTypes extends string, ResultType, ReducerPath extends string = string> = QueryDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath> | MutationDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  453. export type EndpointDefinitions = Record<string, EndpointDefinition<any, any, any, any>>;
  454. export declare function isQueryDefinition(e: EndpointDefinition<any, any, any, any>): e is QueryDefinition<any, any, any, any>;
  455. export declare function isMutationDefinition(e: EndpointDefinition<any, any, any, any>): e is MutationDefinition<any, any, any, any>;
  456. export type EndpointBuilder<BaseQuery extends BaseQueryFn, TagTypes extends string, ReducerPath extends string> = {
  457. /**
  458. * An endpoint definition that retrieves data, and may provide tags to the cache.
  459. *
  460. * @example
  461. * ```js
  462. * // codeblock-meta title="Example of all query endpoint options"
  463. * const api = createApi({
  464. * baseQuery,
  465. * endpoints: (build) => ({
  466. * getPost: build.query({
  467. * query: (id) => ({ url: `post/${id}` }),
  468. * // Pick out data and prevent nested properties in a hook or selector
  469. * transformResponse: (response) => response.data,
  470. * // Pick out error and prevent nested properties in a hook or selector
  471. * transformErrorResponse: (response) => response.error,
  472. * // `result` is the server response
  473. * providesTags: (result, error, id) => [{ type: 'Post', id }],
  474. * // trigger side effects or optimistic updates
  475. * onQueryStarted(id, { dispatch, getState, extra, requestId, queryFulfilled, getCacheEntry, updateCachedData }) {},
  476. * // handle subscriptions etc
  477. * onCacheEntryAdded(id, { dispatch, getState, extra, requestId, cacheEntryRemoved, cacheDataLoaded, getCacheEntry, updateCachedData }) {},
  478. * }),
  479. * }),
  480. *});
  481. *```
  482. */
  483. query<ResultType, QueryArg>(definition: OmitFromUnion<QueryDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>, 'type'>): QueryDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  484. /**
  485. * An endpoint definition that alters data on the server or will possibly invalidate the cache.
  486. *
  487. * @example
  488. * ```js
  489. * // codeblock-meta title="Example of all mutation endpoint options"
  490. * const api = createApi({
  491. * baseQuery,
  492. * endpoints: (build) => ({
  493. * updatePost: build.mutation({
  494. * query: ({ id, ...patch }) => ({ url: `post/${id}`, method: 'PATCH', body: patch }),
  495. * // Pick out data and prevent nested properties in a hook or selector
  496. * transformResponse: (response) => response.data,
  497. * // Pick out error and prevent nested properties in a hook or selector
  498. * transformErrorResponse: (response) => response.error,
  499. * // `result` is the server response
  500. * invalidatesTags: (result, error, id) => [{ type: 'Post', id }],
  501. * // trigger side effects or optimistic updates
  502. * onQueryStarted(id, { dispatch, getState, extra, requestId, queryFulfilled, getCacheEntry }) {},
  503. * // handle subscriptions etc
  504. * onCacheEntryAdded(id, { dispatch, getState, extra, requestId, cacheEntryRemoved, cacheDataLoaded, getCacheEntry }) {},
  505. * }),
  506. * }),
  507. * });
  508. * ```
  509. */
  510. mutation<ResultType, QueryArg>(definition: OmitFromUnion<MutationDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>, 'type'>): MutationDefinition<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>;
  511. };
  512. export type AssertTagTypes = <T extends FullTagDescription<string>>(t: T) => T;
  513. export declare function calculateProvidedBy<ResultType, QueryArg, ErrorType, MetaType>(description: ResultDescription<string, ResultType, QueryArg, ErrorType, MetaType> | undefined, result: ResultType | undefined, error: ErrorType | undefined, queryArg: QueryArg, meta: MetaType | undefined, assertTagTypes: AssertTagTypes): readonly FullTagDescription<string>[];
  514. export declare function expandTagDescription(description: TagDescription<string>): FullTagDescription<string>;
  515. export type QueryArgFrom<D extends BaseEndpointDefinition<any, any, any>> = D extends BaseEndpointDefinition<infer QA, any, any> ? QA : unknown;
  516. export type ResultTypeFrom<D extends BaseEndpointDefinition<any, any, any>> = D extends BaseEndpointDefinition<any, any, infer RT> ? RT : unknown;
  517. export type ReducerPathFrom<D extends EndpointDefinition<any, any, any, any, any>> = D extends EndpointDefinition<any, any, any, any, infer RP> ? RP : unknown;
  518. export type TagTypesFrom<D extends EndpointDefinition<any, any, any, any>> = D extends EndpointDefinition<any, any, infer RP, any> ? RP : unknown;
  519. export type TagTypesFromApi<T> = T extends Api<any, any, any, infer TagTypes> ? TagTypes : never;
  520. export type DefinitionsFromApi<T> = T extends Api<any, infer Definitions, any, any> ? Definitions : never;
  521. export type TransformedResponse<NewDefinitions extends EndpointDefinitions, K, ResultType> = K extends keyof NewDefinitions ? NewDefinitions[K]['transformResponse'] extends undefined ? ResultType : UnwrapPromise<ReturnType<NonUndefined<NewDefinitions[K]['transformResponse']>>> : ResultType;
  522. export type OverrideResultType<Definition, NewResultType> = Definition extends QueryDefinition<infer QueryArg, infer BaseQuery, infer TagTypes, any, infer ReducerPath> ? QueryDefinition<QueryArg, BaseQuery, TagTypes, NewResultType, ReducerPath> : Definition extends MutationDefinition<infer QueryArg, infer BaseQuery, infer TagTypes, any, infer ReducerPath> ? MutationDefinition<QueryArg, BaseQuery, TagTypes, NewResultType, ReducerPath> : never;
  523. export type UpdateDefinitions<Definitions extends EndpointDefinitions, NewTagTypes extends string, NewDefinitions extends EndpointDefinitions> = {
  524. [K in keyof Definitions]: Definitions[K] extends QueryDefinition<infer QueryArg, infer BaseQuery, any, infer ResultType, infer ReducerPath> ? QueryDefinition<QueryArg, BaseQuery, NewTagTypes, TransformedResponse<NewDefinitions, K, ResultType>, ReducerPath> : Definitions[K] extends MutationDefinition<infer QueryArg, infer BaseQuery, any, infer ResultType, infer ReducerPath> ? MutationDefinition<QueryArg, BaseQuery, NewTagTypes, TransformedResponse<NewDefinitions, K, ResultType>, ReducerPath> : never;
  525. };
  526. export {};