1
1
import { InternalSerializeQueryArgs } from '.' ;
2
- import { Api , ApiEndpointQuery , BaseQueryFn , BaseQueryArg } from './apiTypes' ;
2
+ import { Api , ApiEndpointQuery , BaseQueryFn , BaseQueryArg , BaseQueryError } from './apiTypes' ;
3
3
import { InternalRootState , QueryKeys , QueryStatus , QuerySubstateIdentifier } from './apiState' ;
4
4
import { StartQueryActionCreatorOptions } from './buildActionMaps' ;
5
5
import {
@@ -11,12 +11,11 @@ import {
11
11
QueryDefinition ,
12
12
ResultTypeFrom ,
13
13
} from './endpointDefinitions' ;
14
- import { Draft } from '@reduxjs/toolkit' ;
14
+ import { Draft , isAllOf , isFulfilled , isPending , isRejected } from '@reduxjs/toolkit' ;
15
15
import { Patch , isDraftable , produceWithPatches , enablePatches } from 'immer' ;
16
16
import { AnyAction , createAsyncThunk , ThunkAction , ThunkDispatch , AsyncThunk } from '@reduxjs/toolkit' ;
17
17
18
18
import { PrefetchOptions } from './buildHooks' ;
19
- import { Id } from './tsHelpers' ;
20
19
21
20
declare module './apiTypes' {
22
21
export interface ApiEndpointQuery <
@@ -30,37 +29,33 @@ declare module './apiTypes' {
30
29
> extends Matchers < MutationThunk , Definition > { }
31
30
}
32
31
33
- export type PendingAction <
32
+ type EndpointThunk <
34
33
Thunk extends AsyncThunk < any , any , any > ,
35
34
Definition extends EndpointDefinition < any , any , any , any >
36
- > = Definition extends EndpointDefinition < infer QueryArg , any , any , infer ResultType >
37
- ? OverrideActionProps < ReturnType < Thunk [ 'pending' ] > , QueryArg >
35
+ > = Definition extends EndpointDefinition < infer QueryArg , infer BaseQueryFn , any , infer ResultType >
36
+ ? Thunk extends AsyncThunk < infer ATResult , infer ATArg , infer ATConfig >
37
+ ? AsyncThunk <
38
+ ATResult & { result : ResultType } ,
39
+ ATArg & { originalArgs : QueryArg } ,
40
+ ATConfig & { rejectValue : BaseQueryError < BaseQueryFn > }
41
+ >
42
+ : never
38
43
: never ;
39
44
45
+ export type PendingAction <
46
+ Thunk extends AsyncThunk < any , any , any > ,
47
+ Definition extends EndpointDefinition < any , any , any , any >
48
+ > = ReturnType < EndpointThunk < Thunk , Definition > [ 'pending' ] > ;
49
+
40
50
export type FulfilledAction <
41
51
Thunk extends AsyncThunk < any , any , any > ,
42
52
Definition extends EndpointDefinition < any , any , any , any >
43
- > = Definition extends EndpointDefinition < infer QueryArg , any , any , infer ResultType >
44
- ? OverrideActionProps < ReturnType < Thunk [ 'fulfilled' ] > , QueryArg , ResultType >
45
- : never ;
53
+ > = ReturnType < EndpointThunk < Thunk , Definition > [ 'fulfilled' ] > ;
46
54
47
55
export type RejectedAction <
48
56
Thunk extends AsyncThunk < any , any , any > ,
49
57
Definition extends EndpointDefinition < any , any , any , any >
50
- > = Definition extends EndpointDefinition < infer QueryArg , any , any , infer ResultType >
51
- ? OverrideActionProps < ReturnType < Thunk [ 'rejected' ] > , QueryArg >
52
- : never ;
53
-
54
- type OverrideActionProps <
55
- A extends { payload ?: any ; meta : { arg : { originalArgs : any } } } ,
56
- QueryArg ,
57
- Payload = void
58
- > = Id <
59
- Omit < A , 'payload' | 'meta' > & {
60
- payload : [ void ] extends [ Payload ] ? A [ 'payload' ] : Payload ;
61
- meta : Id < Omit < A [ 'meta' ] , 'arg' > & { arg : Id < Omit < A [ 'meta' ] [ 'arg' ] , 'originalArgs' > & { originalArgs : QueryArg } > } > ;
62
- }
63
- > ;
58
+ > = ReturnType < EndpointThunk < Thunk , Definition > [ 'rejected' ] > ;
64
59
65
60
export type Matcher < M > = ( value : any ) => value is M ;
66
61
@@ -292,17 +287,18 @@ export function buildThunks<
292
287
}
293
288
} ;
294
289
290
+ function matchesEndpoint ( endpoint : string ) {
291
+ return ( action : any ) : action is AnyAction => action ?. meta ?. arg ?. endpoint === endpoint ;
292
+ }
293
+
295
294
function buildMatchThunkActions <
296
295
Thunk extends AsyncThunk < any , QueryThunkArg < any > , any > | AsyncThunk < any , MutationThunkArg < any > , any >
297
- > ( thunk : Thunk , endpoint : string ) : Matchers < Thunk , any > {
296
+ > ( thunk : Thunk , endpoint : string ) {
298
297
return {
299
- matchPending : ( action ) : action is PendingAction < Thunk , any > =>
300
- thunk . pending . match ( action ) && action . meta . arg . endpoint === endpoint ,
301
- matchFulfilled : ( action ) : action is FulfilledAction < Thunk , any > =>
302
- thunk . fulfilled . match ( action ) && action . meta . arg . endpoint === endpoint ,
303
- matchRejected : ( action ) : action is RejectedAction < Thunk , any > =>
304
- thunk . rejected . match ( action ) && action . meta . arg . endpoint === endpoint ,
305
- } ;
298
+ matchPending : isAllOf ( isPending ( thunk ) , matchesEndpoint ( endpoint ) ) ,
299
+ matchFulfilled : isAllOf ( isFulfilled ( thunk ) , matchesEndpoint ( endpoint ) ) ,
300
+ matchRejected : isAllOf ( isRejected ( thunk ) , matchesEndpoint ( endpoint ) ) ,
301
+ } as Matchers < Thunk , any > ;
306
302
}
307
303
308
304
return { queryThunk, mutationThunk, prefetchThunk, updateQueryResult, patchQueryResult, buildMatchThunkActions } ;
0 commit comments