Skip to content
Myr

Fix `shouldRetry` type rejecting functions that return nothing

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 224034d · Browse files · 3 files+21 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+21 added−3 removed

  • other:+21 added−3 removed

3 files+21 added−3 removed

modified:source/types/retry.ts+2 added-2 removed
modified:test-d/normalized-options.ts+18 added
60 unmodified lines
61const withoutSignal: Options = {signal: undefined};62expectTypeOf(withoutHooks.hooks?.beforeRequest).toEqualTypeOf<BeforeRequestHook[] | undefined>();63// eslint-disable-next-line @typescript-eslint/no-restricted-types64expectTypeOf(withoutSignal.signal).toEqualTypeOf<AbortSignal | null | undefined>();6566// `shouldRetry` may return nothing to fall back to the default retry logic, like every hook type.67const shouldRetryOptions: Options = {68 retry: {69 shouldRetry() {70 // Nothing to decide here.71 },72 },73};74const asyncShouldRetryOptions: Options = {75 retry: {76 async shouldRetry() {77 await Promise.resolve();78 },79 },80};81expectTypeOf(shouldRetryOptions.retry).toMatchTypeOf<RetryOptions | number | undefined>();82expectTypeOf(asyncShouldRetryOptions.retry).toMatchTypeOf<RetryOptions | number | undefined>();
modified:readme.md+1 added-1 removed
303 unmodified lines
304305The function receives a state object with the error and retry count (starts at 1 for the first retry), and should return:306- `true` to force a retry (bypasses `retryOnTimeout`, status code checks, and other default validations)307- `false` to prevent a retry (no retry will occur)308- `undefined` to use the default retry logic (`retryOnTimeout`, status codes, network errors). Unrecognized error types are not retried.308- `undefined` (or nothing) to use the default retry logic (`retryOnTimeout`, status codes, network errors). Unrecognized error types are not retried.309310**General example**311312```js
1544 unmodified lines