server-side requests made ez
yarn add @davalapar/request
const request = require('@davalapar/request');
(async () => {
const response = await request({
// optional Object
headers: {},
// optional String
auth: '',
// optional String
userAgent: '',
// optional String
referer: '',
// required String
url: 'https://...',
// optional Object
query: {},
// optional Boolean
text: true,
// optional Boolean
json: true,
// optional Boolean
compression: true,
// optional Object with JSON-compatible values:
// non-NaN finite numbers, booleans, undefined, null, plain arrays, plain objects
body: {},
// optional Array
form: [
{
// required String
name: 'photo',
// optional String
filename: 'photo.jpg',
// required String or Buffer or JSON-compatible values:
// non-NaN finite numbers, booleans, undefined, null, plain arrays, plain objects
data: '',
}
],
// optional String
destination: './dir/filename.ext',
// optional Number
timeout: 30000,
// optional Number
maxSize: 1000,
// optional Function
onProgress: (chunkSize, rContentLength, hContentLength) => {
console.log('received:', chunkSize);
console.log('total received:', rContentLength);
console.log('total expected:', hContentLength);
console.log('progress:', ((rContentLength / hContentLength) * 100).toFixed(2), ' %');
},
});
})();
headers
parameter
headers
userAgent
parameter
user-agent: userAgent
referer
or referrer
parameter
referrer: referer || referrer
auth
or authorization
parameter
authorization: auth || authorization
query
parameter
?foo=bar
body
, form
text
parameter
accept: text/*
String
if content-type: text/*
json
, destination
json
parameter
accept: application/json
Object
if content-type: application/json
text
, destination
body
parameter
method: POST
accept: application/json
content-type: application/json
form
POST
methodform
parameter
method: POST
content-type: multipart/form-data
body
destination
parameter
2XX
responsestext
, json
timeout
parameter
maxSize
parameter
compression
parameter
accept-encoding: br, gzip, deflate
content-encoding: br/gzip/deflate
200
, 201
, 204
301
, 302
, 307
, 308
location
if exists, then follows itcontent-length
if exists, for both compressed & non-compressed responsesBuffer
if text
& json
parameter not seterror
on response data json
parsing errorerror
on valid responses, with response data as error.data
ttl
, with Map
instance accessible at request.dnsCache
MIT | @davalapar