@osimatic/helpers-js 1.1.39 → 1.1.41

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/form_date.js CHANGED
@@ -125,6 +125,26 @@ class InputPeriod {
125
125
 
126
126
  // input period de type : <select class="period">Aujourd'hui / Ce mois-ci / etc. / Personnalisé</select>
127
127
  class FormDate {
128
+
129
+ static fillYearSelect(select, nbYearsBefore=5, nbYearsAfter=0) {
130
+ const currentDate = new Date();
131
+ for (let year=currentDate.getUTCFullYear()-nbYearsBefore; year<=(currentDate.getUTCFullYear()+nbYearsAfter); year++) {
132
+ select.append('<option value="'+year+'">'+year+'</option>');
133
+ }
134
+ }
135
+
136
+ static fillMonthSelect(select, locale) {
137
+ for (let month=1; month<=12; month++) {
138
+ select.append('<option value="'+month+'">'+DateTime.getMonthNameByMonth(month, locale).capitalize()+'</option>');
139
+ }
140
+ }
141
+
142
+ static fillDayOfWeekSelect(select, locale) {
143
+ for (let dayOfWeek=1; dayOfWeek<=7; dayOfWeek++) {
144
+ select.append('<option value="'+dayOfWeek+'">'+DateTime.getDayNameByDayOfWeek(dayOfWeek, locale).capitalize()+'</option>');
145
+ }
146
+ }
147
+
128
148
  static initForm(form) {
129
149
 
130
150
  function fillPeriodSelect(select) {
package/http_client.js CHANGED
@@ -28,14 +28,14 @@ class HTTPClient {
28
28
  HTTPClient.onInvalidTokenCallback = callback;
29
29
  }
30
30
 
31
- static getHeaders(asObject) {
31
+ static getHeaders(asObject=false, additionalHeaders={}) {
32
32
  HTTPClient.setAuthorizationToken(JwtSession.getToken());
33
33
 
34
34
  if (typeof HTTPClient.headers == 'undefined') {
35
35
  HTTPClient.headers = {};
36
36
  }
37
37
 
38
- if (typeof asObject != 'undefined' && asObject) {
38
+ if (asObject) {
39
39
  return HTTPClient.headers;
40
40
  }
41
41
 
@@ -44,6 +44,12 @@ class HTTPClient {
44
44
  httpHeaders.append(key, value);
45
45
  });
46
46
 
47
+ if (typeof additionalHeaders == 'object') {
48
+ for (const [key, value] of Object.entries(additionalHeaders)) {
49
+ httpHeaders.append(key, value);
50
+ }
51
+ }
52
+
47
53
  return httpHeaders;
48
54
  }
49
55
 
@@ -137,6 +143,18 @@ class HTTPClient {
137
143
  return data;
138
144
  }
139
145
 
146
+ static formatJsonData(data) {
147
+ if (typeof data == 'string') {
148
+ return data;
149
+ }
150
+
151
+ let formData = HTTPClient.formatFormData(data);
152
+
153
+ let object = {};
154
+ formData.forEach((value, key) => object[key] = value);
155
+ return JSON.stringify(object);
156
+ }
157
+
140
158
  static logRequestFailure(response, json) {
141
159
  console.error('Request failure. Status: '+response.statusText+' ; HTTP Code : '+response.status, json);
142
160
  }
@@ -176,7 +194,7 @@ class HTTPClient {
176
194
  JwtSession.logout(HTTPClient.onInvalidTokenRedirectUrl, HTTPClient.onInvalidTokenCallback);
177
195
  }
178
196
 
179
- static async request(method, url, data, successCallback, errorCallback, formErrorCallback) {
197
+ static async request(method, url, data, successCallback=null, errorCallback=null, formErrorCallback=null, additionalHeaders={}) {
180
198
  if (!window.fetch) {
181
199
  return;
182
200
  }
@@ -184,14 +202,20 @@ class HTTPClient {
184
202
  let body = null;
185
203
  method = method.toUpperCase();
186
204
 
187
- let headers = HTTPClient.getHeaders();
205
+ let headers = HTTPClient.getHeaders(false, additionalHeaders);
206
+
188
207
  if ('PATCH' === method) {
189
208
  headers.append('Content-Type', 'application/x-www-form-urlencoded');
190
209
  // 30/01/2023 : ajout encodeURIComponent() sinon les valeurs contenant des "+" pose pb (signe "+" retiré)
191
210
  body = encodeURIComponent(new URLSearchParams(HTTPClient.formatFormData(data)).toString());
192
211
  }
193
212
  else if ('POST' === method) {
194
- body = HTTPClient.formatFormData(data);
213
+ if (headers.get('Content-Type') === 'application/json') {
214
+ body = HTTPClient.formatJsonData(data);
215
+ }
216
+ else {
217
+ body = HTTPClient.formatFormData(data);
218
+ }
195
219
  }
196
220
  else {
197
221
  url += (!url.includes('?') ? '?' : '') + HTTPClient.formatQueryString(data);
@@ -199,11 +223,11 @@ class HTTPClient {
199
223
  }
200
224
 
201
225
  const requestOptions = {
226
+ method: method,
202
227
  headers: headers,
228
+ body: body,
203
229
  mode: 'cors',
204
- cache: 'no-cache',
205
- method,
206
- body
230
+ cache: 'no-cache'
207
231
  }
208
232
 
209
233
  const response = await fetch(url, requestOptions);
@@ -215,7 +239,7 @@ class HTTPClient {
215
239
  }
216
240
 
217
241
  if (HTTPClient.isExpiredToken(response, jsonData)) {
218
- HTTPClient.refreshToken(() => HTTPClient.request(method, url, data, successCallback, errorCallback, formErrorCallback), errorCallback);
242
+ HTTPClient.refreshToken(() => HTTPClient.request(method, url, data, successCallback, errorCallback, formErrorCallback, additionalHeaders), errorCallback);
219
243
  return;
220
244
  }
221
245
 
@@ -250,15 +274,16 @@ class HTTPClient {
250
274
  }
251
275
  }
252
276
 
253
- static download(method, url, data, errorCallback, completeCallback) {
277
+ static download(method, url, data, errorCallback=null, completeCallback=null, additionalHeaders={}) {
254
278
  HTTPClient.requestBlob(method, url, data,
255
279
  (blobData, response) => File.download(blobData, response.headers.get('content-type'), response.headers.get('content-disposition')),
256
280
  errorCallback,
257
- completeCallback
281
+ completeCallback,
282
+ additionalHeaders
258
283
  );
259
284
  }
260
285
 
261
- static async requestBlob(method, url, data, successCallback, errorCallback, completeCallback) {
286
+ static async requestBlob(method, url, data, successCallback=null, errorCallback=null, completeCallback=null, additionalHeaders={}) {
262
287
  if (!window.fetch) {
263
288
  return;
264
289
  }
@@ -266,13 +291,18 @@ class HTTPClient {
266
291
  let body = null;
267
292
  method = method.toUpperCase();
268
293
 
269
- let headers = HTTPClient.getHeaders();
294
+ let headers = HTTPClient.getHeaders(false, additionalHeaders);
270
295
  if ('PATCH' === method) {
271
296
  headers.append('Content-Type', 'application/x-www-form-urlencoded');
272
297
  body = encodeURIComponent(new URLSearchParams(HTTPClient.formatFormData(data)).toString());
273
298
  }
274
299
  else if ('POST' === method) {
275
- body = HTTPClient.formatFormData(data);
300
+ if (headers.get('Content-Type') === 'application/json') {
301
+ body = HTTPClient.formatJsonData(data);
302
+ }
303
+ else {
304
+ body = HTTPClient.formatFormData(data);
305
+ }
276
306
  }
277
307
  else {
278
308
  url += (!url.includes('?') ? '?' : '') + HTTPClient.formatQueryString(data);
@@ -290,7 +320,7 @@ class HTTPClient {
290
320
  const response = await fetch(url, requestOptions);
291
321
  try {
292
322
  if (response.status === 401 && response.statusText === 'Expired JWT Token') {
293
- HTTPClient.refreshToken(() => HTTPClient.requestBlob(method, url, data, successCallback, errorCallback, completeCallback), errorCallback);
323
+ HTTPClient.refreshToken(() => HTTPClient.requestBlob(method, url, data, successCallback, errorCallback, completeCallback, additionalHeaders), errorCallback);
294
324
  return;
295
325
  }
296
326
 
@@ -323,7 +353,7 @@ class HTTPClient {
323
353
  }
324
354
  }
325
355
 
326
- static refreshToken(completeCallback, errorCallback) {
356
+ static refreshToken(completeCallback, errorCallback=null) {
327
357
  if (typeof HTTPClient.listCompleteCallbackAfterRefreshTokenFinished == 'undefined') {
328
358
  HTTPClient.listCompleteCallbackAfterRefreshTokenFinished = [];
329
359
  }
package/location.js CHANGED
@@ -8,11 +8,15 @@ class Country {
8
8
  }
9
9
 
10
10
  static fillCountrySelect(select, defaultValue) {
11
- Object.entries(Country.getCountries()).forEach(([countryCode, countryName]) => select.append('<option value="'+countryCode+'">'+countryName+'</option>'));
11
+ if (select.children().length === 0) {
12
+ Object.entries(Country.getCountries()).forEach(([countryCode, countryName]) => select.append('<option value="' + countryCode + '">' + countryName + '</option>'));
13
+ }
12
14
  if (typeof defaultValue != 'undefined') {
13
15
  select.val(defaultValue);
14
16
  }
15
- select.selectpicker('refresh');
17
+ if (typeof select.selectpicker != 'undefined') {
18
+ select.selectpicker('refresh');
19
+ }
16
20
  }
17
21
  static getCountryName(countryCode) {
18
22
  if (Country.getCountries().hasOwnProperty(countryCode)) {
package/network.js CHANGED
@@ -1,22 +1,22 @@
1
1
  class Cookie {
2
- static set(cname, cvalue, exdays) {
3
- var d = new Date();
4
- d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
5
- var expires = "expires=" + d.toUTCString();
6
- document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
7
- }
8
-
9
- static get(cname) {
10
- var name = cname + "=";
11
- var decodedCookie = decodeURIComponent(document.cookie);
12
- var ca = decodedCookie.split(';');
13
- for (var i = 0; i < ca.length; i++) {
14
- var c = ca[i];
2
+ static set(name, value, nbValidityDays=30) {
3
+ let d = new Date();
4
+ d.setTime(d.getTime() + (nbValidityDays * 24 * 60 * 60 * 1000));
5
+ let expires = "expires=" + d.toUTCString();
6
+ document.cookie = name + "=" + value + ";" + expires + ";path=/";
7
+ }
8
+
9
+ static get(name) {
10
+ let nameWithSeparator = name + "=";
11
+ let decodedCookie = decodeURIComponent(document.cookie);
12
+ let ca = decodedCookie.split(';');
13
+ for (let i = 0; i < ca.length; i++) {
14
+ let c = ca[i];
15
15
  while (c.charAt(0) == ' ') {
16
16
  c = c.substring(1);
17
17
  }
18
- if (c.indexOf(name) === 0) {
19
- return c.substring(name.length, c.length);
18
+ if (c.indexOf(nameWithSeparator) === 0) {
19
+ return c.substring(nameWithSeparator.length, c.length);
20
20
  }
21
21
  }
22
22
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.1.39",
3
+ "version": "1.1.41",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"