Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly allow extraOptions with required properties, or non-objects #4208

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rtk-query/api/createApi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export type BaseQueryFn<
Args = any,
Result = unknown,
Error = unknown,
DefinitionExtraOptions = {},
DefinitionExtraOptions = any,
Meta = {},
> = (
args: Args,
Expand Down
33 changes: 12 additions & 21 deletions docs/rtk-query/usage/customizing-queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ const axiosBaseQuery =
headers?: AxiosRequestConfig['headers']
},
unknown,
unknown
unknown,
{}
> =>
async ({ url, method, data, params, headers }) => {
try {
Expand Down Expand Up @@ -491,19 +492,14 @@ declare function loggedOut(): void
export { tokenReceived, loggedOut }
// file: baseQueryWithReauth.ts
import { fetchBaseQuery } from '@reduxjs/toolkit/query'
import type {
BaseQueryFn,
FetchArgs,
FetchBaseQueryError,
} from '@reduxjs/toolkit/query'
import { tokenReceived, loggedOut } from './authSlice'

const baseQuery = fetchBaseQuery({ baseUrl: '/' })
const baseQueryWithReauth: BaseQueryFn<
string | FetchArgs,
unknown,
FetchBaseQueryError
> = async (args, api, extraOptions) => {
const baseQueryWithReauth: typeof baseQuery = async (
args,
api,
extraOptions,
) => {
let result = await baseQuery(args, api, extraOptions)
if (result.error && result.error.status === 401) {
// try to get a new token
Expand Down Expand Up @@ -782,11 +778,6 @@ export interface Post {

// file: src/services/api.ts
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import type {
BaseQueryFn,
FetchArgs,
FetchBaseQueryError,
} from '@reduxjs/toolkit/query/react'
import type { Post } from './types'
import { selectProjectId } from './projectSlice'
import type { RootState } from '../store'
Expand All @@ -795,11 +786,11 @@ const rawBaseQuery = fetchBaseQuery({
baseUrl: 'www.my-cool-site.com/',
})

const dynamicBaseQuery: BaseQueryFn<
string | FetchArgs,
unknown,
FetchBaseQueryError
> = async (args, api, extraOptions) => {
const dynamicBaseQuery: typeof rawBaseQuery = async (
args,
api,
extraOptions,
) => {
const projectId = selectProjectId(api.getState() as RootState)
// gracefully handle scenarios where data to generate the URL is missing
if (!projectId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/baseQueryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type BaseQueryFn<
Args = any,
Result = unknown,
Error = unknown,
DefinitionExtraOptions = {},
DefinitionExtraOptions = any,
Meta = {},
> = (
args: Args,
Expand Down
126 changes: 126 additions & 0 deletions packages/toolkit/src/query/tests/createApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
QueryDefinition,
TagDescription,
TagTypesFromApi,
BaseQueryFn,
} from '@reduxjs/toolkit/query'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'

Expand Down Expand Up @@ -374,4 +375,129 @@ describe('type tests', () => {
})
})
})
describe('extraOptions', () => {
describe('object with optional properties', () => {
const baseQuery: BaseQueryFn<
string,
unknown,
unknown,
{ extra?: string }
> = (args, api, extraOptions) => {
expectTypeOf(extraOptions).toEqualTypeOf<{ extra?: string }>()
return { data: 'success' }
}

const api = createApi({
baseQuery,
endpoints: (build) => ({
noExtra: build.query<unknown, string>({
query: (id) => id,
}),
withEmptyExtra: build.query<unknown, string>({
query: (id) => id,
extraOptions: {},
}),
withExtra: build.query<unknown, string>({
query: (id) => id,
extraOptions: { extra: 'value' },
}),
}),
})
})
describe('object with required properties', () => {
const baseQuery: BaseQueryFn<
string,
unknown,
unknown,
{ extra: string }
> = (args, api, extraOptions) => {
expectTypeOf(extraOptions).toEqualTypeOf<{ extra: string }>()
return { data: 'success' }
}

const api = createApi({
baseQuery,
endpoints: (build) => ({
// @ts-expect-error missing extra
noExtra: build.query<unknown, string>({
query: (id) => id,
}),
withEmptyExtra: build.query<unknown, string>({
query: (id) => id,
// @ts-expect-error key is missing
extraOptions: {},
}),
withExtra: build.query<unknown, string>({
query: (id) => id,
extraOptions: { extra: 'value' },
}),
}),
})
})
describe('non-object', () => {
const baseQuery: BaseQueryFn<string, unknown, unknown, string> = (
args,
api,
extraOptions,
) => {
expectTypeOf(extraOptions).toEqualTypeOf<string>()
return { data: 'success' }
}

const api = createApi({
baseQuery,
endpoints: (build) => ({
// @ts-expect-error
noExtra: build.query<unknown, string>({
query: (id) => id,
}),
withExtra: build.query<unknown, string>({
query: (id) => id,
extraOptions: 'value',
}),
}),
})
})
describe('optional non-object', () => {
const baseQuery: BaseQueryFn<
string,
unknown,
unknown,
string | undefined
> = (args, api, extraOptions) => {
expectTypeOf(extraOptions).toEqualTypeOf<string | undefined>()
return { data: 'success' }
}

const api = createApi({
baseQuery,
endpoints: (build) => ({
noExtra: build.query<unknown, string>({
query: (id) => id,
}),
withExtra: build.query<unknown, string>({
query: (id) => id,
extraOptions: 'value',
}),
}),
})
})
describe('unspecified', () => {
const baseQuery = (args: string) => ({ data: 'success' })

const api = createApi({
baseQuery,
endpoints: (build) => ({
noExtra: build.query<unknown, string>({
query: (id) => id,
}),
withExtra: build.query<unknown, string>({
query: (id) => id,
// @ts-expect-error no extra options specified
extraOptions: 'value',
}),
}),
})
})
})
})
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/tests/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('configuration', () => {

test('retryCondition with endpoint config that overrides baseQuery config', async () => {
const baseBaseQuery = vi.fn<
Parameters<BaseQueryFn>,
Parameters<BaseQueryFn<any, unknown, unknown, {}>>,
ReturnType<BaseQueryFn>
>()
baseBaseQuery.mockResolvedValue({ error: 'rejected' })
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('configuration', () => {

test('retryCondition also works with mutations', async () => {
const baseBaseQuery = vi.fn<
Parameters<BaseQueryFn>,
Parameters<BaseQueryFn<any, unknown, unknown, {}>>,
ReturnType<BaseQueryFn>
>()

Expand Down