forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-node.js
More file actions
127 lines (100 loc) · 3.96 KB
/
Copy pathweb-node.js
File metadata and controls
127 lines (100 loc) · 3.96 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.get = get;
exports.post = post;
exports.patch = patch;
exports.del = del;
var _superagent = _interopRequireDefault(require("superagent"));
var _flow_interfaces = require("../../core/flow_interfaces");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function log(req) {
var _pickLogger = function _pickLogger() {
if (console && console.log) return console;
if (window && window.console && window.console.log) return window.console;
return console;
};
var start = new Date().getTime();
var timestamp = new Date().toISOString();
var logger = _pickLogger();
logger.log('<<<<<');
logger.log("[".concat(timestamp, "]"), '\n', req.url, '\n', req.qs);
logger.log('-----');
req.on('response', function (res) {
var now = new Date().getTime();
var elapsed = now - start;
var timestampDone = new Date().toISOString();
logger.log('>>>>>>');
logger.log("[".concat(timestampDone, " / ").concat(elapsed, "]"), '\n', req.url, '\n', req.qs, '\n', res.text);
logger.log('-----');
});
}
function xdr(superagentConstruct, endpoint, callback) {
var _this = this;
if (this._config.logVerbosity) {
superagentConstruct = superagentConstruct.use(log);
}
if (this._config.proxy && this._modules.proxy) {
superagentConstruct = this._modules.proxy.call(this, superagentConstruct);
}
if (this._config.keepAlive && this._modules.keepAlive) {
superagentConstruct = this._modules.keepAlive(superagentConstruct);
}
return superagentConstruct.timeout(endpoint.timeout).end(function (err, resp) {
var parsedResponse;
var status = {};
status.error = err !== null;
status.operation = endpoint.operation;
if (resp && resp.status) {
status.statusCode = resp.status;
}
if (err) {
if (err.response && err.response.text && !_this._config.logVerbosity) {
try {
status.errorData = JSON.parse(err.response.text);
} catch (e) {
status.errorData = err;
}
} else {
status.errorData = err;
}
status.category = _this._detectErrorCategory(err);
return callback(status, null);
}
try {
parsedResponse = JSON.parse(resp.text);
} catch (e) {
status.errorData = resp;
status.error = true;
return callback(status, null);
}
if (parsedResponse.error && parsedResponse.error === 1 && parsedResponse.status && parsedResponse.message && parsedResponse.service) {
status.errorData = parsedResponse;
status.statusCode = parsedResponse.status;
status.error = true;
status.category = _this._detectErrorCategory(status);
return callback(status, null);
} else if (parsedResponse.error && parsedResponse.error.message) {
status.errorData = parsedResponse.error;
}
return callback(status, parsedResponse);
});
}
function get(params, endpoint, callback) {
var superagentConstruct = _superagent["default"].get(this.getStandardOrigin() + endpoint.url).set(endpoint.headers).query(params);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
function post(params, body, endpoint, callback) {
var superagentConstruct = _superagent["default"].post(this.getStandardOrigin() + endpoint.url).query(params).set(endpoint.headers).send(body);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
function patch(params, body, endpoint, callback) {
var superagentConstruct = _superagent["default"].patch(this.getStandardOrigin() + endpoint.url).query(params).set(endpoint.headers).send(body);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
function del(params, endpoint, callback) {
var superagentConstruct = _superagent["default"]["delete"](this.getStandardOrigin() + endpoint.url).set(endpoint.headers).query(params);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
//# sourceMappingURL=web-node.js.map