@osimatic/helpers-js 1.0.28 → 1.0.29

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.
Files changed (2) hide show
  1. package/network.js +46 -22
  2. package/package.json +1 -1
package/network.js CHANGED
@@ -43,6 +43,15 @@ 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
+
46
55
  static logRequestFailure(response, json) {
47
56
  console.error('Request failure. Status: '+response.statusText+' ; HTTP Code : '+response.status, json);
48
57
  }
@@ -52,18 +61,18 @@ class HTTPRequest {
52
61
  }
53
62
 
54
63
  static async get(url, data, successCallback, errorCallback) {
55
- data = this.formatQueryString(data);
64
+ url += (!url.includes('?') ? '?' : '') + this.formatQueryString(data);
65
+ data = null;
56
66
 
57
67
  if (window.fetch) {
58
- let requestInit = {
68
+ const response = await fetch(url, {
59
69
  method: 'GET',
60
70
  headers: HTTPRequest.getHeaders(),
61
71
  mode: 'cors',
62
72
  cache: 'no-cache'
63
- }
73
+ });
64
74
 
65
75
  let jsonData = {};
66
- const response = await fetch(url + (!url.includes('?') ? '?' : '') + data, requestInit);
67
76
  try {
68
77
  jsonData = await response.json();
69
78
  //console.log(url, jsonData);
@@ -98,7 +107,7 @@ class HTTPRequest {
98
107
  console.error('fetch\'s polyfill used');
99
108
  $.ajax({
100
109
  type: 'GET',
101
- url: url + (!url.includes('?') ? '?' : '') + data,
110
+ url: url,
102
111
  headers: HTTPRequest.getHeaders(),
103
112
  dataType: 'json',
104
113
  cache: false,
@@ -117,8 +126,13 @@ class HTTPRequest {
117
126
  });
118
127
  }
119
128
 
120
- static async download(url, data, errorCallback, completeCallback) {
121
- 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
+ }
122
136
 
123
137
  if (window.fetch) {
124
138
  let requestInit = {
@@ -128,14 +142,19 @@ class HTTPRequest {
128
142
  cache: 'no-cache'
129
143
  }
130
144
 
131
- 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);
132
151
  try {
133
152
  const blobData = await response.blob();
134
153
  /*console.log(url);
135
154
  console.log(blobData);*/
136
155
 
137
156
  if (response.status == 401 && response.statusText === "Expired JWT Token") {
138
- HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
157
+ HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
139
158
  return;
140
159
  }
141
160
 
@@ -163,18 +182,29 @@ class HTTPRequest {
163
182
 
164
183
  //l'api fetch n'est pas dispo pour ce navigateur => normalement ce cas ne devrait pas arriver car le polyfill est chargé
165
184
  console.error('fetch\'s polyfill used');
166
- $.ajax({
185
+
186
+
187
+ let ajaxOptions = {
167
188
  type: 'GET',
168
- url: url + (!url.includes('?') ? '?' : '') + data,
189
+ url: url,
169
190
  headers: HTTPRequest.getHeaders(),
170
191
  cache: false,
171
192
  xhrFields: {
172
193
  responseType: 'blob'
173
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}, {
174
204
  success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
175
205
  error: (jqxhr, status, errorThrown) => {
176
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' ))) {
177
- HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback));
207
+ HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
178
208
  return;
179
209
  }
180
210
 
@@ -188,28 +218,22 @@ class HTTPRequest {
188
218
  completeCallback(jqxhr, status);
189
219
  }
190
220
  }
191
- });
221
+ }));
192
222
  }
193
223
 
194
224
  static async post(url, formData, successCallback, errorCallback, formErrorCallback) {
195
- if (!(formData instanceof FormData)) {
196
- let formDataObject = new FormData();
197
- Object.entries(formData).forEach(([key, value]) => formDataObject.append(key, value));
198
- formData = formDataObject;
199
- }
225
+ formData = this.formatFormData(formData);
200
226
 
201
227
  if (window.fetch && false) {
202
- let requestInit = {
228
+ const response = await fetch(url, {
203
229
  method: 'POST',
204
230
  body: formData,
205
231
  headers: HTTPRequest.getHeaders(),
206
232
  mode: 'cors',
207
233
  cache: 'no-cache'
208
- };
234
+ });
209
235
 
210
236
  let jsonData = {};
211
- const response = await fetch(url, requestInit);
212
-
213
237
  try {
214
238
  jsonData = await response.json();
215
239
  //console.log(url, jsonData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"