forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNURESTConnection.js
286 lines (260 loc) · 8.71 KB
/
NURESTConnection.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import NUObject from './NUObject';
import NUInterceptor from './NUInterceptor';
import { getLogger } from './Logger';
import { Enum } from 'enumify';
import axios from 'axios';
class ResponseCodeEnum extends Enum {
static getClassName() {
return 'ResponseCodeEnum';
}
static getEnumForCode (code) {
return ResponseCodeEnum.enumValues.filter( item => item.code === code);
}
getClassName() {
return ResponseCodeEnum.getClassName();
}
static getErrors(error) {
const { response, message } = error;
if (!response) {
getLogger().error(`<<<< Error response without status: ${message}`);
return {
data: message
}
}
switch (response.status) {
case 400:
case 401:
case 403:
case 404:
case 405:
case 500:
if (response && response.data && Array.isArray(response.data.errors)) {
return {data: response.data};
}
const item = ResponseCodeEnum.getEnumForCode(response.status);
if (item && item.length) {
const descriptions = [item[0].errors];
return {
data: {
errors: [{
descriptions
}]
}
};
}
return response.statusText;
default:
return {
data: response.data
}
}
}
}
ResponseCodeEnum.initEnum({
Unauthorized: {
get code() { return 401; },
get errors() {
return {
title: "Permission denied",
description: "You are either not allowed to access this resource or you used an incorrect password."
}
}
},
PermissionDenied: {
get code() { return 403; },
get errors() {
return {
title: "Permission denied",
description: "You are not allowed to access this resource."
}
}
},
NotFound: {
get code() {
return 404;
},
get errors() {
return {
title: "Not Found",
description: "Requested URL is not found or server might not be reachable."
}
},
},
MethodNotAllowed: {
get code () { return 405; },
get errors() { return {}; }
},
PreconditionFailed: {
get code() { return 412; },
get errors() { return {}; }
},
BadRequest: {
get code() { return 400; },
get errors() {
return {
title: "Bad Request",
description: "This API call cannot be processed by the server. Please report this to the UI team"
}
}
},
CodeConflict: {
get code() { return 409; },
get errors() { return {}; }
},
InternalServerError: {
get code() { return 500; },
get errors() {
return {
title: "Internal Server Error",
description: "Please check the log and report this error to the server team"
}
}
},
ServiceUnavailable: {
get code() { return 503; },
get errors() { return {}; }
},
});
/*
This class implements the HTTP actions invoked on the server
*/
const RESPONSE_PARAM = 'responseChoice';
const validateStatus = (status) => (status >= 200 && status <= 300);
export default class NURESTConnection extends NUObject {
constructor(...args) {
super(...args);
this.defineProperties({
onMultipleChoices: null,
interceptor: new NUInterceptor(),
RESTConnectionTimeout: 0,
RESTConnectionLastActionTimer: null,
ignoreRequestIdle: false,
isConnected: false
});
}
static _stringyfyBody = (body) => {
if (!body) {
return "";
}
try {
const string = typeof body === 'string' ? JSON.parse(body) : body;
return JSON.stringify(string, null, 4);
}
catch (e) {
return body;
}
}
/*
Generic method for invoking HTTP/REST requests on the server
*/
makeRequest({requestURL, verb, headers, body, choice, cancelToken}) {
let finalURL;
if (choice !== undefined) {
const tmpURL = new URL(requestURL);
tmpURL.searchParams.set(RESPONSE_PARAM, choice);
finalURL = tmpURL.href;
} else {
finalURL = requestURL;
}
if (!this.ignoreRequestIdle) {
this._resetIdleTimeout();
}
getLogger().log(`>>>> Sending\n\n${verb} ${requestURL}:\n\n${NURESTConnection._stringyfyBody(body)}`);
return axios({
method: verb,
url: finalURL,
data: body,
headers: headers,
validateStatus,
cancelToken
}).then(response => {
const { data } = response;
getLogger().log(`<<<< Response for \n\n${verb} ${requestURL} (${response.status}):\n\n${NURESTConnection._stringyfyBody(data)}`);
//handle response with choice
if (response.status === 300 && this._onMultipleChoices) {
return this._onMultipleChoices(response.data, requestURL)
.then(choice => this.makeRequest({requestURL, verb, headers, body, choice, cancelToken}));
}
const result = ({ data, headers: {...response.headers}, response });
this.interceptor.success(result);
return result;
}).catch((error) => {
if (axios.isCancel(error)) {
// request was cancelled, possibly through the cancelToken
getLogger().error(`<<<< Request canceled ${error.message}`);
return Promise.reject(error);
} else {
// server returned with some sort of error response
const {response} = error;
const result = response ? {headers: {...response.headers}, response} : {};
const errors = ResponseCodeEnum.getErrors(error);
if (response) {
//handle error
if (response.status === 401) {
getLogger().error(`<<<< Authentication Failure: ${response.status}: ${response.statusText}`);
this.interceptor.onAuthenticationFailure({response, result, data: errors});
result.authFailure = true;
return Promise.reject({response, result, data: errors.data});
}
}
this.interceptor.fail({response, result, data: errors.data});
return Promise.reject(result);
}
});
}
/*
Allow connection to the server to timeout
*/
_resetIdleTimeout() {
if (this.RESTConnectionLastActionTimer) {
clearTimeout(this.RESTConnectionLastActionTimer);
}
if (!this.RESTConnectionTimeout) {
return;
}
const interceptor = this.interceptor;
const timeout = this.RESTConnectionTimeout;
this.RESTConnectionLastActionTimer = setTimeout(() => {
if (!timeout) {
return;
}
interceptor.onTimeout();
}, timeout);
}
/*
Invokes a GET request on the server
*/
makeGETRequest(requestURL, headers, cancelToken) {
return this.makeRequest({requestURL, verb: 'GET', headers, cancelToken});
}
/*
Invokes a PUT request on the server
*/
makePUTRequest(requestURL, headers, body, cancelToken) {
return this.makeRequest({requestURL, verb: 'PUT', headers, body, cancelToken});
}
/*
Invokes a POST request on the server
*/
makePOSTRequest(requestURL, headers, body, cancelToken) {
return this.makeRequest({requestURL, verb: 'POST', headers, body, cancelToken});
}
/*
Invokes a DELETE request on the server
*/
makeDELETERequest(requestURL, headers, cancelToken) {
return this.makeRequest({requestURL, verb: 'DELETE', headers, cancelToken});
}
/*
Invokes a HEAD request on the server
*/
makeHEADRequest(requestURL, headers, cancelToken) {
return this.makeRequest({requestURL, verb: 'HEAD', headers, cancelToken});
}
/*
Invokes a PATCH request on the server
*/
makePATCHRequest(requestURL, headers, body, cancelToken) {
return this.makeRequest({requestURL, verb: 'PATCH', headers, body, cancelToken});
}
}