0.0.31 • Published 6 years ago
@davalapar/request v0.0.31
@davalapar/request
server-side requests made ez
Usage
yarn add @davalapar/requestconst 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
headersparameter- sets
headers
- sets
userAgentparameter- sets
user-agent: userAgent
- sets
refererorreferrerparameter- sets
referrer: referer || referrer
- sets
authorauthorizationparameter- sets
authorization: auth || authorization
- sets
queryparameter- for search / query parameters such as
?foo=bar - compatible with:
body,form
- for search / query parameters such as
textparameter- sets
accept: text/* - parse response into
Stringifcontent-type: text/* - incompatible with:
json,destination
- sets
jsonparameter- sets
accept: application/json - parse response into
Objectifcontent-type: application/json - incompatible with:
text,destination
- sets
bodyparameter- sets
method: POST - sets
accept: application/json - sets
content-type: application/json - accepts non-ascii utf8 strings, emojis
- incompatible with:
form - uses
POSTmethod
- sets
formparameter- sets
method: POST - sets
content-type: multipart/form-data - accepts utf8 strings, binary data
- incompatible with:
body
- sets
destinationparameter- file destination path
- creates parent directories
- only works with valid
2XXresponses - incompatible with:
text,json
timeoutparameter- request timeout in ms
maxSizeparameter- response max size in bytes
compressionparameter- 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
locationif exists, then follows it - verifies
content-lengthif exists, for both compressed & non-compressed responses - returns
Bufferiftext&jsonparameter not set - rejects with
erroron response datajsonparsing error - rejects with
erroron valid responses, with response data aserror.data - caching of DNS lookups in respect to
ttl, withMapinstance accessible atrequest.dnsCache
- valid 2XX responses:
License
MIT | @davalapar
0.0.31
6 years ago
0.0.30
6 years ago
0.0.29
6 years ago
0.0.28
6 years ago
0.0.27
6 years ago
0.0.26
6 years ago
0.0.25
6 years ago
0.0.24
6 years ago
0.0.23
6 years ago
0.0.22
6 years ago
0.0.21
6 years ago
0.0.20
6 years ago
0.0.19
6 years ago
0.0.18
6 years ago
0.0.17
6 years ago
0.0.15
6 years ago
0.0.14
6 years ago
0.0.13
6 years ago
0.0.12
6 years ago
0.0.9
6 years ago
0.0.8
6 years ago
0.0.6
6 years ago
0.0.5
6 years ago
0.0.4
6 years ago
0.0.1
6 years ago