Skip to content

Commit 4467403

Browse files
committed
typings
1 parent 5f6d2e1 commit 4467403

File tree

9 files changed

+183
-54
lines changed

9 files changed

+183
-54
lines changed

src/query/baseQueryTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type BaseQueryMeta<BaseQuery extends BaseQueryFn> = UnwrapPromise<
5959

6060
export type BaseQueryError<BaseQuery extends BaseQueryFn> = Exclude<
6161
UnwrapPromise<ReturnType<BaseQuery>>,
62-
{ error: undefined }
62+
{ error?: undefined }
6363
>['error']
6464

6565
export type BaseQueryArg<

src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isAsyncThunkAction, isFulfilled } from '@reduxjs/toolkit'
22
import type { AnyAction } from 'redux'
33
import type { ThunkDispatch } from 'redux-thunk'
4-
import type { BaseQueryFn } from '../../baseQueryTypes'
4+
import type { BaseQueryFn, BaseQueryMeta } from '../../baseQueryTypes'
55
import { DefinitionType } from '../../endpointDefinitions'
66
import type { RootState } from '../apiState'
77
import type {
@@ -78,7 +78,10 @@ declare module '../../endpointDefinitions' {
7878
requestId: string
7979
}
8080

81-
export interface CacheLifecyclePromises<ResultType = unknown> {
81+
export interface CacheLifecyclePromises<
82+
ResultType = unknown,
83+
MetaType = unknown
84+
> {
8285
/**
8386
* Promise that will resolve with the first value for this cache key.
8487
* This allows you to `await` until an actual value is in cache.
@@ -98,6 +101,10 @@ declare module '../../endpointDefinitions' {
98101
* The (transformed) query result.
99102
*/
100103
data: ResultType
104+
/**
105+
* The `meta` returned by the `baseQuery`
106+
*/
107+
meta: MetaType
101108
},
102109
typeof neverResolvedError
103110
>
@@ -115,7 +122,7 @@ declare module '../../endpointDefinitions' {
115122
ResultType,
116123
ReducerPath extends string = string
117124
> extends QueryBaseLifecycleApi<QueryArg, BaseQuery, ResultType, ReducerPath>,
118-
CacheLifecyclePromises<ResultType> {}
125+
CacheLifecyclePromises<ResultType, BaseQueryMeta<BaseQuery>> {}
119126

120127
export interface MutationCacheLifecycleApi<
121128
QueryArg,
@@ -128,7 +135,7 @@ declare module '../../endpointDefinitions' {
128135
ResultType,
129136
ReducerPath
130137
>,
131-
CacheLifecyclePromises<ResultType> {}
138+
CacheLifecyclePromises<ResultType, BaseQueryMeta<BaseQuery>> {}
132139

133140
interface QueryExtraOptions<
134141
TagTypes extends string,
@@ -181,7 +188,7 @@ export const build: SubMiddlewareBuilder = ({
181188

182189
return (mwApi) => {
183190
type CacheLifecycle = {
184-
valueResolved?(value: { data: unknown }): unknown
191+
valueResolved?(value: { data: unknown; meta: unknown }): unknown
185192
cacheEntryRemoved(): void
186193
}
187194
const lifecycleMap: Record<string, CacheLifecycle> = {}
@@ -219,12 +226,10 @@ export const build: SubMiddlewareBuilder = ({
219226
} else if (isFullfilledThunk(action)) {
220227
const lifecycle = lifecycleMap[cacheKey]
221228
if (lifecycle?.valueResolved) {
222-
lifecycle.valueResolved(
223-
{
224-
data: action.payload,
225-
meta: action.meta.baseQueryMeta,
226-
} as any /* TODO typings */
227-
)
229+
lifecycle.valueResolved({
230+
data: action.payload,
231+
meta: action.meta.baseQueryMeta,
232+
})
228233
delete lifecycle.valueResolved
229234
}
230235
} else if (
@@ -273,10 +278,10 @@ export const build: SubMiddlewareBuilder = ({
273278
lifecycle.cacheEntryRemoved = resolve
274279
})
275280
const cacheDataLoaded: PromiseWithKnownReason<
276-
{ data: unknown },
281+
{ data: unknown; meta: unknown },
277282
typeof neverResolvedError
278283
> = Promise.race([
279-
new Promise<{ data: unknown }>((resolve) => {
284+
new Promise<{ data: unknown; meta: unknown }>((resolve) => {
280285
lifecycle.valueResolved = resolve
281286
}),
282287
cacheEntryRemoved.then(() => {

src/query/core/buildMiddleware/queryLifecycle.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { isPending, isRejected, isFulfilled } from '@reduxjs/toolkit'
2-
import type { BaseQueryError, BaseQueryFn } from '../../baseQueryTypes'
2+
import type {
3+
BaseQueryError,
4+
BaseQueryFn,
5+
BaseQueryMeta,
6+
} from '../../baseQueryTypes'
37
import { DefinitionType } from '../../endpointDefinitions'
48
import type { QueryFulfilledRejectionReason } from '../../endpointDefinitions'
59
import type { Recipe } from '../buildThunks'
@@ -31,6 +35,10 @@ declare module '../../endpointDefinitions' {
3135
* The (transformed) query result.
3236
*/
3337
data: ResultType
38+
/**
39+
* The `meta` returned by the `baseQuery`
40+
*/
41+
meta: BaseQueryMeta<BaseQuery>
3442
},
3543
QueryFulfilledRejectionReason<BaseQuery>
3644
>
@@ -43,9 +51,14 @@ declare module '../../endpointDefinitions' {
4351
* If this is `false`, that means this error was returned from the `baseQuery` or `queryFn` in a controlled manner.
4452
*/
4553
isUnhandledError: false
54+
/**
55+
* The `meta` returned by the `baseQuery`
56+
*/
57+
meta: BaseQueryMeta<BaseQuery>
4658
}
4759
| {
4860
error: unknown
61+
meta?: undefined
4962
/**
5063
* If this is `true`, that means that this error is the result of `baseQueryFn`, `queryFn` or `transformResponse` throwing an error instead of handling it properly.
5164
* There can not be made any assumption about the shape of `error`.
@@ -113,8 +126,8 @@ export const build: SubMiddlewareBuilder = ({
113126

114127
return (mwApi) => {
115128
type CacheLifecycle = {
116-
resolve(value: { data: unknown }): unknown
117-
reject(value: { error: unknown; isUnhandledError: boolean }): unknown
129+
resolve(value: { data: unknown; meta: unknown }): unknown
130+
reject(value: QueryFulfilledRejectionReason<any>): unknown
118131
}
119132
const lifecycleMap: Record<string, CacheLifecycle> = {}
120133

@@ -131,7 +144,7 @@ export const build: SubMiddlewareBuilder = ({
131144
if (onQueryStarted) {
132145
const lifecycle = {} as CacheLifecycle
133146
const queryFulfilled = new (Promise as PromiseConstructorWithKnownReason)<
134-
{ data: unknown },
147+
{ data: unknown; meta: unknown },
135148
QueryFulfilledRejectionReason<any>
136149
>((resolve, reject) => {
137150
lifecycle.resolve = resolve
@@ -169,22 +182,18 @@ export const build: SubMiddlewareBuilder = ({
169182
}
170183
} else if (isFullfilledThunk(action)) {
171184
const { requestId, baseQueryMeta } = action.meta
172-
lifecycleMap[requestId]?.resolve(
173-
{
174-
data: action.payload,
175-
meta: baseQueryMeta,
176-
} as any /* TODO typings for this */
177-
)
185+
lifecycleMap[requestId]?.resolve({
186+
data: action.payload,
187+
meta: baseQueryMeta,
188+
})
178189
delete lifecycleMap[requestId]
179190
} else if (isRejectedThunk(action)) {
180191
const { requestId, rejectedWithValue, baseQueryMeta } = action.meta
181-
lifecycleMap[requestId]?.reject(
182-
{
183-
error: action.payload ?? action.error,
184-
isUnhandledError: !rejectedWithValue,
185-
meta: baseQueryMeta,
186-
} as any /* TODO typings for this */
187-
)
192+
lifecycleMap[requestId]?.reject({
193+
error: action.payload ?? action.error,
194+
isUnhandledError: !rejectedWithValue,
195+
meta: baseQueryMeta as any,
196+
})
188197
delete lifecycleMap[requestId]
189198
}
190199

src/query/endpointDefinitions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface EndpointDefinitionWithQuery<
3333
*
3434
* ```ts
3535
* // codeblock-meta title="query example"
36-
*
36+
*
3737
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
3838
* interface Post {
3939
* id: number
@@ -75,7 +75,7 @@ interface EndpointDefinitionWithQueryFn<
7575
* @example
7676
* ```ts
7777
* // codeblock-meta title="Basic queryFn example"
78-
*
78+
*
7979
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
8080
* interface Post {
8181
* id: number
@@ -203,7 +203,7 @@ export interface QueryExtraOptions<
203203
*
204204
* ```ts
205205
* // codeblock-meta title="providesTags example"
206-
*
206+
*
207207
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
208208
* interface Post {
209209
* id: number
@@ -362,14 +362,14 @@ export interface MutationExtraOptions<
362362
arg: QueryArg,
363363
mutationApi: MutationApi<ReducerPath, any>,
364364
error: unknown,
365-
meta: undefined
365+
meta: BaseQueryMeta<BaseQuery>
366366
): void
367367
/** @deprecated please use `onQueryStarted` instead */
368368
onSuccess?(
369369
arg: QueryArg,
370370
mutationApi: MutationApi<ReducerPath, any>,
371371
result: ResultType,
372-
meta: undefined
372+
meta: BaseQueryMeta<BaseQuery>
373373
): void
374374
}
375375

src/query/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export { QueryStatus } from './core/apiState'
2-
export type {
3-
Api,
4-
Module,
5-
ApiModules,
6-
} from './apiTypes'
2+
export type { Api, Module, ApiModules } from './apiTypes'
73
export type { BaseQueryEnhancer, BaseQueryFn } from './baseQueryTypes'
84
export type {
95
EndpointDefinitions,
@@ -12,7 +8,11 @@ export type {
128
MutationDefinition,
139
} from './endpointDefinitions'
1410
export { fetchBaseQuery } from './fetchBaseQuery'
15-
export type { FetchBaseQueryError, FetchArgs } from './fetchBaseQuery'
11+
export type {
12+
FetchBaseQueryError,
13+
FetchBaseQueryMeta,
14+
FetchArgs,
15+
} from './fetchBaseQuery'
1616
export { retry } from './retry'
1717
export { setupListeners } from './core/setupListeners'
1818
export { skipSelector, skipToken, SkipToken } from './core/buildSelectors'

src/query/tests/cacheCollection.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { createApi } from '@reduxjs/toolkit/query'
1+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
22
import { configureStore } from '@reduxjs/toolkit'
3-
import { fetchBaseQuery } from '../fetchBaseQuery'
43
import { waitMs } from './helpers'
54
import type { Middleware, Reducer } from 'redux'
65

src/query/tests/cacheLifecycle.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApi } from '@reduxjs/toolkit/query'
2-
import { fetchBaseQuery } from '../fetchBaseQuery'
3-
import { fakeTimerWaitFor, setupApiStore, waitMs } from './helpers'
2+
import type { FetchBaseQueryMeta } from '@reduxjs/toolkit/query'
3+
import { fetchBaseQuery } from '@reduxjs/toolkit/query'
4+
import { expectType, fakeTimerWaitFor, setupApiStore, waitMs } from './helpers'
45

56
beforeAll(() => {
67
jest.useFakeTimers()
@@ -81,14 +82,17 @@ describe.each([['query'], ['mutation']] as const)(
8182
const extended = api.injectEndpoints({
8283
overrideExisting: true,
8384
endpoints: (build) => ({
84-
injected: build[type as 'mutation']<unknown, string>({
85+
injected: build[type as 'mutation']<number, string>({
8586
query: () => '/success',
8687
async onCacheEntryAdded(
8788
arg,
8889
{ dispatch, getState, cacheEntryRemoved, cacheDataLoaded }
8990
) {
9091
onNewCacheEntry(arg)
9192
const firstValue = await cacheDataLoaded
93+
expectType<{ data: number; meta?: FetchBaseQueryMeta }>(
94+
firstValue
95+
)
9296
gotFirstValue(firstValue)
9397
await cacheEntryRemoved
9498
onCleanup()

0 commit comments

Comments
 (0)