@commercengine/storefront-sdk 0.2.1 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import { StorefrontAPIClient, Environment } from "./lib/client";
2
2
  import { CatalogClient } from "./lib/catalog";
3
3
  import { CartClient } from "./lib/cart";
4
- import { AuthClient, TokenStorage } from "./lib/auth";
4
+ import { AuthClient } from "./lib/auth";
5
+ import { OrderClient } from "./lib/order";
6
+ import { ShippingClient } from "./lib/shipping";
7
+ import { HelpersClient } from "./lib/helper";
8
+ import { CustomerClient } from "./lib/customer";
9
+ import { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, createDefaultAuthMiddleware, createAuthMiddleware, type AuthMiddlewareConfig } from "./lib/middleware";
10
+ import { type UserInfo } from "./lib/jwt-utils";
5
11
  /**
6
12
  * SDK initialization options
7
13
  */
@@ -19,7 +25,7 @@ export interface StorefrontSDKOptions {
19
25
  */
20
26
  baseUrl?: string;
21
27
  /**
22
- * Optional authentication token
28
+ * Optional authentication token (for manual token management)
23
29
  */
24
30
  token?: string;
25
31
  /**
@@ -32,10 +38,18 @@ export interface StorefrontSDKOptions {
32
38
  */
33
39
  timeout?: number;
34
40
  /**
35
- * Optional token storage strategy
36
- * Defaults to MemoryTokenStorage if not provided
41
+ * Optional token storage for automatic token management
42
+ * If provided, enables automatic token refresh and management
37
43
  */
38
44
  tokenStorage?: TokenStorage;
45
+ /**
46
+ * Callback when tokens are updated (login/refresh)
47
+ */
48
+ onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
49
+ /**
50
+ * Callback when tokens are cleared (logout/error)
51
+ */
52
+ onTokensCleared?: () => void;
39
53
  }
40
54
  /**
41
55
  * Main SDK class for the Storefront API
@@ -54,41 +68,35 @@ export declare class StorefrontSDK {
54
68
  */
55
69
  readonly auth: AuthClient;
56
70
  /**
57
- * The client storage instance used by this SDK
71
+ * Client for customer-related endpoints
58
72
  */
59
- private readonly clientStorage;
73
+ readonly customer: CustomerClient;
60
74
  /**
61
- * Create a new StorefrontSDK instance
62
- *
63
- * @param options - Configuration options for the SDK
75
+ * Client for helper-related endpoints
64
76
  */
65
- constructor(options: StorefrontSDKOptions);
77
+ readonly helpers: HelpersClient;
66
78
  /**
67
- * Get the current cart ID if one is stored
68
- *
69
- * @returns The current cart ID or null if none exists
79
+ * Client for shipping-related endpoints
70
80
  */
71
- getCartId(): string | null;
81
+ readonly shipping: ShippingClient;
72
82
  /**
73
- * Set a specific cart ID to use
74
- *
75
- * @param cartId - The cart ID to use
83
+ * Client for order-related endpoints
76
84
  */
77
- setCartId(cartId: string): void;
85
+ readonly order: OrderClient;
78
86
  /**
79
- * Clear the current cart ID
87
+ * Create a new StorefrontSDK instance
88
+ *
89
+ * @param options - Configuration options for the SDK
80
90
  */
81
- clearCartId(): void;
91
+ constructor(options: StorefrontSDKOptions);
82
92
  /**
83
93
  * Set the authentication token for all clients
84
- *
85
- * @param token - The authentication token
86
94
  */
87
- setToken(token: string): void;
95
+ setToken(token: string): Promise<void>;
88
96
  /**
89
97
  * Clear the authentication token from all clients
90
98
  */
91
- clearToken(): void;
99
+ clearToken(): Promise<void>;
92
100
  /**
93
101
  * Set the API key for all clients
94
102
  *
@@ -99,9 +107,51 @@ export declare class StorefrontSDK {
99
107
  * Clear the API key from all clients
100
108
  */
101
109
  clearApiKey(): void;
