@scayle/storefront-core 8.29.0 → 8.30.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,51 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 8.30.1
4
+
5
+ ### Patch Changes
6
+
7
+ **Dependencies**
8
+
9
+ - Updated dependency to @scayle/unstorage-scayle-kv-driver@1.0.2
10
+
11
+ ## 8.30.0
12
+
13
+ ### Minor Changes
14
+
15
+ - Added `defineRpcHandler` utility function that enhances RPC handlers with type-safe metadata for improved registration and invocation of RPC handlers. While defining RPC handlers without this utility remains supported, its usage is recommended and may become mandatory in a future major release.
16
+
17
+ Existing RPC handlers can be easily migrated to use `defineRpcHandler` by passing the existing handler to `defineRpcHandler`.
18
+
19
+ ```ts
20
+ export const existingHandlerWithoutParams = async (_context: RpcContext) => {
21
+ return 'existing handler'
22
+ }
23
+
24
+ export const existingHandlerWithParams = async (
25
+ { name }: { name: string },
26
+ _context: RpcContext,
27
+ ) => {
28
+ return name
29
+ }
30
+
31
+ // will become
32
+
33
+ export const existingHandlerWithoutParams = defineRpcHandler(
34
+ async (_context: RpcContext) => {
35
+ return 'existing handler'
36
+ },
37
+ )
38
+ export const existingHandlerWithParams = defineRpcHandler(
39
+ async ({ name }: { name: string }, _context: RpcContext) => {
40
+ return name
41
+ },
42
+ )
43
+ ```
44
+
45
+ ### Patch Changes
46
+
47
+ - Removed unused `Awaited` and `PromiseReturnType` types.
48
+
3
49
  ## 8.29.0
4
50
 
5
51
  ### Minor Changes
