laravel-request 1.1.29 → 1.1.31
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/package.json +1 -1
- package/src/ApiRequest.js +162 -101
- package/src/types.d.ts +99 -3
package/package.json
CHANGED
package/src/ApiRequest.js
CHANGED
|
@@ -114,7 +114,7 @@ export default class ApiRequest {
|
|
|
114
114
|
}, errorCallback = () => {
|
|
115
115
|
}) {
|
|
116
116
|
this.data['paginateType'] = 'first';
|
|
117
|
-
return this.
|
|
117
|
+
return this.executeRequest(successCallback, errorCallback);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
@@ -127,7 +127,7 @@ export default class ApiRequest {
|
|
|
127
127
|
}, errorCallback = () => {
|
|
128
128
|
}) {
|
|
129
129
|
this.data['paginateType'] = 'all';
|
|
130
|
-
return this.
|
|
130
|
+
return this.executeRequest(successCallback, errorCallback);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
/**
|
|
@@ -144,7 +144,7 @@ export default class ApiRequest {
|
|
|
144
144
|
this.data['paginateType'] = 'paginate';
|
|
145
145
|
this.data['page'] = page;
|
|
146
146
|
this.data['perPage'] = perPage;
|
|
147
|
-
return this.
|
|
147
|
+
return this.executeRequest(successCallback, errorCallback);
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/**
|
|
@@ -159,9 +159,13 @@ export default class ApiRequest {
|
|
|
159
159
|
}) {
|
|
160
160
|
this.data['paginateType'] = 'pluck';
|
|
161
161
|
this.data['fields'] = fields;
|
|
162
|
-
return this.
|
|
162
|
+
return this.executeRequest(successCallback, errorCallback);
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
/**
|
|
166
|
+
*
|
|
167
|
+
* @return {string}
|
|
168
|
+
*/
|
|
165
169
|
getUrl()
|
|
166
170
|
{
|
|
167
171
|
return this.domain + '/api/v1/call/' + this.target + '/' + this.focus;
|
|
@@ -180,62 +184,131 @@ export default class ApiRequest {
|
|
|
180
184
|
*/
|
|
181
185
|
call(successCallback = (r) => {
|
|
182
186
|
}, errorCallback = () => {
|
|
183
|
-
}, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query', byUrl = false)
|
|
184
|
-
|
|
185
|
-
this.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
let url = this.domain + '/api/v1/call/' + this.target + '/' + this.focus;
|
|
189
|
-
|
|
190
|
-
if(byUrl)
|
|
191
|
-
{
|
|
192
|
-
url = this.url;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let notify = null;
|
|
187
|
+
}, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query', byUrl = false)
|
|
188
|
+
{
|
|
189
|
+
return this.executeRequest(successCallback, errorCallback, params, dataKey, argumentsKey, queryKey, byUrl)
|
|
190
|
+
}
|
|
196
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Constructs request data object based on provided keys
|
|
194
|
+
* @param dataKey
|
|
195
|
+
* @param argumentsKey
|
|
196
|
+
* @param queryKey
|
|
197
|
+
* @return {object} data object for request
|
|
198
|
+
*/
|
|
199
|
+
constructRequestData(dataKey, argumentsKey, queryKey) {
|
|
197
200
|
let data = {};
|
|
198
201
|
|
|
199
|
-
if(argumentsKey)
|
|
202
|
+
if (argumentsKey)
|
|
200
203
|
{
|
|
201
204
|
data[argumentsKey] = this.arguments;
|
|
202
205
|
}
|
|
203
|
-
|
|
204
|
-
if(queryKey)
|
|
206
|
+
if (queryKey)
|
|
205
207
|
{
|
|
206
208
|
data[queryKey] = this.builder.toArray();
|
|
207
209
|
}
|
|
208
210
|
|
|
209
|
-
if(dataKey)
|
|
211
|
+
if (dataKey)
|
|
210
212
|
{
|
|
211
213
|
data[dataKey] = this.data;
|
|
212
214
|
}else{
|
|
213
215
|
data = this.data;
|
|
214
216
|
}
|
|
215
217
|
|
|
218
|
+
return data;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Generates error notification based on the xhr response
|
|
223
|
+
* @param xhr
|
|
224
|
+
* @param errorText
|
|
225
|
+
* @return {object|null} notification object
|
|
226
|
+
*/
|
|
227
|
+
getErrorNotification(xhr, errorText) {
|
|
228
|
+
let notify = null;
|
|
229
|
+
|
|
230
|
+
if (xhr.readyState === 4) {
|
|
231
|
+
switch (xhr.status) {
|
|
232
|
+
case 0:
|
|
233
|
+
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
234
|
+
break;
|
|
235
|
+
case 404:
|
|
236
|
+
// handle 404 specifically if needed
|
|
237
|
+
break;
|
|
238
|
+
default:
|
|
239
|
+
notify = this.defaultErrorMessage(xhr, errorText);
|
|
240
|
+
}
|
|
241
|
+
} else if (xhr.readyState === 0) {
|
|
242
|
+
notify = this.getNotifyManager().errorOnce('network_error', 'Ошибка', ' (Network Error) или невозможность получения доступа к сети');
|
|
243
|
+
} else {
|
|
244
|
+
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return notify;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
*
|
|
252
|
+
* @param xhr
|
|
253
|
+
* @param errorText
|
|
254
|
+
* @return {null}
|
|
255
|
+
*/
|
|
256
|
+
defaultErrorMessage(xhr, errorText) {
|
|
257
|
+
let notify = null;
|
|
258
|
+
|
|
259
|
+
if (xhr?.responseJSON?.meta.text) {
|
|
260
|
+
notify = this.getNotifyManager().error('Ошибка', xhr.responseJSON.meta.text);
|
|
261
|
+
} else if (xhr?.responseJSON?.meta.message) {
|
|
262
|
+
notify = this.getNotifyManager().error('Ошибка', xhr.responseJSON.meta.message);
|
|
263
|
+
} else if (typeof errorText === 'string') {
|
|
264
|
+
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
265
|
+
} else if (errorText?.message) {
|
|
266
|
+
notify = this.getNotifyManager().error('Ошибка', errorText.message);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return notify;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Provides a fallback notification in case of an exception
|
|
275
|
+
* @param e
|
|
276
|
+
* @return {object|null} notification object
|
|
277
|
+
*/
|
|
278
|
+
getErrorNotificationFallback(e) {
|
|
279
|
+
console.error(e);
|
|
280
|
+
if (e?.message) return this.getNotifyManager().error('Ошибка', e.message);
|
|
281
|
+
return this.getNotifyManager().error('Ошибка');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Function to handle the common request logic
|
|
286
|
+
* @param successCallback
|
|
287
|
+
* @param errorCallback
|
|
288
|
+
* @param params
|
|
289
|
+
* @param dataKey
|
|
290
|
+
* @param argumentsKey
|
|
291
|
+
* @param queryKey
|
|
292
|
+
* @param byUrl
|
|
293
|
+
* @return {ApiRequest}
|
|
294
|
+
*/
|
|
295
|
+
executeRequest(successCallback, errorCallback, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query', byUrl = false) {
|
|
296
|
+
let self = this;
|
|
297
|
+
this.callSuccess = successCallback;
|
|
298
|
+
this.callError = errorCallback;
|
|
299
|
+
|
|
300
|
+
let url = byUrl ? this.url : this.getUrl();
|
|
301
|
+
let notify = null;
|
|
302
|
+
let data = this.constructRequestData(dataKey, argumentsKey, queryKey);
|
|
303
|
+
|
|
216
304
|
Api.makeRequest({
|
|
217
305
|
url: url,
|
|
218
306
|
method: this.method,
|
|
219
307
|
data: data,
|
|
220
308
|
params: params,
|
|
221
309
|
success: (response, status, xhr) => {
|
|
222
|
-
if(response && response.result === 'success')
|
|
223
|
-
|
|
224
|
-
if (response.meta && response.meta.text)
|
|
225
|
-
{
|
|
226
|
-
let result = this.notifyCallback(xhr.status);
|
|
227
|
-
|
|
228
|
-
if(result)
|
|
229
|
-
{
|
|
230
|
-
if(response?.meta.text)
|
|
231
|
-
{
|
|
232
|
-
notify = this.getNotifyManager().info('Успешно', response.meta.text);
|
|
233
|
-
}else if(response?.meta.message)
|
|
234
|
-
{
|
|
235
|
-
notify = this.getNotifyManager().info('Успешно', response.meta.message);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
310
|
+
if (response && response.result === 'success') {
|
|
311
|
+
notify = this.handleSuccessNotification(response, xhr.status);
|
|
239
312
|
self.toBind(response);
|
|
240
313
|
self.resetBindErrors();
|
|
241
314
|
successCallback(response, status, xhr);
|
|
@@ -249,6 +322,27 @@ export default class ApiRequest {
|
|
|
249
322
|
return this;
|
|
250
323
|
}
|
|
251
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Handles the success notification
|
|
327
|
+
* @param response
|
|
328
|
+
* @param status
|
|
329
|
+
* @return {object|null} notification object
|
|
330
|
+
*/
|
|
331
|
+
handleSuccessNotification(response, status) {
|
|
332
|
+
let notify = null;
|
|
333
|
+
let result = this.notifyCallback(status);
|
|
334
|
+
|
|
335
|
+
if (result) {
|
|
336
|
+
if (response?.meta.text) {
|
|
337
|
+
notify = this.getNotifyManager().info('Успешно', response.meta.text);
|
|
338
|
+
} else if (response?.meta.message) {
|
|
339
|
+
notify = this.getNotifyManager().info('Успешно', response.meta.message);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return notify;
|
|
344
|
+
}
|
|
345
|
+
|
|
252
346
|
/**
|
|
253
347
|
*
|
|
254
348
|
* @param notify
|
|
@@ -258,67 +352,15 @@ export default class ApiRequest {
|
|
|
258
352
|
*/
|
|
259
353
|
handleError(notify, errorCallback, xhr, errorText)
|
|
260
354
|
{
|
|
261
|
-
console.log('------------')
|
|
262
|
-
console.log(xhr)
|
|
263
|
-
console.log(errorText)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
{
|
|
271
|
-
switch (xhr.status) {
|
|
272
|
-
case 0://точно ошибка
|
|
273
|
-
if(errorText === 'Request aborted')
|
|
274
|
-
{
|
|
275
|
-
notify = this.getNotifyManager().errorOnce('request_aborted', 'Ошибка', errorText);
|
|
276
|
-
}else{
|
|
277
|
-
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
break;
|
|
281
|
-
case 404:
|
|
282
|
-
|
|
283
|
-
break;
|
|
284
|
-
default:
|
|
285
|
-
if(xhr?.responseJSON?.meta.text)
|
|
286
|
-
{
|
|
287
|
-
notify = this.getNotifyManager().error('Ошибка', xhr.responseJSON.meta.text);
|
|
288
|
-
}else if(xhr?.responseJSON?.meta.message)
|
|
289
|
-
{
|
|
290
|
-
notify = this.getNotifyManager().error('Ошибка', xhr.responseJSON.meta.message);
|
|
291
|
-
}else{
|
|
292
|
-
if(typeof errorText === 'string')
|
|
293
|
-
{
|
|
294
|
-
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
295
|
-
}else if(errorText?.message && typeof errorText.message === 'string'){
|
|
296
|
-
notify = this.getNotifyManager().error('Ошибка', errorText.message);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
} catch (e) {
|
|
303
|
-
console.error(e);
|
|
304
|
-
if(e?.message && typeof e.message === 'string')
|
|
305
|
-
{
|
|
306
|
-
notify = this.notify ? this.getNotifyManager().error('Ошибка', e.message) : null;
|
|
307
|
-
}else{
|
|
308
|
-
notify = this.notify ? this.getNotifyManager().error('Ошибка') : null;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
else if (xhr.readyState === 0) {
|
|
313
|
-
notify = this.getNotifyManager().errorOnce('network_error', 'Ошибка', ' (Network Error) или невозможность получения доступа к сети');
|
|
314
|
-
}
|
|
315
|
-
else {
|
|
316
|
-
if(typeof errorText === 'string')
|
|
317
|
-
{
|
|
318
|
-
notify = this.getNotifyManager().error('Ошибка', errorText);
|
|
319
|
-
}else if(errorText?.message && typeof errorText.message === 'string'){
|
|
320
|
-
notify = this.getNotifyManager().error('Ошибка', errorText.message);
|
|
321
|
-
}
|
|
355
|
+
console.log('------------');
|
|
356
|
+
console.log(xhr);
|
|
357
|
+
console.log(errorText);
|
|
358
|
+
|
|
359
|
+
try {
|
|
360
|
+
let result = this.notifyCallback(xhr.status);
|
|
361
|
+
if (result) notify = this.getErrorNotification(xhr, errorText);
|
|
362
|
+
} catch (e) {
|
|
363
|
+
notify = this.getErrorNotificationFallback(e);
|
|
322
364
|
}
|
|
323
365
|
|
|
324
366
|
this.toBindErrors(xhr);
|
|
@@ -334,7 +376,7 @@ export default class ApiRequest {
|
|
|
334
376
|
callSync(successCallback = (r) => {
|
|
335
377
|
}, errorCallback = () => {
|
|
336
378
|
}) {
|
|
337
|
-
return this.
|
|
379
|
+
return this.executeRequest(successCallback, errorCallback, {async: false});
|
|
338
380
|
}
|
|
339
381
|
|
|
340
382
|
/**
|
|
@@ -351,7 +393,7 @@ export default class ApiRequest {
|
|
|
351
393
|
}, errorCallback = () => {
|
|
352
394
|
}, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query')
|
|
353
395
|
{
|
|
354
|
-
return this.
|
|
396
|
+
return this.executeRequest(successCallback, errorCallback, params, dataKey, argumentsKey, queryKey, true)
|
|
355
397
|
}
|
|
356
398
|
|
|
357
399
|
/**
|
|
@@ -398,12 +440,19 @@ export default class ApiRequest {
|
|
|
398
440
|
}
|
|
399
441
|
}
|
|
400
442
|
|
|
443
|
+
/**
|
|
444
|
+
*
|
|
445
|
+
*/
|
|
401
446
|
resetBindErrors() {
|
|
402
447
|
if (this.responseBindingErrors !== null) {
|
|
403
448
|
this.responseBindingErrors.fire({});
|
|
404
449
|
}
|
|
405
450
|
}
|
|
406
451
|
|
|
452
|
+
/**
|
|
453
|
+
*
|
|
454
|
+
* @param response
|
|
455
|
+
*/
|
|
407
456
|
toBindErrors(response = {})
|
|
408
457
|
{
|
|
409
458
|
if (this.responseBindingErrors !== null && 'responseJSON' in response && typeof response.responseJSON === 'object')
|
|
@@ -412,11 +461,23 @@ export default class ApiRequest {
|
|
|
412
461
|
}
|
|
413
462
|
}
|
|
414
463
|
|
|
464
|
+
/**
|
|
465
|
+
*
|
|
466
|
+
* @param obj
|
|
467
|
+
* @param item
|
|
468
|
+
* @param key
|
|
469
|
+
* @return {ApiRequest}
|
|
470
|
+
*/
|
|
415
471
|
withValidateForm(obj, item = 'formErrors', key = 'meta') {
|
|
416
472
|
this.responseBindingErrors = new Binding(obj, item, key);
|
|
417
473
|
return this;
|
|
418
474
|
}
|
|
419
475
|
|
|
476
|
+
/**
|
|
477
|
+
*
|
|
478
|
+
* @param callback
|
|
479
|
+
* @return {ApiRequest}
|
|
480
|
+
*/
|
|
420
481
|
withoutNotify(callback)
|
|
421
482
|
{
|
|
422
483
|
//callback(status){} будем проверять статус и по условию если true не показывать уведомление
|
package/src/types.d.ts
CHANGED
|
@@ -1,8 +1,104 @@
|
|
|
1
1
|
// Define an interface for the ApiRequest class
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
declare class ApiRequest {
|
|
3
|
+
url: string;
|
|
4
|
+
static notifyClass: any | null;
|
|
5
|
+
domain: string;
|
|
6
|
+
target: string;
|
|
7
|
+
focus: string;
|
|
8
|
+
method: string;
|
|
9
|
+
data: any;
|
|
10
|
+
headers: any[];
|
|
11
|
+
arguments: any[];
|
|
12
|
+
builder: Builder;
|
|
13
|
+
notifyCallback: (status: number) => boolean;
|
|
14
|
+
responseBinding: Binding | Binding[] | null;
|
|
15
|
+
responseBindingErrors: Binding | null;
|
|
16
|
+
callSuccess: Function;
|
|
17
|
+
callError: Function;
|
|
18
|
+
|
|
19
|
+
constructor(target: string, focus: string, data?: object, method?: string);
|
|
20
|
+
|
|
21
|
+
getNotifyManager(): any | null;
|
|
4
22
|
setUrl(url: string): ApiRequest;
|
|
5
|
-
|
|
23
|
+
setDomain(domain: string): ApiRequest;
|
|
24
|
+
addArg(arg: any | any[]): ApiRequest;
|
|
25
|
+
first(successCallback?: (r: any) => void, errorCallback?: () => void): any;
|
|
26
|
+
all(successCallback?: (r: any) => void, errorCallback?: () => void): any;
|
|
27
|
+
paginate(
|
|
28
|
+
page?: number,
|
|
29
|
+
perPage?: number,
|
|
30
|
+
successCallback?: (r: any) => void,
|
|
31
|
+
errorCallback?: () => void
|
|
32
|
+
): any;
|
|
33
|
+
pluck(
|
|
34
|
+
fields: any,
|
|
35
|
+
successCallback?: (r: any) => void,
|
|
36
|
+
errorCallback?: () => void
|
|
37
|
+
): ApiRequest;
|
|
38
|
+
getUrl(): string;
|
|
39
|
+
call(
|
|
40
|
+
successCallback?: (r: any) => void,
|
|
41
|
+
errorCallback?: () => void,
|
|
42
|
+
params?: object,
|
|
43
|
+
dataKey?: string,
|
|
44
|
+
argumentsKey?: string,
|
|
45
|
+
queryKey?: string,
|
|
46
|
+
byUrl?: boolean
|
|
47
|
+
): ApiRequest;
|
|
48
|
+
callSync(
|
|
49
|
+
successCallback?: (r: any) => void,
|
|
50
|
+
errorCallback?: () => void
|
|
51
|
+
): ApiRequest;
|
|
52
|
+
callUrl(
|
|
53
|
+
successCallback?: (r: any) => void,
|
|
54
|
+
errorCallback?: () => void,
|
|
55
|
+
params?: object,
|
|
56
|
+
dataKey?: string,
|
|
57
|
+
argumentsKey?: string,
|
|
58
|
+
queryKey?: string
|
|
59
|
+
): ApiRequest;
|
|
60
|
+
constructRequestData(
|
|
61
|
+
dataKey: string,
|
|
62
|
+
argumentsKey: string,
|
|
63
|
+
queryKey: string
|
|
64
|
+
): object;
|
|
65
|
+
getErrorNotification(xhr: XMLHttpRequest, errorText: string): object | null;
|
|
66
|
+
defaultErrorMessage(xhr: XMLHttpRequest, errorText: string): object | null;
|
|
67
|
+
getErrorNotificationFallback(e: Error): object | null;
|
|
68
|
+
executeRequest(
|
|
69
|
+
successCallback: (r: any) => void,
|
|
70
|
+
errorCallback: () => void,
|
|
71
|
+
params?: object,
|
|
72
|
+
dataKey?: string,
|
|
73
|
+
argumentsKey?: string,
|
|
74
|
+
queryKey?: string,
|
|
75
|
+
byUrl?: boolean
|
|
76
|
+
): ApiRequest;
|
|
77
|
+
handleSuccessNotification(
|
|
78
|
+
response: any,
|
|
79
|
+
status: number
|
|
80
|
+
): object | null;
|
|
81
|
+
handleError(
|
|
82
|
+
notify: object | null,
|
|
83
|
+
errorCallback: (xhr: XMLHttpRequest) => void,
|
|
84
|
+
xhr: XMLHttpRequest,
|
|
85
|
+
errorText: string
|
|
86
|
+
): void;
|
|
87
|
+
bind(
|
|
88
|
+
obj: any,
|
|
89
|
+
item: string | [string, string][],
|
|
90
|
+
rerender?: boolean,
|
|
91
|
+
cb?: Function
|
|
92
|
+
): ApiRequest;
|
|
93
|
+
toBind(response: any): void;
|
|
94
|
+
resetBindErrors(): void;
|
|
95
|
+
toBindErrors(response?: object): void;
|
|
96
|
+
withValidateForm(
|
|
97
|
+
obj: any,
|
|
98
|
+
item?: string,
|
|
99
|
+
key?: string
|
|
100
|
+
): ApiRequest;
|
|
101
|
+
withoutNotify(callback?: (status: number) => boolean): ApiRequest;
|
|
6
102
|
}
|
|
7
103
|
|
|
8
104
|
// Define the Api class type with all static methods
|