Gathering detailed insights and metrics for reqex
Gathering detailed insights and metrics for reqex
Gathering detailed insights and metrics for reqex
Gathering detailed insights and metrics for reqex
Promise based http client with built in retry and JSON validator for response
npm install reqex
Typescript
Module System
Min. Node Version
Node Version
NPM Version
68.3
Supply Chain
97.7
Quality
76.1
Maintenance
100
Vulnerability
100
License
Updated on 31 Aug 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
0%
Compared to previous day
Last week
50%
Compared to previous week
Last month
14.3%
Compared to previous month
Last year
32.4%
Compared to previous year
Promise based http client with built in retry and JSON validator for response.
1npm install reqex
reqex - is a simple http(s) client which allows to perform
GET
, POST
, PUT
and DELETE
requests.
Also response will contain parsed json and could return it
with a type if validate
method is used.
Implemented for convenience with types, and ability to chain request
methods. Also supports pipe
method for streaming response to writable/duplex stream.
Import reqex
to the TS/JS projects:
1// ESM or TypeScript projects: 2import reqex from 'reqex'; 3 4// CommonJS projects: 5const { reqex } = require('reqex');
Note
All responses for any
Content-Type
will havebody
field of string type. Thejson
field will have a parsed JSON object value for all responses which haveContent-Type: application/json
header, andundefined
value for all other types.
1const response = await reqex.get('http://localhost:3000/user'); 2 3console.log(response);
Expected output:
1{ 2 status: 200, 3 ok: true, 4 contentLength: 23, 5 headers: { 6 'x-powered-by': 'Express', 7 'content-type': 'application/json; charset=utf-8', 8 'content-length': '23', 9 etag: 'W/"17-notW5/nnoifrwkOLKeW655Vz8Xg"', 10 date: 'Fri, 15 Sep 2023 10:38:05 GMT', 11 connection: 'close' 12 }, 13 json: { user: { name: 'some user', id: 'some id' } }, 14 body: '{"user":{"name":"some user","id":"some id"}}' 15}
1const response = await reqex 2 .post('http://localhost:3000/user') 3 .body({ name: 'some new user' }); 4 5console.log(response);
Expected output:
1{ 2 status: 201, 3 ok: true, 4 contentLength: 52, 5 headers: { 6 'x-powered-by': 'Express', 7 'content-type': 'application/json; charset=utf-8', 8 'content-length': '52', 9 etag: 'W/"34-vZ5QItgbUr7K7/mmx0UbwgRbD+w"', 10 date: 'Fri, 15 Sep 2023 10:45:34 GMT', 11 connection: 'close' 12 }, 13 json: { user: { name: 'some new user', id: 'some new id' } }, 14 body: '{"user":{"name":"some new user","id":"some new id"}}' 15}
1const response = await reqex 2 .put('http://localhost:3000/user') 3 .body({ name: 'some updated user' }); 4 5console.log(response);
Expected output:
1{ 2 status: 200, 3 ok: true, 4 contentLength: 60, 5 headers: { 6 'x-powered-by': 'Express', 7 'content-type': 'application/json; charset=utf-8', 8 'content-length': '60', 9 etag: 'W/"3c-EIJ+q9sLuMYRrdJsituIMyqNoJw"', 10 date: 'Fri, 15 Sep 2023 10:47:09 GMT', 11 connection: 'close' 12 }, 13 json: { user: { name: 'some updated user', id: 'some updated id' } }, 14 body: '{"user":{"name":"some updated user","id":"some updated id"}}' 15}
1const response = await reqex 2 .delete('http://localhost:3000/user') 3 .body({ name: 'some user' }); 4 5console.log(response);
Expected output:
1{ 2 status: 204, 3 ok: true, 4 contentLength: 0, 5 headers: { 6 'x-powered-by': 'Express', 7 date: 'Fri, 15 Sep 2023 10:48:00 GMT', 8 connection: 'close' 9 }, 10 json: undefined, 11 body: '' 12}
Note
If
retry
method was called -pipe
method will be disregarded.
Does not mean to be used for 400+ status codes, but for connection errors and etc.
If troubles with API for performing requests to are possible - retry
method can handle such requests and automatically perform new one.
By default retries to do request in 10 seconds. Max interval time can be 60 seconds, and max amount of attempts - 15 times. If bigger value is passed - max value will be set.
Following example will perform up to 3 additional requests, if main request has failed:
1const response = await reqex 2 .get('http://localhost:3000/possible-unavailable') 3 .retry({ attempts: 3 });
Also, next request time can be specified (example - 25 seconds), and log can be added:
1const response = await reqex 2 .get('http://localhost:3000/possible-unavailable') 3 .retry({ attempts: 3, interval: 25, logOnRetry: true });
Note
If
retry
method was called -pipe
method will be disregarded.
Allows to pipe response to any writable
/duplex
stream. Also returns response as
reqex.get()
request:
1const stream = fs.createWriteStream('out.json'); 2 3const response = await reqex.get('http://localhost:3000/users').pipe(stream);
The vator library is used for validation.
.validate()
method is designed to assert data is matches provided schema.
In case of wrong type - error will be thrown.
1import { v } from 'reqex' 2 3const { json } = await reqex 4 .get('http://localhost:3000/user') 5 .validate({ 6 name: v.string; 7 id: v.number; 8 }); 9 10const { name, id } = json; 11 12console.log(`The "name" field has type "${typeof name}" and value "${name}"`); 13console.log(`The "id" field has type "${typeof id}" and value "${id}"`);
Expected output:
1The "name" field has type "string" and value "some user" 2The "id" field has type "number" and value "123"
1const response = await reqex 2 .get('http://localhost:3000/') 3 .headers({ 'content-type': 'text/html' }); 4 5console.log(response);
Expected output:
1{ 2 status: 200, 3 ok: true, 4 contentLength: 20, 5 headers: { 6 'x-powered-by': 'Express', 7 'content-type': 'text/html; charset=utf-8', 8 'content-length': '20', 9 etag: 'W/"14-XVEfTf8cEMbur8HQBK5MH4vJQ7U"', 10 date: 'Fri, 15 Sep 2023 10:52:59 GMT', 11 connection: 'close' 12 }, 13 json: undefined, 14 body: '<h1>Hello reqex!</h1>' 15}
Lets pretend your server responds with json
content-type for 404 Not Found
cases:
1const response = await reqex.get('http://localhost:3000/not-found'); 2 3console.log(response);
Expected output:
1{ 2 status: 404, 3 ok: false, 4 contentLength: 23, 5 headers: { 6 'x-powered-by': 'Express', 7 'content-type': 'application/json; charset=utf-8', 8 'content-length': '23', 9 etag: 'W/"17-SuRA/yvUWUo8rK6x7dKURLeBo+0"', 10 date: 'Fri, 15 Sep 2023 11:04:07 GMT', 11 connection: 'close' 12 }, 13 json: { message: 'Not Found' }, 14 body: '{"message":"Not Found"}' 15}
No vulnerabilities found.
No security vulnerabilities found.