getta 1.0.8 → 1.0.9

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,7 +1,7 @@
1
1
  {
2
2
  "name": "getta",
3
3
  "description": "An isomorphic rest client based on the Fetch API.",
4
- "version": "1.0.8",
4
+ "version": "1.0.9",
5
5
  "author": "Dylan Aubrey",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/getta",
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
- // Based on above code, error is gonna be a type of Error.
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
- const res = { errors: castArray(error) } as FetchResponse;
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({ 'cache-control': cacheability.printCacheControl() }),
389
+ headers: new Headers(newHeaders),
371
390
  };
372
391
  }
373
392