hamzus-ui 0.0.226 → 0.0.227

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hamzus-ui",
3
- "version": "0.0.226",
3
+ "version": "0.0.227",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "svelte": "index.js",
@@ -12,55 +12,61 @@ export function sendRequest(request) {
12
12
  ...request
13
13
  }
14
14
 
15
-
16
- const formData = new FormData();
17
-
18
- if (request.data) {
19
- for (const [key, value] of Object.entries(request.data)) {
20
- formData.append(key, value);
15
+ function buildFormData() {
16
+ const formData = new FormData();
17
+ if (request.data) {
18
+ for (const [key, value] of Object.entries(request.data)) {
19
+ formData.append(key, value);
20
+ }
21
21
  }
22
- }
23
- if (request.includeToken) {
24
- const myToken = getCookie(config['sessionTokenName']);
25
-
26
- formData.append(config['sessionTokenName'], myToken)
27
- }
28
-
29
- if (request.formData) {
30
- for (const [key, value] of request.formData.entries()) {
31
- formData.append(key, value);
22
+ if (request.includeToken) {
23
+ const myToken = getCookie(config['sessionTokenName']);
24
+ formData.append(config['sessionTokenName'], myToken)
32
25
  }
26
+ if (request.formData) {
27
+ for (const [key, value] of request.formData.entries()) {
28
+ formData.append(key, value);
29
+ }
30
+ }
31
+ return formData;
33
32
  }
34
33
 
35
- // Création de la requête XHR
36
- var scriptPath = request.url + request.path;
37
- const xhr = new XMLHttpRequest();
38
- xhr.open('POST', scriptPath, true);
39
- xhr.withCredentials = config.requestWithCredentials;
40
-
41
- xhr.onload = function () {
42
-
43
- if (xhr.status === 200) {
44
-
34
+ function executeRequest(isRetry = false) {
35
+ var scriptPath = request.url + request.path;
36
+ const xhr = new XMLHttpRequest();
37
+ xhr.open('POST', scriptPath, true);
38
+ xhr.withCredentials = config.requestWithCredentials;
45
39
 
46
- const response = JSON.parse(xhr.responseText)
47
- resolve(response)
48
- } else if (xhr.status === 400 || xhr.status === 409) {
49
- const response = JSON.parse(xhr.responseText)
50
- resolve(response)
51
- } else if (xhr.status === 401) {
52
- if (config.on401 !== null) {
53
- config.on401()
40
+ xhr.onload = function () {
41
+ if (xhr.status === 200) {
42
+ const response = JSON.parse(xhr.responseText)
43
+ resolve(response)
44
+ } else if (xhr.status === 400 || xhr.status === 409) {
45
+ const response = JSON.parse(xhr.responseText)
46
+ resolve(response)
47
+ } else if (xhr.status === 401) {
48
+ if (config.on401 !== null) {
49
+ const result = config.on401()
50
+ if (config.replayAfter401 === true && !isRetry) {
51
+ const replay = () => executeRequest(true);
52
+ result instanceof Promise
53
+ ? result.then(replay)
54
+ : replay();
55
+ return;
56
+ }
57
+ }
58
+ resolve(false)
54
59
  }
55
- }
60
+ };
61
+
62
+ xhr.onerror = function () {
63
+ console.error('Erreur réseau.');
64
+ reject(new Error('Erreur réseau.'));
65
+ };
56
66
 
57
- resolve(false)
58
- };
67
+ xhr.send(buildFormData());
68
+ }
59
69
 
60
- xhr.onerror = function () {
61
- console.error('Erreur réseau.');
62
- };
63
- // Envoi des données sous forme JSON
64
- xhr.send(formData);
70
+ executeRequest();
65
71
  })
66
72
  }