UNPKG

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