@radiantabyss/vue 3.3.17 → 3.3.19
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/Request.js +337 -304
package/package.json
CHANGED
package/src/Request.js
CHANGED
|
@@ -1,304 +1,337 @@
|
|
|
1
|
-
let sprite_version = import.meta.env.VITE_SPRITE_VERSION;
|
|
2
|
-
let get_params_format = import.meta.env.VITE_GET_PARAMS_FORMAT || 'php';
|
|
3
|
-
let
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
formData.append(key
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
1
|
+
let sprite_version = import.meta.env.VITE_SPRITE_VERSION;
|
|
2
|
+
let get_params_format = import.meta.env.VITE_GET_PARAMS_FORMAT || 'php';
|
|
3
|
+
let post_format = import.meta.env.VITE_POST_FORMAT || 'default';
|
|
4
|
+
let current = {};
|
|
5
|
+
|
|
6
|
+
//content type used for POST when the caller doesn't set one, picked by VITE_POST_FORMAT
|
|
7
|
+
const post_content_types = {
|
|
8
|
+
default: 'application/x-www-form-urlencoded',
|
|
9
|
+
form_data: 'multipart/form-data',
|
|
10
|
+
json: 'application/json',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const formatErrors = function(response) {
|
|
14
|
+
let errors = [];
|
|
15
|
+
if ( response && response.errors ) {
|
|
16
|
+
for ( let i in response.errors ) {
|
|
17
|
+
if ( typeof response.errors[i] === 'object' ) {
|
|
18
|
+
for (let j in response.errors[i]) {
|
|
19
|
+
errors.push(response.errors[i][j]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
errors.push(response.errors[i]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else if ( response.statusText ) {
|
|
28
|
+
errors.push(response.statusText);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return errors;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const serializeToURLEncoded = (obj, prefix) => {
|
|
35
|
+
const str = [];
|
|
36
|
+
for ( let p in obj ) {
|
|
37
|
+
if ( obj.hasOwnProperty(p) ) {
|
|
38
|
+
const key = prefix ? `${prefix}[${p}]` : p;
|
|
39
|
+
const value = obj[p];
|
|
40
|
+
|
|
41
|
+
if ( typeof value === "object" ) {
|
|
42
|
+
str.push(serializeToURLEncoded(value, key));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
str.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return str.join("&");
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const appendFormData = (formData, key, value) => {
|
|
53
|
+
if ( value instanceof Blob ) {
|
|
54
|
+
formData.append(key, value);
|
|
55
|
+
}
|
|
56
|
+
else if ( Array.isArray(value) ) {
|
|
57
|
+
value.forEach((v) => formData.append(`${key}[]`, v));
|
|
58
|
+
}
|
|
59
|
+
else if ( typeof value === 'object' && value !== null ) {
|
|
60
|
+
for ( let subKey in value ) {
|
|
61
|
+
appendFormData(formData, `${key}[${subKey}]`, value[subKey]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
formData.append(key, value);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const buildParams = (obj, prefix = '', params = new URLSearchParams()) => {
|
|
70
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
71
|
+
const full_key = prefix ? `${prefix}[${key}]` : key;
|
|
72
|
+
|
|
73
|
+
if ( Array.isArray(value) ) {
|
|
74
|
+
if ( value.length ) {
|
|
75
|
+
value.forEach((v, i) => {
|
|
76
|
+
if (typeof v === 'object') {
|
|
77
|
+
buildParams(v, `${full_key}[${i}]`, params);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
params.append(`${full_key}[]`, v);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
params.append(full_key, value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if ( typeof value === 'object' && value !== null ) {
|
|
89
|
+
buildParams(value, full_key, params);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
params.append(full_key, value);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return params;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//recursively drop undefined values. nulls and empty arrays follow the `empty` mode:
|
|
100
|
+
//'drop' removes them, 'empty' sends them as an empty string (form bodies can't carry null or [],
|
|
101
|
+
//and `key=` lets the backend read it as null to clear a field), 'keep' leaves them as-is (json)
|
|
102
|
+
const cleanPayload = (value, empty) => {
|
|
103
|
+
if ( value === null ) {
|
|
104
|
+
if ( empty == 'keep' ) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return empty == 'empty' ? '' : undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if ( Array.isArray(value) ) {
|
|
112
|
+
const cleaned = value.map((v) => cleanPayload(v, empty)).filter((v) => v !== undefined);
|
|
113
|
+
|
|
114
|
+
if ( !cleaned.length && empty != 'keep' ) {
|
|
115
|
+
return empty == 'empty' ? '' : undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return cleaned;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if ( typeof value === 'object' && !(value instanceof Blob) ) {
|
|
122
|
+
const cleaned = {};
|
|
123
|
+
for ( let key in value ) {
|
|
124
|
+
const v = cleanPayload(value[key], empty);
|
|
125
|
+
|
|
126
|
+
if ( v !== undefined ) {
|
|
127
|
+
cleaned[key] = v;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return cleaned;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return value;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const request = function(method, edge, payload = {}, display_errors = false, base_url = null, auth_token = null, headers = {}, upload_progress = null) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
if ( !base_url ) {
|
|
140
|
+
base_url = import.meta.env.VITE_BACK_URL;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if ( !auth_token ) {
|
|
144
|
+
auth_token = localStorage.getItem('jwt_token');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if ( method == 'POST' ) {
|
|
148
|
+
Alert.hide();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
headers = { ...headers };
|
|
152
|
+
|
|
153
|
+
if ( method == 'POST' && !headers['Content-Type'] ) {
|
|
154
|
+
headers['Content-Type'] = post_content_types[post_format] || post_content_types.default;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let _button;
|
|
158
|
+
if ( payload._button !== undefined ) {
|
|
159
|
+
_button = payload._button;
|
|
160
|
+
delete payload._button;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let _event;
|
|
164
|
+
if ( payload._event !== undefined ) {
|
|
165
|
+
_event = payload._event;
|
|
166
|
+
_event.preventDefault();
|
|
167
|
+
|
|
168
|
+
if ( _button === undefined ) {
|
|
169
|
+
_button = _event.target;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
delete payload._event;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if ( _button !== undefined ) {
|
|
176
|
+
if ( _button.disabled ) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
_button.disabled = true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let _cancel;
|
|
183
|
+
if ( payload._cancel !== undefined ) {
|
|
184
|
+
_cancel = new AbortController();
|
|
185
|
+
delete payload._cancel;
|
|
186
|
+
|
|
187
|
+
if ( current[edge] ) {
|
|
188
|
+
try {
|
|
189
|
+
current[edge].abort();
|
|
190
|
+
}
|
|
191
|
+
catch(e) {}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
current[edge] = _cancel;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//drop undefined keys; nulls and empty arrays are dropped on GET, sent as empty values
|
|
198
|
+
//in form bodies and kept as-is in json
|
|
199
|
+
let empty = 'keep';
|
|
200
|
+
if ( method == 'GET' ) {
|
|
201
|
+
empty = 'drop';
|
|
202
|
+
}
|
|
203
|
+
else if ( ['multipart/form-data', 'application/x-www-form-urlencoded'].includes(headers['Content-Type']) ) {
|
|
204
|
+
empty = 'empty';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
payload = cleanPayload(payload, empty);
|
|
208
|
+
|
|
209
|
+
let data;
|
|
210
|
+
let url = base_url + edge;
|
|
211
|
+
|
|
212
|
+
if ( method == 'GET' ) {
|
|
213
|
+
if ( get_params_format == 'php' ) {
|
|
214
|
+
url += '?' + buildParams(payload);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
url += '?' + new URLSearchParams(payload).toString();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else if ( method == 'POST' ) {
|
|
221
|
+
if ( headers['Content-Type'] == 'multipart/form-data' ) {
|
|
222
|
+
data = new FormData();
|
|
223
|
+
|
|
224
|
+
for ( let key in payload ) {
|
|
225
|
+
appendFormData(data, key, payload[key]);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
delete headers['Content-Type'];
|
|
229
|
+
}
|
|
230
|
+
else if ( headers['Content-Type'] == 'application/x-www-form-urlencoded' ) {
|
|
231
|
+
data = serializeToURLEncoded(payload).replace(/\&+$/, '');
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
data = JSON.stringify(payload);
|
|
235
|
+
headers['Content-Type'] = 'application/json';
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if ( auth_token ) {
|
|
240
|
+
if ( typeof auth_token !== 'object' ) {
|
|
241
|
+
auth_token = { jwt_token: auth_token };
|
|
242
|
+
}
|
|
243
|
+
for ( let key in auth_token ) {
|
|
244
|
+
if ( auth_token[key] !== '' && auth_token[key] !== null ) {
|
|
245
|
+
url += url.includes('?') ? '&' : '?';
|
|
246
|
+
url += `${key}=${auth_token[key]}`;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
let _button_html;
|
|
252
|
+
if ( typeof _button !== 'undefined' ) {
|
|
253
|
+
_button_html = _button.innerHTML;
|
|
254
|
+
|
|
255
|
+
if ( payload._replace_html ) {
|
|
256
|
+
_button.innerHTML = `<svg class="svg-request-spinner"><use xlink:href="/sprites.svg?v=${sprite_version}#request-spinner"></use></svg>`;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
_button.innerHTML += ` <svg class="svg-request-spinner"><use xlink:href="/sprites.svg?v=${sprite_version}#request-spinner"></use></svg>`;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let options = { method, headers };
|
|
264
|
+
|
|
265
|
+
if ( method != 'GET' ) {
|
|
266
|
+
options.body = data;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if ( _cancel ) {
|
|
270
|
+
options.signal = _cancel.signal;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
fetch(url, options)
|
|
274
|
+
.then(async (response) => {
|
|
275
|
+
if ( _button !== undefined ) {
|
|
276
|
+
_button.disabled = false;
|
|
277
|
+
_button.innerHTML = _button_html;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
delete current[edge];
|
|
281
|
+
|
|
282
|
+
const response_data = await response.json();
|
|
283
|
+
|
|
284
|
+
if ( edge.match(/\.json/) ) {
|
|
285
|
+
return resolve(response_data);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if ( response.ok && response_data.success ) {
|
|
289
|
+
return resolve(response_data.data);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const errors = formatErrors(response_data);
|
|
293
|
+
if ( display_errors ) {
|
|
294
|
+
Alert.error(errors.join('<br/>'), 7000);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
reject(errors);
|
|
298
|
+
})
|
|
299
|
+
.catch((error) => {
|
|
300
|
+
if ( error.name == 'AbortError' ) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
delete current[edge];
|
|
305
|
+
|
|
306
|
+
let errors = [error.message];
|
|
307
|
+
|
|
308
|
+
if ( display_errors ) {
|
|
309
|
+
Alert.error(__('Internal Server Error. We\'re fixing the problem. Please retry in a couple of hours.'), 7000);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if ( _button !== undefined ) {
|
|
313
|
+
_button.disabled = false;
|
|
314
|
+
_button.innerHTML = _button_html;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
reject(errors);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
let self = {
|
|
323
|
+
get(edge, payload = {}, display_errors = false, base_url = null, auth_token = null, headers = {}) {
|
|
324
|
+
return request('GET', edge, payload, display_errors, base_url, auth_token, headers);
|
|
325
|
+
},
|
|
326
|
+
|
|
327
|
+
post(edge, payload = {}, display_errors = true, base_url = null, auth_token = null, headers = {}) {
|
|
328
|
+
return request('POST', edge, payload, display_errors, base_url, auth_token, headers);
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
upload(edge, payload = {}, display_errors = true, upload_progress, base_url = null, auth_token = null, headers = {}) {
|
|
332
|
+
headers['Content-Type'] = 'multipart/form-data';
|
|
333
|
+
return request('POST', edge, payload, display_errors, base_url, auth_token, headers, upload_progress);
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
export default self;
|