110
+ /**
111
+ * Get the current access token if using token storage
112
+ */
113
+ getAccessToken(): Promise<string | null>;
114
+ /**
115
+ * Get user information from the current access token
116
+ *
117
+ * @returns User information extracted from JWT token, or null if no token or invalid token
118
+ */
119
+ getUserInfo(): Promise<UserInfo | null>;
120
+ /**
121
+ * Get the current user ID from the access token
122
+ *
123
+ * @returns User ID (ulid) or null if no token or invalid token
124
+ */
125
+ getUserId(): Promise<string | null>;
126
+ /**
127
+ * Check if the current user is logged in (not anonymous)
128
+ *
129
+ * @returns True if user is logged in, false if anonymous or no token
130
+ */
131
+ isLoggedIn(): Promise<boolean>;
132
+ /**
133
+ * Check if the current user is anonymous
134
+ *
135
+ * @returns True if user is anonymous or no token, false if logged in
136
+ */
137
+ isAnonymous(): Promise<boolean>;
138
+ /**
139
+ * Get the customer ID from the current access token
140
+ *
141
+ * @returns Customer ID or null if no token, invalid token, or user has no customer ID
142
+ */
143
+ getCustomerId(): Promise<string | null>;
144
+ /**
145
+ * Get the customer group ID from the current access token
146
+ *
147
+ * @returns Customer group ID or null if no token, invalid token, or user has no customer group
148
+ */
149
+ getCustomerGroupId(): Promise<string | null>;
102
150
  }
103
151
  export default StorefrontSDK;
104
- export { StorefrontAPIClient, CatalogClient, CartClient, AuthClient };
152
+ export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient };
105
153
  export { Environment };
106
- export { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, CookieTokenStorage, NextCookieTokenStorage, createTokenStorage, } from "./lib/auth";
154
+ export { TokenStorage, MemoryTokenStorage, BrowserTokenStorage, createDefaultAuthMiddleware, createAuthMiddleware, type AuthMiddlewareConfig };
155
+ export { extractUserInfoFromToken, getUserIdFromToken, isUserLoggedIn, isUserAnonymous, isTokenExpired, type UserInfo, type JwtPayload } from "./lib/jwt-utils";
107
156
  export type { components, operations, paths } from "./types/storefront";
157
+ export type * from "./types/storefront-api-types";
package/dist/index.js CHANGED
@@ -1,27 +1,51 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTokenStorage = exports.NextCookieTokenStorage = exports.CookieTokenStorage = exports.BrowserTokenStorage = exports.MemoryTokenStorage = exports.Environment = exports.AuthClient = exports.CartClient = exports.CatalogClient = exports.StorefrontAPIClient = exports.StorefrontSDK = void 0;
4
- const client_1 = require("./lib/client");
5
- Object.defineProperty(exports, "StorefrontAPIClient", { enumerable: true, get: function () { return client_1.StorefrontAPIClient; } });
6
- Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return client_1.Environment; } });
7
- const catalog_1 = require("./lib/catalog");
8
- Object.defineProperty(exports, "CatalogClient", { enumerable: true, get: function () { return catalog_1.CatalogClient; } });
9
- const cart_1 = require("./lib/cart");
10
- Object.defineProperty(exports, "CartClient", { enumerable: true, get: function () { return cart_1.CartClient; } });
11
- const auth_1 = require("./lib/auth");
12
- Object.defineProperty(exports, "AuthClient", { enumerable: true, get: function () { return auth_1.AuthClient; } });
1
+ import { StorefrontAPIClient, Environment, } from "./lib/client";
2
+ import { CatalogClient } from "./lib/catalog";
3
+ import { CartClient } from "./lib/cart";
4
+ import { AuthClient } from "./lib/auth";
5
+ import { OrderClient } from "./lib/order";
6
+ import { ShippingClient } from "./lib/shipping";
7
+ import { HelpersClient } from "./lib/helper";
8
+ import { CustomerClient } from "./lib/customer";
9
+ import { MemoryTokenStorage, BrowserTokenStorage, createDefaultAuthMiddleware, createAuthMiddleware } from "./lib/middleware";
10
+ import { extractUserInfoFromToken, getUserIdFromToken, isUserLoggedIn, isUserAnonymous } from "./lib/jwt-utils";
13
11
  /**
14
12
  * Main SDK class for the Storefront API
15
13
  */
