-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.js
62 lines (53 loc) · 2.34 KB
/
rest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Wrapper over `needle` for making asynchronious HTTP requests, with improved request setting and responses.
const {merge} = require("merge-anything")
const {stringify: stringifyQueryParameters} = require("query-string-for-all")
const {request} = require("needle")
module.exports = /*async*/ function(method, url, payload, query, headers, environment) {
method = method.toUpperCase()
headers = merge(
{
"Cache-Control": "no-cache",
"Accept": "application/json" // prefer to receive JSON
},
headers || {}
)
const querystring = typeof query === "string"
? query
: stringifyQueryParameters(query, {encode: false, sort: false})
const href = querystring.length > 0
? url + "?" + querystring
: url
return new Promise(function(resolve, reject) {
request(
method, // request method
href, // request url (including the querystring)
payload, // body payload
{ // needle settings
headers,
open_timeout: 15000, // the default read_timeout and response_timeout is 0
json: !headers["Content-Type"] || /json/i.test(headers["Content-Type"]), // {json: true} adds headers ["Content-Type", "Accept"] with value "application/json", then stringifies the body payload and automatically parses the response body
parse_cookies: false
},
function(err, res) { // response callback (equal to Promise.then)
if(err || res.statusCode < 200 && res.statusCode > 299) {
return reject(err)
}
let response = merge({}, res.body) // object copy
Object.defineProperty(response, "_request_params", { // IMPORTANT: Cache original request parameters inside the response object as a hidden property! (eg. useful for continuous, paginated requests)
enumerable: false,
writable: false,
value: {
method,
url,
query,
href,
payload,
headers,
environment
}
})
return resolve(response)
}
)
})
}