@scayle/storefront-core 8.7.0 → 8.8.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/api/customer.d.ts +73 -0
  3. package/dist/api/customer.mjs +59 -0
  4. package/dist/api/oauth.d.ts +62 -12
  5. package/dist/api/oauth.mjs +51 -15
  6. package/dist/cache/cache.d.ts +47 -0
  7. package/dist/cache/cached.d.ts +93 -0
  8. package/dist/cache/cached.mjs +62 -0
  9. package/dist/cache/providers/unstorage.d.ts +64 -0
  10. package/dist/cache/providers/unstorage.mjs +64 -0
  11. package/dist/constants/cache.d.ts +4 -0
  12. package/dist/constants/hash.d.ts +4 -0
  13. package/dist/constants/hash.mjs +1 -0
  14. package/dist/constants/httpStatus.d.ts +12 -2
  15. package/dist/constants/httpStatus.mjs +2 -2
  16. package/dist/constants/product.d.ts +3 -0
  17. package/dist/constants/promotion.d.ts +6 -0
  18. package/dist/constants/sorting.d.ts +6 -0
  19. package/dist/constants/withParameters.d.ts +28 -0
  20. package/dist/errors/errorResponse.d.ts +37 -0
  21. package/dist/errors/errorResponse.mjs +14 -0
  22. package/dist/helpers/filterHelper.mjs +1 -1
  23. package/dist/index.d.ts +48 -0
  24. package/dist/rpc/methods/basket/basket.mjs +9 -4
  25. package/dist/rpc/methods/campaign.d.ts +13 -0
  26. package/dist/rpc/methods/campaign.mjs +23 -0
  27. package/dist/rpc/methods/checkout/checkout.mjs +4 -2
  28. package/dist/rpc/methods/index.d.ts +1 -0
  29. package/dist/rpc/methods/index.mjs +1 -0
  30. package/dist/rpc/methods/products.d.ts +4 -4
  31. package/dist/rpc/methods/products.mjs +19 -7
  32. package/dist/rpc/methods/variants.d.ts +1 -1
  33. package/dist/rpc/methods/variants.mjs +4 -1
  34. package/dist/rpc/methods/wishlist.mjs +7 -3
  35. package/dist/types/api/context.d.ts +3 -0
  36. package/dist/utils/basket.d.ts +23 -0
  37. package/dist/utils/campaign.d.ts +23 -0
  38. package/dist/utils/campaign.mjs +17 -0
  39. package/dist/utils/fetch.d.ts +21 -2
  40. package/dist/utils/hash.d.ts +67 -0
  41. package/dist/utils/hash.mjs +3 -3
  42. package/dist/utils/index.d.ts +1 -0
  43. package/dist/utils/index.mjs +1 -0
  44. package/dist/utils/keys.d.ts +38 -0
  45. package/dist/utils/log.d.ts +77 -8
  46. package/dist/utils/log.mjs +61 -6
  47. package/dist/utils/response.d.ts +8 -3
  48. package/dist/utils/sapi.d.ts +10 -2
  49. package/dist/utils/timeout.d.ts +18 -0
  50. package/dist/utils/user.d.ts +22 -0
  51. package/dist/utils/user.mjs +5 -2
  52. package/package.json +1 -1
@@ -1,43 +1,112 @@
1
+ /**
2
+ * Represents a log level.
3
+ */
1
4
  export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
5
+ /**
6
+ * Represents a single log entry.
7
+ */
2
8
  export interface LogEntry {
3
9
  level: LogLevel;
4
10
  space: string;
5
11
  message: string;
6
12
  data?: object;
7
13
  }
14
+ /**
15
+ * A function that handles log entries.
16
+ *
17
+ * @param entry The log entry to handle.
18
+ */
8
19
  export type LogHandler = (entry: LogEntry) => void;
20
+ /**
21
+ * Options for configuring the logger.
22
+ */
9
23
  export interface LogOptions {
10
24
  handler?: LogHandler;
11
25
  space: string;
12
26
  }
13
27
  /**
14
- * This log provides a way to log messages to a custom log handler (in a serial manner).
15
- * If no handler is provided, it will log to the console.
28
+ * A logger that provides structured logging with namespaces.
29
+ * Supports custom log handlers or if no handler is provided, logging to the console.
16
30
  */
