@scayle/storefront-core 7.22.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.
Files changed (268) hide show
  1. package/CHANGELOG.md +480 -0
  2. package/LICENSE +21 -0
  3. package/README.md +29 -0
  4. package/dist/bapi/init.cjs +30 -0
  5. package/dist/bapi/init.d.ts +14 -0
  6. package/dist/bapi/init.mjs +23 -0
  7. package/dist/cache/cache.cjs +1 -0
  8. package/dist/cache/cache.d.ts +8 -0
  9. package/dist/cache/cache.mjs +0 -0
  10. package/dist/cache/cached.cjs +89 -0
  11. package/dist/cache/cached.d.ts +24 -0
  12. package/dist/cache/cached.mjs +92 -0
  13. package/dist/cache/index.cjs +38 -0
  14. package/dist/cache/index.d.ts +3 -0
  15. package/dist/cache/index.mjs +3 -0
  16. package/dist/cache/providers/redis.cjs +94 -0
  17. package/dist/cache/providers/redis.d.ts +28 -0
  18. package/dist/cache/providers/redis.mjs +86 -0
  19. package/dist/cache/providers/unstorage.cjs +54 -0
  20. package/dist/cache/providers/unstorage.d.ts +16 -0
  21. package/dist/cache/providers/unstorage.mjs +49 -0
  22. package/dist/cache/types.cjs +11 -0
  23. package/dist/cache/types.d.ts +6 -0
  24. package/dist/cache/types.mjs +4 -0
  25. package/dist/constants/basket.cjs +13 -0
  26. package/dist/constants/basket.d.ts +8 -0
  27. package/dist/constants/basket.mjs +6 -0
  28. package/dist/constants/cache.cjs +8 -0
  29. package/dist/constants/cache.d.ts +1 -0
  30. package/dist/constants/cache.mjs +1 -0
  31. package/dist/constants/hash.cjs +11 -0
  32. package/dist/constants/hash.d.ts +6 -0
  33. package/dist/constants/hash.mjs +4 -0
  34. package/dist/constants/httpStatus.cjs +636 -0
  35. package/dist/constants/httpStatus.d.ts +632 -0
  36. package/dist/constants/httpStatus.mjs +629 -0
  37. package/dist/constants/index.cjs +93 -0
  38. package/dist/constants/index.d.ts +8 -0
  39. package/dist/constants/index.mjs +8 -0
  40. package/dist/constants/product.cjs +11 -0
  41. package/dist/constants/product.d.ts +6 -0
  42. package/dist/constants/product.mjs +4 -0
  43. package/dist/constants/promotion.cjs +10 -0
  44. package/dist/constants/promotion.d.ts +2 -0
  45. package/dist/constants/promotion.mjs +2 -0
  46. package/dist/constants/sorting.cjs +24 -0
  47. package/dist/constants/sorting.d.ts +20 -0
  48. package/dist/constants/sorting.mjs +17 -0
  49. package/dist/constants/withParameters.cjs +138 -0
  50. package/dist/constants/withParameters.d.ts +30 -0
  51. package/dist/constants/withParameters.mjs +132 -0
  52. package/dist/errors/BAPIError.cjs +16 -0
  53. package/dist/errors/BAPIError.d.ts +6 -0
  54. package/dist/errors/BAPIError.mjs +9 -0
  55. package/dist/errors/baseError.cjs +17 -0
  56. package/dist/errors/baseError.d.ts +5 -0
  57. package/dist/errors/baseError.mjs +10 -0
  58. package/dist/errors/index.cjs +27 -0
  59. package/dist/errors/index.d.ts +2 -0
  60. package/dist/errors/index.mjs +2 -0
  61. package/dist/helpers/advancedAttributeHelpers.cjs +26 -0
  62. package/dist/helpers/advancedAttributeHelpers.d.ts +12 -0
  63. package/dist/helpers/advancedAttributeHelpers.mjs +21 -0
  64. package/dist/helpers/arrayHelpers.cjs +10 -0
  65. package/dist/helpers/arrayHelpers.d.ts +1 -0
  66. package/dist/helpers/arrayHelpers.mjs +3 -0
  67. package/dist/helpers/attributeHelpers.cjs +24 -0
  68. package/dist/helpers/attributeHelpers.d.ts +5 -0
  69. package/dist/helpers/attributeHelpers.mjs +17 -0
  70. package/dist/helpers/autobind.cjs +44 -0
  71. package/dist/helpers/autobind.d.ts +39 -0
  72. package/dist/helpers/autobind.mjs +35 -0
  73. package/dist/helpers/basketHelpers.cjs +12 -0
  74. package/dist/helpers/basketHelpers.d.ts +2 -0
  75. package/dist/helpers/basketHelpers.mjs +5 -0
  76. package/dist/helpers/categoryHelper.cjs +61 -0
  77. package/dist/helpers/categoryHelper.d.ts +7 -0
  78. package/dist/helpers/categoryHelper.mjs +44 -0
  79. package/dist/helpers/filterHelper.cjs +156 -0
  80. package/dist/helpers/filterHelper.d.ts +82 -0
  81. package/dist/helpers/filterHelper.mjs +147 -0
  82. package/dist/helpers/formHelpers.cjs +114 -0
  83. package/dist/helpers/formHelpers.d.ts +48 -0
  84. package/dist/helpers/formHelpers.mjs +106 -0
  85. package/dist/helpers/imageHelpers.cjs +20 -0
  86. package/dist/helpers/imageHelpers.d.ts +4 -0
  87. package/dist/helpers/imageHelpers.mjs +14 -0
  88. package/dist/helpers/index.cjs +192 -0
  89. package/dist/helpers/index.d.ts +17 -0
  90. package/dist/helpers/index.mjs +17 -0
  91. package/dist/helpers/localization.cjs +13 -0
  92. package/dist/helpers/localization.d.ts +7 -0
  93. package/dist/helpers/localization.mjs +6 -0
  94. package/dist/helpers/objectHelpers.cjs +19 -0
  95. package/dist/helpers/objectHelpers.d.ts +1 -0
  96. package/dist/helpers/objectHelpers.mjs +6 -0
  97. package/dist/helpers/orderHelpers.cjs +10 -0
  98. package/dist/helpers/orderHelpers.d.ts +67 -0
  99. package/dist/helpers/orderHelpers.mjs +4 -0
  100. package/dist/helpers/product.fixture.cjs +770 -0
  101. package/dist/helpers/product.fixture.d.ts +415 -0
  102. package/dist/helpers/product.fixture.mjs +838 -0
  103. package/dist/helpers/productDisruptorHelper.cjs +37 -0
  104. package/dist/helpers/productDisruptorHelper.d.ts +10 -0
  105. package/dist/helpers/productDisruptorHelper.mjs +23 -0
  106. package/dist/helpers/productHelpers.cjs +188 -0
  107. package/dist/helpers/productHelpers.d.ts +41 -0
  108. package/dist/helpers/productHelpers.mjs +170 -0
  109. package/dist/helpers/sanitizationHelpers.cjs +15 -0
  110. package/dist/helpers/sanitizationHelpers.d.ts +2 -0
  111. package/dist/helpers/sanitizationHelpers.mjs +6 -0
  112. package/dist/helpers/sortingHelper.cjs +67 -0
  113. package/dist/helpers/sortingHelper.d.ts +69 -0
  114. package/dist/helpers/sortingHelper.mjs +66 -0
  115. package/dist/helpers/stringHelpers.cjs +8 -0
  116. package/dist/helpers/stringHelpers.d.ts +2 -0
  117. package/dist/helpers/stringHelpers.mjs +2 -0
  118. package/dist/index.cjs +85 -0
  119. package/dist/index.d.ts +19 -0
  120. package/dist/index.mjs +8 -0
  121. package/dist/rpc/methods/basket/basket.cjs +183 -0
  122. package/dist/rpc/methods/basket/basket.d.ts +23 -0
  123. package/dist/rpc/methods/basket/basket.mjs +181 -0
  124. package/dist/rpc/methods/brands.cjs +34 -0
  125. package/dist/rpc/methods/brands.d.ts +5 -0
  126. package/dist/rpc/methods/brands.mjs +16 -0
  127. package/dist/rpc/methods/categories.cjs +102 -0
  128. package/dist/rpc/methods/categories.d.ts +21 -0
  129. package/dist/rpc/methods/categories.mjs +64 -0
  130. package/dist/rpc/methods/cbd.cjs +45 -0
  131. package/dist/rpc/methods/cbd.d.ts +4 -0
  132. package/dist/rpc/methods/cbd.mjs +36 -0
  133. package/dist/rpc/methods/checkout/index.cjs +38 -0
  134. package/dist/rpc/methods/checkout/index.d.ts +3 -0
  135. package/dist/rpc/methods/checkout/index.mjs +3 -0
  136. package/dist/rpc/methods/checkout/order.cjs +55 -0
  137. package/dist/rpc/methods/checkout/order.d.ts +4 -0
  138. package/dist/rpc/methods/checkout/order.mjs +43 -0
  139. package/dist/rpc/methods/checkout/shopUser.cjs +126 -0
  140. package/dist/rpc/methods/checkout/shopUser.d.ts +7 -0
  141. package/dist/rpc/methods/checkout/shopUser.mjs +109 -0
  142. package/dist/rpc/methods/checkout/shopUserAddresses.cjs +25 -0
  143. package/dist/rpc/methods/checkout/shopUserAddresses.d.ts +3 -0
  144. package/dist/rpc/methods/checkout/shopUserAddresses.mjs +19 -0
  145. package/dist/rpc/methods/index.cjs +159 -0
  146. package/dist/rpc/methods/index.d.ts +14 -0
  147. package/dist/rpc/methods/index.mjs +14 -0
  148. package/dist/rpc/methods/navigationTrees.cjs +33 -0
  149. package/dist/rpc/methods/navigationTrees.d.ts +5 -0
  150. package/dist/rpc/methods/navigationTrees.mjs +10 -0
  151. package/dist/rpc/methods/products.cjs +261 -0
  152. package/dist/rpc/methods/products.d.ts +13 -0
  153. package/dist/rpc/methods/products.mjs +217 -0
  154. package/dist/rpc/methods/promotion.cjs +53 -0
  155. package/dist/rpc/methods/promotion.d.ts +4 -0
  156. package/dist/rpc/methods/promotion.mjs +38 -0
  157. package/dist/rpc/methods/search.cjs +38 -0
  158. package/dist/rpc/methods/search.d.ts +10 -0
  159. package/dist/rpc/methods/search.mjs +22 -0
  160. package/dist/rpc/methods/session.cjs +274 -0
  161. package/dist/rpc/methods/session.d.ts +20 -0
  162. package/dist/rpc/methods/session.mjs +309 -0
  163. package/dist/rpc/methods/shopConfiguration.cjs +15 -0
  164. package/dist/rpc/methods/shopConfiguration.d.ts +4 -0
  165. package/dist/rpc/methods/shopConfiguration.mjs +6 -0
  166. package/dist/rpc/methods/user.cjs +89 -0
  167. package/dist/rpc/methods/user.d.ts +20 -0
  168. package/dist/rpc/methods/user.mjs +56 -0
  169. package/dist/rpc/methods/variants.cjs +29 -0
  170. package/dist/rpc/methods/variants.d.ts +8 -0
  171. package/dist/rpc/methods/variants.mjs +12 -0
  172. package/dist/rpc/methods/wishlist.cjs +85 -0
  173. package/dist/rpc/methods/wishlist.d.ts +13 -0
  174. package/dist/rpc/methods/wishlist.mjs +61 -0
  175. package/dist/server.cjs +39 -0
  176. package/dist/server.d.ts +5 -0
  177. package/dist/server.mjs +4 -0
  178. package/dist/types/api/auth.cjs +1 -0
  179. package/dist/types/api/auth.d.ts +63 -0
  180. package/dist/types/api/auth.mjs +0 -0
  181. package/dist/types/api/context.cjs +1 -0
  182. package/dist/types/api/context.d.ts +61 -0
  183. package/dist/types/api/context.mjs +0 -0
  184. package/dist/types/api/rpc.cjs +1 -0
  185. package/dist/types/api/rpc.d.ts +4 -0
  186. package/dist/types/api/rpc.mjs +0 -0
  187. package/dist/types/bapi/basket.cjs +1 -0
  188. package/dist/types/bapi/basket.d.ts +25 -0
  189. package/dist/types/bapi/basket.mjs +0 -0
  190. package/dist/types/bapi/brand.cjs +1 -0
  191. package/dist/types/bapi/brand.d.ts +2 -0
  192. package/dist/types/bapi/brand.mjs +0 -0
  193. package/dist/types/bapi/category.cjs +1 -0
  194. package/dist/types/bapi/category.d.ts +6 -0
  195. package/dist/types/bapi/category.mjs +0 -0
  196. package/dist/types/bapi/filter.cjs +1 -0
  197. package/dist/types/bapi/filter.d.ts +49 -0
  198. package/dist/types/bapi/filter.mjs +0 -0
  199. package/dist/types/bapi/navigation.cjs +1 -0
  200. package/dist/types/bapi/navigation.d.ts +10 -0
  201. package/dist/types/bapi/navigation.mjs +0 -0
  202. package/dist/types/bapi/order.cjs +1 -0
  203. package/dist/types/bapi/order.d.ts +554 -0
  204. package/dist/types/bapi/order.mjs +0 -0
  205. package/dist/types/bapi/pagination.cjs +1 -0
  206. package/dist/types/bapi/pagination.d.ts +13 -0
  207. package/dist/types/bapi/pagination.mjs +0 -0
  208. package/dist/types/bapi/product.cjs +1 -0
  209. package/dist/types/bapi/product.d.ts +84 -0
  210. package/dist/types/bapi/product.mjs +0 -0
  211. package/dist/types/bapi/productFilter.cjs +1 -0
  212. package/dist/types/bapi/productFilter.d.ts +7 -0
  213. package/dist/types/bapi/productFilter.mjs +0 -0
  214. package/dist/types/bapi/promotion.cjs +1 -0
  215. package/dist/types/bapi/promotion.d.ts +2 -0
  216. package/dist/types/bapi/promotion.mjs +0 -0
  217. package/dist/types/bapi/redirects.cjs +1 -0
  218. package/dist/types/bapi/redirects.d.ts +1 -0
  219. package/dist/types/bapi/redirects.mjs +0 -0
  220. package/dist/types/bapi/router.cjs +1 -0
  221. package/dist/types/bapi/router.d.ts +1 -0
  222. package/dist/types/bapi/router.mjs +0 -0
  223. package/dist/types/bapi/search.cjs +1 -0
  224. package/dist/types/bapi/search.d.ts +21 -0
  225. package/dist/types/bapi/search.mjs +0 -0
  226. package/dist/types/bapi/sorting.cjs +5 -0
  227. package/dist/types/bapi/sorting.d.ts +10 -0
  228. package/dist/types/bapi/sorting.mjs +1 -0
  229. package/dist/types/bapi/wishlist.cjs +1 -0
  230. package/dist/types/bapi/wishlist.d.ts +9 -0
  231. package/dist/types/bapi/wishlist.mjs +0 -0
  232. package/dist/types/breadcrumb.cjs +1 -0
  233. package/dist/types/breadcrumb.d.ts +4 -0
  234. package/dist/types/breadcrumb.mjs +0 -0
  235. package/dist/types/index.cjs +236 -0
  236. package/dist/types/index.d.ts +22 -0
  237. package/dist/types/index.mjs +21 -0
  238. package/dist/types/promises.cjs +1 -0
  239. package/dist/types/promises.d.ts +2 -0
  240. package/dist/types/promises.mjs +0 -0
  241. package/dist/types/user.cjs +1 -0
  242. package/dist/types/user.d.ts +100 -0
  243. package/dist/types/user.mjs +0 -0
  244. package/dist/utils/compression.cjs +24 -0
  245. package/dist/utils/compression.d.ts +2 -0
  246. package/dist/utils/compression.mjs +16 -0
  247. package/dist/utils/hash.cjs +68 -0
  248. package/dist/utils/hash.d.ts +8 -0
  249. package/dist/utils/hash.mjs +61 -0
  250. package/dist/utils/index.cjs +27 -0
  251. package/dist/utils/index.d.ts +2 -0
  252. package/dist/utils/index.mjs +2 -0
  253. package/dist/utils/keys.cjs +67 -0
  254. package/dist/utils/keys.d.ts +24 -0
  255. package/dist/utils/keys.mjs +56 -0
  256. package/dist/utils/log.cjs +93 -0
  257. package/dist/utils/log.d.ts +43 -0
  258. package/dist/utils/log.mjs +75 -0
  259. package/dist/utils/rpc.cjs +41 -0
  260. package/dist/utils/rpc.d.ts +6 -0
  261. package/dist/utils/rpc.mjs +35 -0
  262. package/dist/utils/timeout.cjs +17 -0
  263. package/dist/utils/timeout.d.ts +2 -0
  264. package/dist/utils/timeout.mjs +9 -0
  265. package/dist/utils/user.cjs +75 -0
  266. package/dist/utils/user.d.ts +14 -0
  267. package/dist/utils/user.mjs +65 -0
  268. package/package.json +97 -0
