axios-rate-limit 1.2.1 → 1.3.1

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/README.md CHANGED
@@ -2,14 +2,19 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/axios-rate-limit.svg?style=flat-square)](https://www.npmjs.com/package/axios-rate-limit)
4
4
  [![npm downloads](https://img.shields.io/npm/dt/axios-rate-limit.svg?style=flat-square)](https://www.npmjs.com/package/axios-rate-limit)
5
- [![Build Status](https://img.shields.io/travis/aishek/axios-rate-limit.svg?style=flat-square)](https://travis-ci.org/aishek/axios-rate-limit)
5
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/axios-rate-limit?style=flat-square)](https://bundlephobia.com/package/axios-rate-limit@latest)
6
+ [![build status](https://img.shields.io/github/actions/workflow/status/aishek/axios-rate-limit/node.js.yml
7
+ )](https://github.com/aishek/axios-rate-limit/actions?query=branch%3Amaster++)
8
+ [![code coverage](https://img.shields.io/coveralls/aishek/axios-rate-limit.svg?style=flat-square)](https://coveralls.io/r/aishek/axios-rate-limit)
9
+ [![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=axios-rate-limit&query=$.install.pretty&label=install%20size&style=flat-square)](https://packagephobia.now.sh/result?p=axios-rate-limit)
10
+ [![known vulnerabilities](https://snyk.io/test/npm/axios-rate-limit/badge.svg)](https://snyk.io/test/npm/axios-rate-limit)
6
11
 
7
- A rate limit for axios: set how many requests per interval should perform immediately, other will be delayed automatically.
12
+ A rate limit for [Axios](https://www.npmjs.com/package/axios): set how many requests per interval should perform immediately, other will be delayed automatically.
8
13
 
9
14
  ## Installing
10
15
 
11
16
  ```bash
12
- yarn add axios-rate-limit
17
+ npm install axios-rate-limit
13
18
  ```
14
19
 
15
20
  ## Usage
@@ -33,6 +38,6 @@ http.getMaxRPS() // 3
33
38
  http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
34
39
  ```
35
40
 
36
- ## A bit of advertising :-)
41
+ ## Alternatives
37
42
 
38
- Since 2010 run my own software development company [Cifronomika](http://cifronomika.com/). We doing Ruby on Rails and JavaScript development. Feel free to contact
43
+ Consider using Axios built-in [rate-limiting](https://www.npmjs.com/package/axios#user-content--rate-limiting) functionality.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "axios-rate-limit",
3
3
  "description": "Rate limit for axios.",
4
- "version": "1.2.1",
4
+ "version": "1.3.1",
5
5
  "license": "MIT",
6
6
  "bugs": {
7
7
  "url": "https://github.com/aishek/axios-rate-limit/issues"
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "homepage": "https://github.com/aishek/axios-rate-limit#readme",
14
14
  "devDependencies": {
15
- "@logux/eslint-config": "^28.1.0",
15
+ "@logux/eslint-config": "28.1.0",
16
16
  "axios": "^0.18.0",
17
17
  "eslint": "^5.16.0",
18
18
  "eslint-ci": "^1.0.0",
@@ -28,8 +28,7 @@
28
28
  "husky": "^1.3.1",
29
29
  "jest": "^24.7.1",
30
30
  "lint-staged": "^8.1.5",
31
- "sinon": "^7.3.2",
32
- "yaspeller-ci": "^1.0.0"
31
+ "sinon": "^7.3.2"
33
32
  },
34
33
  "peerDependencies": {
35
34
  "axios": "*"
@@ -41,10 +40,9 @@
41
40
  "typings/index.d.ts"
42
41
  ],
43
42
  "scripts": {
44
- "watch": "yarn jest --watch",
45
- "test": "jest --forceExit --runInBand --coverage --detectOpenHandles && yarn lint && yarn spell",
46
- "lint": "eslint-ci src/*.js __tests__/*.js",
47
- "spell": "yaspeller-ci *.md"
43
+ "watch": "jest --watch",
44
+ "test": "jest --forceExit --runInBand --coverage --detectOpenHandles",
45
+ "lint": "eslint-ci src/*.js __tests__/*.js"
48
46
  },
49
47
  "jest": {
50
48
  "verbose": true,
@@ -81,7 +79,6 @@
81
79
  }
82
80
  },
83
81
  "lint-staged": {
84
- "*.md": "yaspeller-ci",
85
82
  "*.js": "eslint"
86
83
  },
87
84
  "keywords": [
@@ -89,26 +86,5 @@
89
86
  "rate",
90
87
  "limit"
91
88
  ],
92
- "author": "Alexandr Borisov (aishek@gmail.com)",
93
- "yaspeller": {
94
- "lang": "en",
95
- "dictionary": [
96
- "Cifronomika",
97
- "axios",
98
- "Versioning",
99
- "jsdoc",
100
- "github",
101
- "aishek",
102
- "mcuppi",
103
- "com",
104
- "NodeJS",
105
- "TypeScript",
106
- "Unreleased",
107
- "unref",
108
- "maxRPS",
109
- "setMaxRPS",
110
- "getMaxRPS",
111
- "setRateLimitOptions"
112
- ]
113
- }
89
+ "author": "Alexandr Borisov (aishek@gmail.com)"
114
90
  }
package/src/index.js CHANGED
@@ -49,9 +49,34 @@ AxiosRateLimit.prototype.enable = function (axios) {
49
49
  )
50
50
  }
51
51
 
52
+ /*
53
+ * from axios library (dispatchRequest.js:11)
54
+ * @param config
55
+ */
56
+ function throwIfCancellationRequested (config) {
57
+ if (config.cancelToken) {
58
+ config.cancelToken.throwIfRequested()
59
+ }
60
+ }
61
+
52
62
  AxiosRateLimit.prototype.handleRequest = function (request) {
53
- return new Promise(function (resolve) {
54
- this.push({ resolve: function () { resolve(request) } })
63
+ return new Promise(function (resolve, reject) {
64
+ this.push({
65
+ /*
66
+ * rejects a cancelled request and returns request has been resolved or not
67
+ * @returns {boolean}
68
+ */
69
+ resolve: function () {
70
+ try {
71
+ throwIfCancellationRequested(request)
72
+ } catch (error) {
73
+ reject(error)
74
+ return false
75
+ }
76
+ resolve(request)
77
+ return true
78
+ }
79
+ })
55
80
  }.bind(this))
56
81
  }
57
82
 
@@ -80,7 +105,7 @@ AxiosRateLimit.prototype.shift = function () {
80
105
  }
81
106
 
82
107
  var queued = this.queue.shift()
83
- queued.resolve()
108
+ var resolved = queued.resolve()
84
109
 
85
110
  if (this.timeslotRequests === 0) {
86
111
  this.timeoutId = setTimeout(function () {
@@ -93,6 +118,11 @@ AxiosRateLimit.prototype.shift = function () {
93
118
  }
94
119
  }
95
120
 
121
+ if (!resolved) {
122
+ this.shift() // rejected request --> shift another request
123
+ return
124
+ }
125
+
96
126
  this.timeslotRequests += 1
97
127
  }
98
128
 
@@ -1,10 +1,45 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
 
3
- interface RateLimitedAxiosInstance extends AxiosInstance {}
3
+ export interface RateLimitedAxiosInstance extends AxiosInstance {
4
+ getMaxRPS(): number,
5
+ setMaxRPS(rps:number): void,
6
+ setRateLimitOptions(options: rateLimitOptions): void,
7
+ // enable(axios: any): void,
8
+ // handleRequest(request:any):any,
9
+ // handleResponse(response: any): any,
10
+ // push(requestHandler:any):any,
11
+ // shiftInitial():any,
12
+ // shift():any
13
+ }
4
14
 
5
- declare function axiosRateLimit(
15
+ type rateLimitOptions = { maxRequests?: number, perMilliseconds?: number, maxRPS?:number };
16
+
17
+ /**
18
+ * Apply rate limit to axios instance.
19
+ *
20
+ * @example
21
+ * import axios from 'axios';
22
+ * import rateLimit from 'axios-rate-limit';
23
+ *
24
+ * // sets max 2 requests per 1 second, other will be delayed
25
+ * // note maxRPS is a shorthand for perMilliseconds: 1000, and it takes precedence
26
+ * // if specified both with maxRequests and perMilliseconds
27
+ * const http = rateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000, maxRPS: 2 })
28
+ * http.getMaxRPS() // 2
29
+ * http.get('https://example.com/api/v1/users.json?page=1') // will perform immediately
30
+ * http.get('https://example.com/api/v1/users.json?page=2') // will perform immediately
31
+ * http.get('https://example.com/api/v1/users.json?page=3') // will perform after 1 second from the first one
32
+ * http.setMaxRPS(3)
33
+ * http.getMaxRPS() // 3
34
+ * http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
35
+ *
36
+ * @param {Object} axios axios instance
37
+ * @param {Object} options options for rate limit, available for live update
38
+ * @param {Number} options.maxRequests max requests to perform concurrently in given amount of time.
39
+ * @param {Number} options.perMilliseconds amount of time to limit concurrent requests.
40
+ * @returns {Object} axios instance with interceptors added
41
+ */
42
+ export default function axiosRateLimit(
6
43
  axiosInstance: AxiosInstance,
7
- options: { maxRequests: number, perMilliseconds: number },
44
+ options: rateLimitOptions
8
45
  ): RateLimitedAxiosInstance;
9
-
10
- export = axiosRateLimit;
package/CHANGELOG.md DELETED
@@ -1,38 +0,0 @@
1
- # Change Log
2
- This project adheres to [Semantic Versioning](http://semver.org/).
3
-
4
- ## 1.2.1
5
- * Fix TypeScript imports (see https://github.com/aishek/axios-rate-limit/pull/19/files)
6
-
7
- ## 1.2.0
8
- * Add maxRPS options, getMaxRPS, setMaxRPS, setRateLimitOptions methods
9
-
10
- ## 1.1.3
11
- * Fixed ref/unref timeout behaviour for NodeJS (see https://github.com/aishek/axios-rate-limit/issues/16)
12
-
13
- ## 1.1.2
14
- * Fixed NodeJS delayed shutdown (see https://github.com/aishek/axios-rate-limit/pull/13/files)
15
-
16
- ## 1.1.1
17
- * Fixed TypeScript typings bundle (see https://github.com/aishek/axios-rate-limit/pull/12/files)
18
-
19
- ## 1.1.0
20
- * Added TypeScript typings (see https://github.com/aishek/axios-rate-limit/pull/9)
21
-
22
- ## 1.0.1
23
- * Updated internals (see https://github.com/aishek/axios-rate-limit/pull/5, https://github.com/aishek/axios-rate-limit/pull/7, https://github.com/aishek/axios-rate-limit/pull/8)
24
-
25
- ## 1.0.0
26
- * Changed exports to classic NodeJS modules (see https://github.com/aishek/axios-rate-limit/issues/3)
27
-
28
- ## 0.0.4
29
- * Add errors re-throw by @mcuppi (see https://github.com/aishek/axios-rate-limit/issues/1)
30
-
31
- ## 0.0.3
32
- * Add jsdoc.
33
-
34
- ## 0.0.2
35
- * Fix default import.
36
-
37
- ## 0.0.1
38
- * Initial release.