16
- class StorefrontSDK {
14
+ export class StorefrontSDK {
15
+ /**
16
+ * Client for catalog-related endpoints (products, categories, etc.)
17
+ */
18
+ catalog;
19
+ /**
20
+ * Client for cart-related endpoints
21
+ */
22
+ cart;
23
+ /**
24
+ * Client for authentication-related endpoints
25
+ */
26
+ auth;
27
+ /**
28
+ * Client for customer-related endpoints
29
+ */
30
+ customer;
31
+ /**
32
+ * Client for helper-related endpoints
33
+ */
34
+ helpers;
35
+ /**
36
+ * Client for shipping-related endpoints
37
+ */
38
+ shipping;
39
+ /**
40
+ * Client for order-related endpoints
41
+ */
42
+ order;
17
43
  /**
18
44
  * Create a new StorefrontSDK instance
19
45
  *
20
46
  * @param options - Configuration options for the SDK
21
47
  */
22
48
  constructor(options) {
23
- // Set up client storage
24
- this.clientStorage = options.tokenStorage || new auth_1.MemoryTokenStorage();
25
49
  // Convert options to internal config format
26
50
  const config = {
27
51
  storeId: options.storeId,
@@ -30,52 +54,41 @@ class StorefrontSDK {
30
54
  token: options.token,
31
55
  apiKey: options.apiKey,
32
56
  timeout: options.timeout,
57
+ tokenStorage: options.tokenStorage,
58
+ onTokensUpdated: options.onTokensUpdated,
59
+ onTokensCleared: options.onTokensCleared,
33
60
  };
34
- this.catalog = new catalog_1.CatalogClient(config);
35
- this.cart = new cart_1.CartClient(config);
36
- this.auth = new auth_1.AuthClient(config, this.clientStorage);
37
- // Set client storage for cart management
38
- this.cart.setClientStorage(this.clientStorage);
39
- }
40
- /**
41
- * Get the current cart ID if one is stored
42
- *
43
- * @returns The current cart ID or null if none exists
44
- */
45
- getCartId() {
46
- return this.cart.getCartId();
47
- }
48
- /**
49
- * Set a specific cart ID to use
50
- *
51
- * @param cartId - The cart ID to use
52
- */
53
- setCartId(cartId) {
54
- this.cart.setCartId(cartId);
55
- }
56
- /**
57
- * Clear the current cart ID
58
- */
59
- clearCartId() {
60
- this.cart.clearCartId();
61
+ this.catalog = new CatalogClient(config);
62
+ this.cart = new CartClient(config);
63
+ this.auth = new AuthClient(config);
64
+ this.customer = new CustomerClient(config);
65
+ this.helpers = new HelpersClient(config);
66
+ this.shipping = new ShippingClient(config);
67
+ this.order = new OrderClient(config);
61
68
  }
62
69
  /**
63
70
  * Set the authentication token for all clients
64
- *
65
- * @param token - The authentication token
66
71
  */
67
- setToken(token) {
68
- this.catalog.setToken(token);
69
- this.cart.setToken(token);
70
- this.auth.setToken(token);
72
+ async setToken(token) {
73
+ await this.catalog.setToken(token);
74
+ await this.cart.setToken(token);
75
+ await this.auth.setToken(token);
76
+ await this.customer.setToken(token);
77
+ await this.helpers.setToken(token);
78
+ await this.shipping.setToken(token);
79
+ await this.order.setToken(token);
71
80
  }
72
81
  /**
73
82
  * Clear the authentication token from all clients
74
83
  */
75
- clearToken() {
76
- this.catalog.clearToken();
77
- this.cart.clearToken();
78
- this.auth.clearToken();
84
+ async clearToken() {
85
+ await this.catalog.clearToken();
86
+ await this.cart.clearToken();
87
+ await this.auth.clearToken();
88
+ await this.customer.clearToken();
89
+ await this.helpers.clearToken();
90
+ await this.shipping.clearToken();
91
+ await this.order.clearToken();
79
92
  }
80
93
  /**
81
94
  * Set the API key for all clients
@@ -86,6 +99,10 @@ class StorefrontSDK {
86
99
  this.catalog.setApiKey(apiKey);
87
100
  this.cart.setApiKey(apiKey);
88
101
  this.auth.setApiKey(apiKey);
102
+ this.customer.setApiKey(apiKey);
103
+ this.helpers.setApiKey(apiKey);
104
+ this.shipping.setApiKey(apiKey);
105
+ this.order.setApiKey(apiKey);
89
106
  }
90
107
  /**
91
108
  * Clear the API key from all clients
@@ -94,15 +111,87 @@ class StorefrontSDK {
94
111
  this.catalog.clearApiKey();
95
112
  this.cart.clearApiKey();
96
113
  this.auth.clearApiKey();
114
+ this.customer.clearApiKey();
115
+ this.helpers.clearApiKey();
116
+ this.shipping.clearApiKey();
117
+ this.order.clearApiKey();
118
+ }
119
+ /**
120
+ * Get the current access token if using token storage
121
+ */
122
+ async getAccessToken() {
123
+ return await this.auth.getAuthorizationHeader().then(header => header.startsWith('Bearer ') ? header.substring(7) : null);
124
+ }
125
+ /**
126
+ * Get user information from the current access token
127
+ *
128
+ * @returns User information extracted from JWT token, or null if no token or invalid token
129
+ */
130
+ async getUserInfo() {
131
+ const token = await this.getAccessToken();
132
+ if (!token)
133
+ return null;
134
+ return extractUserInfoFromToken(token);
135
+ }
136
+ /**
137
+ * Get the current user ID from the access token
138
+ *
139
+ * @returns User ID (ulid) or null if no token or invalid token
140
+ */
141
+ async getUserId() {
142
+ const token = await this.getAccessToken();
143
+ if (!token)
144
+ return null;
145
+ return getUserIdFromToken(token);
146
+ }
147
+ /**
148
+ * Check if the current user is logged in (not anonymous)
149
+ *
150
+ * @returns True if user is logged in, false if anonymous or no token
151
+ */
152
+ async isLoggedIn() {
153
+ const token = await this.getAccessToken();
154
+ if (!token)
155
+ return false;
156
+ return isUserLoggedIn(token);
157
+ }
158
+ /**
159
+ * Check if the current user is anonymous
160
+ *
161
+ * @returns True if user is anonymous or no token, false if logged in
162
+ */
163
+ async isAnonymous() {
164
+ const token = await this.getAccessToken();
165
+ if (!token)
166
+ return true;
167
+ return isUserAnonymous(token);
168
+ }
169
+ /**
170
+ * Get the customer ID from the current access token
171
+ *
172
+ * @returns Customer ID or null if no token, invalid token, or user has no customer ID
173
+ */
174
+ async getCustomerId() {
175
+ const userInfo = await this.getUserInfo();
176
+ return userInfo?.customerId || null;
177
+ }
178
+ /**
179
+ * Get the customer group ID from the current access token
180
+ *
181
+ * @returns Customer group ID or null if no token, invalid token, or user has no customer group
182
+ */
183
+ async getCustomerGroupId() {
184
+ const userInfo = await this.getUserInfo();
185
+ return userInfo?.customerGroupId || null;
97
186
  }
98
187
  }
99
- exports.StorefrontSDK = StorefrontSDK;
100
188
  // Export the main SDK class
101
- exports.default = StorefrontSDK;
102
- // Export token storage types
103
- var auth_2 = require("./lib/auth");
104
- Object.defineProperty(exports, "MemoryTokenStorage", { enumerable: true, get: function () { return auth_2.MemoryTokenStorage; } });
105
- Object.defineProperty(exports, "BrowserTokenStorage", { enumerable: true, get: function () { return auth_2.BrowserTokenStorage; } });
106
- Object.defineProperty(exports, "CookieTokenStorage", { enumerable: true, get: function () { return auth_2.CookieTokenStorage; } });
107
- Object.defineProperty(exports, "NextCookieTokenStorage", { enumerable: true, get: function () { return auth_2.NextCookieTokenStorage; } });
108
- Object.defineProperty(exports, "createTokenStorage", { enumerable: true, get: function () { return auth_2.createTokenStorage; } });
189
+ export default StorefrontSDK;
190
+ // Export individual clients for advanced usage
191
+ export { StorefrontAPIClient, AuthClient, CartClient, CatalogClient, CustomerClient, HelpersClient, ShippingClient, OrderClient };
192
+ // Export environment enum
193
+ export { Environment };
194
+ // Export token storage types and middleware
195
+ export { MemoryTokenStorage, BrowserTokenStorage, createDefaultAuthMiddleware, createAuthMiddleware };
196
+ // Export JWT utilities
197
+ export { extractUserInfoFromToken, getUserIdFromToken, isUserLoggedIn, isUserAnonymous, isTokenExpired } from "./lib/jwt-utils";