1
- /**
2
- * Note: this file should import all other files for type discovery and declaration merging
3
- */
4
- import { PatchQueryResultThunk , QueryApi , UpdateQueryResultThunk } from './buildThunks' ;
5
- import { AnyAction , Middleware , Reducer , ThunkDispatch , ThunkAction } from '@reduxjs/toolkit' ;
6
- import { PrefetchOptions } from './buildHooks' ;
7
- import {
8
- EndpointDefinitions ,
9
- EndpointBuilder ,
10
- QueryArgFrom ,
11
- QueryDefinition ,
12
- MutationDefinition ,
13
- } from './endpointDefinitions' ;
14
- import { CombinedState , QueryKeys , RootState } from './apiState' ;
15
- import { UnionToIntersection } from './tsHelpers' ;
16
- import { TS41Hooks } from './ts41Types' ;
17
- import './buildSelectors' ;
18
- import { SliceActions } from './buildSlice' ;
19
- import { onFocus , onFocusLost , onOffline , onOnline } from './setupListeners' ;
1
+ import { EndpointDefinitions , EndpointBuilder , EndpointDefinition } from './endpointDefinitions' ;
2
+ import { UnionToIntersection , Id } from './tsHelpers' ;
3
+ import { CoreModule } from './core/module' ;
4
+ import { CreateApiOptions } from './createApi' ;
5
+ import { BaseQueryFn } from './baseQueryTypes' ;
20
6
21
- type UnwrapPromise < T > = T extends PromiseLike < infer V > ? V : T ;
22
- type MaybePromise < T > = T | PromiseLike < T > ;
23
-
24
- export type BaseQueryFn < Args = any , Result = unknown , Error = unknown , DefinitionExtraOptions = { } > = (
25
- args : Args ,
26
- api : QueryApi ,
27
- extraOptions : DefinitionExtraOptions
28
- ) => MaybePromise < { error : Error ; data ?: undefined } | { error ?: undefined ; data ?: Result } > ;
29
-
30
- export type BaseQueryEnhancer < AdditionalArgs = unknown , AdditionalDefinitionExtraOptions = unknown , Config = void > = <
31
- BaseQuery extends BaseQueryFn
32
- > (
33
- baseQuery : BaseQuery ,
34
- config : Config
35
- ) => BaseQueryFn <
36
- BaseQueryArg < BaseQuery > & AdditionalArgs ,
37
- BaseQueryResult < BaseQuery > ,
38
- BaseQueryError < BaseQuery > ,
39
- BaseQueryExtraOptions < BaseQuery > & AdditionalDefinitionExtraOptions
40
- > ;
41
-
42
- export type BaseQueryResult < BaseQuery extends BaseQueryFn > = Exclude <
43
- UnwrapPromise < ReturnType < BaseQuery > > ,
44
- { data : undefined }
45
- > [ 'data' ] ;
46
-
47
- export type BaseQueryError < BaseQuery extends BaseQueryFn > = Exclude <
48
- UnwrapPromise < ReturnType < BaseQuery > > ,
49
- { error : undefined }
50
- > [ 'error' ] ;
51
-
52
- export type BaseQueryArg < T extends ( arg : any , ...args : any [ ] ) => any > = T extends ( arg : infer A , ...args : any [ ] ) => any
53
- ? A
54
- : any ;
55
-
56
- export type BaseQueryExtraOptions < BaseQuery extends BaseQueryFn > = Parameters < BaseQuery > [ 2 ] ;
57
-
58
- export type Api <
7
+ export interface ApiModules <
8
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
59
9
BaseQuery extends BaseQueryFn ,
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
60
11
Definitions extends EndpointDefinitions ,
61
- ReducerPath extends string ,
62
- EntityTypes extends string
63
- > = {
64
- reducerPath : ReducerPath ;
65
- internalActions : InternalActions ;
66
- reducer : Reducer < CombinedState < Definitions , EntityTypes , ReducerPath > , AnyAction > ;
67
- middleware : Middleware < { } , RootState < Definitions , string , ReducerPath > , ThunkDispatch < any , any , AnyAction > > ;
68
- util : {
69
- updateQueryResult : UpdateQueryResultThunk < Definitions , RootState < Definitions , string , ReducerPath > > ;
70
- patchQueryResult : PatchQueryResultThunk < Definitions , RootState < Definitions , string , ReducerPath > > ;
71
- } ;
72
- // If you actually care about the return value, use useQuery
73
- usePrefetch < EndpointName extends QueryKeys < Definitions > > (
74
- endpointName : EndpointName ,
75
- options ?: PrefetchOptions
76
- ) : ( arg : QueryArgFrom < Definitions [ EndpointName ] > , options ?: PrefetchOptions ) => void ;
77
- injectEndpoints < NewDefinitions extends EndpointDefinitions > ( _ : {
78
- endpoints : ( build : EndpointBuilder < BaseQuery , EntityTypes , ReducerPath > ) => NewDefinitions ;
79
- overrideExisting ?: boolean ;
80
- } ) : Api < BaseQuery , Definitions & NewDefinitions , ReducerPath , EntityTypes > ;
81
- endpoints : {
82
- [ K in keyof Definitions ] : Definitions [ K ] extends QueryDefinition < any , any , any , any , any >
83
- ? ApiEndpointQuery < Definitions [ K ] , Definitions >
84
- : Definitions [ K ] extends MutationDefinition < any , any , any , any , any >
85
- ? ApiEndpointMutation < Definitions [ K ] , Definitions >
86
- : never ;
87
- } ;
88
- } & TS41Hooks < Definitions > ;
89
-
90
- export interface ApiEndpointQuery <
91
12
// eslint-disable-next-line @typescript-eslint/no-unused-vars
92
- Definition extends QueryDefinition < any , any , any , any , any > ,
13
+ ReducerPath extends string ,
93
14
// eslint-disable-next-line @typescript-eslint/no-unused-vars
94
- Definitions extends EndpointDefinitions
15
+ EntityTypes extends string
95
16
> { }
96
17
97
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
- export interface ApiEndpointMutation <
99
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
100
- Definition extends MutationDefinition < any , any , any , any , any > ,
101
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
102
- Definitions extends EndpointDefinitions
103
- > { }
18
+ export type ModuleName = keyof ApiModules < any , any , any , any > ;
19
+
20
+ export type Module < Name extends ModuleName > = {
21
+ name : Name ;
22
+ init <
23
+ BaseQuery extends BaseQueryFn ,
24
+ Definitions extends EndpointDefinitions ,
25
+ ReducerPath extends string ,
26
+ EntityTypes extends string
27
+ > (
28
+ api : Api < BaseQuery , EndpointDefinitions , ReducerPath , EntityTypes , ModuleName > ,
29
+ options : Required < CreateApiOptions < BaseQuery , Definitions , ReducerPath , EntityTypes > > ,
30
+ context : {
31
+ endpointDefinitions : Definitions ;
32
+ }
33
+ ) : {
34
+ injectEndpoint ( endpoint : string , definition : EndpointDefinition < any , any , any , any > ) : void ;
35
+ } ;
36
+ } ;
37
+
38
+ export type Api <
39
+ BaseQuery extends BaseQueryFn ,
40
+ Definitions extends EndpointDefinitions ,
41
+ ReducerPath extends string ,
42
+ EntityTypes extends string ,
43
+ Enhancers extends ModuleName = CoreModule
44
+ > = Id <
45
+ Id < UnionToIntersection < ApiModules < BaseQuery , Definitions , ReducerPath , EntityTypes > [ Enhancers ] > > & {
46
+ injectEndpoints < NewDefinitions extends EndpointDefinitions > ( _ : {
47
+ endpoints : ( build : EndpointBuilder < BaseQuery , EntityTypes , ReducerPath > ) => NewDefinitions ;
48
+ overrideExisting ?: boolean ;
49
+ } ) : Api < BaseQuery , Definitions & NewDefinitions , ReducerPath , EntityTypes , Enhancers > ;
50
+ }
51
+ > ;
104
52
105
53
export type ApiWithInjectedEndpoints <
106
54
ApiDefinition extends Api < any , any , any , any > ,
@@ -111,20 +59,3 @@ export type ApiWithInjectedEndpoints<
111
59
Omit < Injections , 'endpoints' > & {
112
60
endpoints : ApiDefinition [ 'endpoints' ] & Partial < UnionToIntersection < Injections [ number ] [ 'endpoints' ] > > ;
113
61
} ;
114
-
115
- export type InternalActions = SliceActions & {
116
- prefetchThunk : ( endpointName : any , arg : any , options : PrefetchOptions ) => ThunkAction < void , any , any , AnyAction > ;
117
- } & {
118
- /**
119
- * Will cause the RTK Query middleware to trigger any refetchOnReconnect-related behavior
120
- * @link https://rtk-query-docs.netlify.app/api/setupListeners
121
- */
122
- onOnline : typeof onOnline ;
123
- onOffline : typeof onOffline ;
124
- /**
125
- * Will cause the RTK Query middleware to trigger any refetchOnFocus-related behavior
126
- * @link https://rtk-query-docs.netlify.app/api/setupListeners
127
- */
128
- onFocus : typeof onFocus ;
129
- onFocusLost : typeof onFocusLost ;
130
- } ;
0 commit comments