17
31
  export declare class Log {
32
+ /**
33
+ * The default log instance, logging to the console with the `global` namespace.
34
+ */
18
35
  static readonly default: Log;
19
36
  _handler?: LogHandler;
20
37
  _space: string;
21
38
  _type: string;
39
+ /**
40
+ * Creates a new Log instance.
41
+ *
42
+ * @param options Options for the logger.
43
+ */
22
44
  constructor(options?: LogOptions);
45
+ /**
46
+ * Logs a debug message.
47
+ *
48
+ * @param message The debug message.
49
+ * @param data Optional additional data.
50
+ */
23
51
  debug(message: string, data?: any): void;
52
+ /**
53
+ * Logs an informational message.
54
+ *
55
+ * @param message The informational message.
56
+ * @param data Optional additional data.
57
+ */
24
58
  info(message: string, data?: any): void;
59
+ /**
60
+ * Logs a warning message.
61
+ *
62
+ * @param message The warning message.
63
+ * @param data Optional additional data.
64
+ */
25
65
  warn(message: string, data?: any): void;
66
+ /**
67
+ * Logs an error message.
68
+ *
69
+ * @param message The error message or an Error object.
70
+ * @param data Optional additional data.
71
+ */
26
72
  error(message: string | Error, data?: any): void;
73
+ /**
74
+ * Writes a log entry.
75
+ *
76
+ * @param level The log level.
77
+ * @param message The log message.
78
+ * @param data Optional additional data.
79
+ */
27
80
  write(level: LogLevel, message: string, data?: any): void;
28
81
  /**
29
- * Create a new sub-space.
30
- * All spaces are being separated by a dot. E.g. `global.api` and `global.api.getUser`
31
- * @param subSpace The name of the sub-space in camel-case (e.g. `api` and `api.getUser`)
82
+ * Creates a new Log instance with a sub-namespace.
83
+ * Namespaces are separated by dots (e.g., "global.api.getUser").
84
+ *
85
+ * @param subSpace The sub-namespace to add in camel-case (e.g. `api` and `api.getUser`)
86
+ *
87
+ * @returns A new Log instance with the updated namespace.
32
88
  */
33
89
  space(subSpace: string): Log;
34
90
  /**
35
- * Measure the time of a function.
91
+ * Measures the execution time of an asynchronous function and logs a debug message.
92
+ *
93
+ * @param message A message to include in the log output.
94
+ * @param func The asynchronous function to time.
95
+ *
96
+ * @returns A Promise that resolves with the result of the timed function.
36
97
  */
37
98
  time(message: string, func: () => any): Promise<any>;
38
99
  /**
39
- * Attach the log to the request object: `req.$log`.
40
- * This function should be called in a server middleware before storefront-core is initialized.
100
+ * Attaches the log instance to a request object.
101
+ * This is typically used in server middleware.
102
+ *
103
+ * @param req The request object.
104
+ * @param log The Log instance to attach.
105
+ *
106
+ * @example
107
+ * Log.attachToRequest(req, new Log({ space: 'myModule' }))
108
+ *
109
+ * @deprecated
41
110
  */
42
111
  static attachToRequest(req: any, log: Log): void;
43
112
  }
