@osimatic/helpers-js 1.0.26 → 1.0.29
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/changelog.txt +5 -1
- package/flash_message.js +7 -5
- package/form_helper.js +8 -4
- package/network.js +123 -55
- package/package.json +1 -1
package/changelog.txt
CHANGED
|
@@ -33,4 +33,8 @@ majSelectPeriode() / majSelectCompare() -> updatePeriodSelect(form)
|
|
|
33
33
|
|
|
34
34
|
let URL_REFRESH = ... -> HTTPRequest.setRefreshTokenUrl(URL_REFRESH)
|
|
35
35
|
let _httpHeaders = {...} -> HTTPRequest.setHeader(key, value);
|
|
36
|
-
let httpHeaders = {...} -> HTTPRequest.setHeader(key, value);
|
|
36
|
+
let httpHeaders = {...} -> HTTPRequest.setHeader(key, value);
|
|
37
|
+
remplacer l'utilisation des variables httpHeaders / _httpHeaders par HTTPRequest.getHeaders()
|
|
38
|
+
|
|
39
|
+
FlashMessage.displayRequestFailure(status, exception, modal) -> FlashMessage.displayError(labelErrorOccured, modal)
|
|
40
|
+
FormHelper.logRequestFailure(status, exception) -> HTTPRequest.logJqueryRequestFailure(jqxhr, status, exception) (le log de l'erreur est devenu inutile car fait de base dans la classe HTTPRequest)
|
package/flash_message.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
class FlashMessage {
|
|
2
|
-
static displayRequestFailure(status, exception, modal) {
|
|
3
|
-
console.log('request failure. Status: ', status, ' Exception: ', exception);
|
|
4
|
-
this.display('danger', typeof labelErrorOccured != 'undefined' ? labelErrorOccured : "Une erreur s'est produite.", false, modal);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
2
|
static displaySuccess(message, reload, modal) {
|
|
8
3
|
this.display('success', message, reload, modal);
|
|
9
4
|
}
|
|
@@ -32,6 +27,13 @@ class FlashMessage {
|
|
|
32
27
|
document.location.reload();
|
|
33
28
|
}
|
|
34
29
|
}
|
|
30
|
+
|
|
31
|
+
/** @deprecated **/
|
|
32
|
+
static displayRequestFailure(status, exception, modal) {
|
|
33
|
+
console.log('request failure. Status: ', status, ' Exception: ', exception);
|
|
34
|
+
this.display('danger', typeof labelErrorOccured != 'undefined' ? labelErrorOccured : "Une erreur s'est produite.", false, modal);
|
|
35
|
+
}
|
|
36
|
+
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
module.exports = { FlashMessage };
|
package/form_helper.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
class FormHelper {
|
|
2
|
-
static logRequestFailure(status, exception) {
|
|
3
|
-
console.log('request failure. Status: '+status+' ; Exception: '+exception);
|
|
4
|
-
}
|
|
5
|
-
|
|
6
2
|
static resetSelectOption(form, selectName) {
|
|
7
3
|
form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
|
|
8
4
|
}
|
|
@@ -257,6 +253,14 @@ class FormHelper {
|
|
|
257
253
|
|
|
258
254
|
errorsParentDiv.prepend(errorDiv);
|
|
259
255
|
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
/** @deprecated **/
|
|
260
|
+
static logRequestFailure(status, exception) {
|
|
261
|
+
console.log('request failure. Status: '+status+' ; Exception: '+exception);
|
|
262
|
+
}
|
|
263
|
+
|
|
260
264
|
}
|
|
261
265
|
|
|
262
266
|
module.exports = { FormHelper };
|
package/network.js
CHANGED
|
@@ -30,33 +30,55 @@ class HTTPRequest {
|
|
|
30
30
|
return httpHeaders;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
static
|
|
33
|
+
static formatQueryString(data) {
|
|
34
34
|
if (data == null) {
|
|
35
|
-
|
|
35
|
+
return '';
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
if (typeof data == 'object') {
|
|
38
38
|
data = UrlAndQueryString.buildQuery(data);
|
|
39
39
|
}
|
|
40
40
|
if (data !== '' && data.substring(0, 1) !== '&') {
|
|
41
41
|
data = '&' + data;
|
|
42
42
|
}
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static formatFormData(data) {
|
|
47
|
+
if (!(data instanceof FormData)) {
|
|
48
|
+
let formData = new FormData();
|
|
49
|
+
Object.entries(data).forEach(([key, value]) => formData.append(key, value));
|
|
50
|
+
return formData;
|
|
51
|
+
}
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static logRequestFailure(response, json) {
|
|
56
|
+
console.error('Request failure. Status: '+response.statusText+' ; HTTP Code : '+response.status, json);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static logJqueryRequestFailure(jqxhr, status, errorThrown) {
|
|
60
|
+
console.error('Request failure. Status: '+status+' ; HTTP Code: '+jqxhr.responseJSON.code+(null!=errorThrown && ''!==errorThrown ? ' ; Error message: '+errorThrown : ''), jqxhr.responseJSON);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static async get(url, data, successCallback, errorCallback) {
|
|
64
|
+
url += (!url.includes('?') ? '?' : '') + this.formatQueryString(data);
|
|
65
|
+
data = null;
|
|
43
66
|
|
|
44
67
|
if (window.fetch) {
|
|
45
|
-
|
|
68
|
+
const response = await fetch(url, {
|
|
46
69
|
method: 'GET',
|
|
47
70
|
headers: HTTPRequest.getHeaders(),
|
|
48
71
|
mode: 'cors',
|
|
49
72
|
cache: 'no-cache'
|
|
50
|
-
}
|
|
73
|
+
});
|
|
51
74
|
|
|
52
75
|
let jsonData = {};
|
|
53
|
-
const response = await fetch(url + (!url.includes('?') ? '?' : '') + data, requestInit);
|
|
54
76
|
try {
|
|
55
77
|
jsonData = await response.json();
|
|
56
78
|
//console.log(url, jsonData);
|
|
57
79
|
//console.log(response.status, response.statusText, jsonData['error']);
|
|
58
80
|
|
|
59
|
-
if (response.status == 401 && (response.statusText
|
|
81
|
+
if (response.status == 401 && (response.statusText === "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
|
|
60
82
|
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
61
83
|
return;
|
|
62
84
|
}
|
|
@@ -67,11 +89,17 @@ class HTTPRequest {
|
|
|
67
89
|
}
|
|
68
90
|
}
|
|
69
91
|
catch (e) {
|
|
70
|
-
console.
|
|
71
|
-
|
|
92
|
+
console.error(e);
|
|
93
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
94
|
+
errorCallback(response, response.status, e);
|
|
95
|
+
}
|
|
72
96
|
return;
|
|
73
97
|
}
|
|
74
|
-
|
|
98
|
+
|
|
99
|
+
HTTPRequest.logRequestFailure(response, jsonData);
|
|
100
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
101
|
+
errorCallback(response, response.status, null, jsonData);
|
|
102
|
+
}
|
|
75
103
|
return;
|
|
76
104
|
}
|
|
77
105
|
|
|
@@ -79,32 +107,32 @@ class HTTPRequest {
|
|
|
79
107
|
console.error('fetch\'s polyfill used');
|
|
80
108
|
$.ajax({
|
|
81
109
|
type: 'GET',
|
|
82
|
-
url: url
|
|
110
|
+
url: url,
|
|
83
111
|
headers: HTTPRequest.getHeaders(),
|
|
84
112
|
dataType: 'json',
|
|
85
113
|
cache: false,
|
|
86
114
|
success: (data) => successCallback(data),
|
|
87
|
-
error: (jqxhr, status,
|
|
88
|
-
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message
|
|
115
|
+
error: (jqxhr, status, errorThrown) => {
|
|
116
|
+
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && (jqxhr.responseJSON.message === "Expired JWT Token" || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
|
|
89
117
|
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
90
|
-
|
|
91
|
-
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
122
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
123
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
92
124
|
}
|
|
93
125
|
}
|
|
94
126
|
});
|
|
95
127
|
}
|
|
96
128
|
|
|
97
|
-
static async download(url, data, errorCallback, completeCallback) {
|
|
98
|
-
|
|
99
|
-
data = '';
|
|
100
|
-
}
|
|
101
|
-
else if (typeof data == 'object') {
|
|
102
|
-
data = UrlAndQueryString.buildQuery(data);
|
|
103
|
-
}
|
|
104
|
-
if (data !== '' && data.substring(0, 1) !== '&') {
|
|
105
|
-
data = '&' + data;
|
|
106
|
-
}
|
|
129
|
+
static async download(url, data, errorCallback, completeCallback, method) {
|
|
130
|
+
method = typeof method == 'undefined' || null == method ? 'GET' : method;
|
|
107
131
|
|
|
132
|
+
if ('POST' !== method) {
|
|
133
|
+
url += (!url.includes('?') ? '?' : '') + this.formatQueryString(data);
|
|
134
|
+
data = null;
|
|
135
|
+
}
|
|
108
136
|
|
|
109
137
|
if (window.fetch) {
|
|
110
138
|
let requestInit = {
|
|
@@ -114,26 +142,34 @@ class HTTPRequest {
|
|
|
114
142
|
cache: 'no-cache'
|
|
115
143
|
}
|
|
116
144
|
|
|
117
|
-
|
|
145
|
+
if ('POST' === method) {
|
|
146
|
+
requestInit['method'] = 'POST';
|
|
147
|
+
requestInit['body'] = this.formatFormData(data);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const response = await fetch(url, requestInit);
|
|
118
151
|
try {
|
|
119
152
|
const blobData = await response.blob();
|
|
120
153
|
/*console.log(url);
|
|
121
154
|
console.log(blobData);*/
|
|
122
155
|
|
|
123
|
-
if (response.status == 401 && response.statusText
|
|
124
|
-
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
|
|
156
|
+
if (response.status == 401 && response.statusText === "Expired JWT Token") {
|
|
157
|
+
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
|
|
125
158
|
return;
|
|
126
159
|
}
|
|
127
160
|
|
|
128
161
|
if (response.ok) {
|
|
129
162
|
File.download(blobData, response.headers.get('content-type'), response.headers.get('content-disposition'));
|
|
130
163
|
}
|
|
131
|
-
else
|
|
132
|
-
|
|
164
|
+
else {
|
|
165
|
+
HTTPRequest.logRequestFailure(response, null);
|
|
166
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
167
|
+
errorCallback(response, response.status, null);
|
|
168
|
+
}
|
|
133
169
|
}
|
|
134
170
|
}
|
|
135
171
|
catch (e) {
|
|
136
|
-
console.
|
|
172
|
+
console.error(e);
|
|
137
173
|
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
138
174
|
errorCallback(response, response.status, e);
|
|
139
175
|
}
|
|
@@ -146,20 +182,35 @@ class HTTPRequest {
|
|
|
146
182
|
|
|
147
183
|
//l'api fetch n'est pas dispo pour ce navigateur => normalement ce cas ne devrait pas arriver car le polyfill est chargé
|
|
148
184
|
console.error('fetch\'s polyfill used');
|
|
149
|
-
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
let ajaxOptions = {
|
|
150
188
|
type: 'GET',
|
|
151
|
-
url: url
|
|
189
|
+
url: url,
|
|
152
190
|
headers: HTTPRequest.getHeaders(),
|
|
153
191
|
cache: false,
|
|
154
192
|
xhrFields: {
|
|
155
193
|
responseType: 'blob'
|
|
156
194
|
},
|
|
195
|
+
};
|
|
196
|
+
if ('POST' === method) {
|
|
197
|
+
ajaxOptions['type'] = 'POST';
|
|
198
|
+
ajaxOptions['data'] = this.formatFormData(data);
|
|
199
|
+
ajaxOptions['contentType'] = false;
|
|
200
|
+
ajaxOptions['processData'] = false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
$.ajax(Object.assign({...ajaxOptions}, {
|
|
157
204
|
success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
|
|
158
|
-
error: (jqxhr, status,
|
|
159
|
-
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message
|
|
160
|
-
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
|
|
161
|
-
|
|
162
|
-
|
|
205
|
+
error: (jqxhr, status, errorThrown) => {
|
|
206
|
+
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && (jqxhr.responseJSON.message === "Expired JWT Token" || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
|
|
207
|
+
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
212
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
213
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
163
214
|
}
|
|
164
215
|
},
|
|
165
216
|
complete: (jqxhr, status) => {
|
|
@@ -167,33 +218,35 @@ class HTTPRequest {
|
|
|
167
218
|
completeCallback(jqxhr, status);
|
|
168
219
|
}
|
|
169
220
|
}
|
|
170
|
-
});
|
|
221
|
+
}));
|
|
171
222
|
}
|
|
172
223
|
|
|
173
224
|
static async post(url, formData, successCallback, errorCallback, formErrorCallback) {
|
|
225
|
+
formData = this.formatFormData(formData);
|
|
226
|
+
|
|
174
227
|
if (window.fetch && false) {
|
|
175
|
-
|
|
228
|
+
const response = await fetch(url, {
|
|
176
229
|
method: 'POST',
|
|
177
230
|
body: formData,
|
|
178
231
|
headers: HTTPRequest.getHeaders(),
|
|
179
232
|
mode: 'cors',
|
|
180
233
|
cache: 'no-cache'
|
|
181
|
-
};
|
|
234
|
+
});
|
|
182
235
|
|
|
183
236
|
let jsonData = {};
|
|
184
|
-
const response = await fetch(url, requestInit);
|
|
185
|
-
|
|
186
237
|
try {
|
|
187
238
|
jsonData = await response.json();
|
|
188
239
|
//console.log(url, jsonData);
|
|
189
240
|
|
|
190
|
-
if (response.status == 401 && (response.statusText
|
|
241
|
+
if (response.status == 401 && (response.statusText === "Expired JWT Token" || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
|
|
191
242
|
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
192
243
|
return;
|
|
193
244
|
}
|
|
194
245
|
|
|
195
246
|
if (response.ok) {
|
|
196
|
-
successCallback
|
|
247
|
+
if (typeof successCallback != 'undefined' && successCallback != null) {
|
|
248
|
+
successCallback(jsonData);
|
|
249
|
+
}
|
|
197
250
|
return;
|
|
198
251
|
}
|
|
199
252
|
|
|
@@ -203,12 +256,17 @@ class HTTPRequest {
|
|
|
203
256
|
}
|
|
204
257
|
}
|
|
205
258
|
catch (e) {
|
|
206
|
-
console.
|
|
207
|
-
|
|
259
|
+
console.error(e);
|
|
260
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
261
|
+
errorCallback(response, response.status, e);
|
|
262
|
+
}
|
|
208
263
|
return;
|
|
209
264
|
}
|
|
210
265
|
|
|
211
|
-
|
|
266
|
+
HTTPRequest.logRequestFailure(response, jsonData);
|
|
267
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
268
|
+
errorCallback(response, response.status, null, jsonData);
|
|
269
|
+
}
|
|
212
270
|
return;
|
|
213
271
|
}
|
|
214
272
|
|
|
@@ -223,14 +281,24 @@ class HTTPRequest {
|
|
|
223
281
|
cache: false,
|
|
224
282
|
contentType: false,
|
|
225
283
|
processData: false,
|
|
226
|
-
success: (data) =>
|
|
227
|
-
|
|
228
|
-
|
|
284
|
+
success: (data) => {
|
|
285
|
+
if (typeof successCallback != 'undefined' && successCallback != null) {
|
|
286
|
+
successCallback(data);
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
error: (jqxhr, status, errorThrown) => {
|
|
290
|
+
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && (jqxhr.responseJSON.message === "Expired JWT Token" || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
|
|
229
291
|
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
|
|
295
|
+
formErrorCallback(jqxhr, status, errorThrown);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
300
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
301
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
234
302
|
}
|
|
235
303
|
}
|
|
236
304
|
});
|