@weapnl/js-junction 0.0.7 → 0.0.9
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 +9 -0
- package/README.md +7 -4
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/api.js +17 -2
- package/src/builder/model.js +4 -4
- package/src/connection.js +4 -0
- package/src/request.js +55 -26
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## v0.0.9
|
|
6
|
+
- Added option to override the config of an request.
|
|
7
|
+
- Added the body parameters to the post request.
|
|
8
|
+
- Updated the requests variable from a object to an array.
|
|
9
|
+
|
|
10
|
+
## v0.0.8
|
|
11
|
+
- Model post requests.
|
|
12
|
+
- Added onFinished callback.
|
|
13
|
+
|
|
5
14
|
## v0.0.7
|
|
6
15
|
- Request cancel improvements.
|
|
7
16
|
|
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/index.d.ts
CHANGED
|
@@ -88,6 +88,7 @@ declare class Request extends Mixins {
|
|
|
88
88
|
onForbidden(callback?: (response: Response) => void): this;
|
|
89
89
|
triggerResponseEvents(response: Response, successResponse?: any): Promise<void>;
|
|
90
90
|
customParameters(parameters?: object): this;
|
|
91
|
+
setConfig(config: object): this;
|
|
91
92
|
setApi(api: Api): this;
|
|
92
93
|
}
|
|
93
94
|
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -6,7 +6,7 @@ export default class Api {
|
|
|
6
6
|
constructor () {
|
|
7
7
|
this.setHeader('X-Requested-With', 'XMLHttpRequest');
|
|
8
8
|
|
|
9
|
-
this._requests =
|
|
9
|
+
this._requests = [];
|
|
10
10
|
|
|
11
11
|
this.host('/').suffix('');
|
|
12
12
|
|
|
@@ -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/connection.js
CHANGED
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
|
|
|
@@ -58,6 +59,17 @@ export default class Request {
|
|
|
58
59
|
return this;
|
|
59
60
|
}
|
|
60
61
|
|
|
62
|
+
/**
|
|
63
|
+
* @param {object} config
|
|
64
|
+
*
|
|
65
|
+
* @returns {this} The current instance.
|
|
66
|
+
*/
|
|
67
|
+
setConfig (config) {
|
|
68
|
+
this._connection.setConfig(_.merge(this._connection.getConfig(), config));
|
|
69
|
+
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
/**
|
|
62
74
|
* @returns {this} The current instance.
|
|
63
75
|
*/
|
|
@@ -97,7 +109,7 @@ export default class Request {
|
|
|
97
109
|
|
|
98
110
|
this._response = await this._connection.post(
|
|
99
111
|
url,
|
|
100
|
-
data,
|
|
112
|
+
{ ...data, ...this.bodyParameters },
|
|
101
113
|
);
|
|
102
114
|
|
|
103
115
|
await this.triggerResponseEvents(this._response);
|
|
@@ -153,7 +165,7 @@ export default class Request {
|
|
|
153
165
|
|
|
154
166
|
this._connection.cancelRunning(this);
|
|
155
167
|
|
|
156
|
-
this.
|
|
168
|
+
this.setConfig({
|
|
157
169
|
headers: {
|
|
158
170
|
'Content-Type': 'multipart/form-data',
|
|
159
171
|
},
|
|
@@ -239,39 +251,56 @@ export default class Request {
|
|
|
239
251
|
return this;
|
|
240
252
|
}
|
|
241
253
|
|
|
254
|
+
/**
|
|
255
|
+
* @param {function(Response)} callback
|
|
256
|
+
*
|
|
257
|
+
* @returns {this} The current instance.
|
|
258
|
+
*/
|
|
259
|
+
onFinished (callback = () => {}) {
|
|
260
|
+
this._onFinished = callback;
|
|
261
|
+
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
|
|
242
265
|
/**
|
|
243
266
|
* @param {Response} response
|
|
244
267
|
* @param {*} successResponse
|
|
245
268
|
*/
|
|
246
269
|
async triggerResponseEvents (response, successResponse = null) {
|
|
247
270
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
248
|
-
if (
|
|
271
|
+
if (this._onSuccess) {
|
|
272
|
+
await this._onSuccess(...[successResponse, response.data].filter((value) => !! value));
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
switch (response.statusCode) {
|
|
276
|
+
case 401:
|
|
277
|
+
if (this._onUnauthorized) await this._onUnauthorized(response);
|
|
278
|
+
break;
|
|
279
|
+
case 403:
|
|
280
|
+
if (this._onForbidden) await this._onForbidden(response);
|
|
281
|
+
break;
|
|
282
|
+
case 422:
|
|
283
|
+
const validation = {
|
|
284
|
+
message: response.validation.message,
|
|
285
|
+
errors: {},
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
_.each(response.validation.errors, (value, key) => {
|
|
289
|
+
return _.set(validation.errors, key, value);
|
|
290
|
+
});
|
|
249
291
|
|
|
250
|
-
|
|
292
|
+
if (this._onValidationError) await this._onValidationError(validation);
|
|
293
|
+
break;
|
|
294
|
+
default:
|
|
295
|
+
if (this._onError) await this._onError(response);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
251
298
|
}
|
|
252
299
|
|
|
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;
|
|
300
|
+
if (response.statusCode !== 0) {
|
|
301
|
+
if (this._onFinished) {
|
|
302
|
+
await this._onFinished(response);
|
|
303
|
+
}
|
|
275
304
|
}
|
|
276
305
|
}
|
|
277
306
|
|