laravel-request 1.0.0
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 +17 -0
- package/readme.md +34 -0
- package/src/Api.js +171 -0
- package/src/ApiProxy.js +6 -0
- package/src/ApiRequest.js +394 -0
- package/src/Binding.js +56 -0
- package/src/Builder.js +85 -0
- package/src/NewApi.js +170 -0
- package/src/NewApiRequest.js +48 -0
- package/src/UrlBind.js +91 -0
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "laravel-request",
|
|
3
|
+
"productName": "laravel-request",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "laravel-request",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"scripts": {},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Abramov Oleg",
|
|
11
|
+
"email": "olegstan1@gmail.com"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"axios": "^1.3.2"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Usage get data
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
Api.get('active-trade', 'index')
|
|
5
|
+
.where('active_id', 115)
|
|
6
|
+
.with('currency')
|
|
7
|
+
.with('to_account', 'to_account.currency')
|
|
8
|
+
.with('from_account', 'from_account.currency')
|
|
9
|
+
.orderBy('trade_at', 'DESC')
|
|
10
|
+
.all((response) => {
|
|
11
|
+
|
|
12
|
+
})
|
|
13
|
+
.bind(this, 'trades')
|
|
14
|
+
}}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can use
|
|
18
|
+
|
|
19
|
+
all or first or paginate
|
|
20
|
+
|
|
21
|
+
usage POST data
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Api.post('active', 'store', {
|
|
25
|
+
user_id: this.props.client.id,
|
|
26
|
+
type: 2,
|
|
27
|
+
type_id: item.type_id
|
|
28
|
+
})
|
|
29
|
+
.call((response) => {
|
|
30
|
+
//success
|
|
31
|
+
}, (response) => {
|
|
32
|
+
//error
|
|
33
|
+
});
|
|
34
|
+
```
|
package/src/Api.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import ApiRequest from "./ApiRequest";
|
|
2
|
+
import $ from 'jquery';
|
|
3
|
+
|
|
4
|
+
export default class Api {
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param target
|
|
8
|
+
* @param focus
|
|
9
|
+
* @param data
|
|
10
|
+
* @param method
|
|
11
|
+
* @return {ApiRequest}
|
|
12
|
+
*/
|
|
13
|
+
static create(target, focus, data = {}, method = 'GET') {
|
|
14
|
+
return new ApiRequest(target, focus, data, method);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param target
|
|
20
|
+
* @param focus
|
|
21
|
+
* @param arg
|
|
22
|
+
* @param data
|
|
23
|
+
* @param method
|
|
24
|
+
* @return {ApiRequest}
|
|
25
|
+
*/
|
|
26
|
+
static createArg(target, focus, arg, data = {}, method = 'GET') {
|
|
27
|
+
return new ApiRequest(target, focus, data, method).addArg(arg);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @param target
|
|
33
|
+
* @param focus
|
|
34
|
+
* @param arg
|
|
35
|
+
* @param data
|
|
36
|
+
* @return {ApiRequest}
|
|
37
|
+
*/
|
|
38
|
+
static getArg(target, focus, arg, data = {}) {
|
|
39
|
+
return Api.get(target, focus, data).addArg(arg);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param target
|
|
45
|
+
* @param focus
|
|
46
|
+
* @param arg
|
|
47
|
+
* @param data
|
|
48
|
+
* @return {ApiRequest}
|
|
49
|
+
*/
|
|
50
|
+
static postArg(target, focus, arg, data = {}) {
|
|
51
|
+
return Api.post(target, focus, data).addArg(arg);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @param target
|
|
57
|
+
* @param focus
|
|
58
|
+
* @param arg
|
|
59
|
+
* @param data
|
|
60
|
+
* @return {ApiRequest}
|
|
61
|
+
*/
|
|
62
|
+
static putArg(target, focus, arg, data = {}) {
|
|
63
|
+
return Api.put(target, focus, data).addArg(arg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param url
|
|
69
|
+
* @param data
|
|
70
|
+
* @return {ApiRequest}
|
|
71
|
+
*/
|
|
72
|
+
static getUrl(url, data = {})
|
|
73
|
+
{
|
|
74
|
+
return (new ApiRequest('', '', data, 'GET')).setUrl(url);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param url
|
|
80
|
+
* @param data
|
|
81
|
+
* @return {ApiRequest}
|
|
82
|
+
*/
|
|
83
|
+
static postUrl(url, data = {})
|
|
84
|
+
{
|
|
85
|
+
return (new ApiRequest('', '', data, 'POST')).setUrl(url);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @param url
|
|
91
|
+
* @param data
|
|
92
|
+
* @return {ApiRequest}
|
|
93
|
+
*/
|
|
94
|
+
static putUrl(url, data = {})
|
|
95
|
+
{
|
|
96
|
+
return (new ApiRequest('', '', data, 'PUT')).setUrl(url);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param target
|
|
102
|
+
* @param focus
|
|
103
|
+
* @param data
|
|
104
|
+
* @returns {ApiRequest}
|
|
105
|
+
*/
|
|
106
|
+
static get(target, focus, data = {}) {
|
|
107
|
+
return new ApiRequest(target, focus, data, 'GET');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @param target
|
|
112
|
+
* @param focus
|
|
113
|
+
* @param data
|
|
114
|
+
* @returns {ApiRequest}
|
|
115
|
+
*/
|
|
116
|
+
static post(target, focus, data = {}) {
|
|
117
|
+
return new ApiRequest(target, focus, data, 'POST');
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @param target
|
|
122
|
+
* @param focus
|
|
123
|
+
* @param data
|
|
124
|
+
* @returns {ApiRequest}
|
|
125
|
+
*/
|
|
126
|
+
static put(target, focus, data = {}) {
|
|
127
|
+
return new ApiRequest(target, focus, data, 'PUT');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
*
|
|
132
|
+
* @param target
|
|
133
|
+
* @param focus
|
|
134
|
+
* @param id
|
|
135
|
+
* @param data
|
|
136
|
+
* @return {ApiRequest}
|
|
137
|
+
*/
|
|
138
|
+
static delete(target, focus, id, data = {}) {
|
|
139
|
+
return new ApiRequest(target, focus, data, 'DELETE').addArg(id);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static makeRequest(obj) {
|
|
143
|
+
obj.xhrFields = {withCredentials: true};
|
|
144
|
+
|
|
145
|
+
if (typeof obj["data"] === 'undefined') {
|
|
146
|
+
obj["data"] = {};
|
|
147
|
+
}
|
|
148
|
+
if (typeof obj["headers"] === 'undefined') {
|
|
149
|
+
obj["headers"] = {};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
let api_token = localStorage.getItem('api_token');
|
|
154
|
+
|
|
155
|
+
if (api_token)
|
|
156
|
+
{
|
|
157
|
+
obj["headers"]["Authorization"] = api_token;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let one_time_token = localStorage.getItem('token');
|
|
161
|
+
|
|
162
|
+
if (one_time_token)
|
|
163
|
+
{
|
|
164
|
+
obj["data"]['token'] = one_time_token;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// obj["data"]['debug'] = true;
|
|
168
|
+
|
|
169
|
+
return $.ajax(obj)
|
|
170
|
+
}
|
|
171
|
+
}
|
package/src/ApiProxy.js
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import Builder from "./Builder";
|
|
2
|
+
import Binding from "./Binding";
|
|
3
|
+
import Api from "./Api";
|
|
4
|
+
import NotifyManager from "../../interface/notify/NotifyManager";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export default class ApiRequest {
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @type {string}
|
|
13
|
+
*/
|
|
14
|
+
url = '';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @type {XMLHttpRequest|null}
|
|
19
|
+
*/
|
|
20
|
+
xhr = null;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param target
|
|
25
|
+
* @param focus
|
|
26
|
+
* @param data
|
|
27
|
+
* @param method
|
|
28
|
+
*/
|
|
29
|
+
constructor(target, focus, data = {}, method = 'GET') {
|
|
30
|
+
let self = this;
|
|
31
|
+
this.domain = process.env.REACT_APP_API_URL;
|
|
32
|
+
this.target = target;
|
|
33
|
+
this.focus = focus;
|
|
34
|
+
this.method = method;
|
|
35
|
+
this.data = data;
|
|
36
|
+
this.arguments = [];
|
|
37
|
+
this.builder = new Builder();
|
|
38
|
+
this.notifyCallback = (status) => {return true};
|
|
39
|
+
this.responseBinding = null;
|
|
40
|
+
this.responseBindingErrors = null;
|
|
41
|
+
|
|
42
|
+
this.callSuccess = () => {
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
this.callError = () => {
|
|
46
|
+
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
Builder.availableMethod.map(function (val) {
|
|
50
|
+
self[val] = function () {
|
|
51
|
+
self.builder.add(val, Array.prototype.slice.call(arguments));
|
|
52
|
+
return self;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setUrl(url)
|
|
60
|
+
{
|
|
61
|
+
this.url = url;
|
|
62
|
+
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setDomain(domain)
|
|
67
|
+
{
|
|
68
|
+
this.domain = domain;
|
|
69
|
+
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
*
|
|
75
|
+
* @param arg
|
|
76
|
+
* @returns {ApiRequest}
|
|
77
|
+
*/
|
|
78
|
+
addArg(arg) {
|
|
79
|
+
if (Array.isArray(arg)) {
|
|
80
|
+
this.arguments = this.arguments.concat(arg);
|
|
81
|
+
} else {
|
|
82
|
+
this.arguments.push(arg);
|
|
83
|
+
}
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @param successCallback
|
|
90
|
+
* @param errorCallback
|
|
91
|
+
* @returns {*}
|
|
92
|
+
*/
|
|
93
|
+
first(successCallback = (r) => {
|
|
94
|
+
}, errorCallback = () => {
|
|
95
|
+
}) {
|
|
96
|
+
this.data['paginateType'] = 'first';
|
|
97
|
+
return this.call(successCallback, errorCallback);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
*
|
|
102
|
+
* @param successCallback
|
|
103
|
+
* @param errorCallback
|
|
104
|
+
* @returns {*}
|
|
105
|
+
*/
|
|
106
|
+
all(successCallback = (r) => {
|
|
107
|
+
}, errorCallback = () => {
|
|
108
|
+
}) {
|
|
109
|
+
this.data['paginateType'] = 'all';
|
|
110
|
+
return this.call(successCallback, errorCallback);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* @param page
|
|
116
|
+
* @param perPage
|
|
117
|
+
* @param successCallback
|
|
118
|
+
* @param errorCallback
|
|
119
|
+
* @returns {*}
|
|
120
|
+
*/
|
|
121
|
+
paginate(page = 1, perPage = 10, successCallback = (r) => {
|
|
122
|
+
}, errorCallback = () => {
|
|
123
|
+
}) {
|
|
124
|
+
this.data['paginateType'] = 'paginate';
|
|
125
|
+
this.data['page'] = page;
|
|
126
|
+
this.data['perPage'] = perPage;
|
|
127
|
+
return this.call(successCallback, errorCallback);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getUrl()
|
|
131
|
+
{
|
|
132
|
+
return this.domain + '/api/v1/call/' + this.target + '/' + this.focus;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
*
|
|
137
|
+
* @param successCallback
|
|
138
|
+
* @param errorCallback
|
|
139
|
+
* @param params
|
|
140
|
+
* @param dataKey
|
|
141
|
+
* @param argumentsKey
|
|
142
|
+
* @param queryKey
|
|
143
|
+
* @return {ApiRequest}
|
|
144
|
+
*/
|
|
145
|
+
call(successCallback = (r) => {
|
|
146
|
+
}, errorCallback = () => {
|
|
147
|
+
}, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query') {
|
|
148
|
+
let self = this;
|
|
149
|
+
this.callSuccess = successCallback;
|
|
150
|
+
this.callError = errorCallback;
|
|
151
|
+
|
|
152
|
+
let notify = null;
|
|
153
|
+
|
|
154
|
+
let data = {};
|
|
155
|
+
|
|
156
|
+
if(argumentsKey)
|
|
157
|
+
{
|
|
158
|
+
data[argumentsKey] = this.arguments;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if(queryKey)
|
|
162
|
+
{
|
|
163
|
+
data[queryKey] = this.builder.toArray();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if(dataKey)
|
|
167
|
+
{
|
|
168
|
+
data[dataKey] = this.data;
|
|
169
|
+
}else{
|
|
170
|
+
data = this.data;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
this.xhr = Api.makeRequest({
|
|
175
|
+
url: this.domain + '/api/v1/call/' + this.target + '/' + this.focus,
|
|
176
|
+
method: this.method,
|
|
177
|
+
data: data,
|
|
178
|
+
...params,
|
|
179
|
+
dataType: "json",
|
|
180
|
+
success: (response, status, xhr) => {
|
|
181
|
+
if(response && response.result === 'success')
|
|
182
|
+
{
|
|
183
|
+
if (response.meta && response.meta.text)
|
|
184
|
+
{
|
|
185
|
+
let result = this.notifyCallback(xhr.status);
|
|
186
|
+
|
|
187
|
+
if(result)
|
|
188
|
+
{
|
|
189
|
+
notify = NotifyManager.info('Успешно', response.meta.text)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
self.toBind(response);
|
|
193
|
+
self.resetBindErrors();
|
|
194
|
+
successCallback(response, status, xhr);
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
error: (xhr, status, errorText) => {
|
|
198
|
+
this.handleError(notify, errorCallback, xhr, status, errorText);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
handleError(notify, errorCallback, xhr, status, errorText)
|
|
206
|
+
{
|
|
207
|
+
if (xhr.readyState === 4) {
|
|
208
|
+
try {
|
|
209
|
+
let result = this.notifyCallback(xhr.status);
|
|
210
|
+
|
|
211
|
+
if(result)
|
|
212
|
+
{
|
|
213
|
+
switch (xhr.status) {
|
|
214
|
+
case 0://точно ошибка
|
|
215
|
+
notify = NotifyManager.error('Ошибка', errorText);
|
|
216
|
+
break;
|
|
217
|
+
case 404:
|
|
218
|
+
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
if(xhr?.responseJSON?.meta.text)
|
|
222
|
+
{
|
|
223
|
+
notify = NotifyManager.error('Ошибка', xhr.responseJSON.meta.text);
|
|
224
|
+
}else if(xhr?.responseJSON?.meta.message)
|
|
225
|
+
{
|
|
226
|
+
notify = NotifyManager.error('Ошибка', xhr.responseJSON.meta.message);
|
|
227
|
+
}else{
|
|
228
|
+
if(typeof errorText === 'string')
|
|
229
|
+
{
|
|
230
|
+
notify = NotifyManager.error('Ошибка', errorText);
|
|
231
|
+
}else if(errorText?.message && typeof errorText.message === 'string'){
|
|
232
|
+
notify = NotifyManager.error('Ошибка', errorText.message);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.error(e);
|
|
240
|
+
notify = this.notify ? NotifyManager.error('Ошибка') : null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else if (xhr.readyState === 0) {
|
|
244
|
+
notify = NotifyManager.errorOnce('network_error', 'Ошибка', ' (Network Error) или невозможность получения доступа к сети');
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
if(typeof errorText === 'string')
|
|
248
|
+
{
|
|
249
|
+
notify = NotifyManager.error('Ошибка', errorText);
|
|
250
|
+
}else if(errorText?.message && typeof errorText.message === 'string'){
|
|
251
|
+
notify = NotifyManager.error('Ошибка', errorText.message);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.toBindErrors(xhr);
|
|
256
|
+
errorCallback(xhr);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
callSync(successCallback = (r) => {
|
|
260
|
+
}, errorCallback = () => {
|
|
261
|
+
}) {
|
|
262
|
+
return this.call(successCallback, errorCallback, {async: false});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
callUrl(successCallback = (r) => {
|
|
266
|
+
}, errorCallback = () => {
|
|
267
|
+
}, params = {}, dataKey = 'data', argumentsKey = 'arguments', queryKey = 'query')
|
|
268
|
+
{
|
|
269
|
+
let self = this;
|
|
270
|
+
this.callSuccess = successCallback;
|
|
271
|
+
this.callError = errorCallback;
|
|
272
|
+
|
|
273
|
+
let data = {};
|
|
274
|
+
|
|
275
|
+
if(argumentsKey)
|
|
276
|
+
{
|
|
277
|
+
data[argumentsKey] = this.arguments;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if(queryKey)
|
|
281
|
+
{
|
|
282
|
+
data[queryKey] = this.builder.toArray();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if(dataKey)
|
|
286
|
+
{
|
|
287
|
+
data[dataKey] = this.data;
|
|
288
|
+
}else{
|
|
289
|
+
data = this.data;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let notify = null;
|
|
293
|
+
Api.makeRequest({
|
|
294
|
+
url: this.url,
|
|
295
|
+
method: this.method,
|
|
296
|
+
data: data,
|
|
297
|
+
...params,
|
|
298
|
+
success: (response, status, xhr) => {
|
|
299
|
+
if(response && response.result === 'success')
|
|
300
|
+
{
|
|
301
|
+
if (response.meta && response.meta.text)
|
|
302
|
+
{
|
|
303
|
+
notify = this.notify ? NotifyManager.info('Успешно', response.meta.text) : null
|
|
304
|
+
}
|
|
305
|
+
self.toBind(response);
|
|
306
|
+
self.resetBindErrors();
|
|
307
|
+
successCallback(response, status, xhr);
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
error: (xhr, status, errorText) => {
|
|
311
|
+
this.handleError(notify, errorCallback, xhr, status, errorText);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
*
|
|
320
|
+
* @param obj
|
|
321
|
+
* @param item
|
|
322
|
+
* @param rerender
|
|
323
|
+
* @param cb
|
|
324
|
+
*/
|
|
325
|
+
bind(obj, item, rerender = false, cb) {
|
|
326
|
+
let key = 'data';
|
|
327
|
+
let self = this;
|
|
328
|
+
if (typeof item == 'object') {
|
|
329
|
+
this.responseBinding = [];
|
|
330
|
+
item.map(function (data)
|
|
331
|
+
{
|
|
332
|
+
self.responseBinding[self.responseBinding.length] = new Binding(obj, data[0], data[1], rerender, cb);
|
|
333
|
+
|
|
334
|
+
return;
|
|
335
|
+
})
|
|
336
|
+
} else {
|
|
337
|
+
this.responseBinding = new Binding(obj, item, key, rerender, cb);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return this;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
*
|
|
345
|
+
* @param response
|
|
346
|
+
*/
|
|
347
|
+
toBind(response) {
|
|
348
|
+
if (this.responseBinding !== null) {
|
|
349
|
+
if (Array.isArray(this.responseBinding)) {
|
|
350
|
+
this.responseBinding.map(function (data)
|
|
351
|
+
{
|
|
352
|
+
data.fire(response);
|
|
353
|
+
|
|
354
|
+
return;
|
|
355
|
+
})
|
|
356
|
+
} else {
|
|
357
|
+
this.responseBinding.fire(response);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
resetBindErrors() {
|
|
363
|
+
if (this.responseBindingErrors !== null) {
|
|
364
|
+
this.responseBindingErrors.fire({});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
toBindErrors(response = {})
|
|
369
|
+
{
|
|
370
|
+
if (this.responseBindingErrors !== null && 'responseJSON' in response && typeof response.responseJSON === 'object')
|
|
371
|
+
{
|
|
372
|
+
this.responseBindingErrors.fire(response.responseJSON.meta.errors);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
withValidateForm(obj, item = 'formErrors', key = 'meta') {
|
|
377
|
+
this.responseBindingErrors = new Binding(obj, item, key);
|
|
378
|
+
return this;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
withoutNotify(callback)
|
|
382
|
+
{
|
|
383
|
+
//callback(status){} будем проверять статус и по условию если true не показывать уведомление
|
|
384
|
+
|
|
385
|
+
if(typeof callback === "undefined")
|
|
386
|
+
{
|
|
387
|
+
this.notifyCallback = (status) => {return false};
|
|
388
|
+
}else{
|
|
389
|
+
this.notifyCallback = callback;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return this;
|
|
393
|
+
}
|
|
394
|
+
};
|
package/src/Binding.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export default class Binding {
|
|
2
|
+
|
|
3
|
+
constructor(target, pathTarget, pathData, rerender, onSuccess)
|
|
4
|
+
{
|
|
5
|
+
this.target = target;
|
|
6
|
+
this.pathTarget = pathTarget.split('.');
|
|
7
|
+
this.pathData = pathData.split('.');
|
|
8
|
+
this.callback = onSuccess;
|
|
9
|
+
this.rerender = rerender;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fire(data) {
|
|
13
|
+
let self = this;
|
|
14
|
+
let value = this.getData(data);
|
|
15
|
+
let obj = this.target.state;
|
|
16
|
+
|
|
17
|
+
if (this.pathTarget.length === 1) {
|
|
18
|
+
this.target.setState((prv) => {
|
|
19
|
+
obj[self.pathTarget[0]] = value
|
|
20
|
+
|
|
21
|
+
// if(this.rerender)//TODO
|
|
22
|
+
// {
|
|
23
|
+
return prv;
|
|
24
|
+
// }
|
|
25
|
+
|
|
26
|
+
}, self.callback);
|
|
27
|
+
} else {
|
|
28
|
+
for (let i = 0; i < this.pathTarget.length - 1; i++) {
|
|
29
|
+
obj = obj[this.pathTarget[i]];
|
|
30
|
+
}
|
|
31
|
+
this.target.setState(function (prv) {
|
|
32
|
+
obj[self.pathTarget[self.pathTarget.length - 1]] = value;
|
|
33
|
+
|
|
34
|
+
// if(this.rerender)//TODO
|
|
35
|
+
// {
|
|
36
|
+
return prv;
|
|
37
|
+
// }
|
|
38
|
+
}, self.callback);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getData(value) {
|
|
43
|
+
this.pathData.map(function (data)
|
|
44
|
+
{
|
|
45
|
+
for (let i in value) {
|
|
46
|
+
if (i == data) {
|
|
47
|
+
value = value[data];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
};
|
package/src/Builder.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export default class Builder {
|
|
2
|
+
|
|
3
|
+
static availableMethod = [
|
|
4
|
+
'select',
|
|
5
|
+
'where',
|
|
6
|
+
'whereDate',
|
|
7
|
+
'whereYear',
|
|
8
|
+
'whereMonth',
|
|
9
|
+
'has',
|
|
10
|
+
'whereIn',
|
|
11
|
+
'whereNotIn',
|
|
12
|
+
'whereHas',
|
|
13
|
+
'orWhereHas',
|
|
14
|
+
'whereHasMorph',
|
|
15
|
+
'orWhereHasMorph',
|
|
16
|
+
'whereDoesntHave',
|
|
17
|
+
'orWhereDoesntHave',
|
|
18
|
+
'orWhere',
|
|
19
|
+
'orderBy',
|
|
20
|
+
'groupBy',
|
|
21
|
+
'whereNull',
|
|
22
|
+
'orWhereNull',
|
|
23
|
+
'whereNotNull',
|
|
24
|
+
'orWhereNotNull',
|
|
25
|
+
/**
|
|
26
|
+
*@property Builder.availableMethod.with array
|
|
27
|
+
*/
|
|
28
|
+
'with',
|
|
29
|
+
'withCount',
|
|
30
|
+
'offset',
|
|
31
|
+
'limit',
|
|
32
|
+
'distinct',
|
|
33
|
+
'owner',
|
|
34
|
+
'whereAbs',
|
|
35
|
+
|
|
36
|
+
'whereAccount',
|
|
37
|
+
'whereDate',
|
|
38
|
+
'whereYear',
|
|
39
|
+
'whereMonth',
|
|
40
|
+
|
|
41
|
+
'whereIncomeType',
|
|
42
|
+
'whereSalaryType',
|
|
43
|
+
'whereSpendingType',
|
|
44
|
+
'whereObligationType',
|
|
45
|
+
'wherePropertyType',
|
|
46
|
+
'whereInvestType',
|
|
47
|
+
'whereCurrencyType',
|
|
48
|
+
'whereAccountType',
|
|
49
|
+
'whereInsuranceType'
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
constructor() {
|
|
53
|
+
var self = this;
|
|
54
|
+
this.query = [];
|
|
55
|
+
Builder.availableMethod.map(function (val)
|
|
56
|
+
{
|
|
57
|
+
self[val] = function () {
|
|
58
|
+
self.add(val, Array.prototype.slice.call(arguments));
|
|
59
|
+
return self;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
add(method, args) {
|
|
67
|
+
this.query.push({});
|
|
68
|
+
args.map(function (val, key)
|
|
69
|
+
{
|
|
70
|
+
if (typeof val == 'function') {
|
|
71
|
+
var func = val;
|
|
72
|
+
args[key] = {query: new Builder()};
|
|
73
|
+
args[key].query = func(args[key].query).toArray();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return;
|
|
77
|
+
});
|
|
78
|
+
this.query[this.query.length - 1][method] = args;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
toArray() {
|
|
83
|
+
return this.query;
|
|
84
|
+
}
|
|
85
|
+
};
|
package/src/NewApi.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import NewApiRequest from "./NewApiRequest";
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
|
|
4
|
+
export default class NewApi {
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param target
|
|
8
|
+
* @param focus
|
|
9
|
+
* @param data
|
|
10
|
+
* @param method
|
|
11
|
+
* @return {NewApiRequest}
|
|
12
|
+
*/
|
|
13
|
+
static create(target, focus, data = {}, method = 'GET') {
|
|
14
|
+
return new NewApiRequest(target, focus, data, method);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param target
|
|
20
|
+
* @param focus
|
|
21
|
+
* @param arg
|
|
22
|
+
* @param data
|
|
23
|
+
* @param method
|
|
24
|
+
* @return {NewApiRequest}
|
|
25
|
+
*/
|
|
26
|
+
static createArg(target, focus, arg, data = {}, method = 'GET') {
|
|
27
|
+
return new NewApiRequest(target, focus, data, method).addArg(arg);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @param target
|
|
33
|
+
* @param focus
|
|
34
|
+
* @param arg
|
|
35
|
+
* @param data
|
|
36
|
+
* @return {NewApiRequest}
|
|
37
|
+
*/
|
|
38
|
+
static getArg(target, focus, arg, data = {}) {
|
|
39
|
+
return NewApi.get(target, focus, data).addArg(arg);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param target
|
|
45
|
+
* @param focus
|
|
46
|
+
* @param arg
|
|
47
|
+
* @param data
|
|
48
|
+
* @return {NewApiRequest}
|
|
49
|
+
*/
|
|
50
|
+
static postArg(target, focus, arg, data = {}) {
|
|
51
|
+
return NewApi.post(target, focus, data).addArg(arg);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @param target
|
|
57
|
+
* @param focus
|
|
58
|
+
* @param arg
|
|
59
|
+
* @param data
|
|
60
|
+
* @return {NewApiRequest}
|
|
61
|
+
*/
|
|
62
|
+
static putArg(target, focus, arg, data = {}) {
|
|
63
|
+
return NewApi.put(target, focus, data).addArg(arg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param url
|
|
69
|
+
* @param data
|
|
70
|
+
* @return {NewApiRequest}
|
|
71
|
+
*/
|
|
72
|
+
static getUrl(url, data = {})
|
|
73
|
+
{
|
|
74
|
+
return (new NewApiRequest('', '', data, 'GET')).setUrl(url);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param url
|
|
80
|
+
* @param data
|
|
81
|
+
* @return {NewApiRequest}
|
|
82
|
+
*/
|
|
83
|
+
static postUrl(url, data = {})
|
|
84
|
+
{
|
|
85
|
+
return (new NewApiRequest('', '', data, 'POST')).setUrl(url);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @param url
|
|
91
|
+
* @param data
|
|
92
|
+
* @return {NewApiRequest}
|
|
93
|
+
*/
|
|
94
|
+
static putUrl(url, data = {})
|
|
95
|
+
{
|
|
96
|
+
return (new NewApiRequest('', '', data, 'PUT')).setUrl(url);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param target
|
|
102
|
+
* @param focus
|
|
103
|
+
* @param data
|
|
104
|
+
* @returns {NewApiRequest}
|
|
105
|
+
*/
|
|
106
|
+
static get(target, focus, data = {}) {
|
|
107
|
+
return new NewApiRequest(target, focus, data, 'GET');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @param target
|
|
112
|
+
* @param focus
|
|
113
|
+
* @param data
|
|
114
|
+
* @returns {NewApiRequest}
|
|
115
|
+
*/
|
|
116
|
+
static post(target, focus, data = {}) {
|
|
117
|
+
return new NewApiRequest(target, focus, data, 'POST');
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
*
|
|
121
|
+
* @param target
|
|
122
|
+
* @param focus
|
|
123
|
+
* @param data
|
|
124
|
+
* @returns {NewApiRequest}
|
|
125
|
+
*/
|
|
126
|
+
static put(target, focus, data = {}) {
|
|
127
|
+
return new NewApiRequest(target, focus, data, 'PUT');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
*
|
|
132
|
+
* @param target
|
|
133
|
+
* @param focus
|
|
134
|
+
* @param id
|
|
135
|
+
* @param data
|
|
136
|
+
* @return {ApiRequest}
|
|
137
|
+
*/
|
|
138
|
+
static delete(target, focus, id, data = {}) {
|
|
139
|
+
return new NewApiRequest(target, focus, data, 'DELETE').addArg(id);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static makeRequest({url, method, data, params, headers})
|
|
143
|
+
{
|
|
144
|
+
if (typeof headers === 'undefined') {
|
|
145
|
+
headers = {};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
headers['Content-Type'] = 'application/json';
|
|
149
|
+
|
|
150
|
+
let api_token = localStorage.getItem('api_token');
|
|
151
|
+
if (api_token)
|
|
152
|
+
{
|
|
153
|
+
headers["Authorization"] = api_token;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return axios({
|
|
157
|
+
url: url,
|
|
158
|
+
method: method,
|
|
159
|
+
data: data,
|
|
160
|
+
params: params,
|
|
161
|
+
headers: headers
|
|
162
|
+
})
|
|
163
|
+
.then(response => {
|
|
164
|
+
return response.data;
|
|
165
|
+
})
|
|
166
|
+
.catch(error => {
|
|
167
|
+
throw new Error(`API request to ${url} failed: ${error}`);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import Api from "./Api";
|
|
2
|
+
import ApiRequest from "./ApiRequest";
|
|
3
|
+
import NotifyManager from "../../interface/notify/NotifyManager";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export default class NewApiRequest extends ApiRequest
|
|
9
|
+
{
|
|
10
|
+
|
|
11
|
+
callUrl(success = function () {
|
|
12
|
+
}, error = function () {
|
|
13
|
+
}, params = {})
|
|
14
|
+
{
|
|
15
|
+
let self = this;
|
|
16
|
+
this.callSuccess = success;
|
|
17
|
+
this.callError = error;
|
|
18
|
+
|
|
19
|
+
let notify = null;
|
|
20
|
+
Api.makeRequest({
|
|
21
|
+
url: this.url,
|
|
22
|
+
method: this.method,
|
|
23
|
+
data: {
|
|
24
|
+
arguments: this.arguments,
|
|
25
|
+
query: this.builder.toArray(),
|
|
26
|
+
data: this.data,
|
|
27
|
+
},
|
|
28
|
+
...params,
|
|
29
|
+
success: (response, status, xhr) => {
|
|
30
|
+
if(response && response.result === 'success')
|
|
31
|
+
{
|
|
32
|
+
if (response.meta && response.meta.text)
|
|
33
|
+
{
|
|
34
|
+
notify = this.notify ? NotifyManager.info('Успешно', response.meta.text) : null
|
|
35
|
+
}
|
|
36
|
+
self.toBind(response);
|
|
37
|
+
self.resetBindErrors();
|
|
38
|
+
success(response, status, xhr);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
error: (xhr, status, errorText) => {
|
|
42
|
+
this.handleError(notify, error, xhr, status, errorText);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
};
|
package/src/UrlBind.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import QueryString from "query-string";
|
|
2
|
+
|
|
3
|
+
export default class UrlBind {
|
|
4
|
+
|
|
5
|
+
static parse = false;
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
static order(query, options) {
|
|
9
|
+
if (options.value != '') {
|
|
10
|
+
var order = options.value.split('.');
|
|
11
|
+
query.orderBy(order[0], order[1]);
|
|
12
|
+
} else {
|
|
13
|
+
}
|
|
14
|
+
return query;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static filter(query, options) {
|
|
18
|
+
for (var key in options) {
|
|
19
|
+
var data = options[key];
|
|
20
|
+
if (typeof data === 'object') {
|
|
21
|
+
if (data.value !== '') {
|
|
22
|
+
if (!data.hasOwnProperty('method')) {
|
|
23
|
+
query.where(
|
|
24
|
+
data.hasOwnProperty('field') ? data.field : key,
|
|
25
|
+
data.hasOwnProperty('operator') ? data.operator : '=',
|
|
26
|
+
data.operator === 'LIKE' ? '%' + data.value + '%' : data.value
|
|
27
|
+
);
|
|
28
|
+
} else if (typeof data.method === 'function') {
|
|
29
|
+
data.method(query, data.value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const parsed = QueryString.parse(History.location.search);
|
|
33
|
+
parsed[key] = data.value;
|
|
34
|
+
|
|
35
|
+
} else {
|
|
36
|
+
const parsed = QueryString.parse(History.location.search);
|
|
37
|
+
if (key in parsed) {
|
|
38
|
+
delete parsed[key];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return query;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static getParams(query, options)
|
|
48
|
+
{
|
|
49
|
+
let parsed = [];
|
|
50
|
+
|
|
51
|
+
for (var key in options) {
|
|
52
|
+
var data = options[key];
|
|
53
|
+
if (typeof data === 'object') {
|
|
54
|
+
if (data.value !== '') {
|
|
55
|
+
if (!data.hasOwnProperty('method')) {
|
|
56
|
+
query.where(
|
|
57
|
+
data.hasOwnProperty('field') ? data.field : key,
|
|
58
|
+
data.hasOwnProperty('operator') ? data.operator : '=',
|
|
59
|
+
data.operator === 'LIKE' ? '%' + data.value + '%' : data.value
|
|
60
|
+
);
|
|
61
|
+
} else if (typeof data.method === 'function') {
|
|
62
|
+
data.method(query, data.value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
parsed = QueryString.parse(History.location.search);
|
|
66
|
+
parsed[key] = data.value;
|
|
67
|
+
} else {
|
|
68
|
+
parsed = QueryString.parse(History.location.search);
|
|
69
|
+
if (key in parsed) {
|
|
70
|
+
delete parsed[key];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return query;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static urlParse(obj) {
|
|
80
|
+
|
|
81
|
+
UrlBind.parse = true;
|
|
82
|
+
|
|
83
|
+
for (var key in obj) {
|
|
84
|
+
var data = obj[key];
|
|
85
|
+
if (typeof data === 'object') {
|
|
86
|
+
if (data.value === '') {}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|