getta 1.0.8 → 1.0.10
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/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/production.analysis.txt +16 -16
- package/dist/types/cjs/constants.d.cts +2 -0
- package/dist/types/cjs/constants.d.cts.map +1 -1
- package/dist/types/cjs/main.d.cts.map +1 -1
- package/dist/types/esm/constants.d.ts +2 -0
- package/dist/types/esm/constants.d.ts.map +1 -1
- package/dist/types/esm/main.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/constants.ts +2 -0
- package/src/main.ts +25 -7
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -61,3 +61,5 @@ export const CACHE_CONTROL_HEADER = 'Cache-Control';
|
|
|
61
61
|
|
|
62
62
|
export const REQUEST_SENT = 'request_sent';
|
|
63
63
|
export const RESPONSE_RECEIVED = 'response_received';
|
|
64
|
+
export const RESPONSE_FROM_CACHE = 'response_from_cache';
|
|
65
|
+
export const REQUEST_FAILED = 'request_failed';
|
package/src/main.ts
CHANGED
|
@@ -308,11 +308,17 @@ export class Getta {
|
|
|
308
308
|
})();
|
|
309
309
|
});
|
|
310
310
|
} catch (error) {
|
|
311
|
-
|
|
311
|
+
const { startTime, ...rest } = context;
|
|
312
|
+
const endTime = this._performance.now();
|
|
313
|
+
|
|
314
|
+
this._log?.(consts.REQUEST_FAILED, {
|
|
315
|
+
context: { error, url: endpoint, ...rest },
|
|
316
|
+
stats: { duration: startTime ? endTime - startTime : 0, endTime, startTime },
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// Based on above code, error is going to be a type of Error.
|
|
312
320
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
313
|
-
|
|
314
|
-
this._logResponse(res, endpoint, options, context);
|
|
315
|
-
return res;
|
|
321
|
+
return { errors: castArray(error) } as FetchResponse;
|
|
316
322
|
}
|
|
317
323
|
}
|
|
318
324
|
|
|
@@ -365,9 +371,22 @@ export class Getta {
|
|
|
365
371
|
|
|
366
372
|
if (cacheability) {
|
|
367
373
|
if (isCacheabilityValid(cacheability)) {
|
|
374
|
+
const newHeaders = {
|
|
375
|
+
...headers,
|
|
376
|
+
'cache-control': cacheability.printCacheControl(),
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
this._log?.(consts.RESPONSE_FROM_CACHE, {
|
|
380
|
+
context: {
|
|
381
|
+
headers: newHeaders,
|
|
382
|
+
url: endpoint,
|
|
383
|
+
...context,
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
|
|
368
387
|
return {
|
|
369
388
|
data: await this._cacheEntryGet(requestHash),
|
|
370
|
-
headers: new Headers(
|
|
389
|
+
headers: new Headers(newHeaders),
|
|
371
390
|
};
|
|
372
391
|
}
|
|
373
392
|
|
|
@@ -425,7 +444,7 @@ export class Getta {
|
|
|
425
444
|
}
|
|
426
445
|
|
|
427
446
|
private _logResponse(res: FetchResponse, endpoint: string, options: FetchOptions, context: Context) {
|
|
428
|
-
const {
|
|
447
|
+
const { headers, status } = res;
|
|
429
448
|
const { method, redirects, retries } = options;
|
|
430
449
|
const { startTime, ...otherContext } = context;
|
|
431
450
|
const endTime = this._performance.now();
|
|
@@ -433,7 +452,6 @@ export class Getta {
|
|
|
433
452
|
|
|
434
453
|
this._log?.(consts.RESPONSE_RECEIVED, {
|
|
435
454
|
context: {
|
|
436
|
-
body: data ? { data } : { errors: errors ?? [] },
|
|
437
455
|
headers,
|
|
438
456
|
method,
|
|
439
457
|
redirects,
|