@osimatic/helpers-js 1.0.25 → 1.0.28
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 +7 -0
- package/flash_message.js +7 -5
- package/form_helper.js +8 -4
- package/network.js +142 -56
- package/package.json +1 -1
package/changelog.txt
CHANGED
|
@@ -31,3 +31,10 @@ getDateObjectSelected(lien) -> getSelectedDate(lien.closest('.form-group')
|
|
|
31
31
|
selectFormDate(lien) -> setSelectedDate(lien.closest('.form-group')
|
|
32
32
|
majSelectPeriode() / majSelectCompare() -> updatePeriodSelect(form)
|
|
33
33
|
|
|
34
|
+
let URL_REFRESH = ... -> HTTPRequest.setRefreshTokenUrl(URL_REFRESH)
|
|
35
|
+
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
|
@@ -1,20 +1,63 @@
|
|
|
1
1
|
|
|
2
2
|
class HTTPRequest {
|
|
3
|
-
static
|
|
3
|
+
static setRefreshTokenUrl(url) {
|
|
4
|
+
this.refreshTokenUrl = url;
|
|
5
|
+
}
|
|
6
|
+
static setRefreshTokenCallback(callback) {
|
|
7
|
+
this.refreshTokenCallback = callback;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static setHeader(key, value) {
|
|
11
|
+
if (typeof this.headers == 'undefined') {
|
|
12
|
+
this.headers = {};
|
|
13
|
+
}
|
|
14
|
+
this.headers[key] = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static getHeaders() {
|
|
18
|
+
//if (typeof httpHeaders != 'undefined') {
|
|
19
|
+
// return httpHeaders;
|
|
20
|
+
//}
|
|
21
|
+
|
|
22
|
+
if (typeof this.headers == 'undefined') {
|
|
23
|
+
this.headers = {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let httpHeaders = new Headers();
|
|
27
|
+
Object.entries(this.headers).forEach(([key, value]) => {
|
|
28
|
+
httpHeaders.append(key, value);
|
|
29
|
+
});
|
|
30
|
+
return httpHeaders;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static formatQueryString(data) {
|
|
4
34
|
if (data == null) {
|
|
5
|
-
|
|
35
|
+
return '';
|
|
6
36
|
}
|
|
7
|
-
|
|
37
|
+
if (typeof data == 'object') {
|
|
8
38
|
data = UrlAndQueryString.buildQuery(data);
|
|
9
39
|
}
|
|
10
40
|
if (data !== '' && data.substring(0, 1) !== '&') {
|
|
11
41
|
data = '&' + data;
|
|
12
42
|
}
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static logRequestFailure(response, json) {
|
|
47
|
+
console.error('Request failure. Status: '+response.statusText+' ; HTTP Code : '+response.status, json);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static logJqueryRequestFailure(jqxhr, status, errorThrown) {
|
|
51
|
+
console.error('Request failure. Status: '+status+' ; HTTP Code: '+jqxhr.responseJSON.code+(null!=errorThrown && ''!==errorThrown ? ' ; Error message: '+errorThrown : ''), jqxhr.responseJSON);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static async get(url, data, successCallback, errorCallback) {
|
|
55
|
+
data = this.formatQueryString(data);
|
|
13
56
|
|
|
14
57
|
if (window.fetch) {
|
|
15
58
|
let requestInit = {
|
|
16
59
|
method: 'GET',
|
|
17
|
-
headers:
|
|
60
|
+
headers: HTTPRequest.getHeaders(),
|
|
18
61
|
mode: 'cors',
|
|
19
62
|
cache: 'no-cache'
|
|
20
63
|
}
|
|
@@ -24,9 +67,10 @@ class HTTPRequest {
|
|
|
24
67
|
try {
|
|
25
68
|
jsonData = await response.json();
|
|
26
69
|
//console.log(url, jsonData);
|
|
70
|
+
//console.log(response.status, response.statusText, jsonData['error']);
|
|
27
71
|
|
|
28
|
-
if (response.status == 401 && response.statusText
|
|
29
|
-
HTTPRequest.refreshToken(
|
|
72
|
+
if (response.status == 401 && (response.statusText === "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
|
|
73
|
+
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
30
74
|
return;
|
|
31
75
|
}
|
|
32
76
|
|
|
@@ -36,11 +80,17 @@ class HTTPRequest {
|
|
|
36
80
|
}
|
|
37
81
|
}
|
|
38
82
|
catch (e) {
|
|
39
|
-
console.
|
|
40
|
-
|
|
83
|
+
console.error(e);
|
|
84
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
85
|
+
errorCallback(response, response.status, e);
|
|
86
|
+
}
|
|
41
87
|
return;
|
|
42
88
|
}
|
|
43
|
-
|
|
89
|
+
|
|
90
|
+
HTTPRequest.logRequestFailure(response, jsonData);
|
|
91
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
92
|
+
errorCallback(response, response.status, null, jsonData);
|
|
93
|
+
}
|
|
44
94
|
return;
|
|
45
95
|
}
|
|
46
96
|
|
|
@@ -49,36 +99,31 @@ class HTTPRequest {
|
|
|
49
99
|
$.ajax({
|
|
50
100
|
type: 'GET',
|
|
51
101
|
url: url + (!url.includes('?') ? '?' : '') + data,
|
|
52
|
-
headers:
|
|
102
|
+
headers: HTTPRequest.getHeaders(),
|
|
53
103
|
dataType: 'json',
|
|
54
104
|
cache: false,
|
|
55
105
|
success: (data) => successCallback(data),
|
|
56
|
-
error: (jqxhr, status,
|
|
57
|
-
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message
|
|
58
|
-
HTTPRequest.refreshToken(
|
|
59
|
-
|
|
60
|
-
|
|
106
|
+
error: (jqxhr, status, errorThrown) => {
|
|
107
|
+
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' ))) {
|
|
108
|
+
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
113
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
114
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
61
115
|
}
|
|
62
116
|
}
|
|
63
117
|
});
|
|
64
118
|
}
|
|
65
119
|
|
|
66
120
|
static async download(url, data, errorCallback, completeCallback) {
|
|
67
|
-
|
|
68
|
-
data = '';
|
|
69
|
-
}
|
|
70
|
-
else if (typeof data == 'object') {
|
|
71
|
-
data = UrlAndQueryString.buildQuery(data);
|
|
72
|
-
}
|
|
73
|
-
if (data !== '' && data.substring(0, 1) !== '&') {
|
|
74
|
-
data = '&' + data;
|
|
75
|
-
}
|
|
76
|
-
|
|
121
|
+
data = this.formatQueryString(data);
|
|
77
122
|
|
|
78
123
|
if (window.fetch) {
|
|
79
124
|
let requestInit = {
|
|
80
125
|
method: 'GET',
|
|
81
|
-
headers:
|
|
126
|
+
headers: HTTPRequest.getHeaders(),
|
|
82
127
|
mode: 'cors',
|
|
83
128
|
cache: 'no-cache'
|
|
84
129
|
}
|
|
@@ -89,20 +134,23 @@ class HTTPRequest {
|
|
|
89
134
|
/*console.log(url);
|
|
90
135
|
console.log(blobData);*/
|
|
91
136
|
|
|
92
|
-
if (response.status == 401 && response.statusText
|
|
93
|
-
HTTPRequest.refreshToken(
|
|
137
|
+
if (response.status == 401 && response.statusText === "Expired JWT Token") {
|
|
138
|
+
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
|
|
94
139
|
return;
|
|
95
140
|
}
|
|
96
141
|
|
|
97
142
|
if (response.ok) {
|
|
98
143
|
File.download(blobData, response.headers.get('content-type'), response.headers.get('content-disposition'));
|
|
99
144
|
}
|
|
100
|
-
else
|
|
101
|
-
|
|
145
|
+
else {
|
|
146
|
+
HTTPRequest.logRequestFailure(response, null);
|
|
147
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
148
|
+
errorCallback(response, response.status, null);
|
|
149
|
+
}
|
|
102
150
|
}
|
|
103
151
|
}
|
|
104
152
|
catch (e) {
|
|
105
|
-
console.
|
|
153
|
+
console.error(e);
|
|
106
154
|
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
107
155
|
errorCallback(response, response.status, e);
|
|
108
156
|
}
|
|
@@ -118,17 +166,21 @@ class HTTPRequest {
|
|
|
118
166
|
$.ajax({
|
|
119
167
|
type: 'GET',
|
|
120
168
|
url: url + (!url.includes('?') ? '?' : '') + data,
|
|
121
|
-
headers:
|
|
169
|
+
headers: HTTPRequest.getHeaders(),
|
|
122
170
|
cache: false,
|
|
123
171
|
xhrFields: {
|
|
124
172
|
responseType: 'blob'
|
|
125
173
|
},
|
|
126
174
|
success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
|
|
127
|
-
error: (jqxhr, status,
|
|
128
|
-
if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message
|
|
129
|
-
HTTPRequest.refreshToken(
|
|
130
|
-
|
|
131
|
-
|
|
175
|
+
error: (jqxhr, status, errorThrown) => {
|
|
176
|
+
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' ))) {
|
|
177
|
+
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
182
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
183
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
132
184
|
}
|
|
133
185
|
},
|
|
134
186
|
complete: (jqxhr, status) => {
|
|
@@ -140,11 +192,17 @@ class HTTPRequest {
|
|
|
140
192
|
}
|
|
141
193
|
|
|
142
194
|
static async post(url, formData, successCallback, errorCallback, formErrorCallback) {
|
|
195
|
+
if (!(formData instanceof FormData)) {
|
|
196
|
+
let formDataObject = new FormData();
|
|
197
|
+
Object.entries(formData).forEach(([key, value]) => formDataObject.append(key, value));
|
|
198
|
+
formData = formDataObject;
|
|
199
|
+
}
|
|
200
|
+
|
|
143
201
|
if (window.fetch && false) {
|
|
144
202
|
let requestInit = {
|
|
145
203
|
method: 'POST',
|
|
146
204
|
body: formData,
|
|
147
|
-
headers:
|
|
205
|
+
headers: HTTPRequest.getHeaders(),
|
|
148
206
|
mode: 'cors',
|
|
149
207
|
cache: 'no-cache'
|
|
150
208
|
};
|
|
@@ -156,13 +214,15 @@ class HTTPRequest {
|
|
|
156
214
|
jsonData = await response.json();
|
|
157
215
|
//console.log(url, jsonData);
|
|
158
216
|
|
|
159
|
-
if (response.status == 401 && response.statusText
|
|
160
|
-
HTTPRequest.refreshToken(
|
|
217
|
+
if (response.status == 401 && (response.statusText === "Expired JWT Token" || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
|
|
218
|
+
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
161
219
|
return;
|
|
162
220
|
}
|
|
163
221
|
|
|
164
222
|
if (response.ok) {
|
|
165
|
-
successCallback
|
|
223
|
+
if (typeof successCallback != 'undefined' && successCallback != null) {
|
|
224
|
+
successCallback(jsonData);
|
|
225
|
+
}
|
|
166
226
|
return;
|
|
167
227
|
}
|
|
168
228
|
|
|
@@ -172,12 +232,17 @@ class HTTPRequest {
|
|
|
172
232
|
}
|
|
173
233
|
}
|
|
174
234
|
catch (e) {
|
|
175
|
-
console.
|
|
176
|
-
|
|
235
|
+
console.error(e);
|
|
236
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
237
|
+
errorCallback(response, response.status, e);
|
|
238
|
+
}
|
|
177
239
|
return;
|
|
178
240
|
}
|
|
179
241
|
|
|
180
|
-
|
|
242
|
+
HTTPRequest.logRequestFailure(response, jsonData);
|
|
243
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
244
|
+
errorCallback(response, response.status, null, jsonData);
|
|
245
|
+
}
|
|
181
246
|
return;
|
|
182
247
|
}
|
|
183
248
|
|
|
@@ -186,35 +251,56 @@ class HTTPRequest {
|
|
|
186
251
|
$.ajax({
|
|
187
252
|
type: 'POST',
|
|
188
253
|
url: url,
|
|
189
|
-
headers:
|
|
254
|
+
headers: HTTPRequest.getHeaders(),
|
|
190
255
|
dataType: 'json', // 22/09/2020 : à voir si cette ligne pose pb (utilisé pour requete import et peut être d'autres
|
|
191
256
|
data: formData,
|
|
192
257
|
cache: false,
|
|
193
258
|
contentType: false,
|
|
194
259
|
processData: false,
|
|
195
|
-
success: (data) =>
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
260
|
+
success: (data) => {
|
|
261
|
+
if (typeof successCallback != 'undefined' && successCallback != null) {
|
|
262
|
+
successCallback(data);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
error: (jqxhr, status, errorThrown) => {
|
|
266
|
+
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' ))) {
|
|
267
|
+
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
|
|
271
|
+
formErrorCallback(jqxhr, status, errorThrown);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
|
|
276
|
+
if (typeof errorCallback != 'undefined' && errorCallback != null) {
|
|
277
|
+
errorCallback(jqxhr, status, errorThrown);
|
|
203
278
|
}
|
|
204
279
|
}
|
|
205
280
|
});
|
|
206
281
|
}
|
|
207
282
|
|
|
208
|
-
static refreshToken(
|
|
283
|
+
static refreshToken(onCompleteCallback) {
|
|
284
|
+
if (typeof this.refreshTokenCallback == 'function') {
|
|
285
|
+
this.refreshTokenCallback(onCompleteCallback);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (typeof this.refreshTokenUrl == 'undefined') {
|
|
290
|
+
console.error('URL refresh token non définie. Appeler HTTPRequest.setRefreshTokenUrl(url)');
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
209
294
|
let payload = new FormData();
|
|
210
295
|
payload.append('refresh_token', JwtSession.getRefreshToken());
|
|
211
296
|
|
|
212
|
-
HTTPRequest.post(
|
|
297
|
+
HTTPRequest.post(this.refreshTokenUrl, payload,
|
|
213
298
|
(data) => {
|
|
214
299
|
JwtSession.setToken(data.token);
|
|
215
300
|
JwtSession.setRefreshToken(data.refresh_token);
|
|
216
301
|
onCompleteCallback();
|
|
217
|
-
},
|
|
302
|
+
},
|
|
303
|
+
(jqxhr, status, exception) => {
|
|
218
304
|
console.log(exception);
|
|
219
305
|
JwtSession.logout();
|
|
220
306
|
}
|