@weapnl/js-junction 0.0.9 → 0.0.11
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 +12 -10
- package/index.d.ts +3 -0
- package/package.json +1 -1
- package/src/api.js +16 -0
- package/src/builder/model.js +10 -0
- package/src/connection.js +4 -0
- package/src/mixins/paginationMixin.js +14 -0
- package/src/request/pagination.js +6 -0
- package/src/request.js +10 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## v0.0.11
|
|
6
|
+
- Fixed a bug where cancelled requests would cause issues when cancelling subsequent requests.
|
|
7
|
+
|
|
8
|
+
## v0.0.10
|
|
9
|
+
- Updated Readme.md
|
|
10
|
+
- Added onFinished to the index.d.ts file (TS support).
|
|
11
|
+
- Remove request from _requests array after the request was finished.
|
|
12
|
+
- Added support for simple pagination.
|
|
13
|
+
|
|
5
14
|
## v0.0.9
|
|
6
15
|
- Added option to override the config of an request.
|
|
7
16
|
- Added the body parameters to the post request.
|
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,6 +88,7 @@ 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;
|
|
91
94
|
setConfig(config: object): this;
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -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,19 @@ 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
|
+
if (request.response.statusCode !== 0) {
|
|
80
|
+
delete this._requests[request.key];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
68
84
|
/**
|
|
69
85
|
* @param {string} uri
|
|
70
86
|
*
|
package/src/builder/model.js
CHANGED
|
@@ -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
|
@@ -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
|
@@ -92,6 +92,8 @@ export default class Request {
|
|
|
92
92
|
this.bodyParameters,
|
|
93
93
|
);
|
|
94
94
|
|
|
95
|
+
this._connection.removeRequest(this);
|
|
96
|
+
|
|
95
97
|
await this.triggerResponseEvents(this._response);
|
|
96
98
|
|
|
97
99
|
return this;
|
|
@@ -112,6 +114,8 @@ export default class Request {
|
|
|
112
114
|
{ ...data, ...this.bodyParameters },
|
|
113
115
|
);
|
|
114
116
|
|
|
117
|
+
this._connection.removeRequest(this);
|
|
118
|
+
|
|
115
119
|
await this.triggerResponseEvents(this._response);
|
|
116
120
|
|
|
117
121
|
return this;
|
|
@@ -132,6 +136,8 @@ export default class Request {
|
|
|
132
136
|
{ ...data, ...this.bodyParameters },
|
|
133
137
|
);
|
|
134
138
|
|
|
139
|
+
this._connection.removeRequest(this);
|
|
140
|
+
|
|
135
141
|
await this.triggerResponseEvents(this._response);
|
|
136
142
|
|
|
137
143
|
return this;
|
|
@@ -149,6 +155,8 @@ export default class Request {
|
|
|
149
155
|
url,
|
|
150
156
|
);
|
|
151
157
|
|
|
158
|
+
this._connection.removeRequest(this);
|
|
159
|
+
|
|
152
160
|
await this.triggerResponseEvents(this._response);
|
|
153
161
|
|
|
154
162
|
return this;
|
|
@@ -178,6 +186,8 @@ export default class Request {
|
|
|
178
186
|
formData,
|
|
179
187
|
);
|
|
180
188
|
|
|
189
|
+
this._connection.removeRequest(this);
|
|
190
|
+
|
|
181
191
|
await this.triggerResponseEvents(this._response);
|
|
182
192
|
|
|
183
193
|
return this;
|