@osimatic/helpers-js 1.0.33 → 1.0.34

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
@@ -39,4 +39,7 @@ remplacer l'utilisation des variables httpHeaders / _httpHeaders par HTTPRequest
39
39
  FlashMessage.displayRequestFailure(status, exception, modal) -> FlashMessage.displayError(labelErrorOccured, modal)
40
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)
41
41
 
42
- Location.checkCoordinates(...) -> GeographicCoordinates.check(...) (car Location est déjà défini en javascript natif)
42
+ Location.checkCoordinates(...) -> GeographicCoordinates.check(...) (car Location est déjà défini en javascript natif)
43
+
44
+ FormHelper.getFormErrorTextBis(...) -> FormHelper.getFormErrorText(...)
45
+ FormHelper.displayFormErrorsFromXhr(form, btnSubmit, xhr) -> FormHelper.displayFormErrors(form, btnSubmit, xhr.responseJSON)
package/form_helper.js CHANGED
@@ -183,16 +183,6 @@ class FormHelper {
183
183
  }
184
184
 
185
185
  static getFormErrorText(errors) {
186
- let errorLabels = '';
187
- for (let property in errors) {
188
- if (typeof errors[property] != 'function') {
189
- errorLabels += '<span>' + errors[property] + '</span><br>';
190
- }
191
- }
192
- return errorLabels;
193
- }
194
-
195
- static getFormErrorTextBis(errors) {
196
186
  let errorLabels = '';
197
187
  for (let property in errors) {
198
188
  // console.log(property);
@@ -208,7 +198,7 @@ class FormHelper {
208
198
  }
209
199
 
210
200
  static displayFormErrors(form, btnSubmit, errors, errorWrapperDiv) {
211
- this.displayFormErrorsFromText(form, this.getFormErrorTextBis(errors), errorWrapperDiv);
201
+ this.displayFormErrorsFromText(form, this.getFormErrorText(errors), errorWrapperDiv);
212
202
  if (btnSubmit != null) {
213
203
  if (btnSubmit.buttonLoader != null) {
214
204
  btnSubmit.buttonLoader('reset');
@@ -218,10 +208,6 @@ class FormHelper {
218
208
  }
219
209
  }
220
210
 
221
- static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
222
- this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
223
- }
224
-
225
211
  static displayFormErrorsFromText(form, errorLabels, errorWrapperDiv) {
226
212
  let errorDiv = '<div class="alert alert-danger form_errors">'+errorLabels+'</div>';
227
213
 
@@ -292,6 +278,11 @@ class FormHelper {
292
278
  console.log('request failure. Status: '+status+' ; Exception: '+exception);
293
279
  }
294
280
 
281
+ /** @deprecated **/
282
+ static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
283
+ this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
284
+ }
285
+
295
286
  }
296
287
 
297
288
  module.exports = { FormHelper };
package/network.js CHANGED
@@ -82,27 +82,27 @@ class HTTPRequest {
82
82
  //console.log(url, jsonData);
83
83
  //console.log(response.status, response.statusText, jsonData['error']);
84
84
 
85
- if (response.status == 401 && (response.statusText === "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
85
+ if (response.status == 401 && (response.statusText === 'Expired JWT Token' || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
86
86
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
87
87
  return;
88
88
  }
89
89
 
90
90
  if (response.ok) {
91
- successCallback(jsonData);
91
+ successCallback(jsonData, response);
92
92
  return;
93
93
  }
94
94
  }
95
95
  catch (e) {
96
96
  console.error(e);
97
97
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
98
- errorCallback(response, response.status, e);
98
+ errorCallback(response);
99
99
  }
100
100
  return;
101
101
  }
102
102
 
103
103
  HTTPRequest.logRequestFailure(response, jsonData);
104
104
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
105
- errorCallback(response, response.status, null, jsonData);
105
+ errorCallback(response, jsonData);
106
106
  }
107
107
  return;
108
108
  }
@@ -115,16 +115,20 @@ class HTTPRequest {
115
115
  headers: HTTPRequest.getHeaders(true),
116
116
  dataType: 'json',
117
117
  cache: false,
118
- success: (data) => successCallback(data),
118
+ success: (data, status, jqxhr) => {
119
+ if (typeof successCallback != 'undefined' && successCallback != null) {
120
+ successCallback(data, jqxhr);
121
+ }
122
+ },
119
123
  error: (jqxhr, status, errorThrown) => {
120
- 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' ))) {
124
+ if (jqxhr.status == 401 && (jqxhr.statusText === 'Expired JWT Token' || (typeof jqxhr.responseJSON['message'] != 'undefined' && jqxhr.responseJSON['message'] === 'Expired JWT Token') || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
121
125
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
122
126
  return;
123
127
  }
124
128
 
125
129
  HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
126
130
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
127
- errorCallback(jqxhr, status, errorThrown);
131
+ errorCallback(jqxhr, jqxhr.responseJSON);
128
132
  }
129
133
  }
130
134
  });
@@ -157,7 +161,7 @@ class HTTPRequest {
157
161
  /*console.log(url);
158
162
  console.log(blobData);*/
159
163
 
160
- if (response.status == 401 && response.statusText === "Expired JWT Token") {
164
+ if (response.status == 401 && response.statusText === 'Expired JWT Token') {
161
165
  HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
162
166
  return;
163
167
  }
@@ -168,18 +172,18 @@ class HTTPRequest {
168
172
  else {
169
173
  HTTPRequest.logRequestFailure(response, null);
170
174
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
171
- errorCallback(response, response.status, null);
175
+ errorCallback(response);
172
176
  }
173
177
  }
174
178
  }
175
179
  catch (e) {
176
180
  console.error(e);
177
181
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
178
- errorCallback(response, response.status, e);
182
+ errorCallback(response);
179
183
  }
180
184
  }
181
185
  if (typeof completeCallback != 'undefined' && completeCallback != null) {
182
- completeCallback(response, response.status);
186
+ completeCallback(response);
183
187
  }
184
188
  return;
185
189
  }
