@4i4/multi-request
v1.0.2
Published
Multi-request for AXIOS
Readme
MultiRequest
Build multiple requests based on Axios with option for subrequests and execute them as concurrent requests.
How this package can help you?
This module is using Axios as HTTP client and can be considered as helper If you need to execute concurrent requests or/and have dependencies between the requests.
Installing:
Using npm:
$ npm install @4i4/multi-requestUsing yarn:
$ yarn add @4i4/multi-requestUsing pnpm:
$ pnpm add @4i4/multi-requestExample
import MultiRequest from "@4i4/multi-request";
const baseUrl = "https://swapi.dev"
const client = new MultiRequest(baseUrl);
client.addRequest({
id: "film",
url: "/api/films/1",
subrequest: response => {
const { characters } = response.data;
return characters.map((url, index) => ({
id: `character-${index}`,
url: url
}));
}
});
client.addRequest({
id: "people",
url: "/api/people/",
onSuccess: response => {
console.log("People:");
console.table(response.data.results);
}
});
client.addRequest({
id: "planets",
url: "/api/planets/",
onSuccess: response => {
console.log("Planets:");
console.table(response.data.results);
}
});
client.addRequest({
id: "species",
url: "/api/species/",
onSuccess: response => {
console.log("Species:");
console.table(response.data.results);
}
});
await client.execute();Creating an instance
new MultiRequest()
const client = new MultiRequest();new MultiRequest([baseUrl])
const client = new MultiRequest("https://swapi.dev"); // common baseUrlnew MultiRequest([options])
const client = new MultiRequest({
baseUrl: "https://swapi.dev", // common baseUrl
headers: {} // common headers
});Instance methods
.addRequest(config)
Request Config
The multi-request configuration is extended version of Axios Request configuration.
The list bellow is what MultiRequest introduce. For all other properties and methods please check here.
| Property | Type | Optional | Description |
|--------------|------------|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| id | string | Yes | id is the request ID that can be used to identify the response for this request. If one is not provided it will be automatically created. |
| weight | number | Yes | It will be used to sort the responses. The default value is 0 |
| subrequest | function | Yes | The subrequest method receives two parameters - response and requestId. It should return new request configuration or array of request configurations. |
| onSuccess | function | Yes | The onSuccess method receives two parameters - response and requestId. |
| onError | function | Yes | It's receiving an error object. |
.execute()
Execute all requests and returns the responses sorted by the weight property.
.getResponseHeaders([requestId])
| Request ID | Description |
|----------------|--------------------------------------------------|
| null | Returns the response headers from all requests. |
| string | Returns the response headers for single request. |
| RegExp | Returns the response headers for each match. |
.getResponseData([requestId])
| Request ID | Description |
|----------------|-----------------------------------------------|
| null | Returns the response data from all requests. |
| string | Returns the response data for single request. |
| RegExp | Returns the response data for each match. |
