@osimatic/helpers-js 1.0.27 → 1.0.30

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 CHANGED
@@ -34,4 +34,7 @@ majSelectPeriode() / majSelectCompare() -> updatePeriodSelect(form)
34
34
  let URL_REFRESH = ... -> HTTPRequest.setRefreshTokenUrl(URL_REFRESH)
35
35
  let _httpHeaders = {...} -> HTTPRequest.setHeader(key, value);
36
36
  let httpHeaders = {...} -> HTTPRequest.setHeader(key, value);
37
- remplacer l'utilisation des variables httpHeaders / _httpHeaders par HTTPRequest.getHeaders()
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)
@@ -78,7 +78,7 @@ class DetailsSubArray {
78
78
  $.ajax({
79
79
  url: link.data("url_details"),
80
80
  method: 'GET',
81
- headers: httpHeaders,
81
+ headers: HTTPRequest.getHeaders(),
82
82
  cache: false,
83
83
  dataType: 'json',
84
84
  success: function (jsonObj) {
package/file.js CHANGED
@@ -103,7 +103,7 @@ class Img {
103
103
  $.ajax({
104
104
  type: 'GET',
105
105
  url: url,
106
- headers: httpHeaders,
106
+ headers: HTTPRequest.getHeaders(),
107
107
  cache: false,
108
108
  xhrFields: {responseType: 'blob'},
109
109
  success: (data) => {
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
@@ -43,19 +43,36 @@ class HTTPRequest {
43
43
  return data;
44
44
  }
45
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
+
46
63
  static async get(url, data, successCallback, errorCallback) {
47
- data = this.formatQueryString(data);
64
+ url += (!url.includes('?') ? '?' : '') + this.formatQueryString(data);
65
+ data = null;
48
66
 
49
67
  if (window.fetch) {
50
- let requestInit = {
68
+ const response = await fetch(url, {
51
69
  method: 'GET',
52
70
  headers: HTTPRequest.getHeaders(),
53
71
  mode: 'cors',
54
72
  cache: 'no-cache'
55
- }
73
+ });
56
74
 
57
75
  let jsonData = {};
58
- const response = await fetch(url + (!url.includes('?') ? '?' : '') + data, requestInit);
59
76
  try {
60
77
  jsonData = await response.json();
61
78
  //console.log(url, jsonData);
@@ -72,12 +89,14 @@ class HTTPRequest {
72
89
  }
73
90
  }
74
91
  catch (e) {
75
- console.log(e);
92
+ console.error(e);
76
93
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
77
94
  errorCallback(response, response.status, e);
78
95
  }
79
96
  return;
80
97
  }
98
+
99
+ HTTPRequest.logRequestFailure(response, jsonData);
81
100
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
82
101
  errorCallback(response, response.status, null, jsonData);
83
102
  }
@@ -88,25 +107,32 @@ class HTTPRequest {
88
107
  console.error('fetch\'s polyfill used');
89
108
  $.ajax({
90
109
  type: 'GET',
91
- url: url + (!url.includes('?') ? '?' : '') + data,
110
+ url: url,
92
111
  headers: HTTPRequest.getHeaders(),
93
112
  dataType: 'json',
94
113
  cache: false,
95
114
  success: (data) => successCallback(data),
96
- error: (jqxhr, status, exception) => {
115
+ error: (jqxhr, status, errorThrown) => {
97
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' ))) {
98
117
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
99
118
  return;
100
119
  }
120
+
121
+ HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
101
122
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
102
- errorCallback(jqxhr, status, exception);
123
+ errorCallback(jqxhr, status, errorThrown);
103
124
  }
104
125
  }
105
126
  });
106
127
  }
107
128
 
108
- static async download(url, data, errorCallback, completeCallback) {
109
- data = this.formatQueryString(data);
129
+ static async download(url, data, errorCallback, completeCallback, method) {
130
+ method = typeof method == 'undefined' || null == method ? 'GET' : method;
131
+
132
+ if ('POST' !== method) {
133
+ url += (!url.includes('?') ? '?' : '') + this.formatQueryString(data);
134
+ data = null;
135
+ }
110
136
 
111
137
  if (window.fetch) {
112
138
  let requestInit = {
@@ -116,26 +142,34 @@ class HTTPRequest {
116
142
  cache: 'no-cache'
117
143
  }
118
144
 
119
- const response = await fetch(url + (!url.includes('?') ? '?' : '') + data, requestInit);
145
+ if ('POST' === method) {
146
+ requestInit['method'] = 'POST';
147
+ requestInit['body'] = this.formatFormData(data);
148
+ }
149
+
150
+ const response = await fetch(url, requestInit);
120
151
  try {
121
152
  const blobData = await response.blob();
122
153
  /*console.log(url);
123
154
  console.log(blobData);*/
124
155
 
125
156
  if (response.status == 401 && response.statusText === "Expired JWT Token") {
126
- HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
157
+ HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
127
158
  return;
128
159
  }
129
160
 
130
161
  if (response.ok) {
131
162
  File.download(blobData, response.headers.get('content-type'), response.headers.get('content-disposition'));
132
163
  }
133
- else if (typeof errorCallback != 'undefined' && errorCallback != null) {
134
- errorCallback(response, response.status, null);
164
+ else {
165
+ HTTPRequest.logRequestFailure(response, null);
166
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
167
+ errorCallback(response, response.status, null);
168
+ }
135
169
  }
136
170
  }
137
171
  catch (e) {
138
- console.log(e);
172
+ console.error(e);
139
173
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
140
174
  errorCallback(response, response.status, e);
141
175
  }
@@ -148,22 +182,35 @@ class HTTPRequest {
148
182
 
149
183
  //l'api fetch n'est pas dispo pour ce navigateur => normalement ce cas ne devrait pas arriver car le polyfill est chargé
150
184
  console.error('fetch\'s polyfill used');
151
- $.ajax({
185
+
186
+
187
+ let ajaxOptions = {
152
188
  type: 'GET',
153
- url: url + (!url.includes('?') ? '?' : '') + data,
189
+ url: url,
154
190
  headers: HTTPRequest.getHeaders(),
155
191
  cache: false,
156
192
  xhrFields: {
157
193
  responseType: 'blob'
158
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}, {
159
204
  success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
160
- error: (jqxhr, status, exception) => {
205
+ error: (jqxhr, status, errorThrown) => {
161
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' ))) {
162
- HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
207
+ HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
163
208
  return;
164
209
  }
210
+
211
+ HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
165
212
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
166
- errorCallback(jqxhr, status, exception);
213
+ errorCallback(jqxhr, status, errorThrown);
167
214
  }
168
215
  },
169
216
  complete: (jqxhr, status) => {
@@ -171,28 +218,22 @@ class HTTPRequest {
171
218
  completeCallback(jqxhr, status);
172
219
  }
173
220
  }
174
- });
221
+ }));
175
222
  }
176
223
 
177
224
  static async post(url, formData, successCallback, errorCallback, formErrorCallback) {
178
- if (!(formData instanceof FormData)) {
179
- let formDataObject = new FormData();
180
- Object.entries(formData).forEach(([key, value]) => formDataObject.append(key, value));
181
- formData = formDataObject;
182
- }
225
+ formData = this.formatFormData(formData);
183
226
 
184
227
  if (window.fetch && false) {
185
- let requestInit = {
228
+ const response = await fetch(url, {
186
229
  method: 'POST',
187
230
  body: formData,
188
231
  headers: HTTPRequest.getHeaders(),
189
232
  mode: 'cors',
190
233
  cache: 'no-cache'
191
- };
234
+ });
192
235
 
193
236
  let jsonData = {};
194
- const response = await fetch(url, requestInit);
195
-
196
237
  try {
197
238
  jsonData = await response.json();
198
239
  //console.log(url, jsonData);
@@ -203,7 +244,9 @@ class HTTPRequest {
203
244
  }
204
245
 
205
246
  if (response.ok) {
206
- successCallback(jsonData);
247
+ if (typeof successCallback != 'undefined' && successCallback != null) {
248
+ successCallback(jsonData);
249
+ }
207
250
  return;
208
251
  }
209
252
 
@@ -213,13 +256,14 @@ class HTTPRequest {
213
256
  }
214
257
  }
215
258
  catch (e) {
216
- console.log(e);
259
+ console.error(e);
217
260
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
218
261
  errorCallback(response, response.status, e);
219
262
  }
220
263
  return;
221
264
  }
222
265
 
266
+ HTTPRequest.logRequestFailure(response, jsonData);
223
267
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
224
268
  errorCallback(response, response.status, null, jsonData);
225
269
  }
@@ -237,18 +281,24 @@ class HTTPRequest {
237
281
  cache: false,
238
282
  contentType: false,
239
283
  processData: false,
240
- success: (data) => successCallback(data),
241
- error: (jqxhr, status, exception) => {
284
+ success: (data) => {
285
+ if (typeof successCallback != 'undefined' && successCallback != null) {
286
+ successCallback(data);
287
+ }
288
+ },
289
+ error: (jqxhr, status, errorThrown) => {
242
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' ))) {
243
291
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
244
292
  return;
245
293
  }
246
294
  if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
247
- formErrorCallback(jqxhr, status, exception);
295
+ formErrorCallback(jqxhr, status, errorThrown);
248
296
  return;
249
297
  }
298
+
299
+ HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
250
300
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
251
- errorCallback(jqxhr, status, exception);
301
+ errorCallback(jqxhr, status, errorThrown);
252
302
  }
253
303
  }
254
304
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.27",
3
+ "version": "1.0.30",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"