-
Notifications
You must be signed in to change notification settings - Fork 3
/
promise-request.js
51 lines (49 loc) · 1.58 KB
/
promise-request.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
'use strict';
var http = require('http'),
https = require('https'),
util = require('util');
/**
* Request
*
* returns a promise of an http request
*
* Parameters:
* - options:
- scheme: [string] http or https. Default: http
- serialize: [function] will be executed on request body. Default: JSON.stringify()
- deserialize: [function] will be executed on response body. Default: JSON.parse()
- others: see https://nodejs.org/api/http.html#http_http_request_options_callback
* - body: body string to send with the request (POST or PUT)
*/
module.exports = function(options, body) {
var _options = {
scheme: 'http',
serialize: JSON.stringify,
deserialize: JSON.parse
};
options = util._extend(_options, options);
return new Promise(function(resolve, reject) {
var module = options.scheme === 'https' ? https : http;
var request = module.request(options, function(response) {
if (response.statusCode >= 400) {
reject(response);
}
var _data = '';
response.on('data', function(data) {
_data += data.toString();
});
response.on('end', function() {
resolve(util._extend(response, {
data: options.deserialize(_data)
}));
});
response.on('error', function(error) {
reject(error);
});
});
if (body) {
request.write(options.serialize(body));
}
request.end();
});
};