@weapnl/js-junction 0.0.6 → 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 CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.0.8
6
+ - Model post requests.
7
+ - Added onFinished callback.
8
+
9
+ ## v0.0.7
10
+ - Request cancel improvements.
11
+
5
12
  ## v0.0.6
6
13
  - Added support for request cancellation by key.
7
14
 
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapnl/js-junction",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "This project allows you to easily consume API's built with Junction.",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
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
@@ -116,8 +116,10 @@ export default class Model extends Request {
116
116
  * @returns {this[]} List of models.
117
117
  */
118
118
  async index () {
119
- this._response = await this._connection.get(
120
- this._queryString(),
119
+ this._connection.cancelRunning(this);
120
+
121
+ this._response = await this._connection.post(
122
+ this._queryString() + '/index',
121
123
  this.bodyParameters,
122
124
  );
123
125
 
@@ -146,8 +148,10 @@ export default class Model extends Request {
146
148
 
147
149
  if (! identifier) return null;
148
150
 
149
- this._response = await this._connection.get(
150
- this._queryString(identifier),
151
+ this._connection.cancelRunning(this);
152
+
153
+ this._response = await this._connection.post(
154
+ this._queryString(identifier) + '/show',
151
155
  this.bodyParameters,
152
156
  );
153
157
 
@@ -170,6 +174,8 @@ export default class Model extends Request {
170
174
  * @returns {this} The created model.
171
175
  */
172
176
  async store (extraData = {}) {
177
+ this._connection.cancelRunning(this);
178
+
173
179
  this._response = await this._connection.post(
174
180
  this._queryString(),
175
181
  { ...this._attributes.toJson(this), ...extraData },
@@ -194,6 +200,8 @@ export default class Model extends Request {
194
200
  * @returns {this} The updated model.
195
201
  */
196
202
  async update (extraData = {}) {
203
+ this._connection.cancelRunning(this);
204
+
197
205
  this._response = await this._connection.put(
198
206
  this._queryString(this._identifier),
199
207
  { ...this._attributes.toJson(this), ...extraData },
@@ -216,6 +224,8 @@ export default class Model extends Request {
216
224
  * @returns {boolean} Whether the deletion was successful.
217
225
  */
218
226
  async destroy () {
227
+ this._connection.cancelRunning(this);
228
+
219
229
  this._response = await this._connection.delete(
220
230
  this._queryString(this._identifier),
221
231
  );
package/src/connection.js CHANGED
@@ -11,8 +11,6 @@ export default class Connection {
11
11
  this.running = false;
12
12
  this.canceled = false;
13
13
  this.failed = false;
14
-
15
- this._request = null;
16
14
  }
17
15
 
18
16
  cancel () {
@@ -23,8 +21,8 @@ export default class Connection {
23
21
  this.canceled = true;
24
22
  }
25
23
 
26
- setRequest (request) {
27
- this._request = request;
24
+ cancelRunning (request) {
25
+ this._api.cancelRunning(request);
28
26
  }
29
27
 
30
28
  setConfig (config) {
@@ -58,8 +56,6 @@ export default class Connection {
58
56
  url = `/${url}`;
59
57
  }
60
58
 
61
- this._api.cancelRunning(this._request);
62
-
63
59
  const config = {
64
60
  url: (this._api ? this._api.baseUrl : api.baseUrl) + url,
65
61
  method,
package/src/request.js CHANGED
@@ -30,9 +30,9 @@ 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
- this._connection.setRequest(this);
36
36
 
37
37
  this._response = null;
38
38
  this.key = null;
@@ -74,6 +74,8 @@ export default class Request {
74
74
  async get () {
75
75
  const url = this.url ?? this.constructor.endpoint;
76
76
 
77
+ this._connection.cancelRunning(this);
78
+
77
79
  this._response = await this._connection.get(
78
80
  url,
79
81
  this.bodyParameters,
@@ -92,6 +94,8 @@ export default class Request {
92
94
  async post (data = {}) {
93
95
  const url = this.url ?? this.constructor.endpoint;
94
96
 
97
+ this._connection.cancelRunning(this);
98
+
95
99
  this._response = await this._connection.post(
96
100
  url,
97
101
  data,
@@ -110,6 +114,8 @@ export default class Request {
110
114
  async put (data = {}) {
111
115
  const url = this.url ?? this.constructor.endpoint;
112
116
 
117
+ this._connection.cancelRunning(this);
118
+
113
119
  this._response = await this._connection.put(
114
120
  url,
115
121
  { ...data, ...this.bodyParameters },
@@ -126,6 +132,8 @@ export default class Request {
126
132
  async delete () {
127
133
  const url = this.url ?? this.constructor.endpoint;
128
134
 
135
+ this._connection.cancelRunning(this);
136
+
129
137
  this._response = await this._connection.delete(
130
138
  url,
131
139
  );
@@ -144,6 +152,8 @@ export default class Request {
144
152
  async storeFiles (files = {}, data = {}) {
145
153
  const url = this.url ?? this.constructor.endpoint;
146
154
 
155
+ this._connection.cancelRunning(this);
156
+
147
157
  this._connection.setConfig({
148
158
  headers: {
149
159
  'Content-Type': 'multipart/form-data',
@@ -230,39 +240,56 @@ export default class Request {
230
240
  return this;
231
241
  }
232
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
+
233
254
  /**
234
255
  * @param {Response} response
235
256
  * @param {*} successResponse
236
257
  */
237
258
  async triggerResponseEvents (response, successResponse = null) {
238
259
  if (response.statusCode >= 200 && response.statusCode < 300) {
239
- if (! this._onSuccess) return;
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
+ });
240
280
 
241
- return await this._onSuccess(...[successResponse, response.data].filter((value) => !! value));
281
+ if (this._onValidationError) await this._onValidationError(validation);
282
+ break;
283
+ default:
284
+ if (this._onError) await this._onError(response);
285
+ break;
286
+ }
242
287
  }
243
288
 
244
- switch (response.statusCode) {
245
- case 401:
246
- if (this._onUnauthorized) await this._onUnauthorized(response);
247
- break;
248
- case 403:
249
- if (this._onForbidden) await this._onForbidden(response);
250
- break;
251
- case 422:
252
- const validation = {
253
- message: response.validation.message,
254
- errors: {},
255
- };
256
-
257
- _.each(response.validation.errors, (value, key) => {
258
- return _.set(validation.errors, key, value);
259
- });
260
-
261
- if (this._onValidationError) await this._onValidationError(validation);
262
- break;
263
- default:
264
- if (this._onError) await this._onError(response);
265
- break;
289
+ if (response.statusCode !== 0) {
290
+ if (this._onFinished) {
291
+ await this._onFinished(response);
292
+ }
266
293
  }
267
294
  }
268
295