@@ -187,7 +191,6 @@ class HTTPRequest {
187
191
  //l'api fetch n'est pas dispo pour ce navigateur => normalement ce cas ne devrait pas arriver car le polyfill est chargé
188
192
  console.error('fetch\'s polyfill used');
189
193
 
190
-
191
194
  let ajaxOptions = {
192
195
  type: 'GET',
193
196
  url: url,
@@ -207,19 +210,19 @@ class HTTPRequest {
207
210
  $.ajax(Object.assign({...ajaxOptions}, {
208
211
  success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
209
212
  error: (jqxhr, status, errorThrown) => {
210
- 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' ))) {
213
+ if (jqxhr.status == 401 && (jqxhr.statusText === 'Expired JWT Token' || (typeof jqxhr.responseJSON['message'] != 'undefined' && jqxhr.responseJSON['message'] === 'Expired JWT Token') || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
211
214
  HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
212
215
  return;
213
216
  }
214
217
 
215
218
  HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
216
219
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
217
- errorCallback(jqxhr, status, errorThrown);
220
+ errorCallback(jqxhr);
218
221
  }
219
222
  },
220
- complete: (jqxhr, status) => {
223
+ complete: (jqxhr) => {
221
224
  if (typeof completeCallback != 'undefined' && completeCallback != null) {
222
- completeCallback(jqxhr, status);
225
+ completeCallback(jqxhr);
223
226
  }
224
227
  }
225
228
  }));
@@ -228,7 +231,7 @@ class HTTPRequest {
228
231
  static async post(url, formData, successCallback, errorCallback, formErrorCallback) {
229
232
  formData = this.formatFormData(formData);
230
233
 
231
- if (window.fetch && false) {
234
+ if (window.fetch) {
232
235
  const response = await fetch(url, {
233
236
  method: 'POST',
234
237
  body: formData,
@@ -239,37 +242,39 @@ class HTTPRequest {
239
242
 
240
243
  let jsonData = {};
241
244
  try {
242
- jsonData = await response.json();
245
+ if (response.statusText !== 'No Content') {
246
+ jsonData = await response.json();
247
+ }
243
248
  //console.log(url, jsonData);
244
249
 
245
- if (response.status == 401 && url !== HTTPRequest.refreshTokenUrl && (response.statusText === "Expired JWT Token" || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
250
+ if (response.status == 401 && url !== HTTPRequest.refreshTokenUrl && (response.statusText === 'Expired JWT Token' || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
246
251
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
247
252
  return;
248
253
  }
249
254
 
250
255
  if (response.ok) {
251
256
  if (typeof successCallback != 'undefined' && successCallback != null) {
252
- successCallback(jsonData);
257
+ successCallback(jsonData, response);
253
258
  }
254
259
  return;
255
260
  }
256
261
 
257
262
  if (response.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
258
- formErrorCallback(response, response.status, jsonData);
263
+ formErrorCallback(jsonData, response);
259
264
  return;
260
265
  }
261
266
  }
262
267
  catch (e) {
263
268
  console.error(e);
264
269
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
265
- errorCallback(response, response.status, e);
270
+ errorCallback(response);
266
271
  }
267
272
  return;
268
273
  }
269
274
 
270
275
  HTTPRequest.logRequestFailure(response, jsonData);
271
276
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
272
- errorCallback(response, response.status, null, jsonData);
277
+ errorCallback(response, jsonData);
273
278
  }
274
279
  return;
275
280
  }
@@ -285,24 +290,24 @@ class HTTPRequest {
285
290
  cache: false,
286
291
  contentType: false,
287
292
  processData: false,
288
- success: (data) => {
293
+ success: (data, status, jqxhr) => {
289
294
  if (typeof successCallback != 'undefined' && successCallback != null) {
290
- successCallback(data);
295
+ successCallback(data, jqxhr);
291
296
  }
292
297
  },
293
298
  error: (jqxhr, status, errorThrown) => {
294
- if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && url !== HTTPRequest.refreshTokenUrl && (jqxhr.responseJSON.message === "Expired JWT Token" || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
299
+ if (url !== HTTPRequest.refreshTokenUrl && jqxhr.status == 401 && (jqxhr.statusText === 'Expired JWT Token' || (typeof jqxhr.responseJSON['message'] != 'undefined' && jqxhr.responseJSON['message'] === 'Expired JWT Token') || (typeof jqxhr.responseJSON['error'] != 'undefined' && jqxhr.responseJSON['error'] === 'expired_token' ))) {
295
300
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
296
301
  return;
297
302
  }
298
303
  if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
299
- formErrorCallback(jqxhr, status, errorThrown);
304
+ formErrorCallback(jqxhr.responseJSON, jqxhr);
300
305
  return;
301
306
  }
302
307
 
303
308
  HTTPRequest.logJqueryRequestFailure(jqxhr, status, errorThrown);
304
309
  if (typeof errorCallback != 'undefined' && errorCallback != null) {
305
- errorCallback(jqxhr, status, errorThrown);
310
+ errorCallback(jqxhr, jqxhr.responseJSON);
306
311
  }
307
312
  }
308
313
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"