@scayle/storefront-core 8.53.3 → 8.54.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,18 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 8.54.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 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.
8
+
9
+ ### Patch Changes
10
+
11
+ **Dependencies**
12
+
13
+ - Updated dependency to @scayle/storefront-api@18.18.1
14
+ - Updated dependency to @scayle/unstorage-scayle-kv-driver@2.0.9
15
+
3
16
  ## 8.53.3
4
17
 
5
18
  ## 8.53.2
@@ -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.54.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.54.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,8 +48,8 @@
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",
52
- "@scayle/unstorage-scayle-kv-driver": "2.0.9"
51
+ "@scayle/unstorage-scayle-kv-driver": "2.0.9",
52
+ "@scayle/storefront-api": "18.18.1"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/crypto-js": "4.2.2",
@@ -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"