@@ -36,7 +36,7 @@ export const getCheckoutToken = async function getCheckoutToken2(jwtPayload = {}
36
36
  carrier,
37
37
  basketId: context.basketKey,
38
38
  campaignKey
39
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.29.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
39
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.30.1"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
40
40
  return {
41
41
  accessToken: refreshedAccessToken,
42
42
  checkoutJwt
@@ -2,16 +2,24 @@ import type { RpcContext } from './context';
2
2
  /**
3
3
  * Type for an RPC handler that accepts parameters.
4
4
  *
5
+ * @note In the upcoming major release `rpcType` property will be mandatory.
6
+ *
5
7
  * @template Params The type of the parameters object.
6
8
  * @template ResponseType The type of the response.
7
9
  */
8
- export type ParamRpcHandler<Params extends Record<string, any>, ResponseType> = (params: Params, context: RpcContext) => Promise<(ResponseType extends undefined ? void : ResponseType) | Response> | ResponseType | Response;
10
+ export type ParamRpcHandler<Params extends Record<string, any>, ResponseType> = ((params: Params, context: RpcContext) => Promise<(ResponseType extends undefined ? void : ResponseType) | Response> | ResponseType | Response) & {
11
+ rpcType?: 'WithParam';
12
+ };
9
13
  /**
10
14
  * Type for an RPC handler that does not accept parameters.
11
15
  *
16
+ * @note In the upcoming major release `rpcType` property will be mandatory.
17
+ *
12
18
  * @template ResponseType The type of the response.
13
19
  */
14
- export type NoParamRpcHandler<ResponseType> = (context: RpcContext) => Promise<ResponseType | Response> | ResponseType | Response;
20
+ export type NoParamRpcHandler<ResponseType> = ((context: RpcContext) => Promise<ResponseType | Response> | ResponseType | Response) & {
21
+ rpcType?: 'NoParam';
22
+ };
15
23
  /**
16
24
  * Generic RPC handler type. Handles both cases with and without parameters.
17
25
  *
@@ -1,4 +1,3 @@
1
- export * from './promises';
2
1
  export * from './user';
3
2
  export * from './breadcrumb';
4
3
  export * from './api/rpc';
@@ -1,4 +1,3 @@
1
- export * from "./promises.mjs";
2
1
  export * from "./user.mjs";
3
2
  export * from "./breadcrumb.mjs";
4
3
  export * from "./api/rpc.mjs";
@@ -3,3 +3,4 @@ export * from './log';
3
3
  export * from './fetch';
4
4
  export * from './basket';
5
5
  export * from './campaign';
6
+ export { defineRpcHandler } from './rpc';
@@ -3,3 +3,4 @@ export * from "./log.mjs";
3
3
  export * from "./fetch.mjs";
4
4
  export * from "./basket.mjs";
5
5
  export * from "./campaign.mjs";
6
+ export { defineRpcHandler } from "./rpc.mjs";
@@ -0,0 +1,27 @@
1
+ import type { RpcMethods, RpcMethodName } from '..';
2
+ import type { RpcContext, RpcHandler } from '../types';
3
+ type RpcType = 'NoParam' | 'WithParam';
4
+ /**
5
+ * Defines an RPC method handler.
6
+ *
7
+ * @param handler - The RPC handler function.
8
+ * @returns The handler itself, with correct types enforced and internal properties set.
9
+ */
10
+ export declare function defineRpcHandler<Params extends Record<string, unknown>, Result = unknown>(handler: (params: Params, context: RpcContext) => Result | Promise<Result>): RpcHandler<Params, Result>;
11
+ /**
12
+ * Defines an RPC method handler.
13
+ *
14
+ * @param handler - The RPC handler function.
15
+ * @returns The handler itself, with correct types enforced and internal properties set.
16
+ */
17
+ export declare function defineRpcHandler<Result = unknown>(handler: (context: RpcContext) => Result | Promise<Result>): RpcHandler<Result>;
18
+ /**
19
+ * Defines an RPC method handler.
20
+ *
21
+ * @param handler The RPC handler function.
22
+ * @returns The handler itself, with correct types enforced and internal properties set.
23
+ */
24
+ export declare function defineRpcHandler<T extends RpcMethods[RpcMethodName]>(handler: T): T & {
25
+ rpcType: RpcType;
26
+ };
27
+ export {};
@@ -0,0 +1,5 @@
1
+ export function defineRpcHandler(handler) {
2
+ const rpcType = handler.length === 1 ? "NoParam" : "WithParam";
3
+ const handlerWithType = Object.assign(handler, { rpcType });
4
+ return handlerWithType;
5
+ }
@@ -10,4 +10,4 @@
10
10
  *
11
11
  * @throws Re-throws any error that is not an instance of FetchError.
12
12
  */
13
- export declare function mapSAPIFetchErrorToResponse<T extends (...args: any) => any>(func: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | Response>;
13
+ export declare function mapSAPIFetchErrorToResponse<T extends (...args: any[]) => any>(func: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | Response>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "8.29.0",
3
+ "version": "8.30.1",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -65,7 +65,7 @@
65
65
  },
66
66
  "dependencies": {
67
67
  "@scayle/storefront-api": "18.9.0",
68
- "@scayle/unstorage-scayle-kv-driver": "1.0.1",
68
+ "@scayle/unstorage-scayle-kv-driver": "1.0.2",
69
69
  "crypto-js": "^4.2.0",
70
70
  "hookable": "^5.5.3",
71
71
  "jose": "^6.0.8",
@@ -75,11 +75,11 @@
75
75
  "utility-types": "^3.11.0"
76
76
  },
77
77
  "devDependencies": {
78
- "@scayle/eslint-config-storefront": "4.5.5",
78
+ "@scayle/eslint-config-storefront": "4.5.7",
79
79
  "@types/crypto-js": "4.2.2",
80
80
  "@types/node": "22.15.32",
81
81
  "@types/webpack-env": "1.18.8",
82
- "@vitest/coverage-v8": "3.2.3",
82
+ "@vitest/coverage-v8": "3.2.4",
83
83
  "dprint": "0.50.0",
84
84
  "eslint-formatter-gitlab": "6.0.1",
85
85
  "eslint": "9.29.0",
@@ -89,7 +89,7 @@
89
89
  "typescript": "5.8.3",
90
90
  "unbuild": "3.5.0",
91
91
  "unstorage": "1.16.0",
92
- "vitest": "3.2.3"
92
+ "vitest": "3.2.4"
93
93
  },
94
94
  "volta": {
95
95
  "node": "22.16.0"
@@ -1,12 +0,0 @@
1
- /**
2
- * Extracts the type resolved by a promise or promise-like value.
3
- *
4
- * @template T The input type, which can be a promise-like value.
5
- */
6
- export type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;
7
- /**
8
- * Extracts the return type of a promise-returning function, after the promise resolves.
9
- *
10
- * @template T The promise-returning function type.
11
- */
12
- export type PromiseReturnType<T extends (...args: any) => Promise<any>> = Awaited<ReturnType<T>>;
File without changes