@@ -0,0 +1,24 @@
1
+ import { Log } from '../utils';
2
+ import { Cache as CacheInterface } from './cache';
3
+ export type CacheOptions = Partial<{
4
+ ttl: number;
5
+ cacheKey: string;
6
+ cacheKeyPrefix: string;
7
+ timeout: number;
8
+ }>;
9
+ type Fn<TArgs extends unknown[], TResult> = (...args: TArgs) => Promise<TResult>;
10
+ export declare class Cached {
11
+ private cache;
12
+ private log;
13
+ private prefix;
14
+ private enabled;
15
+ constructor(cache: CacheInterface, log: Log, prefix?: string | number, enabled?: boolean);
16
+ get isCacheEnabled(): boolean | undefined;
17
+ execute<TArgs extends unknown[], TResult>(fn: Fn<TArgs, TResult>, options?: CacheOptions): Awaited<Fn<TArgs, TResult>>;
18
+ private getCacheValue;
19
+ private setCacheValue;
20
+ private createCacheKey;
21
+ private handleError;
22
+ }
23
+ export type { Cache as CacheInterface } from './cache';
24
+ export type CachedType = Cached['execute'];
@@ -0,0 +1,92 @@
1
+ import { tryit } from "radash";
2
+ import { autobind } from "../helpers/autobind.mjs";
3
+ import { timeout } from "../utils/index.mjs";
4
+ import { sha256 } from "../utils/hash.mjs";
5
+ import { CACHE_TIMEOUT } from "../constants/index.mjs";
6
+ const CACHE_NOT_INITIALIZED_MSG = "Cache is not initialized";
7
+ export class Cached {
8
+ cache;
9
+ log;
10
+ prefix;
11
+ enabled;
12
+ constructor(cache, log, prefix = "", enabled = process.env.AY_CACHE_DISABLED !== "true") {
13
+ if (!cache) {
14
+ log.error(CACHE_NOT_INITIALIZED_MSG);
15
+ throw new Error(CACHE_NOT_INITIALIZED_MSG);
16
+ }
17
+ this.cache = cache;
18
+ this.log = log.space("cached.init");
19
+ this.prefix = prefix;
20
+ this.enabled = enabled;
21
+ autobind(this);
22
+ }
23
+ get isCacheEnabled() {
24
+ return this.enabled;
25
+ }
26
+ execute(fn, options) {
27
+ return async (...args) => {
28
+ const prefix = options?.cacheKeyPrefix ?? fn.name;
29
+ const params = [...args, this?.prefix];
30
+ 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;
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) {
46
+ return response;
47
+ }
48
+ const [cacheSetError] = await tryit(this.setCacheValue)(
49
+ cacheKey,
50
+ response,
51
+ options
52
+ );
53
+ if (cacheSetError) {
54
+ this.handleError(cacheSetError);
55
+ }
56
+ return response;
57
+ };
58
+ }
59
+ async getCacheValue(cacheKey, options) {
60
+ if (!this.isCacheEnabled) {
61
+ return;
62
+ }
63
+ const data = await timeout(
64
+ options?.timeout ?? CACHE_TIMEOUT,
65
+ this.cache.get(cacheKey)
66
+ );
67
+ if (data !== null) {
68
+ return data;
69
+ }
70
+ }
71
+ async setCacheValue(cacheKey, value, options) {
72
+ if (!this.isCacheEnabled) {
73
+ return;
74
+ }
75
+ const ttl = options?.ttl || 60 * 60;
76
+ if (ttl) {
77
+ await timeout(CACHE_TIMEOUT, this.cache.set(cacheKey, value, ttl));
78
+ }
79
+ }
80
+ async createCacheKey(params, prefix = "") {
81
+ return [prefix, await sha256(JSON.stringify(params))].join(":");
82
+ }
83
+ handleError(error, shouldThrow = true) {
84
+ if (error.message === "timeout") {
85
+ return this.log.error("Cache timeout");
86
+ }
87
+ this.log.error(error);
88
+ if (shouldThrow) {
89
+ throw error;
90
+ }
91
+ }
92
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _cache = require("./cache.cjs");
7
+ Object.keys(_cache).forEach(function (key) {
8
+ if (key === "default" || key === "__esModule") return;
9
+ if (key in exports && exports[key] === _cache[key]) return;
10
+ Object.defineProperty(exports, key, {
11
+ enumerable: true,
12
+ get: function () {
13
+ return _cache[key];
14
+ }
15
+ });
16
+ });
17
+ var _cached = require("./cached.cjs");
18
+ Object.keys(_cached).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _cached[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _cached[key];
25
+ }
26
+ });
27
+ });
28
+ var _types = require("./types.cjs");
29
+ Object.keys(_types).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _types[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function () {
35
+ return _types[key];
36
+ }
37
+ });
38
+ });
@@ -0,0 +1,3 @@
1
+ export * from './cache';
2
+ export * from './cached';
3
+ export * from './types';
@@ -0,0 +1,3 @@
1
+ export * from "./cache.mjs";
2
+ export * from "./cached.mjs";
3
+ export * from "./types.mjs";
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createClient = exports.RedisCache = void 0;
7
+ var _redis = require("redis");
8
+ var _compression = require("../../utils/compression.cjs");
9
+ const connections = {};
10
+ const createClient = async (options, log) => {
11
+ log = log.space("redis.createClient");
12
+ const connectionIndex = [options.host, options.database].join("_");
13
+ let client;
14
+ if (connections[connectionIndex] !== void 0) {
15
+ client = connections[connectionIndex];
16
+ } else {
17
+ client = connections[connectionIndex] = (0, _redis.createClient)({
18
+ ...options,
19
+ ...(options && "username" in options && {
20
+ username: options.username
21
+ }),
22
+ socket: {
23
+ host: options.host,
24
+ port: options.port,
25
+ ...(options.sslTransit === true ? {
26
+ tls: true,
27
+ checkServerIdentity: () => void 0
28
+ } : {
29
+ tls: false
30
+ })
31
+ }
32
+ });
33
+ client.on("error", error => {
34
+ log.error("Redis connection error", error);
35
+ });
36
+ await client.connect();
37
+ }
38
+ return client;
39
+ };
40
+ exports.createClient = createClient;
41
+ class RedisCache {
42
+ client;
43
+ prefix;
44
+ constructor(client, prefix = "") {
45
+ this.client = client;
46
+ this.prefix = prefix;
47
+ }
48
+ async get(key) {
49
+ const value = await this.client.get(this.getKey(key));
50
+ if (value === null) {
51
+ return null;
52
+ }
53
+ return this.deserialize(value);
54
+ }
55
+ async has(key) {
56
+ return (await this.client.exists(this.getKey(key))) !== 0;
57
+ }
58
+ async purgeTags(tags) {
59
+ await Promise.all(tags.map(tag => this.purgeTag(tag)));
60
+ }
61
+ async purgeTag(tag) {
62
+ const keys = await this.client.sMembers(this.getKey(`tag:${tag}`));
63
+ await this.client.del(keys);
64
+ await this.client.del(this.getKey(`tag:${tag}`));
65
+ }
66
+ async purgeKeys(keys) {
67
+ await this.client.del(keys);
68
+ }
69
+ async purgeAll() {
70
+ await this.client.flushDb();
71
+ }
72
+ async set(key, value, ttl, tags = []) {
73
+ const _value = await this.serialize(value);
74
+ await this.client.set(this.getKey(key), _value, {
75
+ EX: ttl
76
+ });
77
+ await Promise.all(tags.map(tag => this.addKeyToTag(tag, this.getKey(key))));
78
+ }
79
+ async addKeyToTag(tag, key) {
80
+ await this.client.sAdd(this.getKey(`tag:${tag}`), key);
81
+ }
82
+ getKey(key) {
83
+ return [this.prefix, key].join(":");
84
+ }
85
+ async deserialize(value) {
86
+ const data = await (0, _compression.decompress)(value);
87
+ return JSON.parse(data);
88
+ }
89
+ async serialize(value) {
90
+ const data = JSON.stringify(value);
91
+ return await (0, _compression.compress)(data);
92
+ }
93
+ }
94
+ exports.RedisCache = RedisCache;
@@ -0,0 +1,28 @@
1
+ import { RedisClientType } from 'redis';
2
+ import { Log } from '../../utils';
3
+ import { Cache as CacheInterface } from '../../cache/cache';
4
+ export interface RedisOptions {
5
+ username?: string;
6
+ password?: string;
7
+ host?: string;
8
+ port?: number;
9
+ database?: number;
10
+ sslTransit?: boolean;
11
+ }
12
+ export declare const createClient: (options: RedisOptions, log: Log) => Promise<RedisClientType>;
13
+ export declare class RedisCache implements CacheInterface {
14
+ client: RedisClientType;
15
+ prefix: string;
16
+ constructor(client: RedisClientType, prefix?: string);
17
+ get(key: string): Promise<any>;
18
+ has(key: string): Promise<boolean>;
19
+ purgeTags(tags: string[]): Promise<void>;
20
+ purgeTag(tag: string): Promise<void>;
21
+ purgeKeys(keys: string[]): Promise<void>;
22
+ purgeAll(): Promise<void>;
23
+ set(key: string, value: any, ttl: number, tags?: string[]): Promise<void>;
24
+ private addKeyToTag;
25
+ private getKey;
26
+ private deserialize;
27
+ private serialize;
28
+ }
@@ -0,0 +1,86 @@
1
+ import { createClient as createRedisClient } from "redis";
2
+ import { compress, decompress } from "../../utils/compression.mjs";
3
+ const connections = {};
4
+ export const createClient = async (options, log) => {
5
+ log = log.space("redis.createClient");
6
+ const connectionIndex = [options.host, options.database].join("_");
7
+ let client;
8
+ if (connections[connectionIndex] !== void 0) {
9
+ client = connections[connectionIndex];
10
+ } else {
11
+ client = connections[connectionIndex] = createRedisClient({
12
+ ...options,
13
+ ...options && "username" in options && { username: options.username },
14
+ socket: {
15
+ host: options.host,
16
+ port: options.port,
17
+ ...options.sslTransit === true ? {
18
+ tls: true,
19
+ checkServerIdentity: () => void 0
20
+ } : {
21
+ tls: false
22
+ }
23
+ }
24
+ });
25
+ client.on("error", (error) => {
26
+ log.error("Redis connection error", error);
27
+ });
28
+ await client.connect();
29
+ }
30
+ return client;
31
+ };
32
+ export class RedisCache {
33
+ client;
34
+ prefix;
35
+ constructor(client, prefix = "") {
36
+ this.client = client;
37
+ this.prefix = prefix;
38
+ }
39
+ async get(key) {
40
+ const value = await this.client.get(this.getKey(key));
41
+ if (value === null) {
42
+ return null;
43
+ }
44
+ return this.deserialize(value);
45
+ }
46
+ async has(key) {
47
+ return await this.client.exists(this.getKey(key)) !== 0;
48
+ }
49
+ async purgeTags(tags) {
50
+ await Promise.all(tags.map((tag) => this.purgeTag(tag)));
51
+ }
52
+ async purgeTag(tag) {
53
+ const keys = await this.client.sMembers(this.getKey(`tag:${tag}`));
54
+ await this.client.del(keys);
55
+ await this.client.del(this.getKey(`tag:${tag}`));
56
+ }
57
+ async purgeKeys(keys) {
58
+ await this.client.del(keys);
59
+ }
60
+ async purgeAll() {
61
+ await this.client.flushDb();
62
+ }
63
+ async set(key, value, ttl, tags = []) {
64
+ const _value = await this.serialize(value);
65
+ await this.client.set(this.getKey(key), _value, {
66
+ EX: ttl
67
+ });
68
+ await Promise.all(
69
+ tags.map((tag) => this.addKeyToTag(tag, this.getKey(key)))
70
+ );
71
+ }
72
+ async addKeyToTag(tag, key) {
73
+ await this.client.sAdd(this.getKey(`tag:${tag}`), key);
74
+ }
75
+ getKey(key) {
76
+ return [this.prefix, key].join(":");
77
+ }
78
+ async deserialize(value) {
79
+ const data = await decompress(value);
80
+ return JSON.parse(data);
81
+ }
82
+ async serialize(value) {
83
+ const data = JSON.stringify(value);
84
+ return await compress(data);
85
+ }
86
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.UnstorageCache = void 0;
7
+ class UnstorageCache {
8
+ storage;
9
+ prefix;
10
+ constructor(storage, prefix = "") {
11
+ this.storage = storage;
12
+ this.prefix = prefix;
13
+ }
14
+ async get(key) {
15
+ const value = await this.storage.getItem(this.getKey(key));
16
+ if (value === null) {
17
+ return null;
18
+ }
19
+ return value;
20
+ }
21
+ async has(key) {
22
+ return await this.storage.hasItem(this.getKey(key));
23
+ }
24
+ async purgeTags(tags) {
25
+ await Promise.all(tags.map(tag => this.purgeTag(tag)));
26
+ }
27
+ async purgeTag(tag) {
28
+ const keys = await this.storage.getItem(this.getKey(`tag:${tag}`));
29
+ await this.purgeKeys(keys ?? []);
30
+ await this.storage.removeItem(this.getKey(`tag:${tag}`));
31
+ }
32
+ async purgeKeys(keys) {
33
+ await Promise.all(keys.map(k => this.storage.removeItem(k)));
34
+ }
35
+ async purgeAll() {
36
+ await this.storage.clear();
37
+ }
38
+ async set(key, value, ttl, tags = []) {
39
+ await this.storage.setItem(this.getKey(key), value, {
40
+ ttl
41
+ });
42
+ await Promise.all(tags.map(tag => this.addKeyToTag(tag, this.getKey(key))));
43
+ }
44
+ async addKeyToTag(tag, key) {
45
+ const keys = await this.storage.getItem(tag);
46
+ const set = new Set(keys ?? []);
47
+ set.add(key);
48
+ await this.storage.setItem(this.getKey(`tag:${tag}`), Array.from(set));
49
+ }
50
+ getKey(key) {
51
+ return [this.prefix, key].join(":");
52
+ }
53
+ }
54
+ exports.UnstorageCache = UnstorageCache;
@@ -0,0 +1,16 @@
1
+ import type { Storage } from 'unstorage';
2
+ import { Cache as CacheInterface } from '../cache';
3
+ export declare class UnstorageCache implements CacheInterface {
4
+ storage: Storage;
5
+ prefix: string;
6
+ constructor(storage: Storage, prefix?: string);
7
+ get(key: string): Promise<any | null>;
8
+ has(key: string): Promise<boolean>;
9
+ purgeTags(tags: string[]): Promise<void>;
10
+ purgeTag(tag: string): Promise<void>;
11
+ purgeKeys(keys: string[]): Promise<void>;
12
+ purgeAll(): Promise<void>;
13
+ set(key: string, value: any, ttl: number, tags?: string[]): Promise<void>;
14
+ private addKeyToTag;
15
+ private getKey;
16
+ }
@@ -0,0 +1,49 @@
1
+ export class UnstorageCache {
2
+ storage;
3
+ prefix;
4
+ constructor(storage, prefix = "") {
5
+ this.storage = storage;
6
+ this.prefix = prefix;
7
+ }
8
+ async get(key) {
9
+ const value = await this.storage.getItem(this.getKey(key));
10
+ if (value === null) {
11
+ return null;
12
+ }
13
+ return value;
14
+ }
15
+ async has(key) {
16
+ return await this.storage.hasItem(this.getKey(key));
17
+ }
18
+ async purgeTags(tags) {
19
+ await Promise.all(tags.map((tag) => this.purgeTag(tag)));
20
+ }
21
+ async purgeTag(tag) {
22
+ const keys = await this.storage.getItem(this.getKey(`tag:${tag}`));
23
+ await this.purgeKeys(keys ?? []);
24
+ await this.storage.removeItem(this.getKey(`tag:${tag}`));
25
+ }
26
+ async purgeKeys(keys) {
27
+ await Promise.all(keys.map((k) => this.storage.removeItem(k)));
28
+ }
29
+ async purgeAll() {
30
+ await this.storage.clear();
31
+ }
32
+ async set(key, value, ttl, tags = []) {
33
+ await this.storage.setItem(this.getKey(key), value, {
34
+ ttl
35
+ });
36
+ await Promise.all(
37
+ tags.map((tag) => this.addKeyToTag(tag, this.getKey(key)))
38
+ );
39
+ }
40
+ async addKeyToTag(tag, key) {
41
+ const keys = await this.storage.getItem(tag);
42
+ const set = new Set(keys ?? []);
43
+ set.add(key);
44
+ await this.storage.setItem(this.getKey(`tag:${tag}`), Array.from(set));
45
+ }
46
+ getKey(key) {
47
+ return [this.prefix, key].join(":");
48
+ }
49
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CacheProvider = void 0;
7
+ const CacheProvider = {
8
+ REDIS: "redis",
9
+ UNSTORAGE: "unstorage"
10
+ };
11
+ exports.CacheProvider = CacheProvider;
@@ -0,0 +1,6 @@
1
+ import { ValuesType } from 'utility-types';
2
+ export declare const CacheProvider: {
3
+ readonly REDIS: "redis";
4
+ readonly UNSTORAGE: "unstorage";
5
+ };
6
+ export type CacheProvider = ValuesType<typeof CacheProvider> | null;
@@ -0,0 +1,4 @@
1
+ export const CacheProvider = {
2
+ REDIS: "redis",
3
+ UNSTORAGE: "unstorage"
4
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ExistingItemHandling = void 0;
7
+ const ExistingItemHandling = {
8
+ KeepExisting: 0,
9
+ AddQuantityToExisting: 1,
10
+ ReplaceExisting: 2,
11
+ ReplaceExistingWithCombinedQuantity: 3
12
+ };
13
+ exports.ExistingItemHandling = ExistingItemHandling;
@@ -0,0 +1,8 @@
1
+ import { ValuesType } from 'utility-types';
2
+ export declare const ExistingItemHandling: {
3
+ readonly KeepExisting: 0;
4
+ readonly AddQuantityToExisting: 1;
5
+ readonly ReplaceExisting: 2;
6
+ readonly ReplaceExistingWithCombinedQuantity: 3;
7
+ };
8
+ export type ExistingItemHandling = ValuesType<typeof ExistingItemHandling>;
@@ -0,0 +1,6 @@
1
+ export const ExistingItemHandling = {
2
+ KeepExisting: 0,
3
+ AddQuantityToExisting: 1,
4
+ ReplaceExisting: 2,
5
+ ReplaceExistingWithCombinedQuantity: 3
6
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CACHE_TIMEOUT = void 0;
7
+ const CACHE_TIMEOUT = 500;
8
+ exports.CACHE_TIMEOUT = CACHE_TIMEOUT;
@@ -0,0 +1 @@
1
+ export declare const CACHE_TIMEOUT = 500;
@@ -0,0 +1 @@
1
+ export const CACHE_TIMEOUT = 500;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.HashAlgorithm = void 0;
7
+ const HashAlgorithm = {
8
+ MD5: "md5",
9
+ SHA256: "sha256"
10
+ };
11
+ exports.HashAlgorithm = HashAlgorithm;
@@ -0,0 +1,6 @@
1
+ import { ValuesType } from 'utility-types';
2
+ export declare const HashAlgorithm: {
3
+ readonly MD5: "md5";
4
+ readonly SHA256: "sha256";
5
+ };
6
+ export type HashAlgorithm = ValuesType<typeof HashAlgorithm> | null;
@@ -0,0 +1,4 @@
1
+ export const HashAlgorithm = {
2
+ MD5: "md5",
3
+ SHA256: "sha256"
4
+ };