@weapnl/js-junction 0.0.7 → 0.0.8
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 +4 -0
- package/README.md +7 -4
- package/package.json +1 -1
- package/src/api.js +16 -1
- package/src/builder/model.js +4 -4
- package/src/request.js +42 -24
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -292,20 +292,23 @@ let request = await api.request('users/files')
|
|
|
292
292
|
```javascript
|
|
293
293
|
let request = await api.request('users')
|
|
294
294
|
.onSuccess((data) => {
|
|
295
|
-
// Request successful
|
|
295
|
+
// Request successful.
|
|
296
296
|
})
|
|
297
297
|
.onUnauthorized((response) => {
|
|
298
|
-
// Missing or invalid token
|
|
298
|
+
// Missing or invalid token.
|
|
299
299
|
})
|
|
300
300
|
.onForbidden((response) => {
|
|
301
|
-
// Access not allowed
|
|
301
|
+
// Access not allowed.
|
|
302
302
|
})
|
|
303
303
|
.onValidationError((validation) => {
|
|
304
304
|
// validation.message
|
|
305
305
|
// validation.errors
|
|
306
306
|
})
|
|
307
307
|
.onError((response) => {
|
|
308
|
-
// Any other status code not caught by other callbacks
|
|
308
|
+
// Any other status code not caught by other callbacks.
|
|
309
|
+
})
|
|
310
|
+
.onFinished((response) => {
|
|
311
|
+
// After the request was finished (cancelled requests excluded).
|
|
309
312
|
})
|
|
310
313
|
.get();
|
|
311
314
|
```
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -15,6 +15,7 @@ export default class Api {
|
|
|
15
15
|
this._onValidationError = () => {};
|
|
16
16
|
this._onUnauthorized = () => {};
|
|
17
17
|
this._onForbidden = () => {};
|
|
18
|
+
this._onFinished = () => {};
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -84,7 +85,8 @@ export default class Api {
|
|
|
84
85
|
.onError(this._onError)
|
|
85
86
|
.onValidationError(this._onValidationError)
|
|
86
87
|
.onUnauthorized(this._onUnauthorized)
|
|
87
|
-
.onForbidden(this._onForbidden)
|
|
88
|
+
.onForbidden(this._onForbidden)
|
|
89
|
+
.onFinished(this._onFinished);
|
|
88
90
|
|
|
89
91
|
return request;
|
|
90
92
|
}
|
|
@@ -212,6 +214,19 @@ export default class Api {
|
|
|
212
214
|
return this;
|
|
213
215
|
}
|
|
214
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Set the default 'onFinished'. Can be overridden in the Request class.
|
|
219
|
+
*
|
|
220
|
+
* @param {function(Response)} callback
|
|
221
|
+
*
|
|
222
|
+
* @returns {this} The current instance.
|
|
223
|
+
*/
|
|
224
|
+
onFinished (callback = () => {}) {
|
|
225
|
+
this._onFinished = callback;
|
|
226
|
+
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
|
|
215
230
|
/**
|
|
216
231
|
* @param {function(Response)} onSuccess
|
|
217
232
|
* @param {function(Error)} onError
|
package/src/builder/model.js
CHANGED
|
@@ -118,8 +118,8 @@ export default class Model extends Request {
|
|
|
118
118
|
async index () {
|
|
119
119
|
this._connection.cancelRunning(this);
|
|
120
120
|
|
|
121
|
-
this._response = await this._connection.
|
|
122
|
-
this._queryString(),
|
|
121
|
+
this._response = await this._connection.post(
|
|
122
|
+
this._queryString() + '/index',
|
|
123
123
|
this.bodyParameters,
|
|
124
124
|
);
|
|
125
125
|
|
|
@@ -150,8 +150,8 @@ export default class Model extends Request {
|
|
|
150
150
|
|
|
151
151
|
this._connection.cancelRunning(this);
|
|
152
152
|
|
|
153
|
-
this._response = await this._connection.
|
|
154
|
-
this._queryString(identifier),
|
|
153
|
+
this._response = await this._connection.post(
|
|
154
|
+
this._queryString(identifier) + '/show',
|
|
155
155
|
this.bodyParameters,
|
|
156
156
|
);
|
|
157
157
|
|
package/src/request.js
CHANGED
|
@@ -30,6 +30,7 @@ export default class Request {
|
|
|
30
30
|
this._onValidationError = () => {};
|
|
31
31
|
this._onUnauthorized = () => {};
|
|
32
32
|
this._onForbidden = () => {};
|
|
33
|
+
this._onFinished = () => {};
|
|
33
34
|
|
|
34
35
|
this._connection = new Connection();
|
|
35
36
|
|
|
@@ -239,39 +240,56 @@ export default class Request {
|
|
|
239
240
|
return this;
|
|
240
241
|
}
|
|
241
242
|
|
|
243
|
+
/**
|
|
244
|
+
* @param {function(Response)} callback
|
|
245
|
+
*
|
|
246
|
+
* @returns {this} The current instance.
|
|
247
|
+
*/
|
|
248
|
+
onFinished (callback = () => {}) {
|
|
249
|
+
this._onFinished = callback;
|
|
250
|
+
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
242
254
|
/**
|
|
243
255
|
* @param {Response} response
|
|
244
256
|
* @param {*} successResponse
|
|
245
257
|
*/
|
|
246
258
|
async triggerResponseEvents (response, successResponse = null) {
|
|
247
259
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
248
|
-
if (
|
|
260
|
+
if (this._onSuccess) {
|
|
261
|
+
await this._onSuccess(...[successResponse, response.data].filter((value) => !! value));
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
switch (response.statusCode) {
|
|
265
|
+
case 401:
|
|
266
|
+
if (this._onUnauthorized) await this._onUnauthorized(response);
|
|
267
|
+
break;
|
|
268
|
+
case 403:
|
|
269
|
+
if (this._onForbidden) await this._onForbidden(response);
|
|
270
|
+
break;
|
|
271
|
+
case 422:
|
|
272
|
+
const validation = {
|
|
273
|
+
message: response.validation.message,
|
|
274
|
+
errors: {},
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
_.each(response.validation.errors, (value, key) => {
|
|
278
|
+
return _.set(validation.errors, key, value);
|
|
279
|
+
});
|
|
249
280
|
|
|
250
|
-
|
|
281
|
+
if (this._onValidationError) await this._onValidationError(validation);
|
|
282
|
+
break;
|
|
283
|
+
default:
|
|
284
|
+
if (this._onError) await this._onError(response);
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
251
287
|
}
|
|
252
288
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
case 403:
|
|
258
|
-
if (this._onForbidden) await this._onForbidden(response);
|
|
259
|
-
break;
|
|
260
|
-
case 422:
|
|
261
|
-
const validation = {
|
|
262
|
-
message: response.validation.message,
|
|
263
|
-
errors: {},
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
_.each(response.validation.errors, (value, key) => {
|
|
267
|
-
return _.set(validation.errors, key, value);
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
if (this._onValidationError) await this._onValidationError(validation);
|
|
271
|
-
break;
|
|
272
|
-
default:
|
|
273
|
-
if (this._onError) await this._onError(response);
|
|
274
|
-
break;
|
|
289
|
+
if (response.statusCode !== 0) {
|
|
290
|
+
if (this._onFinished) {
|
|
291
|
+
await this._onFinished(response);
|
|
292
|
+
}
|
|
275
293
|
}
|
|
276
294
|
}
|
|
277
295
|
|