@osimatic/helpers-js 1.0.26 → 1.0.27

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
@@ -33,4 +33,5 @@ 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()
package/network.js CHANGED
@@ -30,16 +30,21 @@ class HTTPRequest {
30
30
  return httpHeaders;
31
31
  }
32
32
 
33
- static async get(url, data, successCallback, errorCallback) {
33
+ static formatQueryString(data) {
34
34
  if (data == null) {
35
- data = '';
35
+ return '';
36
36
  }
37
- else if (typeof data == 'object') {
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 async get(url, data, successCallback, errorCallback) {
47
+ data = this.formatQueryString(data);
43
48
 
44
49
  if (window.fetch) {
45
50
  let requestInit = {
@@ -56,7 +61,7 @@ class HTTPRequest {
56
61
  //console.log(url, jsonData);
57
62
  //console.log(response.status, response.statusText, jsonData['error']);
58
63
 
59
- if (response.status == 401 && (response.statusText == "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
64
+ if (response.status == 401 && (response.statusText === "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
60
65
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
61
66
  return;
62
67
  }
@@ -68,10 +73,14 @@ class HTTPRequest {
68
73
  }
69
74
  catch (e) {
70
75
  console.log(e);
71
- errorCallback(response, response.status, e);
76
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
77
+ errorCallback(response, response.status, e);
78
+ }
72
79
  return;
73
80
  }
74
- errorCallback(response, response.status, null, jsonData);
81
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
82
+ errorCallback(response, response.status, null, jsonData);
83
+ }
75
84
  return;
76
85
  }
77
86
 
@@ -85,9 +94,11 @@ class HTTPRequest {
85
94
  cache: false,
86
95
  success: (data) => successCallback(data),
87
96
  error: (jqxhr, status, exception) => {
88
- if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message == "Expired JWT Token") {
97
+ 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
98
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
90
- } else {
99
+ return;
100
+ }
101
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
91
102
  errorCallback(jqxhr, status, exception);
92
103
  }
93
104
  }
@@ -95,16 +106,7 @@ class HTTPRequest {
95
106
  }
96
107
 
97
108
  static async download(url, data, errorCallback, completeCallback) {
98
- if (data == null) {
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
- }
107
-
109
+ data = this.formatQueryString(data);
108
110
 
109
111
  if (window.fetch) {
110
112
  let requestInit = {
@@ -120,7 +122,7 @@ class HTTPRequest {
120
122
  /*console.log(url);
121
123
  console.log(blobData);*/
122
124
 
123
- if (response.status == 401 && response.statusText == "Expired JWT Token") {
125
+ if (response.status == 401 && response.statusText === "Expired JWT Token") {
124
126
  HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
125
127
  return;
126
128
  }
@@ -156,9 +158,11 @@ class HTTPRequest {
156
158
  },
157
159
  success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
158
160
  error: (jqxhr, status, exception) => {
159
- if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message == "Expired JWT Token") {
161
+ 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' ))) {
160
162
  HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
161
- } else if (typeof errorCallback != 'undefined' && errorCallback != null) {
163
+ return;
164
+ }
165
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
162
166
  errorCallback(jqxhr, status, exception);
163
167
  }
164
168
  },
@@ -171,6 +175,12 @@ class HTTPRequest {
171
175
  }
172
176
 
173
177
  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
+ }
183
+
174
184
  if (window.fetch && false) {
175
185
  let requestInit = {
176
186
  method: 'POST',
@@ -187,7 +197,7 @@ class HTTPRequest {
187
197
  jsonData = await response.json();
188
198
  //console.log(url, jsonData);
189
199
 
190
- if (response.status == 401 && (response.statusText == "Expired JWT Token" || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
200
+ if (response.status == 401 && (response.statusText === "Expired JWT Token" || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
191
201
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
192
202
  return;
193
203
  }
@@ -204,11 +214,15 @@ class HTTPRequest {
204
214
  }
205
215
  catch (e) {
206
216
  console.log(e);
207
- errorCallback(response, response.status, e);
217
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
218
+ errorCallback(response, response.status, e);
219
+ }
208
220
  return;
209
221
  }
210
222
 
211
- errorCallback(response, response.status, null, jsonData);
223
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
224
+ errorCallback(response, response.status, null, jsonData);
225
+ }
212
226
  return;
213
227
  }
214
228
 
@@ -225,11 +239,15 @@ class HTTPRequest {
225
239
  processData: false,
226
240
  success: (data) => successCallback(data),
227
241
  error: (jqxhr, status, exception) => {
228
- if (typeof jqxhr.responseJSON != 'undefined' && jqxhr.responseJSON.code == 401 && jqxhr.responseJSON.message == "Expired JWT Token") {
242
+ 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
243
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
230
- } else if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
244
+ return;
245
+ }
246
+ if (jqxhr.status == 400 && typeof formErrorCallback != 'undefined' && formErrorCallback != null) {
231
247
  formErrorCallback(jqxhr, status, exception);
232
- } else {
248
+ return;
249
+ }
250
+ if (typeof errorCallback != 'undefined' && errorCallback != null) {
233
251
  errorCallback(jqxhr, status, exception);
234
252
  }
235
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.26",
3
+ "version": "1.0.27",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"