diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5486079d76..d18385c584 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -111,7 +111,7 @@ jobs: fail-fast: false matrix: node: ['20.x'] - ts: ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5'] + ts: ['5.0', '5.1', '5.2', '5.3', '5.4', '5.5'] steps: - name: Checkout repo uses: actions/checkout@v4 diff --git a/docs/rtk-query/usage/customizing-queries.mdx b/docs/rtk-query/usage/customizing-queries.mdx index 092f990687..d3f716df0d 100644 --- a/docs/rtk-query/usage/customizing-queries.mdx +++ b/docs/rtk-query/usage/customizing-queries.mdx @@ -636,7 +636,7 @@ const staggeredBaseQueryWithBailOut = retry( // bail out of re-tries immediately if unauthorized, // because we know successive re-retries would be redundant if (result.error?.status === 401) { - retry.fail(result.error) + retry.fail(result.error, result.meta) } return result diff --git a/packages/toolkit/src/query/retry.ts b/packages/toolkit/src/query/retry.ts index 05ab05dd01..3947dd4acd 100644 --- a/packages/toolkit/src/query/retry.ts +++ b/packages/toolkit/src/query/retry.ts @@ -5,6 +5,7 @@ import type { BaseQueryError, BaseQueryExtraOptions, BaseQueryFn, + BaseQueryMeta, } from './baseQueryTypes' import type { FetchBaseQueryError } from './fetchBaseQuery' import { HandledError } from './HandledError' @@ -64,8 +65,11 @@ export type RetryOptions = { } ) -function fail(e: any): never { - throw Object.assign(new HandledError({ error: e }), { +function fail( + error: BaseQueryError, + meta?: BaseQueryMeta, +): never { + throw Object.assign(new HandledError({ error, meta }), { throwImmediately: true, }) } diff --git a/packages/toolkit/src/query/tests/retry.test-d.ts b/packages/toolkit/src/query/tests/retry.test-d.ts index 26a9eb210f..ef8aee5f7f 100644 --- a/packages/toolkit/src/query/tests/retry.test-d.ts +++ b/packages/toolkit/src/query/tests/retry.test-d.ts @@ -1,4 +1,9 @@ -import type { RetryOptions } from '@internal/query/retry' +import { retry, type RetryOptions } from '@internal/query/retry' +import { + fetchBaseQuery, + type FetchBaseQueryError, + type FetchBaseQueryMeta, +} from '@internal/query/fetchBaseQuery' describe('type tests', () => { test('RetryOptions only accepts one of maxRetries or retryCondition', () => { @@ -14,6 +19,28 @@ describe('type tests', () => { retryCondition: () => false, }).not.toMatchTypeOf() }) -}) + test('fail can be pretyped to only accept correct error and meta', () => { + expectTypeOf(retry.fail).parameter(0).toEqualTypeOf() + expectTypeOf(retry.fail).parameter(1).toEqualTypeOf<{} | undefined>() + expectTypeOf(retry.fail).toBeCallableWith('Literally anything', {}) + + const myBaseQuery = fetchBaseQuery() + const typedFail = retry.fail + + expectTypeOf(typedFail).parameter(0).toMatchTypeOf() + expectTypeOf(typedFail) + .parameter(1) + .toMatchTypeOf() -export {} + expectTypeOf(typedFail).toBeCallableWith( + { + status: 401, + data: 'Unauthorized', + }, + { request: new Request('http://localhost') }, + ) + + expectTypeOf(typedFail).parameter(0).not.toMatchTypeOf() + expectTypeOf(typedFail).parameter(1).not.toMatchTypeOf<{}>() + }) +})