@weapnl/js-junction 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +17 -1
- package/dist/index.js +23 -12
- package/docker-compose.yml +12 -7
- package/package.json +1 -1
- package/src/api.js +35 -19
- package/src/batch.js +2 -2
- package/src/connection.js +3 -9
- package/src/request.js +118 -23
- package/src/response.js +50 -10
package/docker-compose.yml
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
services:
|
|
2
2
|
dev:
|
|
3
|
-
image:
|
|
3
|
+
image: node:22
|
|
4
|
+
user: "node"
|
|
5
|
+
working_dir: /home/node/app
|
|
4
6
|
volumes:
|
|
5
|
-
- ./:/
|
|
6
|
-
- ../Shared:/var/shared
|
|
7
|
+
- ./:/home/node/app
|
|
7
8
|
command: "tail -f /dev/null"
|
|
8
9
|
|
|
9
|
-
build:
|
|
10
|
-
image:
|
|
10
|
+
build-prod:
|
|
11
|
+
image: node:22
|
|
12
|
+
profiles: [prod]
|
|
13
|
+
user: "node"
|
|
14
|
+
working_dir: /home/node/app
|
|
15
|
+
environment:
|
|
16
|
+
- NODE_ENV=production
|
|
11
17
|
volumes:
|
|
12
|
-
- ./:/
|
|
13
|
-
- ../Shared:/var/shared
|
|
18
|
+
- ./:/home/node/app
|
|
14
19
|
command: "npm run prod"
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -10,12 +10,13 @@ export default class Api {
|
|
|
10
10
|
|
|
11
11
|
this.host('/').suffix('');
|
|
12
12
|
|
|
13
|
-
this._onSuccess =
|
|
14
|
-
this._onError =
|
|
15
|
-
this._onValidationError =
|
|
16
|
-
this._onUnauthorized =
|
|
17
|
-
this._onForbidden =
|
|
18
|
-
this._onFinished =
|
|
13
|
+
this._onSuccess = null;
|
|
14
|
+
this._onError = null;
|
|
15
|
+
this._onValidationError = null;
|
|
16
|
+
this._onUnauthorized = null;
|
|
17
|
+
this._onForbidden = null;
|
|
18
|
+
this._onFinished = null;
|
|
19
|
+
this._onCancelled = null;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
@@ -96,13 +97,15 @@ export default class Api {
|
|
|
96
97
|
const request = new Request();
|
|
97
98
|
|
|
98
99
|
request.setUrl(uri)
|
|
99
|
-
.setApi(this)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
100
|
+
.setApi(this);
|
|
101
|
+
|
|
102
|
+
if (this._onSuccess) request.onSuccess(this._onSuccess);
|
|
103
|
+
if (this._onError) request.onError(this._onError);
|
|
104
|
+
if (this._onValidationError) request.onValidationError(this._onValidationError);
|
|
105
|
+
if (this._onUnauthorized) request.onUnauthorized(this._onUnauthorized);
|
|
106
|
+
if (this._onForbidden) request.onForbidden(this._onForbidden);
|
|
107
|
+
if (this._onFinished) request.onFinished(this._onFinished);
|
|
108
|
+
if (this._onCancelled) request.onCancelled(this._onCancelled);
|
|
106
109
|
|
|
107
110
|
return request;
|
|
108
111
|
}
|
|
@@ -166,7 +169,7 @@ export default class Api {
|
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
/**
|
|
169
|
-
* Set the default 'onSuccess'
|
|
172
|
+
* Set the default 'onSuccess' event handler.
|
|
170
173
|
*
|
|
171
174
|
* @param {function(Response.data)} callback
|
|
172
175
|
*
|
|
@@ -179,7 +182,7 @@ export default class Api {
|
|
|
179
182
|
}
|
|
180
183
|
|
|
181
184
|
/**
|
|
182
|
-
* Set the default 'onError'
|
|
185
|
+
* Set the default 'onError' event handler.
|
|
183
186
|
*
|
|
184
187
|
* @param {function(Response)} callback
|
|
185
188
|
*
|
|
@@ -192,7 +195,7 @@ export default class Api {
|
|
|
192
195
|
}
|
|
193
196
|
|
|
194
197
|
/**
|
|
195
|
-
* Set the default 'onValidationError'
|
|
198
|
+
* Set the default 'onValidationError' event handler.
|
|
196
199
|
*
|
|
197
200
|
* @param {function(Response.validation)} callback
|
|
198
201
|
*
|
|
@@ -205,7 +208,7 @@ export default class Api {
|
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
/**
|
|
208
|
-
* Set the default 'onUnauthorized'
|
|
211
|
+
* Set the default 'onUnauthorized' event handler.
|
|
209
212
|
*
|
|
210
213
|
* @param {function(Response)} callback
|
|
211
214
|
*
|
|
@@ -218,7 +221,7 @@ export default class Api {
|
|
|
218
221
|
}
|
|
219
222
|
|
|
220
223
|
/**
|
|
221
|
-
* Set the default 'onForbidden'
|
|
224
|
+
* Set the default 'onForbidden' event handler.
|
|
222
225
|
*
|
|
223
226
|
* @param {function(Response)} callback
|
|
224
227
|
*
|
|
@@ -231,7 +234,7 @@ export default class Api {
|
|
|
231
234
|
}
|
|
232
235
|
|
|
233
236
|
/**
|
|
234
|
-
* Set the default 'onFinished'
|
|
237
|
+
* Set the default 'onFinished' event handler.
|
|
235
238
|
*
|
|
236
239
|
* @param {function(Response)} callback
|
|
237
240
|
*
|
|
@@ -243,6 +246,19 @@ export default class Api {
|
|
|
243
246
|
return this;
|
|
244
247
|
}
|
|
245
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Set the default 'onCancelled' event handler.
|
|
251
|
+
*
|
|
252
|
+
* @param {function(Response)} callback
|
|
253
|
+
*
|
|
254
|
+
* @returns {this} The current instance.
|
|
255
|
+
*/
|
|
256
|
+
onCancelled (callback = () => {}) {
|
|
257
|
+
this._onCancelled = callback;
|
|
258
|
+
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
246
262
|
/**
|
|
247
263
|
* @param {function(Response)} onSuccess
|
|
248
264
|
* @param {function(Error)} onError
|
package/src/batch.js
CHANGED
|
@@ -8,11 +8,11 @@ export default class Batch {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
get successfulRequests () {
|
|
11
|
-
return _.filter(this._requests, ['response.
|
|
11
|
+
return _.filter(this._requests, ['response.isFailed', false]);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
get failedRequests () {
|
|
15
|
-
return _.filter(this._requests, ['response.
|
|
15
|
+
return _.filter(this._requests, ['response.isFailed', true]);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
async get () { return this.execute('get'); }
|
package/src/connection.js
CHANGED
|
@@ -78,18 +78,12 @@ export default class Connection {
|
|
|
78
78
|
|
|
79
79
|
await request
|
|
80
80
|
.then((axiosResponse) => {
|
|
81
|
-
response.
|
|
81
|
+
response.setAxiosResponse(axiosResponse);
|
|
82
82
|
})
|
|
83
|
-
.catch((
|
|
83
|
+
.catch((axiosError) => {
|
|
84
84
|
this.failed = true;
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (error.response) {
|
|
89
|
-
statusCode = error.response.status;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
response.set(statusCode, error);
|
|
86
|
+
response.setAxiosError(axiosError);
|
|
93
87
|
})
|
|
94
88
|
.finally(() => {
|
|
95
89
|
this.running = false;
|
package/src/request.js
CHANGED
|
@@ -25,12 +25,13 @@ export default class Request {
|
|
|
25
25
|
this._pagination = new Pagination();
|
|
26
26
|
this._customParameters = [];
|
|
27
27
|
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
28
|
+
this._onSuccessCallbacks = [];
|
|
29
|
+
this._onErrorCallbacks = [];
|
|
30
|
+
this._onValidationErrorCallbacks = [];
|
|
31
|
+
this._onUnauthorizedCallbacks = [];
|
|
32
|
+
this._onForbiddenCallbacks = [];
|
|
33
|
+
this._onFinishedCallbacks = [];
|
|
34
|
+
this._onCancelledCallbacks = [];
|
|
34
35
|
|
|
35
36
|
this._connection = new Connection();
|
|
36
37
|
|
|
@@ -223,7 +224,7 @@ export default class Request {
|
|
|
223
224
|
* @returns {this} The current instance.
|
|
224
225
|
*/
|
|
225
226
|
onSuccess (callback = () => {}) {
|
|
226
|
-
this.
|
|
227
|
+
this._onSuccessCallbacks.push(callback);
|
|
227
228
|
|
|
228
229
|
return this;
|
|
229
230
|
}
|
|
@@ -234,7 +235,7 @@ export default class Request {
|
|
|
234
235
|
* @returns {this} The current instance.
|
|
235
236
|
*/
|
|
236
237
|
onError (callback = () => {}) {
|
|
237
|
-
this.
|
|
238
|
+
this._onErrorCallbacks.push(callback);
|
|
238
239
|
|
|
239
240
|
return this;
|
|
240
241
|
}
|
|
@@ -245,7 +246,7 @@ export default class Request {
|
|
|
245
246
|
* @returns {this} The current instance.
|
|
246
247
|
*/
|
|
247
248
|
onValidationError (callback = () => {}) {
|
|
248
|
-
this.
|
|
249
|
+
this._onValidationErrorCallbacks.push(callback);
|
|
249
250
|
|
|
250
251
|
return this;
|
|
251
252
|
}
|
|
@@ -256,7 +257,7 @@ export default class Request {
|
|
|
256
257
|
* @returns {this} The current instance.
|
|
257
258
|
*/
|
|
258
259
|
onUnauthorized (callback = () => {}) {
|
|
259
|
-
this.
|
|
260
|
+
this._onUnauthorizedCallbacks.push(callback);
|
|
260
261
|
|
|
261
262
|
return this;
|
|
262
263
|
}
|
|
@@ -267,7 +268,7 @@ export default class Request {
|
|
|
267
268
|
* @returns {this} The current instance.
|
|
268
269
|
*/
|
|
269
270
|
onForbidden (callback = () => {}) {
|
|
270
|
-
this.
|
|
271
|
+
this._onForbiddenCallbacks.push(callback);
|
|
271
272
|
|
|
272
273
|
return this;
|
|
273
274
|
}
|
|
@@ -278,7 +279,95 @@ export default class Request {
|
|
|
278
279
|
* @returns {this} The current instance.
|
|
279
280
|
*/
|
|
280
281
|
onFinished (callback = () => {}) {
|
|
281
|
-
this.
|
|
282
|
+
this._onFinishedCallbacks.push(callback);
|
|
283
|
+
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @param {function(Response)} callback
|
|
289
|
+
*
|
|
290
|
+
* @returns {this} The current instance.
|
|
291
|
+
*/
|
|
292
|
+
onCancelled (callback = () => {}) {
|
|
293
|
+
this._onCancelledCallbacks.push(callback);
|
|
294
|
+
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Clears all `onSuccess` callbacks.
|
|
300
|
+
*
|
|
301
|
+
* @returns {this} The current instance.
|
|
302
|
+
*/
|
|
303
|
+
clearOnSuccessCallbacks () {
|
|
304
|
+
this._onSuccessCallbacks = [];
|
|
305
|
+
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Clears all `onError` callbacks.
|
|
311
|
+
*
|
|
312
|
+
* @returns {this} The current instance.
|
|
313
|
+
*/
|
|
314
|
+
clearOnErrorCallbacks () {
|
|
315
|
+
this._onErrorCallbacks = [];
|
|
316
|
+
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Clears all `onValidationError` callbacks.
|
|
322
|
+
*
|
|
323
|
+
* @returns {this} The current instance.
|
|
324
|
+
*/
|
|
325
|
+
clearOnValidationErrorCallbacks () {
|
|
326
|
+
this._onValidationErrorCallbacks = [];
|
|
327
|
+
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Clears all `onUnauthorized` callbacks.
|
|
333
|
+
*
|
|
334
|
+
* @returns {this} The current instance.
|
|
335
|
+
*/
|
|
336
|
+
clearOnUnauthorizedCallbacks () {
|
|
337
|
+
this._onUnauthorizedCallbacks = [];
|
|
338
|
+
|
|
339
|
+
return this;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Clears all `onForbidden` callbacks.
|
|
344
|
+
*
|
|
345
|
+
* @returns {this} The current instance.
|
|
346
|
+
*/
|
|
347
|
+
clearOnForbiddenCallbacks () {
|
|
348
|
+
this._onForbiddenCallbacks = [];
|
|
349
|
+
|
|
350
|
+
return this;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Clears all `onFinished` callbacks.
|
|
355
|
+
*
|
|
356
|
+
* @returns {this} The current instance.
|
|
357
|
+
*/
|
|
358
|
+
clearOnFinishedCallbacks () {
|
|
359
|
+
this._onFinishedCallbacks = [];
|
|
360
|
+
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Clears all `onCancelled` callbacks.
|
|
366
|
+
*
|
|
367
|
+
* @returns {this} The current instance.
|
|
368
|
+
*/
|
|
369
|
+
clearOnCancelledCallbacks () {
|
|
370
|
+
this._onCancelledCallbacks = [];
|
|
282
371
|
|
|
283
372
|
return this;
|
|
284
373
|
}
|
|
@@ -288,17 +377,21 @@ export default class Request {
|
|
|
288
377
|
* @param {*} successResponse
|
|
289
378
|
*/
|
|
290
379
|
async triggerResponseEvents (response, successResponse = null) {
|
|
380
|
+
function executeCallbacks(callbacks, ...data) {
|
|
381
|
+
return Promise.all(callbacks.map(async callback => {
|
|
382
|
+
await callback(...data);
|
|
383
|
+
}))
|
|
384
|
+
}
|
|
385
|
+
|
|
291
386
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
292
|
-
|
|
293
|
-
await this._onSuccess(...[successResponse, response.data].filter((value) => !! value));
|
|
294
|
-
}
|
|
387
|
+
await executeCallbacks(this._onSuccessCallbacks, ...[successResponse, response.data].filter((value) => !! value));
|
|
295
388
|
} else {
|
|
296
389
|
switch (response.statusCode) {
|
|
297
390
|
case 401:
|
|
298
|
-
|
|
391
|
+
await executeCallbacks(this._onUnauthorizedCallbacks, response);
|
|
299
392
|
break;
|
|
300
393
|
case 403:
|
|
301
|
-
|
|
394
|
+
await executeCallbacks(this._onForbiddenCallbacks, response);
|
|
302
395
|
break;
|
|
303
396
|
case 422:
|
|
304
397
|
const validation = {
|
|
@@ -310,18 +403,20 @@ export default class Request {
|
|
|
310
403
|
return _.set(validation.errors, key, value);
|
|
311
404
|
});
|
|
312
405
|
|
|
313
|
-
|
|
406
|
+
await executeCallbacks(this._onValidationErrorCallbacks, validation);
|
|
314
407
|
break;
|
|
315
408
|
default:
|
|
316
|
-
|
|
409
|
+
await executeCallbacks(this._onErrorCallbacks, response);
|
|
317
410
|
break;
|
|
318
411
|
}
|
|
319
412
|
}
|
|
320
413
|
|
|
321
|
-
if (response.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
414
|
+
if (response.isFinished) {
|
|
415
|
+
await executeCallbacks(this._onFinishedCallbacks, response);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (response.isCancelled) {
|
|
419
|
+
await executeCallbacks(this._onCancelledCallbacks, response);
|
|
325
420
|
}
|
|
326
421
|
}
|
|
327
422
|
|
package/src/response.js
CHANGED
|
@@ -1,33 +1,73 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
1
3
|
export default class Response {
|
|
2
4
|
constructor () {
|
|
3
5
|
this._axiosResponse = null;
|
|
4
|
-
|
|
5
|
-
this.statusCode = null;
|
|
6
|
+
this._axiosError = null;
|
|
6
7
|
|
|
7
8
|
this.data = null;
|
|
8
|
-
|
|
9
9
|
this.validation = null;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `isFailed` instead.
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
12
16
|
get failed () {
|
|
17
|
+
return this.isFailed;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
get isFailed () {
|
|
13
24
|
return this.statusCode >= 400;
|
|
14
25
|
}
|
|
15
26
|
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Check whether the request is finished and returned a response.
|
|
29
|
+
*
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
get isFinished () {
|
|
33
|
+
return this._axiosResponse !== null;
|
|
34
|
+
}
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Check whether the request was cancelled.
|
|
38
|
+
*
|
|
39
|
+
* @returns {boolean}
|
|
40
|
+
*/
|
|
41
|
+
get isCancelled () {
|
|
42
|
+
return axios.isCancel(this._axiosError);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @returns {Number} The HTTP response status code.
|
|
47
|
+
*/
|
|
48
|
+
get statusCode () {
|
|
49
|
+
return this._axiosResponse?.status;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setAxiosResponse(axiosResponse) {
|
|
53
|
+
this._axiosResponse = axiosResponse;
|
|
20
54
|
|
|
21
55
|
switch (this.statusCode) {
|
|
22
56
|
case 200:
|
|
23
|
-
this.data = this._axiosResponse.data;
|
|
24
|
-
break;
|
|
25
57
|
case 400:
|
|
26
|
-
this.data = this._axiosResponse.
|
|
58
|
+
this.data = this._axiosResponse.data;
|
|
27
59
|
break;
|
|
28
60
|
case 422:
|
|
29
|
-
this.validation = this._axiosResponse.
|
|
61
|
+
this.validation = this._axiosResponse.data;
|
|
30
62
|
break;
|
|
31
63
|
}
|
|
32
64
|
}
|
|
65
|
+
|
|
66
|
+
setAxiosError (axiosError) {
|
|
67
|
+
this._axiosError = axiosError;
|
|
68
|
+
|
|
69
|
+
if (axiosError.response) {
|
|
70
|
+
this.setAxiosResponse(axiosError.response);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
33
73
|
}
|