@codebit-programando-solucoes/codebit-web 1.1.19 → 1.1.20
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/dist/api/api.d.ts +2 -0
- package/dist/index.cjs +103 -1
- package/package.json +1 -1
package/dist/api/api.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface RequestConfig {
|
|
|
12
12
|
signal?: AbortSignal;
|
|
13
13
|
/** Desired response format */
|
|
14
14
|
responseType?: 'json' | 'text' | 'blob' | 'arraybuffer' | 'stream';
|
|
15
|
+
/** Callback for upload progress events */
|
|
16
|
+
onUploadProgress?: (progressEvent: ProgressEvent) => void;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
/** API response object. */
|
package/dist/index.cjs
CHANGED
|
@@ -174,8 +174,39 @@ function isBinaryContent(contentType) {
|
|
|
174
174
|
];
|
|
175
175
|
return binaryTypes.some((type)=>contentType.startsWith(type));
|
|
176
176
|
}
|
|
177
|
+
function parseResponseHeaders(headerStr) {
|
|
178
|
+
const headers = new Headers();
|
|
179
|
+
if (!headerStr) return headers;
|
|
180
|
+
const lines = headerStr.split('\r\n').filter((line)=>!!line);
|
|
181
|
+
lines.forEach((line)=>{
|
|
182
|
+
const [key, value] = line.split(': ');
|
|
183
|
+
if (key && value) headers.append(key, value);
|
|
184
|
+
});
|
|
185
|
+
return headers;
|
|
186
|
+
}
|
|
187
|
+
async function createApiErrorFromXhr(xhr, request) {
|
|
188
|
+
let responseData;
|
|
189
|
+
try {
|
|
190
|
+
const contentType = xhr.getResponseHeader('content-type');
|
|
191
|
+
responseData = contentType?.includes('application/json') ? JSON.parse(xhr.responseText) : xhr.responseText;
|
|
192
|
+
} catch {
|
|
193
|
+
responseData = xhr.responseText;
|
|
194
|
+
}
|
|
195
|
+
const responseObject = {
|
|
196
|
+
status: xhr.status,
|
|
197
|
+
statusText: xhr.statusText || '',
|
|
198
|
+
headers: parseResponseHeaders(xhr.getAllResponseHeaders()),
|
|
199
|
+
data: responseData
|
|
200
|
+
};
|
|
201
|
+
return new ApiError(responseObject, request);
|
|
202
|
+
}
|
|
177
203
|
async function makeRequest(url, options = {}) {
|
|
178
|
-
const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, ...restOptions } = options;
|
|
204
|
+
const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, onUploadProgress, ...restOptions } = options;
|
|
205
|
+
if (onUploadProgress && restOptions.method && [
|
|
206
|
+
'POST',
|
|
207
|
+
'PUT',
|
|
208
|
+
'PATCH'
|
|
209
|
+
].includes(restOptions.method.toUpperCase()) && options.body) return makeRequestWithProgress(url, options);
|
|
179
210
|
const { timeoutController, timer, fullUrl } = setupRequestControl(url, externalSignal, timeout);
|
|
180
211
|
const config = {
|
|
181
212
|
credentials: 'include',
|
|
@@ -205,6 +236,77 @@ async function makeRequest(url, options = {}) {
|
|
|
205
236
|
headers: response.headers
|
|
206
237
|
};
|
|
207
238
|
}
|
|
239
|
+
function makeRequestWithProgress(url, options) {
|
|
240
|
+
return new Promise((resolve, reject)=>{
|
|
241
|
+
const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, onUploadProgress, method = 'POST', body } = options;
|
|
242
|
+
const fullUrl = url.startsWith('http') ? url : `${API_CONFIG.baseURL}${url}`;
|
|
243
|
+
const xhr = new XMLHttpRequest();
|
|
244
|
+
if ('blob' === responseType || 'arraybuffer' === responseType) xhr.responseType = responseType;
|
|
245
|
+
else if ('stream' === responseType) console.warn('Stream responseType is not supported with XMLHttpRequest');
|
|
246
|
+
const { timeoutController, timer } = setupRequestControl(fullUrl, externalSignal, timeout);
|
|
247
|
+
const originalAbort = timeoutController.abort;
|
|
248
|
+
timeoutController.abort = function(reason) {
|
|
249
|
+
originalAbort.call(this, reason);
|
|
250
|
+
xhr.abort();
|
|
251
|
+
};
|
|
252
|
+
xhr.open(method, fullUrl, true);
|
|
253
|
+
xhr.withCredentials = true;
|
|
254
|
+
const finalHeaders = buildHeaders(headers);
|
|
255
|
+
Object.keys(finalHeaders).forEach((key)=>{
|
|
256
|
+
if ('content-type' === key.toLowerCase() && body instanceof FormData) return;
|
|
257
|
+
xhr.setRequestHeader(key, finalHeaders[key]);
|
|
258
|
+
});
|
|
259
|
+
const config = {
|
|
260
|
+
headers: finalHeaders,
|
|
261
|
+
body
|
|
262
|
+
};
|
|
263
|
+
prepareBodyAndContentType(config);
|
|
264
|
+
const requestBody = config.body;
|
|
265
|
+
if (onUploadProgress) xhr.upload.addEventListener('progress', onUploadProgress);
|
|
266
|
+
xhr.onload = function() {
|
|
267
|
+
if (timer) clearTimeout(timer);
|
|
268
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
269
|
+
const contentType = xhr.getResponseHeader('content-type');
|
|
270
|
+
let data;
|
|
271
|
+
let inferredResponseType;
|
|
272
|
+
inferredResponseType = contentType?.includes('application/json') ? 'json' : isBinaryContent(contentType) ? 'blob' : 'text';
|
|
273
|
+
const finalResponseType = responseType || inferredResponseType;
|
|
274
|
+
if ('json' === finalResponseType) try {
|
|
275
|
+
data = JSON.parse(xhr.responseText);
|
|
276
|
+
} catch {
|
|
277
|
+
data = xhr.responseText;
|
|
278
|
+
}
|
|
279
|
+
else data = 'blob' === finalResponseType ? xhr.response : 'arraybuffer' === finalResponseType ? xhr.response : xhr.responseText;
|
|
280
|
+
resolve({
|
|
281
|
+
data,
|
|
282
|
+
status: xhr.status,
|
|
283
|
+
headers: parseResponseHeaders(xhr.getAllResponseHeaders())
|
|
284
|
+
});
|
|
285
|
+
} else createApiErrorFromXhr(xhr, options).then(reject);
|
|
286
|
+
};
|
|
287
|
+
xhr.onerror = function() {
|
|
288
|
+
if (timer) clearTimeout(timer);
|
|
289
|
+
const networkError = new Error('Network Error');
|
|
290
|
+
networkError.name = 'TypeError';
|
|
291
|
+
const errorCode = analyzeNetworkError(networkError);
|
|
292
|
+
const apiNetworkError = new ApiNetworkError(networkError);
|
|
293
|
+
apiNetworkError.errorCode = errorCode;
|
|
294
|
+
reject(apiNetworkError);
|
|
295
|
+
};
|
|
296
|
+
xhr.ontimeout = function() {
|
|
297
|
+
if (timer) clearTimeout(timer);
|
|
298
|
+
const timeoutError = new ApiError({
|
|
299
|
+
status: 0,
|
|
300
|
+
statusText: 'TIMEOUT',
|
|
301
|
+
headers: new Headers(),
|
|
302
|
+
data: null
|
|
303
|
+
}, options);
|
|
304
|
+
timeoutError.code = ErrorCode.ERR_TIMEOUT;
|
|
305
|
+
reject(timeoutError);
|
|
306
|
+
};
|
|
307
|
+
xhr.send(requestBody);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
208
310
|
const Api = {
|
|
209
311
|
configure: (config)=>{
|
|
210
312
|
if (void 0 !== config.baseURL) API_CONFIG.baseURL = config.baseURL;
|