0.0.31 • Published 4 years ago
@davalapar/request v0.0.31
@davalapar/request
server-side requests made ez
Usage
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), ' %');
},
});
})();
Highlights
headers
parameter- sets
headers
- sets
userAgent
parameter- sets
user-agent: userAgent
- sets
referer
orreferrer
parameter- sets
referrer: referer || referrer
- sets
auth
orauthorization
parameter- sets
authorization: auth || authorization
- sets
query
parameter- for search / query parameters such as
?foo=bar
- compatible with:
body
,form
- for search / query parameters such as
text
parameter- sets
accept: text/*
- parse response into
String
ifcontent-type: text/*
- incompatible with:
json
,destination
- sets
json
parameter- sets
accept: application/json
- parse response into
Object
ifcontent-type: application/json
- incompatible with:
text
,destination
- sets
body
parameter- sets
method: POST
- sets
accept: application/json
- sets
content-type: application/json
- accepts non-ascii utf8 strings, emojis
- incompatible with:
form
- uses
POST
method
- sets
form
parameter- sets
method: POST
- sets
content-type: multipart/form-data
- accepts utf8 strings, binary data
- incompatible with:
body
- sets
destination
parameter- file destination path
- creates parent directories
- only works with valid
2XX
responses - incompatible with:
text
,json
timeout
parameter- request timeout in ms
maxSize
parameter- response max size in bytes
compression
parameter- sets
accept-encoding: br, gzip, deflate
- accepts
content-encoding: br/gzip/deflate
- sets
- built-ins
- valid 2XX responses:
200
,201
,204
- valid 3XX responses:
301
,302
,307
,308
- for 3XX responses: verifies
location
if exists, then follows it - verifies
content-length
if exists, for both compressed & non-compressed responses - returns
Buffer
iftext
&json
parameter not set - rejects with
error
on response datajson
parsing error - rejects with
error
on valid responses, with response data aserror.data
- caching of DNS lookups in respect to
ttl
, withMap
instance accessible atrequest.dnsCache
- valid 2XX responses:
License
MIT | @davalapar
0.0.31
4 years ago
0.0.30
4 years ago
0.0.29
4 years ago
0.0.28
4 years ago
0.0.27
4 years ago
0.0.26
4 years ago
0.0.25
4 years ago
0.0.24
5 years ago
0.0.23
5 years ago
0.0.22
5 years ago
0.0.21
5 years ago
0.0.20
5 years ago
0.0.19
5 years ago
0.0.18
5 years ago
0.0.17
5 years ago
0.0.15
5 years ago
0.0.14
5 years ago
0.0.13
5 years ago
0.0.12
5 years ago
0.0.9
5 years ago
0.0.8
5 years ago
0.0.6
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.1
5 years ago