@scayle/storefront-core 7.24.0 → 7.24.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 7.24.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Forbid saving `null` and `undefined` results to the cache
8
+
3
9
  ## 7.24.0
4
10
 
5
11
  ### Minor Changes
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Cached = void 0;
7
- var _radash = require("radash");
8
7
  var _autobind = require("../helpers/autobind.cjs");
9
8
  var _utils = require("../utils/index.cjs");
10
9
  var _hash = require("../utils/hash.cjs");
@@ -34,23 +33,22 @@ class Cached {
34
33
  const prefix = options?.cacheKeyPrefix ?? fn.name;
35
34
  const params = [...args, this?.prefix];
36
35
  const cacheKey = options?.cacheKey ?? (await this.createCacheKey(params, prefix));
37
- const [cacheGetError, cachedResponse] = await (0, _radash.tryit)(this.getCacheValue)(cacheKey, options);
38
- if (cachedResponse) {
39
- return cachedResponse;
36
+ try {
37
+ const cachedResponse = await this.getCacheValue(cacheKey, options);
38
+ if (cachedResponse !== void 0 && cachedResponse !== null) {
39
+ return cachedResponse;
40
+ }
41
+ } catch (e) {
42
+ this.handleError(e);
40
43
  }
41
- if (cacheGetError) {
42
- this.handleError(cacheGetError, false);
43
- }
44
- const [error, response] = await (0, _radash.tryit)(fn)(...args);
45
- if (error) {
46
- this.handleError(error);
47
- }
48
- if (cacheGetError) {
44
+ const response = await fn(...args);
45
+ if (response === void 0 || response === null) {
49
46
  return response;
50
47
  }
51
- const [cacheSetError] = await (0, _radash.tryit)(this.setCacheValue)(cacheKey, response, options);
52
- if (cacheSetError) {
53
- this.handleError(cacheSetError);
48
+ try {
49
+ await this.setCacheValue(cacheKey, response, options);
50
+ } catch (e) {
51
+ this.handleError(e);
54
52
  }
55
53
  return response;
56
54
  };
@@ -76,14 +74,11 @@ class Cached {
76
74
  async createCacheKey(params, prefix = "") {
77
75
  return [prefix, await (0, _hash.sha256)(JSON.stringify(params))].join(":");
78
76
  }
79
- handleError(error, shouldThrow = true) {
77
+ handleError(error) {
80
78
  if (error.message === "timeout") {
81
79
  return this.log.error("Cache timeout");
82
80
  }
83
81
  this.log.error(error);
84
- if (shouldThrow) {
85
- throw error;
86
- }
87
82
  }
88
83
  }
89
84
  exports.Cached = Cached;
@@ -1,4 +1,3 @@
1
- import { tryit } from "radash";
2
1
  import { autobind } from "../helpers/autobind.mjs";
3
2
  import { timeout } from "../utils/index.mjs";
4
3
  import { sha256 } from "../utils/hash.mjs";
@@ -28,30 +27,22 @@ export class Cached {
28
27
  const prefix = options?.cacheKeyPrefix ?? fn.name;
29
28
  const params = [...args, this?.prefix];
30
29
  const cacheKey = options?.cacheKey ?? await this.createCacheKey(params, prefix);
31
- const [cacheGetError, cachedResponse] = await tryit(this.getCacheValue)(
32
- cacheKey,
33
- options
34
- );
35
- if (cachedResponse) {
36
- return cachedResponse;
30
+ try {
31
+ const cachedResponse = await this.getCacheValue(cacheKey, options);
32
+ if (cachedResponse !== void 0 && cachedResponse !== null) {
33
+ return cachedResponse;
34
+ }
35
+ } catch (e) {
36
+ this.handleError(e);
37
37
  }
38
- if (cacheGetError) {
39
- this.handleError(cacheGetError, false);
40
- }
41
- const [error, response] = await tryit(fn)(...args);
42
- if (error) {
43
- this.handleError(error);
44
- }
45
- if (cacheGetError) {
38
+ const response = await fn(...args);
39
+ if (response === void 0 || response === null) {
46
40
  return response;
47
41
  }
48
- const [cacheSetError] = await tryit(this.setCacheValue)(
49
- cacheKey,
50
- response,
51
- options
52
- );
53
- if (cacheSetError) {
54
- this.handleError(cacheSetError);
42
+ try {
43
+ await this.setCacheValue(cacheKey, response, options);
44
+ } catch (e) {
45
+ this.handleError(e);
55
46
  }
56
47
  return response;
57
48
  };
@@ -80,13 +71,10 @@ export class Cached {
80
71
  async createCacheKey(params, prefix = "") {
81
72
  return [prefix, await sha256(JSON.stringify(params))].join(":");
82
73
  }
83
- handleError(error, shouldThrow = true) {
74
+ handleError(error) {
84
75
  if (error.message === "timeout") {
85
76
  return this.log.error("Cache timeout");
86
77
  }
87
78
  this.log.error(error);
88
- if (shouldThrow) {
89
- throw error;
90
- }
91
79
  }
92
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "7.24.0",
3
+ "version": "7.24.1",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",