@@ -1,4 +1,7 @@
1
1
  export class Log {
2
+ /**
3
+ * The default log instance, logging to the console with the `global` namespace.
4
+ */
2
5
  static default = new this({
3
6
  space: "global",
4
7
  handler: consoleLogHandler
@@ -6,19 +9,48 @@ export class Log {
6
9
  _handler;
7
10
  _space;
8
11
  _type = "log";
12
+ /**
13
+ * Creates a new Log instance.
14
+ *
15
+ * @param options Options for the logger.
16
+ */
9
17
  constructor(options) {
10
18
  this._handler = options?.handler || Log.default._handler;
11
19
  this._space = options?.space || Log.default._space;
12
20
  }
21
+ /**
22
+ * Logs a debug message.
23
+ *
24
+ * @param message The debug message.
25
+ * @param data Optional additional data.
26
+ */
13
27
  debug(message, data) {
14
28
  this.write("debug", message, data);
15
29
  }
30
+ /**
31
+ * Logs an informational message.
32
+ *
33
+ * @param message The informational message.
34
+ * @param data Optional additional data.
35
+ */
16
36
  info(message, data) {
17
37
  this.write("info", message, data);
18
38
  }
39
+ /**
40
+ * Logs a warning message.
41
+ *
42
+ * @param message The warning message.
43
+ * @param data Optional additional data.
44
+ */
19
45
  warn(message, data) {
20
46
  this.write("warn", message, data);
21
47
  }
48
+ /**
49
+ * Logs an error message.
50
+ *
51
+ * @param message The error message or an Error object.
52
+ * @param data Optional additional data.
53
+ */
22
54
  error(message, data) {
23
55
  const error = message instanceof Error ? message : null;
24
56
  if (error) {
@@ -29,6 +61,13 @@ export class Log {
29
61
  }
30
62
  this.write("error", error?.message ?? message, data);
31
63
  }
64
+ /**
65
+ * Writes a log entry.
66
+ *
67
+ * @param level The log level.
68
+ * @param message The log message.
69
+ * @param data Optional additional data.
70
+ */
32
71
  write(level, message, data) {
33
72
  if (typeof data !== "undefined") {
34
73
  data = typeof data !== "object" ? { data } : data;
@@ -41,9 +80,12 @@ export class Log {
41
80
  }
42
81
  }
43
82
  /**
44
- * Create a new sub-space.
45
- * All spaces are being separated by a dot. E.g. `global.api` and `global.api.getUser`
46
- * @param subSpace The name of the sub-space in camel-case (e.g. `api` and `api.getUser`)
83
+ * Creates a new Log instance with a sub-namespace.
84
+ * Namespaces are separated by dots (e.g., "global.api.getUser").
85
+ *
86
+ * @param subSpace The sub-namespace to add in camel-case (e.g. `api` and `api.getUser`)
87
+ *
88
+ * @returns A new Log instance with the updated namespace.
47
89
  */
48
90
  space(subSpace) {
49
91
  return new Log({
@@ -52,7 +94,12 @@ export class Log {
52
94
  });
53
95
  }
54
96
  /**
55
- * Measure the time of a function.
97
+ * Measures the execution time of an asynchronous function and logs a debug message.
98
+ *
99
+ * @param message A message to include in the log output.
100
+ * @param func The asynchronous function to time.
101
+ *
102
+ * @returns A Promise that resolves with the result of the timed function.
56
103
  */
57
104
  async time(message, func) {
58
105
  const start = Date.now();
@@ -62,8 +109,16 @@ export class Log {
62
109
  return result;
63
110
  }
64
111
  /**
65
- * Attach the log to the request object: `req.$log`.
66
- * This function should be called in a server middleware before storefront-core is initialized.
112
+ * Attaches the log instance to a request object.
113
+ * This is typically used in server middleware.
114
+ *
115
+ * @param req The request object.
116
+ * @param log The Log instance to attach.
117
+ *
118
+ * @example
119
+ * Log.attachToRequest(req, new Log({ space: 'myModule' }))
120
+ *
121
+ * @deprecated
67
122
  */
68
123
  static attachToRequest(req, log) {
69
124
  req.$log = log;
@@ -1,6 +1,11 @@
1
1
  /**
2
- * Unwrap a possible response into its body
3
- * @param res
4
- * @returns unwrapped response
2
+ * Unwraps a value that might be a `Response` object or a direct value. If it's a `Response`,
3
+ * it parses the JSON body and returns it; otherwise, it returns the value directly.
4
+ *
5
+ * @template T The expected type of the unwrapped value.
6
+ *
7
+ * @param res The value to unwrap, which can be a `Promise`, `Response`, or a direct value.
8
+ *
9
+ * @returns A Promise resolving to the unwrapped value.
5
10
  */
6
11
  export declare function unwrap<T>(res: Promise<Response | T> | Response | T): Promise<T>;
@@ -1,5 +1,13 @@
1
1
  /**
2
- * Wrap a SAPI API function, catching any SAPI FetchErrors thrown
3
- * and translating them to a Response return
2
+ * Wraps a SAPI API function, catching SAPI FetchErrors and converting them to a structured Response.
3
+ * This allows handling SAPI errors in a consistent way, returning a JSON response with status information.
4
+ *
5
+ * @template T The type of the SAPI API function.
6
+ *
7
+ * @param func The SAPI API function to wrap.
8
+ *
9
+ * @returns A wrapped function that returns a Promise resolving to either the original function's result or a JSON Response representing the error.
10
+ *
11
+ * @throws Re-throws any error that is not an instance of FetchError.
4
12
  */
5
13
  export declare function mapSAPIFetchErrorToResponse<T extends (...args: any) => any>(func: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | Response>;
@@ -1,2 +1,20 @@
1
+ /**
2
+ * Pauses execution for a specified number of milliseconds.
3
+ *
4
+ * @param ms The number of milliseconds to wait.
5
+ *
6
+ * @returns A Promise that resolves after the specified delay.
7
+ */
1
8
  export declare const wait: (ms: number) => Promise<unknown>;
9
+ /**
10
+ * Wraps a promise with a timeout. Rejects the promise if it doesn't resolve within the given time.
11
+ *
12
+ * @template T The type of the promise's resolved value.
13
+ * @param ms The timeout duration in milliseconds.
14
+ * @param promise The promise to wrap.
15
+ *
16
+ * @returns A new promise that resolves or rejects based on the original promise or the timeout.
17
+ *
18
+ * @throws {Error} If the promise times out. The error message will be "timeout".
19
+ */
2
20
  export declare const timeout: <T>(ms: number, promise: Promise<T>) => Promise<T>;
@@ -1,4 +1,15 @@
1
1
  import type { BasketWithOptions, RpcContext } from '../types';
2
+ /**
3
+ * Merges two baskets. Adds the items from the `fromBasketKey` basket to the `toBasketKey` basket
4
+ * and then clears the `fromBasketKey` basket. Handles existing items by adding quantities.
5
+ *
6
+ * @param fromBasketKey The key of the source basket.
7
+ * @param toBasketKey The key of the destination basket.
8
+ * @param withOptions Options for retrieving basket data (including custom and order data).
9
+ * @param context The RPC context.
10
+ *
11
+ * @returns The merged basket.
12
+ */
2
13
  export declare const mergeBaskets: (fromBasketKey: string, toBasketKey: string, withOptions: BasketWithOptions & {
3
14
  orderCustomData?: Record<string, unknown>;
4
15
  }, context: RpcContext) => Promise<{
@@ -13,4 +24,15 @@ export declare const mergeBaskets: (fromBasketKey: string, toBasketKey: string,
13
24
  readonly basket: import("@scayle/storefront-api").BasketResponseData<import("@scayle/storefront-api").Product, import("@scayle/storefront-api").Variant>;
14
25
  readonly errors: import("@scayle/storefront-api").AddOrUpdateItemError[];
15
26
  } | undefined>;
27
+ /**
28
+ * Merges two wishlists. Adds the items from the `fromWishlistKey` wishlist to the `toWishlistKey` wishlist
29
+ * and then clears the `fromWishlistKey` wishlist.
30
+ *
31
+ * @param fromWishlistKey The key of the source wishlist.
32
+ * @param toWishlistKey The key of the destination wishlist.
33
+ * @param withOptions Options for retrieving wishlist data.
34
+ * @param context The RPC context.
35
+ *
36
+ * @returns Nothing (resolves when the merge operation is complete).
37
+ */
16
38
  export declare const mergeWishlists: (fromWishlistKey: string, toWishlistKey: string, withOptions: BasketWithOptions, context: RpcContext) => Promise<void>;
@@ -1,6 +1,8 @@
1
1
  import { ExistingItemHandling } from "@scayle/storefront-api";
2
+ import { getCampaignKey } from "../rpc/methods/index.mjs";
2
3
  export const mergeBaskets = async (fromBasketKey, toBasketKey, withOptions, context) => {
3
- const { sapiClient, campaignKey } = context;
4
+ const { sapiClient } = context;
5
+ const campaignKey = await getCampaignKey(context);
4
6
  const fromOriginBasket = await sapiClient.basket.get(fromBasketKey, {
5
7
  with: withOptions,
6
8
  campaignKey,
@@ -43,7 +45,8 @@ export const mergeBaskets = async (fromBasketKey, toBasketKey, withOptions, cont
43
45
  }
44
46
  };
45
47
  export const mergeWishlists = async (fromWishlistKey, toWishlistKey, withOptions, context) => {
46
- const { sapiClient, campaignKey } = context;
48
+ const { sapiClient } = context;
49
+ const campaignKey = await getCampaignKey(context);
47
50
  const oldWishlist = await sapiClient.wishlist.get(fromWishlistKey, {
48
51
  with: withOptions,
49
52
  campaignKey
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "8.7.0",
3
+ "version": "8.8.0",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",