laravel-request 1.1.8 → 1.1.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "laravel-request",
3
3
  "productName": "laravel-request",
4
- "version": "1.1.8",
4
+ "version": "1.1.10",
5
5
  "description": "laravel-request",
6
6
  "main": "src/index.js",
7
7
  "scripts": {},
package/src/Api.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import ApiRequest from "./ApiRequest";
2
- import $ from "jquery";
2
+ import axios from "axios";
3
3
 
4
4
  export default class Api {
5
5
 
@@ -18,7 +18,7 @@ export default class Api {
18
18
  * @return {ApiRequest}
19
19
  */
20
20
  static create(target, focus, data = {}, method = 'GET') {
21
- return this.requestClass(target, focus, data, method);
21
+ return new this.requestClass(target, focus, data, method);
22
22
  }
23
23
 
24
24
  /**
@@ -31,7 +31,7 @@ export default class Api {
31
31
  * @return {ApiRequest}
32
32
  */
33
33
  static createArg(target, focus, arg, data = {}, method = 'GET') {
34
- return this.requestClass(target, focus, data, method).addArg(arg);
34
+ return new this.requestClass(target, focus, data, method).addArg(arg);
35
35
  }
36
36
 
37
37
  /**
@@ -78,7 +78,7 @@ export default class Api {
78
78
  */
79
79
  static getUrl(url, data = {})
80
80
  {
81
- return (this.requestClass('', '', data, 'GET')).setUrl(url);
81
+ return (new this.requestClass('', '', data, 'GET')).setUrl(url);
82
82
  }
83
83
 
84
84
  /**
@@ -89,7 +89,7 @@ export default class Api {
89
89
  */
90
90
  static postUrl(url, data = {})
91
91
  {
92
- return (this.requestClass('', '', data, 'POST')).setUrl(url);
92
+ return (new this.requestClass('', '', data, 'POST')).setUrl(url);
93
93
  }
94
94
 
95
95
  /**
@@ -100,7 +100,7 @@ export default class Api {
100
100
  */
101
101
  static putUrl(url, data = {})
102
102
  {
103
- return (this.requestClass('', '', data, 'PUT')).setUrl(url);
103
+ return (new this.requestClass('', '', data, 'PUT')).setUrl(url);
104
104
  }
105
105
 
106
106
  /**
@@ -111,7 +111,7 @@ export default class Api {
111
111
  * @returns {ApiRequest}
112
112
  */
113
113
  static get(target, focus, data = {}) {
114
- return this.requestClass(target, focus, data, 'GET');
114
+ return new this.requestClass(target, focus, data, 'GET');
115
115
  }
116
116
  /**
117
117
  *
@@ -121,7 +121,7 @@ export default class Api {
121
121
  * @returns {ApiRequest}
122
122
  */
123
123
  static post(target, focus, data = {}) {
124
- return this.requestClass(target, focus, data, 'POST');
124
+ return new this.requestClass(target, focus, data, 'POST');
125
125
  }
126
126
  /**
127
127
  *
@@ -131,7 +131,7 @@ export default class Api {
131
131
  * @returns {ApiRequest}
132
132
  */
133
133
  static put(target, focus, data = {}) {
134
- return this.requestClass(target, focus, data, 'PUT');
134
+ return new this.requestClass(target, focus, data, 'PUT');
135
135
  }
136
136
 
137
137
  /**
@@ -143,7 +143,7 @@ export default class Api {
143
143
  * @return {ApiRequest}
144
144
  */
145
145
  static delete(target, focus, id, data = {}) {
146
- return this.requestClass(target, focus, data, 'DELETE').addArg(id);
146
+ return new this.requestClass(target, focus, data, 'DELETE').addArg(id);
147
147
  }
148
148
 
149
149
  /**
@@ -151,33 +151,85 @@ export default class Api {
151
151
  * @param obj
152
152
  * @return {*}
153
153
  */
154
- static makeRequest(obj) {
155
- obj.xhrFields = {withCredentials: true};
156
-
157
- if (typeof obj["data"] === 'undefined') {
158
- obj["data"] = {};
159
- }
160
- if (typeof obj["headers"] === 'undefined') {
161
- obj["headers"] = {};
162
- }
163
-
164
-
165
- let api_token = localStorage.getItem('api_token');
166
-
167
- if (api_token)
168
- {
169
- obj["headers"]["Authorization"] = api_token;
170
- }
171
-
172
- let one_time_token = localStorage.getItem('token');
173
-
174
- if (one_time_token)
175
- {
176
- obj["data"]['token'] = one_time_token;
154
+ static async makeRequest({url, method, data, params, headers, success = () => {}, error = () => {}})
155
+ {
156
+ try {
157
+ if (typeof headers === 'undefined') {
158
+ headers = {};
159
+ }
160
+ if (typeof params === 'undefined') {
161
+ params = {};
162
+ }
163
+ if (typeof data === 'undefined') {
164
+ data = {};
165
+ }
166
+
167
+ headers['Content-Type'] = 'application/json';
168
+
169
+ let api_token = localStorage.getItem('api_token');
170
+ if (api_token)
171
+ {
172
+ headers["Authorization"] = api_token;
173
+ }
174
+
175
+ let response;
176
+ switch (method)
177
+ {
178
+ case 'GET':
179
+ data.timestamp = new Date().getTime();
180
+
181
+ response = await axios.request({
182
+ url: url,
183
+ method: method,
184
+ params: data,
185
+ headers: headers
186
+ });
187
+ break;
188
+ default:
189
+ params.timestamp = new Date().getTime();
190
+
191
+ response = await axios.request({
192
+ url: url,
193
+ method: method,
194
+ data: data,
195
+ params: params,
196
+ headers: headers
197
+ });
198
+ break;
199
+ }
200
+
201
+
202
+ // get status code
203
+ const statusCode = response.status;
204
+
205
+ // get full response object
206
+ const xhr = response;
207
+
208
+ const responseData = response.data;
209
+
210
+ success(responseData, statusCode, xhr);
211
+
212
+ return responseData;
213
+ } catch(e) {
214
+ console.error(`API request to ${url} failed: ${e}`);
215
+
216
+ const xhr = e.request;
217
+
218
+ //написано для обратной совместимости после перехода с jquery
219
+ try{
220
+ xhr.responseJSON = e.response.data;
221
+ }catch (errorJson){
222
+ console.log(errorJson)
223
+ }
224
+
225
+
226
+ // status code
227
+ const statusCode = e.response.status;
228
+
229
+ // status text
230
+ const statusText = e.response.statusText;
231
+
232
+ error(xhr, statusCode, statusText);
177
233
  }
178
-
179
- // obj["data"]['debug'] = true;
180
-
181
- return $.ajax(obj)
182
234
  }
183
235
  }
package/src/ApiRequest.js CHANGED
@@ -1,6 +1,6 @@
1
+ import Api from "./Api";
1
2
  import Builder from "./Builder";
2
3
  import Binding from "./Binding";
3
- import Api from "./Api";
4
4
 
5
5
  /**
6
6
  *
@@ -12,11 +12,6 @@ export default class ApiRequest {
12
12
  */
13
13
  url = '';
14
14
 
15
- /**
16
- *
17
- * @type {XMLHttpRequest|null}
18
- */
19
- xhr = null;
20
15
 
21
16
  /**
22
17
  *
@@ -154,11 +149,18 @@ export default class ApiRequest {
154
149
  */
155
150
  call(successCallback = (r) => {
156
151
  }, errorCallback = () => {
157
- }, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query') {
152
+ }, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query', byUrl = false) {
158
153
  let self = this;
159
154
  this.callSuccess = successCallback;
160
155
  this.callError = errorCallback;
161
156
 
157
+ let url = this.domain + '/api/v1/call/' + this.target + '/' + this.focus;
158
+
159
+ if(byUrl)
160
+ {
161
+ url = this.url;
162
+ }
163
+
162
164
  let notify = null;
163
165
 
164
166
  let data = {};
@@ -180,13 +182,11 @@ export default class ApiRequest {
180
182
  data = this.data;
181
183
  }
182
184
 
183
-
184
- this.xhr = Api.makeRequest({
185
- url: this.domain + '/api/v1/call/' + this.target + '/' + this.focus,
185
+ Api.makeRequest({
186
+ url: url,
186
187
  method: this.method,
187
188
  data: data,
188
- ...params,
189
- dataType: "json",
189
+ params: params,
190
190
  success: (response, status, xhr) => {
191
191
  if(response && response.result === 'success')
192
192
  {
@@ -212,6 +212,14 @@ export default class ApiRequest {
212
212
  return this;
213
213
  }
214
214
 
215
+ /**
216
+ *
217
+ * @param notify
218
+ * @param errorCallback
219
+ * @param xhr
220
+ * @param status
221
+ * @param errorText
222
+ */
215
223
  handleError(notify, errorCallback, xhr, status, errorText)
216
224
  {
217
225
  if (xhr.readyState === 4) {
@@ -266,63 +274,33 @@ export default class ApiRequest {
266
274
  errorCallback(xhr);
267
275
  }
268
276
 
277
+ /**
278
+ *
279
+ * @param successCallback
280
+ * @param errorCallback
281
+ * @return {ApiRequest}
282
+ */
269
283
  callSync(successCallback = (r) => {
270
284
  }, errorCallback = () => {
271
285
  }) {
272
286
  return this.call(successCallback, errorCallback, {async: false});
273
287
  }
274
288
 
289
+ /**
290
+ *
291
+ * @param successCallback
292
+ * @param errorCallback
293
+ * @param params
294
+ * @param dataKey
295
+ * @param argumentsKey
296
+ * @param queryKey
297
+ * @return {ApiRequest}
298
+ */
275
299
  callUrl(successCallback = (r) => {
276
300
  }, errorCallback = () => {
277
301
  }, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query')
278
302
  {
279
- let self = this;
280
- this.callSuccess = successCallback;
281
- this.callError = errorCallback;
282
-
283
- let data = {};
284
-
285
- if(argumentsKey)
286
- {
287
- data[argumentsKey] = this.arguments;
288
- }
289
-
290
- if(queryKey)
291
- {
292
- data[queryKey] = this.builder.toArray();
293
- }
294
-
295
- if(dataKey)
296
- {
297
- data[dataKey] = this.data;
298
- }else{
299
- data = this.data;
300
- }
301
-
302
- let notify = null;
303
- Api.makeRequest({
304
- url: this.url,
305
- method: this.method,
306
- data: data,
307
- ...params,
308
- success: (response, status, xhr) => {
309
- if(response && response.result === 'success')
310
- {
311
- if (response.meta && response.meta.text)
312
- {
313
- notify = this.notify ? this.getNotifyManager().info('Успешно', response.meta.text) : null
314
- }
315
- self.toBind(response);
316
- self.resetBindErrors();
317
- successCallback(response, status, xhr);
318
- }
319
- },
320
- error: (xhr, status, errorText) => {
321
- this.handleError(notify, errorCallback, xhr, status, errorText);
322
- }
323
- });
324
-
325
- return this;
303
+ return this.call(successCallback, errorCallback, params, dataKey, argumentsKey, queryKey, true)
326
304
  }
327
305
 
328
306
  /**
package/src/index.js CHANGED
@@ -3,8 +3,6 @@ import ApiProxy from "./ApiProxy";
3
3
  import ApiRequest from "./ApiRequest";
4
4
  import Binding from "./Binding";
5
5
  import Builder from "./Builder";
6
- import NewApi from "./NewApi";
7
- import NewApiRequest from "./NewApiRequest";
8
6
  import UrlBind from "./UrlBind";
9
7
 
10
8
 
@@ -15,7 +13,5 @@ export {
15
13
  ApiRequest,
16
14
  Binding,
17
15
  Builder,
18
- NewApi,
19
- NewApiRequest,
20
16
  UrlBind,
21
17
  }
package/src/NewApi.js DELETED
@@ -1,63 +0,0 @@
1
- import NewApiRequest from "./NewApiRequest";
2
- import axios from 'axios';
3
- import Api from "./Api";
4
-
5
- export default class NewApi extends Api
6
- {
7
- /**
8
- *
9
- * @type {ApiRequest}
10
- */
11
- static requestClass = NewApiRequest;
12
-
13
- static async makeRequest({url, method, data, dataType, params, headers, success, error})
14
- {
15
- try {
16
- if (typeof headers === 'undefined') {
17
- headers = {};
18
- }
19
-
20
- if(dataType === 'json')
21
- {
22
- headers['Content-Type'] = 'application/json';
23
- }
24
-
25
- let api_token = localStorage.getItem('api_token');
26
- if (api_token)
27
- {
28
- headers["Authorization"] = api_token;
29
- }
30
-
31
- const response = await axios({
32
- url: url,
33
- method: method,
34
- data: data,
35
- params: params,
36
- headers: headers
37
- });
38
-
39
- // get status code
40
- const statusCode = response.status;
41
-
42
- // get full response object
43
- const xhr = response;
44
-
45
- const data = response.data;
46
-
47
- success(data, statusCode, xhr);
48
-
49
- } catch(e) {
50
- throw new Error(`API request to ${url} failed: ${error}`);
51
-
52
- const xhr = e.response;
53
-
54
- // status code
55
- const statusCode = e.response.status;
56
-
57
- // status text
58
- const statusText = e.response.statusText;
59
-
60
- error(xhr, statusCode, statusText);
61
- }
62
- }
63
- }
@@ -1,9 +0,0 @@
1
- import ApiRequest from "./ApiRequest";
2
-
3
- /**
4
- *
5
- */
6
- export default class NewApiRequest extends ApiRequest
7
- {
8
-
9
- };