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
162 lines (144 loc) · 4.31 KB
/
web-node.js
File metadata and controls
162 lines (144 loc) · 4.31 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* @flow */
/* global window */
import superagent from 'superagent';
import {
EndpointDefinition,
StatusAnnouncement,
} from '../../core/flow_interfaces';
function log(req: Object) {
let _pickLogger = () => {
if (console && console.log) return console; // eslint-disable-line no-console
if (window && window.console && window.console.log) return window.console;
return console;
};
let start = new Date().getTime();
let timestamp = new Date().toISOString();
let logger = _pickLogger();
logger.log('<<<<<'); // eslint-disable-line no-console
logger.log(`[${timestamp}]`, '\n', req.url, '\n', req.qs); // eslint-disable-line no-console
logger.log('-----'); // eslint-disable-line no-console
req.on('response', (res) => {
let now = new Date().getTime();
let elapsed = now - start;
let timestampDone = new Date().toISOString();
logger.log('>>>>>>'); // eslint-disable-line no-console
logger.log(
`[${timestampDone} / ${elapsed}]`,
'\n',
req.url,
'\n',
req.qs,
'\n',
res.text
); // eslint-disable-line no-console
logger.log('-----'); // eslint-disable-line no-console
});
}
function xdr(
superagentConstruct: superagent,
endpoint: EndpointDefinition,
callback: Function
): Object {
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((err, resp) => {
let parsedResponse;
let status: StatusAnnouncement = {};
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);
});
}
export function get(
params: Object,
endpoint: EndpointDefinition,
callback: Function
): superagent {
let superagentConstruct = superagent
.get(this.getStandardOrigin() + endpoint.url)
.set(endpoint.headers)
.query(params);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
export function post(
params: Object,
body: string,
endpoint: EndpointDefinition,
callback: Function
): superagent {
let superagentConstruct = superagent
.post(this.getStandardOrigin() + endpoint.url)
.query(params)
.set(endpoint.headers)
.send(body);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
export function patch(
params: Object,
body: string,
endpoint: EndpointDefinition,
callback: Function
): superagent {
let superagentConstruct = superagent
.patch(this.getStandardOrigin() + endpoint.url)
.query(params)
.set(endpoint.headers)
.send(body);
return xdr.call(this, superagentConstruct, endpoint, callback);
}
export function del(
params: Object,
endpoint: EndpointDefinition,
callback: Function
): superagent {
let superagentConstruct = superagent
.delete(this.getStandardOrigin() + endpoint.url)
.set(endpoint.headers)
.query(params);
return xdr.call(this, superagentConstruct, endpoint, callback);
}