|
1 |
| -# AxiosQueue.js |
| 1 | +## Why to use axios-queue-js? |
2 | 2 |
|
3 |
| -## Description |
| 3 | +The `axios-queue-js` is a lightweight wrapper of `axios` that enables you to make requests using a queue. In simple words, it improves significantly the performance of your requests, once no request is made unless there's an open slot for it. This lib works for both web and node environments and can be easily placed in codebases that already use `axios` to handle requests, even if they are using a custom client. |
4 | 4 |
|
5 |
| -AxiosQueue.js is a wrapper of axios which enables you to enqueue axios requests and run them in a pipeline. |
| 5 | +## Performance of axios with axios-queue-js |
6 | 6 |
|
7 |
| -## Instalation |
| 7 | +> All the following results were colleted by doing requests to a localhost server and all the scripts used the default configs for axios-queue-js. |
8 | 8 |
|
9 |
| -`npm install axios-queue-js` for npm users and `yarn add axios-queue-js` for yarn users. |
| 9 | +The following chart represents the performance of a node script trying to do 1k sequential requests using nothing but `axios`. |
| 10 | + |
| 11 | +After almost 2min of execution, `axios` triggered an error that terminated the script, that was able to do and finish only 209 requests. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +Now you can see the results of the same test after using `axios` along with `axios-queue-js` in the script. |
| 16 | + |
| 17 | +It's worth mentioning that all the 1k requests were successfully made in only 6 seconds. That is about **5x more requests being made in only 5% of the time**. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +For the next chart we repreated the previous testing, but this time with **100k requests**. |
| 22 | + |
| 23 | +As you can see below, `axios-queue-js` was able to make `axios` handle about **478x more requests in only 3x more time than axios did alone**. |
| 24 | + |
| 25 | + |
10 | 26 |
|
11 | 27 | ## Usage
|
12 | 28 |
|
| 29 | +You can install `axios-queue-js` by running a `npm install axios-queue-js` or `yarn add axios-queue-js` in your project. After that you can use it as follows. |
| 30 | + |
13 | 31 | ```typescript
|
| 32 | +// By using module imports |
14 | 33 | import axios from 'axios-queue-js';
|
15 | 34 |
|
16 | 35 | axios.get('https://endpoint.test.net').then((response) => {
|
17 |
| - console.log('Hey look, I am getting a response using AxiosQueue.js :D'); |
| 36 | + console.log('Hey look, I am getting a response using axios-queue-js :D'); |
18 | 37 | });
|
19 | 38 |
|
20 | 39 | axios.post('https://endpoint.test.net', {}).then((response) => {
|
21 |
| - console.log('Hey look, I am posting something and getting a response using AxiosQueue.js :D'); |
| 40 | + console.log('Hey look, I am posting something and getting a response using axios-queue-js :D'); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +```typescript |
| 45 | +// By using commonjs |
| 46 | +const { default: axios } = require('axios-queue-js'); |
| 47 | + |
| 48 | +axios.get('https://endpoint.test.net').then((response) => { |
| 49 | + console.log('Hey look, I am getting a response using axios-queue-js :D'); |
| 50 | +}); |
| 51 | + |
| 52 | +axios.post('https://endpoint.test.net', {}).then((response) => { |
| 53 | + console.log('Hey look, I am posting something and getting a response using axios-queue-js :D'); |
22 | 54 | });
|
23 | 55 | ```
|
24 | 56 |
|
25 | 57 | ## Customizing
|
26 | 58 |
|
27 |
| -If you want to customize the queue length, you can create your own AxiosQueue.js and use it as a client, just like below. |
| 59 | +If you want to customize the queue length or even use an external axios client to handle the requests, you can create your own `AxiosQueueManager` instance and use it as you will. Eg.: |
28 | 60 |
|
29 | 61 | ```typescript
|
| 62 | +// By using module imports |
| 63 | +import axios from 'axios'; |
30 | 64 | import { AxiosQueueManager } from 'axios-queue-js';
|
31 | 65 |
|
32 |
| -const client = new AxiosQueueManager({ queueSize: 10 }); |
| 66 | +const axiosQueueClient = new AxiosQueueManager({ queueSize: 10, client: axios.create() }); |
33 | 67 |
|
34 |
| -client.get('https://endpoint.test.net').then((response) => { |
35 |
| - console.log('Hey look, I am getting a response using AxiosQueue.js :D'); |
| 68 | +axiosQueueClient.get('https://endpoint.test.net').then((response) => { |
| 69 | + console.log('Hey look, I am getting a response using axios-queue-js :D'); |
36 | 70 | });
|
37 | 71 | ```
|
38 | 72 |
|
39 |
| -## Configuration |
| 73 | +```typescript |
| 74 | +// By using commonjs |
| 75 | +const axios = require('axios') |
| 76 | +const { AxiosQueueManager } = require('axios-queue-js'); |
| 77 | + |
| 78 | +const axiosQueueClient = new AxiosQueueManager({ queueSize: 10, client: axios.create() }); |
| 79 | + |
| 80 | +axiosQueueClient.get('https://endpoint.test.net').then((response) => { |
| 81 | + console.log('Hey look, I am getting a response using axios-queue-js :D'); |
| 82 | +}); |
| 83 | +``` |
40 | 84 |
|
41 |
| -As showed previously, you can create and configure your own AxiosQueueManager instance, which can receives an object with the following configs. |
| 85 | +Following is a table with all the possible properties that you can set when creating your own `AxiosQueueManager` instance. |
42 | 86 |
|
43 |
| -| Key | Optional | Default Value | Type | Description | |
44 |
| -| --------- | -------- | ------------- | ------------- | --------------------------------------------------------- | |
45 |
| -| queueSize | True | 10 | Number | Represents the queue's maximum size | |
46 |
| -| client | True | axios.default | AxiosInstance | Can be set to use a custom axios instance in the requests | |
| 87 | +| Key | Optional | Default Value | Type | Description | |
| 88 | +| --------- | -------- | ------------- | ------------- | ----------------------------------------------------------------------------- | |
| 89 | +| queueSize | True | 10 | Number | Represents the queue's maximum size | |
| 90 | +| client | True | axios.default | AxiosInstance | Custom axios instance that will be used under the hood to handle the requests | |
0 commit comments