UNPKG

5.69 kBTypeScriptView Raw
1import type { EndpointDefinitions, QueryDefinition, MutationDefinition, QueryArgFrom, ResultTypeFrom } from '../endpointDefinitions';
2import type { QueryThunk, MutationThunk } from './buildThunks';
3import type { AnyAction, ThunkAction, SerializedError } from '@reduxjs/toolkit';
4import type { QuerySubState, SubscriptionOptions } from './apiState';
5import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs';
6import type { Api } from '../apiTypes';
7import type { BaseQueryError } from '../baseQueryTypes';
8declare module './module' {
9 interface ApiEndpointQuery<Definition extends QueryDefinition<any, any, any, any, any>, Definitions extends EndpointDefinitions> {
10 initiate: StartQueryActionCreator<Definition>;
11 }
12 interface ApiEndpointMutation<Definition extends MutationDefinition<any, any, any, any, any>, Definitions extends EndpointDefinitions> {
13 initiate: StartMutationActionCreator<Definition>;
14 }
15}
16export interface StartQueryActionCreatorOptions {
17 subscribe?: boolean;
18 forceRefetch?: boolean | number;
19 subscriptionOptions?: SubscriptionOptions;
20}
21declare type StartQueryActionCreator<D extends QueryDefinition<any, any, any, any, any>> = (arg: QueryArgFrom<D>, options?: StartQueryActionCreatorOptions) => ThunkAction<QueryActionCreatorResult<D>, any, any, AnyAction>;
22export declare type QueryActionCreatorResult<D extends QueryDefinition<any, any, any, any>> = Promise<QuerySubState<D>> & {
23 arg: QueryArgFrom<D>;
24 requestId: string;
25 subscriptionOptions: SubscriptionOptions | undefined;
26 abort(): void;
27 unsubscribe(): void;
28 refetch(): void;
29 updateSubscriptionOptions(options: SubscriptionOptions): void;
30};
31declare type StartMutationActionCreator<D extends MutationDefinition<any, any, any, any>> = (arg: QueryArgFrom<D>, options?: {
32 /**
33 * If this mutation should be tracked in the store.
34 * If you just want to manually trigger this mutation using `dispatch` and don't care about the
35 * result, state & potential errors being held in store, you can set this to false.
36 * (defaults to `true`)
37 */
38 track?: boolean;
39}) => ThunkAction<MutationActionCreatorResult<D>, any, any, AnyAction>;
40export declare type MutationActionCreatorResult<D extends MutationDefinition<any, any, any, any>> = Promise<{
41 data: ResultTypeFrom<D>;
42} | {
43 error: Exclude<BaseQueryError<D extends MutationDefinition<any, infer BaseQuery, any, any> ? BaseQuery : never>, undefined> | SerializedError;
44}> & {
45 /** @internal */
46 arg: {
47 /**
48 * The name of the given endpoint for the mutation
49 */
50 endpointName: string;
51 /**
52 * The original arguments supplied to the mutation call
53 */
54 originalArgs: QueryArgFrom<D>;
55 /**
56 * Whether the mutation is being tracked in the store.
57 */
58 track?: boolean;
59 };
60 /**
61 * A unique string generated for the request sequence
62 */
63 requestId: string;
64 /**
65 * A method to cancel the mutation promise. Note that this is not intended to prevent the mutation
66 * that was fired off from reaching the server, but only to assist in handling the response.
67 *
68 * Calling `abort()` prior to the promise resolving will force it to reach the error state with
69 * the serialized error:
70 * `{ name: 'AbortError', message: 'Aborted' }`
71 *
72 * @example
73 * ```ts
74 * const [updateUser] = useUpdateUserMutation();
75 *
76 * useEffect(() => {
77 * const promise = updateUser(id);
78 * promise
79 * .unwrap()
80 * .catch((err) => {
81 * if (err.name === 'AbortError') return;
82 * // else handle the unexpected error
83 * })
84 *
85 * return () => {
86 * promise.abort();
87 * }
88 * }, [id, updateUser])
89 * ```
90 */
91 abort(): void;
92 /**
93 * Unwraps a mutation call to provide the raw response/error.
94 *
95 * @remarks
96 * If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
97 *
98 * @example
99 * ```ts
100 * // codeblock-meta title="Using .unwrap"
101 * addPost({ id: 1, name: 'Example' })
102 * .unwrap()
103 * .then((payload) => console.log('fulfilled', payload))
104 * .catch((error) => console.error('rejected', error));
105 * ```
106 *
107 * @example
108 * ```ts
109 * // codeblock-meta title="Using .unwrap with async await"
110 * try {
111 * const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
112 * console.log('fulfilled', payload)
113 * } catch (error) {
114 * console.error('rejected', error);
115 * }
116 * ```
117 */
118 unwrap(): Promise<ResultTypeFrom<D>>;
119 /**
120 * A method to manually unsubscribe from the mutation call, meaning it will be removed from cache after the usual caching grace period.
121 The value returned by the hook will reset to `isUninitialized` afterwards.
122 */
123 unsubscribe(): void;
124};
125export declare function buildInitiate({ serializeQueryArgs, queryThunk, mutationThunk, api, }: {
126 serializeQueryArgs: InternalSerializeQueryArgs;
127 queryThunk: QueryThunk;
128 mutationThunk: MutationThunk;
129 api: Api<any, EndpointDefinitions, any, any>;
130}): {
131 buildInitiateQuery: (endpointName: string, endpointDefinition: QueryDefinition<any, any, any, any>) => StartQueryActionCreator<any>;
132 buildInitiateMutation: (endpointName: string, definition: MutationDefinition<any, any, any, any>) => StartMutationActionCreator<any>;
133};
134export {};