@scayle/storefront-core 8.53.3 → 8.55.0

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,30 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 8.55.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Updated the type of the `removeItemFromBasket` RPC to accurately reflect that error responses can be returned if removing an item from the basket fails (these error responses were already being returned before; only the type was updated). Additionally, the `clearBasket` RPC now removes items sequentially to avoid conflicts between basket updates and returns an error response if any items in the basket could not be removed.
8
+
9
+ ### Patch Changes
10
+
11
+ **Dependencies**
12
+
13
+ - Updated dependency to @scayle/storefront-api@18.19.0
14
+
15
+ ## 8.54.0
16
+
17
+ ### Minor Changes
18
+
19
+ - The `getCheckoutToken` RPC was updated to refresh the access token only when it was about to expire within 30 minutes. This change reduced unnecessary access token generation and minimized the risk of using outdated or revoked tokens.
20
+
21
+ ### Patch Changes
22
+
23
+ **Dependencies**
24
+
25
+ - Updated dependency to @scayle/storefront-api@18.18.1
26
+ - Updated dependency to @scayle/unstorage-scayle-kv-driver@2.0.9
27
+
3
28
  ## 8.53.3
4
29
 
5
30
  ## 8.53.2
@@ -86,12 +86,12 @@ export declare const removeItemFromBasket: RpcHandler<{
86
86
  basket: BasketResponseData<Product, Variant>;
87
87
  }>;
88
88
  /**
89
- * Clears the basket, removing all items.
89
+ * Clears the basket, by removing all items sequentially.
90
90
  *
91
91
  * @param context The RPC context.
92
92
  *
93
93
  * @returns True if the basket was cleared successfully. It will return
94
- * an `ErrorResponse` alternatively iIf an error occurs during basket clearing.
94
+ * an `ErrorResponse` alternatively if an error occurs during basket clearing.
95
95
  */
96
96
  export declare const clearBasket: RpcHandler<boolean>;
97
97
  /**
@@ -213,7 +213,7 @@ export const removeItemFromBasket = defineRpcHandler(async (options, context) =>
213
213
  const campaignKey = await context.callRpc?.("getCampaignKey");
214
214
  const _orderCustomData = await context.callRpc?.("getOrderCustomData");
215
215
  const resolvedWith = getWithParams(options, context);
216
- const basket = await sapiClient.basket.deleteItem(
216
+ const response = await sapiClient.basket.deleteItem(
217
217
  basketKey,
218
218
  options.itemKey,
219
219
  {
@@ -227,7 +227,14 @@ export const removeItemFromBasket = defineRpcHandler(async (options, context) =>
227
227
  customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
228
228
  }
229
229
  );
230
- return { basket };
230
+ if ("code" in response) {
231
+ return new ErrorResponse(
232
+ response.code,
233
+ SAPI_ERROR_NAME,
234
+ response.message
235
+ );
236
+ }
237
+ return { basket: response };
231
238
  });
232
239
  export const clearBasket = defineRpcHandler(
233
240
  async (context) => {
@@ -236,12 +243,15 @@ export const clearBasket = defineRpcHandler(
236
243
  return getBasketResponse;
237
244
  }
238
245
  const { basket } = await unwrap(getBasketResponse);
239
- await Promise.all(
240
- basket.items.map(async (item) => {
241
- await removeItemFromBasket({ itemKey: item.key }, context);
242
- })
243
- );
244
- context.log.debug("The basket has been cleared");
246
+ for (const item of basket.items) {
247
+ const response = await removeItemFromBasket(
248
+ { itemKey: item.key },
249
+ context
250
+ );
251
+ if (response instanceof ErrorResponse) {
252
+ return response;
253
+ }
254
+ }
245
255
  return true;
246
256
  }
247
257
  );
@@ -34,7 +34,7 @@ interface CheckoutJwtPayload {
34
34
  carrier?: string;
35
35
  }
36
36
  /**
37
- * Generates a checkout token, which includes a refreshed access token and
37
+ * Generates a checkout token, which includes a access token token that is valid for at least 30 minutes and
38
38
  * a JWT containing checkout information.
39
39
  *
40
40
  * The generated JWT is signed with a secret and includes information from the
@@ -1,9 +1,10 @@
1
- import { SignJWT } from "jose";
1
+ import { SignJWT, decodeJwt } from "jose";
2
2
  import { hasSession } from "../../../types/index.mjs";
3
3
  import { ErrorResponse } from "../../../errors/index.mjs";
4
4
  import { HttpStatusCode, HttpStatusMessage } from "../../../constants/index.mjs";
5
5
  import { getAccessToken } from "../user.mjs";
6
6
  import { defineRpcHandler } from "../../../utils/rpc.mjs";
7
+ const ACCESS_TOKEN_REFRESH_THRESHOLD_IN_MILLISECONDS = 30 * 60 * 1e3;
7
8
  export const getCheckoutToken = defineRpcHandler(async (jwtPayload = {}, context) => {
8
9
  if (!hasSession(context)) {
9
10
  return new ErrorResponse(
@@ -14,8 +15,10 @@ export const getCheckoutToken = defineRpcHandler(async (jwtPayload = {}, context
14
15
  }
15
16
  let refreshedAccessToken;
16
17
  if (context.accessToken) {
18
+ const accessTokenPayload = decodeJwt(context.accessToken);
19
+ const shouldRefresh = !!accessTokenPayload.exp && accessTokenPayload.exp * 1e3 - Date.now() <= ACCESS_TOKEN_REFRESH_THRESHOLD_IN_MILLISECONDS;
17
20
  refreshedAccessToken = await getAccessToken(
18
- { forceTokenRefresh: true },
21
+ { forceTokenRefresh: shouldRefresh },
19
22
  context
20
23
  );
21
24
  if (refreshedAccessToken instanceof Response) {
@@ -37,7 +40,7 @@ export const getCheckoutToken = defineRpcHandler(async (jwtPayload = {}, context
37
40
  ...customData,
38
41
  ...orderCustomData
39
42
  }
40
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.53.3"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
43
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.55.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
41
44
  return {
42
45
  accessToken: refreshedAccessToken,
43
46
  checkoutJwt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "8.53.3",
3
+ "version": "8.55.0",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "ufo": "^1.5.3",
49
49
  "uncrypto": "^0.1.3",
50
50
  "utility-types": "^3.11.0",
51
- "@scayle/storefront-api": "18.18.1",
51
+ "@scayle/storefront-api": "18.19.0",
52
52
  "@scayle/unstorage-scayle-kv-driver": "2.0.9"
53
53
  },
54
54
  "devDependencies": {
@@ -57,7 +57,7 @@
57
57
  "@types/webpack-env": "1.18.8",
58
58
  "@vitest/coverage-v8": "4.0.14",
59
59
  "dprint": "0.50.2",
60
- "eslint-formatter-gitlab": "6.0.1",
60
+ "eslint-formatter-gitlab": "7.0.0",
61
61
  "eslint": "9.39.1",
62
62
  "fishery": "2.3.1",
63
63
  "publint": "0.3.15",
@@ -66,8 +66,8 @@
66
66
  "unbuild": "3.6.1",
67
67
  "unstorage": "1.17.3",
68
68
  "vitest": "4.0.14",
69
- "@scayle/eslint-config-storefront": "4.7.14",
70
- "@scayle/vitest-config-storefront": "1.0.0"
69
+ "@scayle/vitest-config-storefront": "1.0.0",
70
+ "@scayle/eslint-config-storefront": "4.7.15"
71
71
  },
72
72
  "volta": {
73
73
  "node": "22.21.1"