keq 2.5.2 → 2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [2.5.3](https://github.com/keq-request/keq/compare/v2.5.2...v2.5.3) (2024-05-23)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * retryTimes avoids throw errors caused by NaN ([a5ece3f](https://github.com/keq-request/keq/commit/a5ece3f8afec3c58d2daa3c5e1a2f85fae044665))
11
+
5
12
  ## [2.5.2](https://github.com/keq-request/keq/compare/v2.5.1...v2.5.2) (2024-05-22)
6
13
 
7
14
 
package/README.md CHANGED
@@ -291,11 +291,11 @@ There are multiple parsing methods for us to choose from
291
291
 
292
292
  No retry by default, invoke `.retry(retryTimes[, retryDelay[, retryOn]])` to set retry parameters
293
293
 
294
- | Parameter | Description |
295
- | :--------- | :----------------------------------------------------------------------------------------------------------------------- |
296
- | retryTimes | Max number of retries per call. |
297
- | retryDelay | Initial value used to calculate the retry in milliseconds (This is still randomized following the randomization factor). |
298
- | retryOn | Will be called after request used to control whether the next retry runs. If it return `false`, stop retrying. |
294
+ | Parameter | Default | Description |
295
+ | :--------- |:------- | :----------------------------------------------------------------------------------------------------------------------- |
296
+ | retryTimes | `0` | Max number of retries per call. |
297
+ | retryDelay |`0` | Initial value used to calculate the retry in milliseconds (This is still randomized following the randomization factor). |
298
+ | retryOn | `(attempt, error) => !!error` | Will be called after request used to control whether the next retry runs. If it return `false`, stop retrying. |
299
299
 
300
300
  ```javascript
301
301
  import { request } from "keq";
@@ -4,7 +4,9 @@ function sleep(ms) {
4
4
  }
5
5
  export function retryMiddleware() {
6
6
  return async function retryMiddleware(ctx, next) {
7
- const retryTimes = (Number(ctx.options.retryTimes) || 0) + 1;
7
+ const retryTimes = Number.isInteger(ctx.options.retryTimes)
8
+ ? ctx.options.retryTimes + 1
9
+ : 1;
8
10
  const retryDelay = (attempt, error, ctx) => {
9
11
  if (typeof ctx.options.retryDelay === 'function') {
10
12
  return ctx.options.retryDelay(attempt, error, ctx);
@@ -12,9 +14,11 @@ export function retryMiddleware() {
12
14
  else if (typeof ctx.options.retryDelay === 'number') {
13
15
  return ctx.options.retryDelay;
14
16
  }
15
- return 10;
17
+ return 0;
16
18
  };
17
- const retryOn = typeof ctx.options.retryOn === 'function' ? ctx.options.retryOn : undefined;
19
+ const retryOn = typeof ctx.options.retryOn === 'function'
20
+ ? ctx.options.retryOn
21
+ : (attempt, error) => !!error;
18
22
  // Avoid multiple middleware from being added repeatedly
19
23
  ctx.options = {
20
24
  ...ctx.options,
@@ -37,14 +41,11 @@ export function retryMiddleware() {
37
41
  throw err;
38
42
  break;
39
43
  }
40
- if (retryOn && retryOn(i, err, ctx) === false) {
44
+ if (retryOn(i, err, ctx) === false) {
41
45
  if (err)
42
46
  throw err;
43
47
  break;
44
48
  }
45
- else if (!retryOn && !err) {
46
- break;
47
- }
48
49
  const delay = retryDelay(i, err, ctx);
49
50
  if (delay > 0)
50
51
  await sleep(delay);
@@ -16,7 +16,9 @@
16
16
  }
17
17
  function retryMiddleware() {
18
18
  return async function retryMiddleware(ctx, next) {
19
- const retryTimes = (Number(ctx.options.retryTimes) || 0) + 1;
19
+ const retryTimes = Number.isInteger(ctx.options.retryTimes)
20
+ ? ctx.options.retryTimes + 1
21
+ : 1;
20
22
  const retryDelay = (attempt, error, ctx) => {
21
23
  if (typeof ctx.options.retryDelay === 'function') {
22
24
  return ctx.options.retryDelay(attempt, error, ctx);
@@ -24,9 +26,11 @@
24
26
  else if (typeof ctx.options.retryDelay === 'number') {
25
27
  return ctx.options.retryDelay;
26
28
  }
27
- return 10;
29
+ return 0;
28
30
  };
29
- const retryOn = typeof ctx.options.retryOn === 'function' ? ctx.options.retryOn : undefined;
31
+ const retryOn = typeof ctx.options.retryOn === 'function'
32
+ ? ctx.options.retryOn
33
+ : (attempt, error) => !!error;
30
34
  // Avoid multiple middleware from being added repeatedly
31
35
  ctx.options = {
32
36
  ...ctx.options,
@@ -49,14 +53,11 @@
49
53
  throw err;
50
54
  break;
51
55
  }
52
- if (retryOn && retryOn(i, err, ctx) === false) {
56
+ if (retryOn(i, err, ctx) === false) {
53
57
  if (err)
54
58
  throw err;
55
59
  break;
56
60
  }
57
- else if (!retryOn && !err) {
58
- break;
59
- }
60
61
  const delay = retryDelay(i, err, ctx);
61
62
  if (delay > 0)
62
63
  await sleep(delay);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.5.2",
3
+ "version": "2.5.3",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",