Skip to content

Commit 36dfe5f

Browse files
AntiavantiWojciech Stachowski
andauthored
Improve typing and usage of getDefaultDelay (#432)
Co-authored-by: Wojciech Stachowski <[email protected]>
1 parent fde6556 commit 36dfe5f

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ Given example
320320
```js
321321
const customRetryLogic = ({err, req, res, attempt, getDefaultRetry}) => {
322322
//If this block is not included all non 500 errors will not be retried
323-
const defaultDelay = getDefaultDelay();
323+
const defaultDelay = getDefaultDelay(req, res, err, attempt);
324324
if (defaultDelay) return defaultDelay();
325325

326326
//Custom retry logic

test/retry-with-a-custom-handler.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ test('a 500 status code with no custom handler should fail', async (t) => {
5555
})
5656

5757
test("a server 500's with a custom handler and should revive", async (t) => {
58-
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
59-
const defaultDelay = getDefaultDelay()
58+
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => {
59+
const defaultDelay = getDefaultDelay(req, res, err, attempt)
6060
if (defaultDelay) return defaultDelay
6161

6262
if (res && res.statusCode === 500 && req.method === 'GET') {
@@ -92,9 +92,9 @@ test('custom retry does not invoke the default delay causing a 501', async (t) =
9292
})
9393

9494
test('custom retry delay functions can invoke the default delay', async (t) => {
95-
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
95+
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => {
9696
// registering the default retry logic for non 500 errors if it occurs
97-
const defaultDelay = getDefaultDelay()
97+
const defaultDelay = getDefaultDelay(req, res, err, attempt)
9898
if (defaultDelay) return defaultDelay
9999

100100
if (res && res.statusCode === 500 && req.method === 'GET') {

types/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ declare namespace fastifyReplyFrom {
5454
res: FastifyReply<RouteGenericInterface, RawServerBase>;
5555
attempt: number;
5656
retriesCount: number;
57-
getDefaultDelay: () => number | null;
57+
getDefaultDelay: (
58+
req: FastifyRequest<RequestGenericInterface, RawServerBase>,
59+
res: FastifyReply<RouteGenericInterface, RawServerBase>,
60+
err: Error,
61+
retries: number,
62+
) => number | null;
5863
}
5964

6065
export type RawServerResponse<T extends RawServerBase> = RawReplyDefaultExpression<T> & {
@@ -64,7 +69,7 @@ declare namespace fastifyReplyFrom {
6469
export interface FastifyReplyFromHooks {
6570
queryString?: { [key: string]: unknown } | QueryStringFunction;
6671
contentType?: string;
67-
retryDelay?: (details: RetryDetails) => {} | null;
72+
retryDelay?: (details: RetryDetails) => number | null;
6873
retriesCount?: number;
6974
onResponse?: (
7075
request: FastifyRequest<RequestGenericInterface, RawServerBase>,

types/index.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ async function main () {
103103
instance.get('/http2', (_request, reply) => {
104104
reply.from('/', {
105105
method: 'POST',
106-
retryDelay: ({ req, res, getDefaultDelay }) => {
107-
const defaultDelay = getDefaultDelay()
106+
retryDelay: ({ req, res, err, attempt, getDefaultDelay }) => {
107+
const defaultDelay = getDefaultDelay(req, res, err, attempt)
108108
if (defaultDelay) return defaultDelay
109109

110110
if (res && res.statusCode === 500 && req.method === 'GET') {

0 commit comments

Comments
 (0)