@scayle/storefront-core 7.65.0 → 7.65.2

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,19 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 7.65.2
4
+
5
+ ### Patch Changes
6
+
7
+ **Dependencies**
8
+
9
+ - Updated dependency to @scayle/storefront-api@17.7.0
10
+
11
+ ## 7.65.1
12
+
13
+ ### Patch Changes
14
+
15
+ - Fix regresion in product RPC methods where the categoryId may not be resolved from the category path
16
+
3
17
  ## 7.65.0
4
18
 
5
19
  ### Minor Changes
@@ -37,7 +37,7 @@ const getCheckoutToken = exports.getCheckoutToken = async function getCheckoutTo
37
37
  carrier,
38
38
  basketId: context.basketKey,
39
39
  campaignKey: context.campaignKey
40
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.65.0"}`).setProtectedHeader({
40
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.65.2"}`).setProtectedHeader({
41
41
  alg: "HS256",
42
42
  typ: "JWT"
43
43
  }).sign(secret);
@@ -35,7 +35,7 @@ export const getCheckoutToken = async function getCheckoutToken2(jwtPayload = {}
35
35
  carrier,
36
36
  basketId: context.basketKey,
37
37
  campaignKey: context.campaignKey
38
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.65.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
38
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.65.2"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
39
39
  return {
40
40
  accessToken: refreshedAccessToken,
41
41
  checkoutJwt
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getProductsCount = exports.getProductsByReferenceKeys = exports.getProductsByIds = exports.getProductsByCategory = exports.getProductById = exports.getFilters = exports.fetchAllFiltersForCategory = void 0;
7
+ exports.resolveCategoryIdFromParams = resolveCategoryIdFromParams;
7
8
  var _helpers = require("../../helpers/index.cjs");
8
9
  var _constants = require("../../constants/index.cjs");
9
10
  var _cache = require("../../cache/index.cjs");
@@ -22,6 +23,25 @@ const sanitizeAttributesForBAPI = ({
22
23
  const mapFiltersToSlugs = (filters = []) => filters.map(({
23
24
  slug
24
25
  }) => slug.toLowerCase());
26
+ async function resolveCategoryIdFromParams(context, params) {
27
+ let category;
28
+ let categoryId;
29
+ if ("categoryId" in params) {
30
+ categoryId = params.categoryId;
31
+ } else if (params.category && params.category !== "/") {
32
+ category = params.category;
33
+ const result = await context.cached(context.bapiClient.categories.getByPath, {
34
+ cacheKeyPrefix: `getByPath-categories-${category}`
35
+ })((0, _helpers.splitAndRemoveEmpty)(category));
36
+ categoryId = result.id;
37
+ } else {
38
+ category = "/";
39
+ }
40
+ return {
41
+ category,
42
+ categoryId
43
+ };
44
+ }
25
45
  const getProductById = exports.getProductById = async function getProductById2(options, {
26
46
  bapiClient,
27
47
  cached,
@@ -68,30 +88,21 @@ const getProductsByReferenceKeys = exports.getProductsByReferenceKeys = async fu
68
88
  pricePromotionKey: options.pricePromotionKey
69
89
  });
70
90
  };
71
- const getProductsCount = exports.getProductsCount = async function getProductsCount2(params, {
72
- cached,
73
- bapiClient,
74
- campaignKey
75
- }) {
91
+ const getProductsCount = exports.getProductsCount = async function getProductsCount2(params, context) {
76
92
  const {
77
93
  where = void 0,
78
94
  includeSoldOut = false,
79
95
  includeSellableForFree = false,
80
96
  orFiltersOperator = void 0
81
97
  } = params;
82
- let category;
83
- let categoryId;
84
- if ("category" in params && !params.category) {
85
- category = "/";
86
- }
87
- if ("categoryId" in params) {
88
- categoryId = params.categoryId;
89
- } else if (category && category !== "/") {
90
- const result = await cached(bapiClient.categories.getByPath, {
91
- cacheKeyPrefix: `getByPath-categories-${category}`
92
- })((0, _helpers.splitAndRemoveEmpty)(category));
93
- categoryId = result.id;
94
- }
98
+ const {
99
+ cached,
100
+ bapiClient,
101
+ campaignKey
102
+ } = context;
103
+ const {
104
+ categoryId
105
+ } = await resolveCategoryIdFromParams(context, params);
95
106
  const productCount = await cached(bapiClient.products.query, {
96
107
  ttl: 15 * _cache.MINUTE,
97
108
  cacheKeyPrefix: "products-query"
@@ -147,24 +158,15 @@ const getFilters = exports.getFilters = async function getFilters2(params, conte
147
158
  includeSellableForFree = false,
148
159
  orFiltersOperator
149
160
  } = params;
150
- let category;
151
- let categoryId;
152
161
  const {
153
162
  cached,
154
163
  bapiClient,
155
164
  campaignKey
156
165
  } = context;
157
- if ("category" in params && !params.category) {
158
- category = "/";
159
- }
160
- if ("categoryId" in params) {
161
- categoryId = params.categoryId;
162
- } else if (category && category !== "/") {
163
- const result = await cached(bapiClient.categories.getByPath, {
164
- cacheKeyPrefix: `getByPath-categories-${category}`
165
- })((0, _helpers.splitAndRemoveEmpty)(category));
166
- categoryId = result.id;
167
- }
166
+ const {
167
+ category,
168
+ categoryId
169
+ } = await resolveCategoryIdFromParams(context, params);
168
170
  const response = await fetchAllFiltersForCategory({
169
171
  category: {
170
172
  id: categoryId
@@ -181,7 +183,7 @@ const getFilters = exports.getFilters = async function getFilters2(params, conte
181
183
  includedFilters
182
184
  });
183
185
  const [filters, productCount] = await Promise.all([cached(bapiClient.filters.get, {
184
- cacheKeyPrefix: `get-filters-${category}`
186
+ cacheKeyPrefix: `get-filters-${categoryId}`
185
187
  })({
186
188
  where: {
187
189
  categoryId,
@@ -197,7 +199,7 @@ const getFilters = exports.getFilters = async function getFilters2(params, conte
197
199
  orFiltersOperator
198
200
  }), cached(bapiClient.products.query, {
199
201
  ttl: 15 * _cache.MINUTE,
200
- cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : category}`
202
+ cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : categoryId}`
201
203
  })({
202
204
  where: {
203
205
  categoryId,
@@ -236,19 +238,10 @@ const getProductsByCategory = exports.getProductsByCategory = async function get
236
238
  bapiClient,
237
239
  campaignKey
238
240
  } = context;
239
- let category;
240
- let categoryId;
241
- if ("category" in params && !params.category) {
242
- category = "/";
243
- }
244
- if ("categoryId" in params) {
245
- categoryId = params.categoryId;
246
- } else if (category && category !== "/") {
247
- const result = await cached(bapiClient.categories.getByPath, {
248
- cacheKeyPrefix: `getByPath-categories-${category}`
249
- })((0, _helpers.splitAndRemoveEmpty)(category));
250
- categoryId = result.id;
251
- }
241
+ const {
242
+ category,
243
+ categoryId
244
+ } = await resolveCategoryIdFromParams(context, params);
252
245
  const response = await fetchAllFiltersForCategory({
253
246
  category: {
254
247
  id: categoryId
@@ -268,7 +261,7 @@ const getProductsByCategory = exports.getProductsByCategory = async function get
268
261
  } = await cached(bapiClient.products.query, {
269
262
  ...cache,
270
263
  ttl: 15 * _cache.MINUTE,
271
- cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : category}`
264
+ cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : categoryId}`
272
265
  })({
273
266
  where: {
274
267
  term: where?.term,
@@ -1,10 +1,29 @@
1
1
  import type { FiltersEndpointResponseData } from '@scayle/storefront-api';
2
2
  import type { FetchFiltersParams, FetchProductParams, FetchProductsByCategoryParams, FetchProductsByIdsParams, FetchProductsByReferenceKeysParams, FetchProductsCountParams, Product, RpcContext } from '../../types';
3
3
  import { ErrorResponse } from '../../errors';
4
+ /**
5
+ * For functions which accept either a category path or category ID,
6
+ * this function can resolve the ID which is necessary for the calling
7
+ * the Storefront APIs.
8
+ * @param context
9
+ * @param params
10
+ * @returns A object with `category` and `categoryId` properties.
11
+ * If no `category` was provided in the params, it will be undefined or '/'
12
+ */
13
+ export declare function resolveCategoryIdFromParams(context: RpcContext, params: {
14
+ category?: string;
15
+ categoryId?: undefined;
16
+ } | {
17
+ categoryId: number;
18
+ category?: undefined;
19
+ }): Promise<{
20
+ category: string | undefined;
21
+ categoryId: number | undefined;
22
+ }>;
4
23
  export declare const getProductById: (options: FetchProductParams, { bapiClient, cached, campaignKey, withParams }: RpcContext) => Promise<Product>;
5
24
  export declare const getProductsByIds: (options: FetchProductsByIdsParams, { bapiClient, cached, campaignKey, withParams }: RpcContext) => Promise<Product[]>;
6
25
  export declare const getProductsByReferenceKeys: (options: FetchProductsByReferenceKeysParams, { bapiClient, cached, campaignKey, withParams }: RpcContext) => Promise<Product[]>;
7
- export declare const getProductsCount: (params: FetchProductsCountParams, { cached, bapiClient, campaignKey }: RpcContext) => Promise<{
26
+ export declare const getProductsCount: (params: FetchProductsCountParams, context: RpcContext) => Promise<{
8
27
  count: number;
9
28
  }>;
10
29
  export declare const fetchAllFiltersForCategory: ({ includedFilters, category }: {
@@ -14,6 +14,25 @@ const sanitizeAttributesForBAPI = ({
14
14
  );
15
15
  };
16
16
  const mapFiltersToSlugs = (filters = []) => filters.map(({ slug }) => slug.toLowerCase());
17
+ export async function resolveCategoryIdFromParams(context, params) {
18
+ let category;
19
+ let categoryId;
20
+ if ("categoryId" in params) {
21
+ categoryId = params.categoryId;
22
+ } else if (params.category && params.category !== "/") {
23
+ category = params.category;
24
+ const result = await context.cached(
25
+ context.bapiClient.categories.getByPath,
26
+ {
27
+ cacheKeyPrefix: `getByPath-categories-${category}`
28
+ }
29
+ )(splitAndRemoveEmpty(category));
30
+ categoryId = result.id;
31
+ } else {
32
+ category = "/";
33
+ }
34
+ return { category, categoryId };
35
+ }
17
36
  export const getProductById = async function getProductById2(options, { bapiClient, cached, campaignKey, withParams }) {
18
37
  return await cached(bapiClient.products.getById, {
19
38
  cacheKeyPrefix: `getById-product-${options.id}`,
@@ -45,26 +64,15 @@ export const getProductsByReferenceKeys = async function getProductsByReferenceK
45
64
  pricePromotionKey: options.pricePromotionKey
46
65
  });
47
66
  };
48
- export const getProductsCount = async function getProductsCount2(params, { cached, bapiClient, campaignKey }) {
67
+ export const getProductsCount = async function getProductsCount2(params, context) {
49
68
  const {
50
69
  where = void 0,
51
70
  includeSoldOut = false,
52
71
  includeSellableForFree = false,
53
72
  orFiltersOperator = void 0
54
73
  } = params;
55
- let category;
56
- let categoryId;
57
- if ("category" in params && !params.category) {
58
- category = "/";
59
- }
60
- if ("categoryId" in params) {
61
- categoryId = params.categoryId;
62
- } else if (category && category !== "/") {
63
- const result = await cached(bapiClient.categories.getByPath, {
64
- cacheKeyPrefix: `getByPath-categories-${category}`
65
- })(splitAndRemoveEmpty(category));
66
- categoryId = result.id;
67
- }
74
+ const { cached, bapiClient, campaignKey } = context;
75
+ const { categoryId } = await resolveCategoryIdFromParams(context, params);
68
76
  const productCount = await cached(bapiClient.products.query, {
69
77
  ttl: 15 * MINUTE,
70
78
  cacheKeyPrefix: "products-query"
@@ -111,20 +119,11 @@ export const getFilters = async function getFilters2(params, context) {
111
119
  includeSellableForFree = false,
112
120
  orFiltersOperator
113
121
  } = params;
114
- let category;
115
- let categoryId;
116
122
  const { cached, bapiClient, campaignKey } = context;
117
- if ("category" in params && !params.category) {
118
- category = "/";
119
- }
120
- if ("categoryId" in params) {
121
- categoryId = params.categoryId;
122
- } else if (category && category !== "/") {
123
- const result = await cached(bapiClient.categories.getByPath, {
124
- cacheKeyPrefix: `getByPath-categories-${category}`
125
- })(splitAndRemoveEmpty(category));
126
- categoryId = result.id;
127
- }
123
+ const { category, categoryId } = await resolveCategoryIdFromParams(
124
+ context,
125
+ params
126
+ );
128
127
  const response = await fetchAllFiltersForCategory(
129
128
  {
130
129
  category: { id: categoryId },
@@ -143,7 +142,7 @@ export const getFilters = async function getFilters2(params, context) {
143
142
  });
144
143
  const [filters, productCount] = await Promise.all([
145
144
  cached(bapiClient.filters.get, {
146
- cacheKeyPrefix: `get-filters-${category}`
145
+ cacheKeyPrefix: `get-filters-${categoryId}`
147
146
  })({
148
147
  where: {
149
148
  categoryId,
@@ -163,7 +162,7 @@ export const getFilters = async function getFilters2(params, context) {
163
162
  }),
164
163
  cached(bapiClient.products.query, {
165
164
  ttl: 15 * MINUTE,
166
- cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : category}`
165
+ cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : categoryId}`
167
166
  })({
168
167
  where: {
169
168
  categoryId,
@@ -202,19 +201,10 @@ export const getProductsByCategory = async function getProductsByCategory2(param
202
201
  orFiltersOperator = void 0
203
202
  } = params;
204
203
  const { cached, bapiClient, campaignKey } = context;
205
- let category;
206
- let categoryId;
207
- if ("category" in params && !params.category) {
208
- category = "/";
209
- }
210
- if ("categoryId" in params) {
211
- categoryId = params.categoryId;
212
- } else if (category && category !== "/") {
213
- const result = await cached(bapiClient.categories.getByPath, {
214
- cacheKeyPrefix: `getByPath-categories-${category}`
215
- })(splitAndRemoveEmpty(category));
216
- categoryId = result.id;
217
- }
204
+ const { category, categoryId } = await resolveCategoryIdFromParams(
205
+ context,
206
+ params
207
+ );
218
208
  const response = await fetchAllFiltersForCategory(
219
209
  {
220
210
  category: {
@@ -236,7 +226,7 @@ export const getProductsByCategory = async function getProductsByCategory2(param
236
226
  {
237
227
  ...cache,
238
228
  ttl: 15 * MINUTE,
239
- cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : category}`
229
+ cacheKeyPrefix: `products-query-category-${category === "/" ? "root" : categoryId}`
240
230
  }
241
231
  )({
242
232
  where: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "7.65.0",
3
+ "version": "7.65.2",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -59,7 +59,7 @@
59
59
  "test:ci": "vitest --run --passWithNoTests --coverage --reporter=default --reporter=junit"
60
60
  },
61
61
  "dependencies": {
62
- "@scayle/storefront-api": "17.6.0",
62
+ "@scayle/storefront-api": "17.7.0",
63
63
  "crypto-js": "^4.2.0",
64
64
  "hookable": "^5.5.3",
65
65
  "jose": "^5.6.3",
@@ -73,17 +73,17 @@
73
73
  "@types/crypto-js": "4.2.2",
74
74
  "@types/node": "20.16.5",
75
75
  "@types/webpack-env": "1.18.5",
76
- "@vitest/coverage-v8": "2.0.5",
76
+ "@vitest/coverage-v8": "2.1.1",
77
77
  "dprint": "0.47.2",
78
78
  "eslint": "9.10.0",
79
79
  "eslint-formatter-gitlab": "5.1.0",
80
- "publint": "0.2.10",
80
+ "publint": "0.2.11",
81
81
  "rimraf": "6.0.1",
82
82
  "ts-node": "10.9.2",
83
83
  "typescript": "5.6.2",
84
84
  "unbuild": "2.0.0",
85
85
  "unstorage": "1.12.0",
86
- "vitest": "2.0.5"
86
+ "vitest": "2.1.1"
87
87
  },
88
88
  "optionalDependencies": {
89
89
  "redis": "4"