laravel-request 1.2.3 → 1.2.5
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/Api.js +38 -25
- package/src/ApiRequest.js +14 -0
package/package.json
CHANGED
package/src/Api.js
CHANGED
|
@@ -4,11 +4,6 @@ import { decode } from "@msgpack/msgpack";
|
|
|
4
4
|
|
|
5
5
|
export default class Api {
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @type {ApiRequest}
|
|
10
|
-
*/
|
|
11
|
-
static debug = false;
|
|
12
7
|
/**
|
|
13
8
|
*
|
|
14
9
|
* @type {ApiRequest}
|
|
@@ -182,7 +177,7 @@ export default class Api {
|
|
|
182
177
|
*/
|
|
183
178
|
static logRequest(request)
|
|
184
179
|
{
|
|
185
|
-
if(
|
|
180
|
+
if(this.isDebug())
|
|
186
181
|
{
|
|
187
182
|
console.log('Url: ' + request.url)
|
|
188
183
|
console.log('Method: ' + request.method)
|
|
@@ -249,7 +244,7 @@ export default class Api {
|
|
|
249
244
|
* @param obj
|
|
250
245
|
* @return {*}
|
|
251
246
|
*/
|
|
252
|
-
static async makeRequest({url, method, data = {}, params = {}, headers = {}, success = () => {}, error = () => {}})
|
|
247
|
+
static async makeRequest({url, method, data = {}, params = {}, source, headers = {}, success = () => {}, error = () => {}})
|
|
253
248
|
{
|
|
254
249
|
try {
|
|
255
250
|
headers['Accept'] = 'application/json, application/msgpack, text/plain, */*';
|
|
@@ -265,10 +260,10 @@ export default class Api {
|
|
|
265
260
|
switch (method)
|
|
266
261
|
{
|
|
267
262
|
case 'GET':
|
|
268
|
-
response = await Api.handleGetRequest({ url, data, headers });
|
|
263
|
+
response = await Api.handleGetRequest({ url, data, headers, source });
|
|
269
264
|
break;
|
|
270
265
|
default:
|
|
271
|
-
response = await Api.handleDefaultRequest({ url, method, data, params, headers });
|
|
266
|
+
response = await Api.handleDefaultRequest({ url, method, data, params, headers, source });
|
|
272
267
|
break;
|
|
273
268
|
}
|
|
274
269
|
|
|
@@ -297,18 +292,31 @@ export default class Api {
|
|
|
297
292
|
console.error(e);
|
|
298
293
|
|
|
299
294
|
const response = e.response;
|
|
300
|
-
const xhr = response.request;
|
|
301
|
-
|
|
302
|
-
const contentType = response.headers.get("Content-Type");
|
|
303
|
-
const responseData = await Api.decodeResponse(url, response, contentType);
|
|
304
295
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
296
|
+
//если прервали запрос или проблему с соединение то response может не быть
|
|
297
|
+
if(response)
|
|
298
|
+
{
|
|
299
|
+
const xhr = response.request;
|
|
300
|
+
|
|
301
|
+
const contentType = response.headers.get("Content-Type");
|
|
302
|
+
const responseData = await Api.decodeResponse(url, response, contentType);
|
|
303
|
+
|
|
304
|
+
const statusCode = response.status;
|
|
305
|
+
const statusText = response.statusText;
|
|
306
|
+
|
|
307
|
+
try {
|
|
308
|
+
error(xhr, responseData, statusCode, statusText);
|
|
309
|
+
}catch (error){
|
|
310
|
+
console.error(error);
|
|
311
|
+
}
|
|
312
|
+
}else{
|
|
313
|
+
console.error(e)
|
|
314
|
+
|
|
315
|
+
try {
|
|
316
|
+
error({}, {}, '', e.message);
|
|
317
|
+
}catch (error){
|
|
318
|
+
console.error(error);
|
|
319
|
+
}
|
|
312
320
|
}
|
|
313
321
|
}else{
|
|
314
322
|
console.error(e)
|
|
@@ -336,9 +344,10 @@ export default class Api {
|
|
|
336
344
|
* @param url
|
|
337
345
|
* @param data
|
|
338
346
|
* @param headers
|
|
347
|
+
* @param source
|
|
339
348
|
* @return {Promise<AxiosResponse<any>>}
|
|
340
349
|
*/
|
|
341
|
-
static async handleGetRequest({ url, data, headers }) {
|
|
350
|
+
static async handleGetRequest({ url, data, headers, source }) {
|
|
342
351
|
let query = Api.encodeQueryString(data);
|
|
343
352
|
let response;
|
|
344
353
|
|
|
@@ -352,7 +361,8 @@ export default class Api {
|
|
|
352
361
|
data: data,
|
|
353
362
|
headers: headers,
|
|
354
363
|
timeout: 0,
|
|
355
|
-
responseType: 'arraybuffer'
|
|
364
|
+
responseType: 'arraybuffer',
|
|
365
|
+
cancelToken: source?.token
|
|
356
366
|
};
|
|
357
367
|
Api.logRequest(request);
|
|
358
368
|
response = await axios.request(request);
|
|
@@ -364,7 +374,8 @@ export default class Api {
|
|
|
364
374
|
params: data,
|
|
365
375
|
headers: headers,
|
|
366
376
|
timeout: 0,
|
|
367
|
-
responseType: 'arraybuffer'
|
|
377
|
+
responseType: 'arraybuffer',
|
|
378
|
+
cancelToken: source?.token
|
|
368
379
|
};
|
|
369
380
|
Api.logRequest(request);
|
|
370
381
|
response = await axios.request(request);
|
|
@@ -380,9 +391,10 @@ export default class Api {
|
|
|
380
391
|
* @param data
|
|
381
392
|
* @param params
|
|
382
393
|
* @param headers
|
|
394
|
+
* @param source
|
|
383
395
|
* @return {Promise<AxiosResponse<any>>}
|
|
384
396
|
*/
|
|
385
|
-
static async handleDefaultRequest({ url, method, data, params, headers }) {
|
|
397
|
+
static async handleDefaultRequest({ url, method, data, params, headers, source }) {
|
|
386
398
|
params.timestamp = new Date().getTime();
|
|
387
399
|
|
|
388
400
|
params.unique_hash = Api.generateHash();
|
|
@@ -394,7 +406,8 @@ export default class Api {
|
|
|
394
406
|
params: params,
|
|
395
407
|
headers: headers,
|
|
396
408
|
timeout: 0,
|
|
397
|
-
responseType: 'arraybuffer'
|
|
409
|
+
responseType: 'arraybuffer',
|
|
410
|
+
cancelToken: source?.token
|
|
398
411
|
};
|
|
399
412
|
Api.logRequest(request);
|
|
400
413
|
return await axios.request(request);
|
package/src/ApiRequest.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Api from "./Api";
|
|
2
2
|
import Builder from "./Builder";
|
|
3
3
|
import Binding from "./Binding";
|
|
4
|
+
import axios from "axios";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
*
|
|
@@ -46,6 +47,7 @@ export default class ApiRequest {
|
|
|
46
47
|
this.callError = () => {
|
|
47
48
|
|
|
48
49
|
};
|
|
50
|
+
this.source = null;
|
|
49
51
|
|
|
50
52
|
Builder.availableMethod.map(function (val) {
|
|
51
53
|
self[val] = function () {
|
|
@@ -171,6 +173,16 @@ export default class ApiRequest {
|
|
|
171
173
|
return this.domain + '/api/v1/call/' + this.target + '/' + this.focus;
|
|
172
174
|
}
|
|
173
175
|
|
|
176
|
+
/**
|
|
177
|
+
* вернет токен для отмены запроса
|
|
178
|
+
*
|
|
179
|
+
* @returns {null}
|
|
180
|
+
*/
|
|
181
|
+
getSource()
|
|
182
|
+
{
|
|
183
|
+
return this.source;
|
|
184
|
+
}
|
|
185
|
+
|
|
174
186
|
/**
|
|
175
187
|
*
|
|
176
188
|
* @param successCallback
|
|
@@ -298,6 +310,7 @@ export default class ApiRequest {
|
|
|
298
310
|
let self = this;
|
|
299
311
|
this.callSuccess = successCallback;
|
|
300
312
|
this.callError = errorCallback;
|
|
313
|
+
this.source = axios.CancelToken.source();
|
|
301
314
|
|
|
302
315
|
let url = byUrl ? this.url : this.getUrl();
|
|
303
316
|
let notify = null;
|
|
@@ -308,6 +321,7 @@ export default class ApiRequest {
|
|
|
308
321
|
method: this.method,
|
|
309
322
|
data: data,
|
|
310
323
|
params: params,
|
|
324
|
+
source: this.source,
|
|
311
325
|
success: (response, status, xhr) => {
|
|
312
326
|
if (response && response.result === 'success') {
|
|
313
327
|
notify = this.handleSuccessNotification(response, xhr.status);
|