Skip to content

Commit

Permalink
fix: retryTimes avoids throw errors caused by NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed May 23, 2024
1 parent 7b15304 commit a5ece3f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ There are multiple parsing methods for us to choose from

No retry by default, invoke `.retry(retryTimes[, retryDelay[, retryOn]])` to set retry parameters

| Parameter | Description |
| :--------- | :----------------------------------------------------------------------------------------------------------------------- |
| retryTimes | Max number of retries per call. |
| retryDelay | Initial value used to calculate the retry in milliseconds (This is still randomized following the randomization factor). |
| retryOn | Will be called after request used to control whether the next retry runs. If it return `false`, stop retrying. |
| Parameter | Default | Description |
| :--------- |:------- | :----------------------------------------------------------------------------------------------------------------------- |
| retryTimes | `0` | Max number of retries per call. |
| retryDelay |`0` | Initial value used to calculate the retry in milliseconds (This is still randomized following the randomization factor). |
| retryOn | `(attempt, error) => !!error` | Will be called after request used to control whether the next retry runs. If it return `false`, stop retrying. |

```javascript
import { request } from "keq";
Expand Down
14 changes: 8 additions & 6 deletions src/middlewares/retry-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function sleep(ms: number): Promise<void> {

export function retryMiddleware(): KeqMiddleware {
return async function retryMiddleware(ctx, next) {
const retryTimes = (Number(ctx.options.retryTimes) || 0) + 1
const retryTimes = Number.isInteger(ctx.options.retryTimes)
? ctx.options.retryTimes! + 1
: 1

const retryDelay: KeqRetryDelay = (attempt, error, ctx): number => {
if (typeof ctx.options.retryDelay === 'function') {
Expand All @@ -17,10 +19,12 @@ export function retryMiddleware(): KeqMiddleware {
return ctx.options.retryDelay
}

return 10
return 0
}

const retryOn = typeof ctx.options.retryOn === 'function' ? ctx.options.retryOn : undefined
const retryOn = typeof ctx.options.retryOn === 'function'
? ctx.options.retryOn
: (attempt, error) => !!error

// Avoid multiple middleware from being added repeatedly
ctx.options = {
Expand All @@ -47,11 +51,9 @@ export function retryMiddleware(): KeqMiddleware {
break
}

if (retryOn && retryOn(i, err, ctx) === false) {
if (retryOn(i, err, ctx) === false) {
if (err) throw err
break
} else if (!retryOn && !err) {
break
}

const delay = retryDelay(i, err, ctx)
Expand Down
11 changes: 11 additions & 0 deletions src/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ test('send request retry twice', async () => {
expect(retryOn.mock.calls[1][0]).toBe(1)
})

test('send request retry once', async () => {
const mockedFetch = jest.fn()

await request
.get('http://test.com')
.retry(2, 0)
.option('fetchAPI', mockedFetch)

expect(mockedFetch.mock.calls).toHaveLength(1)
})

test('throw error when fetch failed', async () => {
const mockedFetch = jest.fn(() => {
throw new Error('fetch failed')
Expand Down

0 comments on commit a5ece3f

Please sign in to comment.