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

Allow passing meta to retry.fail, and passing baseQuery to ensure types match #4723

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/rtk-query/usage/customizing-queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions packages/toolkit/src/query/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
BaseQueryError,
BaseQueryExtraOptions,
BaseQueryFn,
BaseQueryMeta,
} from './baseQueryTypes'
import type { FetchBaseQueryError } from './fetchBaseQuery'
import { HandledError } from './HandledError'
Expand Down Expand Up @@ -64,8 +65,11 @@ export type RetryOptions = {
}
)

function fail(e: any): never {
throw Object.assign(new HandledError({ error: e }), {
function fail<BaseQuery extends BaseQueryFn = BaseQueryFn>(
error: BaseQueryError<BaseQuery>,
meta?: BaseQueryMeta<BaseQuery>,
): never {
throw Object.assign(new HandledError({ error, meta }), {
throwImmediately: true,
})
}
Expand Down
33 changes: 30 additions & 3 deletions packages/toolkit/src/query/tests/retry.test-d.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -14,6 +19,28 @@ describe('type tests', () => {
retryCondition: () => false,
}).not.toMatchTypeOf<RetryOptions>()
})
})
test('fail can be pretyped to only accept correct error and meta', () => {
expectTypeOf(retry.fail).parameter(0).toEqualTypeOf<unknown>()
expectTypeOf(retry.fail).parameter(1).toEqualTypeOf<{} | undefined>()
expectTypeOf(retry.fail).toBeCallableWith('Literally anything', {})

const myBaseQuery = fetchBaseQuery()
const typedFail = retry.fail<typeof myBaseQuery>

expectTypeOf(typedFail).parameter(0).toMatchTypeOf<FetchBaseQueryError>()
expectTypeOf(typedFail)
.parameter(1)
.toMatchTypeOf<FetchBaseQueryMeta | undefined>()

export {}
expectTypeOf(typedFail).toBeCallableWith(
{
status: 401,
data: 'Unauthorized',
},
{ request: new Request('http://localhost') },
)

expectTypeOf(typedFail).parameter(0).not.toMatchTypeOf<string>()
expectTypeOf(typedFail).parameter(1).not.toMatchTypeOf<{}>()
})
})