Skip to content

Commit aa7588a

Browse files
committed
Add runtime checks for maxPages values
1 parent a89883b commit aa7588a

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

packages/toolkit/src/query/createApi.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import type {
77
EndpointBuilder,
88
EndpointDefinitions,
99
} from './endpointDefinitions'
10-
import { DefinitionType, isQueryDefinition } from './endpointDefinitions'
10+
import {
11+
DefinitionType,
12+
isInfiniteQueryDefinition,
13+
isQueryDefinition,
14+
} from './endpointDefinitions'
1115
import { nanoid } from './core/rtkImports'
1216
import type { UnknownAction } from '@reduxjs/toolkit'
1317
import type { NoInfer } from './tsHelpers'
@@ -347,7 +351,8 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
347351
const evaluatedEndpoints = inject.endpoints({
348352
query: (x) => ({ ...x, type: DefinitionType.query }) as any,
349353
mutation: (x) => ({ ...x, type: DefinitionType.mutation }) as any,
350-
infiniteQuery: (x) => ({ ...x, type: DefinitionType.infinitequery } as any),
354+
infiniteQuery: (x) =>
355+
({ ...x, type: DefinitionType.infinitequery }) as any,
351356
})
352357

353358
for (const [endpointName, definition] of Object.entries(
@@ -373,6 +378,30 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
373378
continue
374379
}
375380

381+
if (
382+
typeof process !== 'undefined' &&
383+
process.env.NODE_ENV === 'development'
384+
) {
385+
if (isInfiniteQueryDefinition(definition)) {
386+
const { infiniteQueryOptions } = definition
387+
const { maxPages, getPreviousPageParam } = infiniteQueryOptions
388+
389+
if (typeof maxPages === 'number') {
390+
if (maxPages < 1) {
391+
throw new Error(
392+
`maxPages for endpoint '${endpointName}' must be a number greater than 0`,
393+
)
394+
}
395+
396+
if (typeof getPreviousPageParam !== 'function') {
397+
throw new Error(
398+
`getPreviousPageParam for endpoint '${endpointName}' must be a function if maxPages is used`,
399+
)
400+
}
401+
}
402+
}
403+
}
404+
376405
context.endpointDefinitions[endpointName] = definition
377406
for (const m of initializedModules) {
378407
m.injectEndpoint(endpointName, definition)

0 commit comments

Comments
 (0)