@weapnl/js-junction 0.0.8 → 0.0.10

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,17 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.0.10
6
+ - Updated Readme.md
7
+ - Added onFinished to the index.d.ts file (TS support).
8
+ - Remove request from _requests array after the request was finished.
9
+ - Added support for simple pagination.
10
+
11
+ ## v0.0.9
12
+ - Added option to override the config of an request.
13
+ - Added the body parameters to the post request.
14
+ - Updated the requests variable from a object to an array.
15
+
5
16
  ## v0.0.8
6
17
  - Model post requests.
7
18
  - Added onFinished callback.
package/README.md CHANGED
@@ -45,20 +45,20 @@ class User extends Model {
45
45
  // Create a new user.
46
46
  const user = new User();
47
47
  user.name = 'John';
48
- user.store();
48
+ user.store(); // Or .save()
49
49
 
50
50
  // Retrieve a user.
51
- const user = User.show(1);
51
+ const user = await new User().show(1);
52
52
 
53
53
  // Update the user.
54
54
  user.name = 'Jane';
55
- user.update();
55
+ user.update(); // Or .save()
56
56
 
57
57
  // Delete the user.
58
58
  user.destroy();
59
59
 
60
60
  // List all users.
61
- const users = User.index();
61
+ const users = await new User().index();
62
62
  ```
63
63
 
64
64
  ## Usage
@@ -165,7 +165,7 @@ user.name = 'John';
165
165
  user.store();
166
166
 
167
167
  // Retrieve a user.
168
- const user = new User().show(1);
168
+ const user = await new User().show(1);
169
169
 
170
170
  // Update the user.
171
171
  user.name = 'Jane';
@@ -175,7 +175,7 @@ user.save(); // This will call store() or update() depending on the id of the mo
175
175
  user.destroy();
176
176
 
177
177
  // List all users.
178
- const users = new User().index();
178
+ const users = await new User().index();
179
179
  ```
180
180
 
181
181
  #### Applying filters and scopes
@@ -203,6 +203,7 @@ const request = await new User()
203
203
  .pluck(['name', 'company.name']) // Only retrieve the given fields
204
204
  .pagination(1, 25) // Paginate 25 per page, page 1
205
205
  .pagination(1, 25, 50) // The 3th parameter is a pageForId parameter. This is used to find the correct page for the given id. If the id can not be found, the given page will be used. It will now look for the user with id 50, and returns that page.
206
+ .simplePagination(1, 25) // You can use this to simply navigate through pages (without receiving the total pages). This can be helpful for large database tables. The first parameter is the page number and the second parameter is the `items per page` amount.
206
207
  .get();
207
208
  ```
208
209
 
@@ -215,15 +216,16 @@ To create a new record or update an existing one, you can use the `.save()` meth
215
216
  **Cloning a model's instance.**
216
217
  ```javascript
217
218
  // Return a clone of the model
219
+ const user = await new User().show(1);
218
220
  const clonedUser = user.clone();
219
221
  ```
220
222
 
221
223
  **Batch requests**
222
224
  ```javascript
223
- const firstUser = new User().show(1);
225
+ const firstUser = await new User().show(1);
224
226
  firstUser.name = 'John';
225
227
 
226
- const secondUser = new User().show(2);
228
+ const secondUser = await new User().show(2);
227
229
  secondUser.name = 'Jane';
228
230
 
229
231
  const batch = await api.batch([
@@ -266,7 +268,7 @@ const request = await api
266
268
  // Extra data
267
269
  });
268
270
 
269
- const user = new User().show(1);
271
+ const user = await new User().show(1);
270
272
 
271
273
  const request = await user
272
274
  .action('actionName') // It will take the id of the user now, since it is a model. The id is 1 in this case.
@@ -337,7 +339,7 @@ A previously pending request with the same key will be cancelled, and this new r
337
339
 
338
340
  This is also possible on a model:
339
341
  ```javascript
340
- const user = new User()
342
+ const user = await new User()
341
343
  .setKey('get-user')
342
344
  .index();
343
345
  ```
package/index.d.ts CHANGED
@@ -29,6 +29,7 @@ declare class Api {
29
29
  onValidationError(callback?: (validation: any) => void): this;
30
30
  onUnauthorized(callback?: (response: AxiosResponse) => void): this;
31
31
  onForbidden(callback?: (response: AxiosResponse) => void): this;
32
+ onFinished(callback?: (response: AxiosResponse) => void): this;
32
33
 
33
34
  responseInterceptors(
34
35
  onSuccess?: (response: AxiosResponse) => void,
@@ -67,6 +68,7 @@ declare class Mixins {
67
68
  action(name: string, id?: number): this;
68
69
 
69
70
  pagination(page: number, perPage?: number, findPageForId?: number | null): this;
71
+ simplePagination(page: number, perPage?: number): this;
70
72
  }
71
73
 
72
74
  declare class Request extends Mixins {
@@ -86,8 +88,10 @@ declare class Request extends Mixins {
86
88
  onValidationError(callback?: (validation: object) => void): this;
87
89
  onUnauthorized(callback?: (response: Response) => void): this;
88
90
  onForbidden(callback?: (response: Response) => void): this;
91
+ onFinished(callback?: (response: Response) => void): this;
89
92
  triggerResponseEvents(response: Response, successResponse?: any): Promise<void>;
90
93
  customParameters(parameters?: object): this;
94
+ setConfig(config: object): this;
91
95
  setApi(api: Api): this;
92
96
  }
93
97
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapnl/js-junction",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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
@@ -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
 
@@ -56,6 +56,9 @@ export default class Api {
56
56
  return url;
57
57
  }
58
58
 
59
+ /**
60
+ * @param {Request} request
61
+ */
59
62
  cancelRunning (request) {
60
63
  if (! request.key) {
61
64
  return;
@@ -65,6 +68,17 @@ export default class Api {
65
68
  this._requests[request.key] = request;
66
69
  }
67
70
 
71
+ /**
72
+ * @param {Request} request
73
+ */
74
+ removeRequest (request) {
75
+ if (! request.key) {
76
+ return;
77
+ }
78
+
79
+ delete this._requests[request.key];
80
+ }
81
+
68
82
  /**
69
83
  * @param {string} uri
70
84
  *
@@ -123,6 +123,8 @@ export default class Model extends Request {
123
123
  this.bodyParameters,
124
124
  );
125
125
 
126
+ this._connection.removeRequest(this);
127
+
126
128
  let items;
127
129
 
128
130
  if (this._response.data) {
@@ -155,6 +157,8 @@ export default class Model extends Request {
155
157
  this.bodyParameters,
156
158
  );
157
159
 
160
+ this._connection.removeRequest(this);
161
+
158
162
  let item;
159
163
 
160
164
  if (this._response.data) {
@@ -181,6 +185,8 @@ export default class Model extends Request {
181
185
  { ...this._attributes.toJson(this), ...extraData },
182
186
  );
183
187
 
188
+ this._connection.removeRequest(this);
189
+
184
190
  let item;
185
191
 
186
192
  if (this._response.data) {
@@ -207,6 +213,8 @@ export default class Model extends Request {
207
213
  { ...this._attributes.toJson(this), ...extraData },
208
214
  );
209
215
 
216
+ this._connection.removeRequest(this);
217
+
210
218
  let item;
211
219
 
212
220
  if (this._response.data) {
@@ -230,6 +238,8 @@ export default class Model extends Request {
230
238
  this._queryString(this._identifier),
231
239
  );
232
240
 
241
+ this._connection.removeRequest(this);
242
+
233
243
  await this.triggerResponseEvents(this._response);
234
244
 
235
245
  return !! this._response.data;
package/src/connection.js CHANGED
@@ -25,6 +25,14 @@ export default class Connection {
25
25
  this._api.cancelRunning(request);
26
26
  }
27
27
 
28
+ removeRequest (request) {
29
+ this._api.removeRequest(request);
30
+ }
31
+
32
+ getConfig () {
33
+ return this._config;
34
+ }
35
+
28
36
  setConfig (config) {
29
37
  this._config = config;
30
38
  }
@@ -12,6 +12,20 @@ const paginationMixin = {
12
12
  this._pagination.page(page);
13
13
  this._pagination.perPage(perPage);
14
14
  this._pagination.findPageForId(findPageForId);
15
+ this._pagination.simplePagination(false);
16
+
17
+ return this;
18
+ },
19
+
20
+ /**
21
+ * @param {int} page
22
+ * @param {int} [perPage]
23
+ * @returns {this}
24
+ */
25
+ simplePagination (page, perPage = 25) {
26
+ this._pagination.page(page);
27
+ this._pagination.perPage(perPage);
28
+ this._pagination.simplePagination(true);
15
29
 
16
30
  return this;
17
31
  },
@@ -3,6 +3,7 @@ export default class Pagination {
3
3
  this._page = null;
4
4
  this._perPage = null;
5
5
  this._findPageForId = null;
6
+ this._simplePagination = false;
6
7
  }
7
8
 
8
9
  filled () {
@@ -21,6 +22,10 @@ export default class Pagination {
21
22
  this._findPageForId = id;
22
23
  }
23
24
 
25
+ simplePagination (simplePagination) {
26
+ this._simplePagination = simplePagination;
27
+ }
28
+
24
29
  toObject () {
25
30
  const data = {};
26
31
 
@@ -28,6 +33,7 @@ export default class Pagination {
28
33
  data.page = this._page;
29
34
  data.paginate = this._perPage;
30
35
  data.page_for_id = this._findPageForId;
36
+ data.simple_pagination = this._simplePagination;
31
37
  }
32
38
 
33
39
  return data;
package/src/request.js CHANGED
@@ -59,6 +59,17 @@ export default class Request {
59
59
  return this;
60
60
  }
61
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
+
62
73
  /**
63
74
  * @returns {this} The current instance.
64
75
  */
@@ -81,6 +92,8 @@ export default class Request {
81
92
  this.bodyParameters,
82
93
  );
83
94
 
95
+ this._connection.removeRequest(this);
96
+
84
97
  await this.triggerResponseEvents(this._response);
85
98
 
86
99
  return this;
@@ -98,9 +111,11 @@ export default class Request {
98
111
 
99
112
  this._response = await this._connection.post(
100
113
  url,
101
- data,
114
+ { ...data, ...this.bodyParameters },
102
115
  );
103
116
 
117
+ this._connection.removeRequest(this);
118
+
104
119
  await this.triggerResponseEvents(this._response);
105
120
 
106
121
  return this;
@@ -121,6 +136,8 @@ export default class Request {
121
136
  { ...data, ...this.bodyParameters },
122
137
  );
123
138
 
139
+ this._connection.removeRequest(this);
140
+
124
141
  await this.triggerResponseEvents(this._response);
125
142
 
126
143
  return this;
@@ -138,6 +155,8 @@ export default class Request {
138
155
  url,
139
156
  );
140
157
 
158
+ this._connection.removeRequest(this);
159
+
141
160
  await this.triggerResponseEvents(this._response);
142
161
 
143
162
  return this;
@@ -154,7 +173,7 @@ export default class Request {
154
173
 
155
174
  this._connection.cancelRunning(this);
156
175
 
157
- this._connection.setConfig({
176
+ this.setConfig({
158
177
  headers: {
159
178
  'Content-Type': 'multipart/form-data',
160
179
  },
@@ -167,6 +186,8 @@ export default class Request {
167
186
  formData,
168
187
  );
169
188
 
189
+ this._connection.removeRequest(this);
190
+
170
191
  await this.triggerResponseEvents(this._response);
171
192
 
172
193
  return this;