@commercengine/storefront-sdk 0.2.0 → 0.3.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/dist/index.d.ts +26 -8
- package/dist/index.js +83 -13
- package/dist/lib/auth.d.ts +65 -91
- package/dist/lib/auth.js +290 -318
- package/dist/lib/cart.d.ts +4 -4
- package/dist/lib/cart.js +119 -94
- package/dist/lib/catalog.js +73 -59
- package/dist/lib/client.d.ts +1 -0
- package/dist/lib/client.js +36 -8
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -57,38 +57,56 @@ export declare class StorefrontSDK {
|
|
|
57
57
|
* The client storage instance used by this SDK
|
|
58
58
|
*/
|
|
59
59
|
private readonly clientStorage;
|
|
60
|
+
/**
|
|
61
|
+
* Whether the SDK has been initialized
|
|
62
|
+
*/
|
|
63
|
+
private isInitialized;
|
|
60
64
|
/**
|
|
61
65
|
* Create a new StorefrontSDK instance
|
|
62
66
|
*
|
|
63
67
|
* @param options - Configuration options for the SDK
|
|
64
68
|
*/
|
|
65
69
|
constructor(options: StorefrontSDKOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Set up storage with automatic fallback for non-browser environments
|
|
72
|
+
*/
|
|
73
|
+
private setupStorage;
|
|
74
|
+
/**
|
|
75
|
+
* Initialize the SDK
|
|
76
|
+
* This method should be called before using any SDK features
|
|
77
|
+
* It ensures tokens are available and valid
|
|
78
|
+
* @returns Promise that resolves when initialization is complete
|
|
79
|
+
*/
|
|
80
|
+
init(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Get the current user ID if available
|
|
83
|
+
*/
|
|
84
|
+
getUserId(): Promise<string | null>;
|
|
66
85
|
/**
|
|
67
86
|
* Get the current cart ID if one is stored
|
|
68
87
|
*
|
|
69
88
|
* @returns The current cart ID or null if none exists
|
|
70
89
|
*/
|
|
71
|
-
getCartId(): string | null
|
|
90
|
+
getCartId(): Promise<string | null>;
|
|
72
91
|
/**
|
|
73
92
|
* Set a specific cart ID to use
|
|
74
93
|
*
|
|
75
94
|
* @param cartId - The cart ID to use
|
|
76
95
|
*/
|
|
77
|
-
setCartId(cartId: string): void
|
|
96
|
+
setCartId(cartId: string): Promise<void>;
|
|
78
97
|
/**
|
|
79
98
|
* Clear the current cart ID
|
|
80
99
|
*/
|
|
81
|
-
clearCartId(): void
|
|
100
|
+
clearCartId(): Promise<void>;
|
|
82
101
|
/**
|
|
83
102
|
* Set the authentication token for all clients
|
|
84
|
-
*
|
|
85
|
-
* @param token - The authentication token
|
|
103
|
+
* Also stores the token in the configured storage
|
|
86
104
|
*/
|
|
87
|
-
setToken(token: string): void
|
|
105
|
+
setToken(token: string): Promise<void>;
|
|
88
106
|
/**
|
|
89
|
-
* Clear the authentication token from all clients
|
|
107
|
+
* Clear the authentication token from all clients and storage
|
|
90
108
|
*/
|
|
91
|
-
clearToken(): void
|
|
109
|
+
clearToken(): Promise<void>;
|
|
92
110
|
/**
|
|
93
111
|
* Set the API key for all clients
|
|
94
112
|
*
|
package/dist/index.js
CHANGED
|
@@ -20,8 +20,12 @@ class StorefrontSDK {
|
|
|
20
20
|
* @param options - Configuration options for the SDK
|
|
21
21
|
*/
|
|
22
22
|
constructor(options) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Whether the SDK has been initialized
|
|
25
|
+
*/
|
|
26
|
+
this.isInitialized = false;
|
|
27
|
+
// Set up client storage with fallback handling
|
|
28
|
+
this.clientStorage = this.setupStorage(options.tokenStorage);
|
|
25
29
|
// Convert options to internal config format
|
|
26
30
|
const config = {
|
|
27
31
|
storeId: options.storeId,
|
|
@@ -37,45 +41,111 @@ class StorefrontSDK {
|
|
|
37
41
|
// Set client storage for cart management
|
|
38
42
|
this.cart.setClientStorage(this.clientStorage);
|
|
39
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Set up storage with automatic fallback for non-browser environments
|
|
46
|
+
*/
|
|
47
|
+
setupStorage(requestedStorage) {
|
|
48
|
+
if (!requestedStorage) {
|
|
49
|
+
return new auth_1.MemoryTokenStorage();
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
// Test if the requested storage is available
|
|
53
|
+
requestedStorage.getItem("test");
|
|
54
|
+
// Check if it's a ClientStorage implementation
|
|
55
|
+
if ("getCartId" in requestedStorage &&
|
|
56
|
+
"setCartId" in requestedStorage &&
|
|
57
|
+
"clearCartId" in requestedStorage) {
|
|
58
|
+
return requestedStorage;
|
|
59
|
+
}
|
|
60
|
+
// If it's just a TokenStorage, wrap it in a MemoryTokenStorage for cart functionality
|
|
61
|
+
console.warn("Provided storage does not implement ClientStorage interface, wrapping with MemoryTokenStorage for cart functionality");
|
|
62
|
+
return new auth_1.MemoryTokenStorage();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
// If storage access fails, fallback to memory storage
|
|
66
|
+
console.warn("Requested storage is not available in this environment, falling back to memory storage");
|
|
67
|
+
return new auth_1.MemoryTokenStorage();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Initialize the SDK
|
|
72
|
+
* This method should be called before using any SDK features
|
|
73
|
+
* It ensures tokens are available and valid
|
|
74
|
+
* @returns Promise that resolves when initialization is complete
|
|
75
|
+
*/
|
|
76
|
+
async init() {
|
|
77
|
+
if (this.isInitialized) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
// Try to get existing tokens from storage
|
|
82
|
+
const accessToken = await this.clientStorage.getItem("access_token");
|
|
83
|
+
const userData = await this.clientStorage.getItem("user_data");
|
|
84
|
+
if (accessToken && userData) {
|
|
85
|
+
// Set the token for all clients
|
|
86
|
+
await this.setToken(accessToken);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// If no token exists or no user data, get a new anonymous token
|
|
90
|
+
const authResponse = await this.auth.getAnonymousToken();
|
|
91
|
+
await this.setToken(authResponse.access_token);
|
|
92
|
+
// Store user data
|
|
93
|
+
await this.clientStorage.setItem("user_data", JSON.stringify(authResponse.user));
|
|
94
|
+
}
|
|
95
|
+
this.isInitialized = true;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error("Failed to initialize SDK:", error);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the current user ID if available
|
|
104
|
+
*/
|
|
105
|
+
async getUserId() {
|
|
106
|
+
return this.clientStorage.getItem("user_id");
|
|
107
|
+
}
|
|
40
108
|
/**
|
|
41
109
|
* Get the current cart ID if one is stored
|
|
42
110
|
*
|
|
43
111
|
* @returns The current cart ID or null if none exists
|
|
44
112
|
*/
|
|
45
|
-
getCartId() {
|
|
46
|
-
return this.
|
|
113
|
+
async getCartId() {
|
|
114
|
+
return this.clientStorage.getCartId();
|
|
47
115
|
}
|
|
48
116
|
/**
|
|
49
117
|
* Set a specific cart ID to use
|
|
50
118
|
*
|
|
51
119
|
* @param cartId - The cart ID to use
|
|
52
120
|
*/
|
|
53
|
-
setCartId(cartId) {
|
|
54
|
-
this.
|
|
121
|
+
async setCartId(cartId) {
|
|
122
|
+
await this.clientStorage.setCartId(cartId);
|
|
55
123
|
}
|
|
56
124
|
/**
|
|
57
125
|
* Clear the current cart ID
|
|
58
126
|
*/
|
|
59
|
-
clearCartId() {
|
|
60
|
-
this.
|
|
127
|
+
async clearCartId() {
|
|
128
|
+
await this.clientStorage.clearCartId();
|
|
61
129
|
}
|
|
62
130
|
/**
|
|
63
131
|
* Set the authentication token for all clients
|
|
64
|
-
*
|
|
65
|
-
* @param token - The authentication token
|
|
132
|
+
* Also stores the token in the configured storage
|
|
66
133
|
*/
|
|
67
|
-
setToken(token) {
|
|
134
|
+
async setToken(token) {
|
|
68
135
|
this.catalog.setToken(token);
|
|
69
136
|
this.cart.setToken(token);
|
|
70
137
|
this.auth.setToken(token);
|
|
138
|
+
await this.clientStorage.setItem("access_token", token);
|
|
71
139
|
}
|
|
72
140
|
/**
|
|
73
|
-
* Clear the authentication token from all clients
|
|
141
|
+
* Clear the authentication token from all clients and storage
|
|
74
142
|
*/
|
|
75
|
-
clearToken() {
|
|
143
|
+
async clearToken() {
|
|
76
144
|
this.catalog.clearToken();
|
|
77
145
|
this.cart.clearToken();
|
|
78
146
|
this.auth.clearToken();
|
|
147
|
+
await this.clientStorage.removeItem("access_token");
|
|
148
|
+
await this.clientStorage.removeItem("refresh_token");
|
|
79
149
|
}
|
|
80
150
|
/**
|
|
81
151
|
* Set the API key for all clients
|
package/dist/lib/auth.d.ts
CHANGED
|
@@ -4,28 +4,34 @@ import type { components } from "../types/storefront";
|
|
|
4
4
|
* Client for interacting with authentication endpoints
|
|
5
5
|
*/
|
|
6
6
|
export declare class AuthClient extends StorefrontAPIClient {
|
|
7
|
-
private tokenStorage;
|
|
7
|
+
private readonly tokenStorage;
|
|
8
8
|
private autoRefreshTimer;
|
|
9
|
-
constructor(config: StorefrontAPIConfig, tokenStorage
|
|
10
|
-
setToken(token: string): void
|
|
11
|
-
protected setRefreshToken(token: string): void
|
|
12
|
-
clearToken(): void
|
|
9
|
+
constructor(config: StorefrontAPIConfig, tokenStorage: TokenStorage);
|
|
10
|
+
setToken(token: string): Promise<void>;
|
|
11
|
+
protected setRefreshToken(token: string): Promise<void>;
|
|
12
|
+
clearToken(): Promise<void>;
|
|
13
13
|
private setupAutoRefresh;
|
|
14
14
|
private clearAutoRefresh;
|
|
15
15
|
private handleTokenRefresh;
|
|
16
16
|
private getTokenExpiry;
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
* Store auth data including tokens and user info
|
|
19
|
+
*/
|
|
20
|
+
private storeAuthData;
|
|
21
|
+
/**
|
|
22
|
+
* Get stored user data
|
|
23
|
+
* @returns Promise that resolves to the stored user data or null if none exists
|
|
24
|
+
*/
|
|
25
|
+
getStoredUser(): Promise<components["schemas"]["User"] | components["schemas"]["AnonymousUser"] | null>;
|
|
26
|
+
/**
|
|
27
|
+
* Get anonymous token for guest users
|
|
22
28
|
*/
|
|
23
29
|
getAnonymousToken(options?: {
|
|
24
30
|
apiKey?: string;
|
|
25
31
|
}): Promise<{
|
|
26
|
-
user: components["schemas"]["AnonymousUser"];
|
|
27
32
|
access_token: string;
|
|
28
33
|
refresh_token: string;
|
|
34
|
+
user: components["schemas"]["AnonymousUser"];
|
|
29
35
|
}>;
|
|
30
36
|
/**
|
|
31
37
|
* Login with phone number
|
|
@@ -53,18 +59,16 @@ export declare class AuthClient extends StorefrontAPIClient {
|
|
|
53
59
|
/**
|
|
54
60
|
* Login with password
|
|
55
61
|
*
|
|
56
|
-
* @param
|
|
62
|
+
* @param credentials - Login credentials
|
|
57
63
|
* @returns Promise with user info and tokens
|
|
58
64
|
*/
|
|
59
|
-
loginWithPassword(
|
|
60
|
-
email
|
|
61
|
-
phone?: string;
|
|
62
|
-
country_code?: string;
|
|
65
|
+
loginWithPassword(credentials: {
|
|
66
|
+
email: string;
|
|
63
67
|
password: string;
|
|
64
68
|
}): Promise<{
|
|
65
|
-
user: components["schemas"]["User"];
|
|
66
69
|
access_token: string;
|
|
67
70
|
refresh_token: string;
|
|
71
|
+
user: components["schemas"]["User"];
|
|
68
72
|
}>;
|
|
69
73
|
/**
|
|
70
74
|
* Verify OTP
|
|
@@ -115,10 +119,7 @@ export declare class AuthClient extends StorefrontAPIClient {
|
|
|
115
119
|
refresh_token: string;
|
|
116
120
|
}>;
|
|
117
121
|
/**
|
|
118
|
-
* Refresh token
|
|
119
|
-
*
|
|
120
|
-
* @param refreshToken - Refresh token
|
|
121
|
-
* @returns Promise with new tokens
|
|
122
|
+
* Refresh the access token using a refresh token
|
|
122
123
|
*/
|
|
123
124
|
refreshToken(refreshToken: string): Promise<{
|
|
124
125
|
access_token: string;
|
|
@@ -148,19 +149,21 @@ export declare class AuthClient extends StorefrontAPIClient {
|
|
|
148
149
|
executeWithTokenRefresh<T>(requestFn: () => Promise<T>): Promise<T>;
|
|
149
150
|
}
|
|
150
151
|
/**
|
|
151
|
-
* Interface for
|
|
152
|
+
* Interface for token storage implementations
|
|
152
153
|
*/
|
|
153
|
-
export interface
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
154
|
+
export interface TokenStorage {
|
|
155
|
+
getItem(key: string): Promise<string | null>;
|
|
156
|
+
setItem(key: string, value: string): Promise<void>;
|
|
157
|
+
removeItem(key: string): Promise<void>;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Interface for client storage implementations
|
|
161
|
+
*/
|
|
162
|
+
export interface ClientStorage extends TokenStorage {
|
|
163
|
+
getCartId(): Promise<string | null>;
|
|
164
|
+
setCartId(cartId: string): Promise<void>;
|
|
165
|
+
clearCartId(): Promise<void>;
|
|
162
166
|
}
|
|
163
|
-
export type TokenStorage = ClientStorage;
|
|
164
167
|
/**
|
|
165
168
|
* Default in-memory implementation of client storage
|
|
166
169
|
*/
|
|
@@ -168,14 +171,13 @@ export declare class MemoryTokenStorage implements ClientStorage {
|
|
|
168
171
|
private accessToken;
|
|
169
172
|
private refreshToken;
|
|
170
173
|
private cartId;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
clearCartId(): void;
|
|
174
|
+
private userData;
|
|
175
|
+
getItem(key: string): Promise<string | null>;
|
|
176
|
+
setItem(key: string, value: string): Promise<void>;
|
|
177
|
+
removeItem(key: string): Promise<void>;
|
|
178
|
+
getCartId(): Promise<string | null>;
|
|
179
|
+
setCartId(cartId: string): Promise<void>;
|
|
180
|
+
clearCartId(): Promise<void>;
|
|
179
181
|
}
|
|
180
182
|
/**
|
|
181
183
|
* Browser storage implementation using localStorage
|
|
@@ -184,15 +186,14 @@ export declare class BrowserTokenStorage implements ClientStorage {
|
|
|
184
186
|
private accessTokenKey;
|
|
185
187
|
private refreshTokenKey;
|
|
186
188
|
private cartIdKey;
|
|
189
|
+
private userDataKey;
|
|
187
190
|
constructor(prefix?: string);
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
setCartId(cartId: string): void;
|
|
195
|
-
clearCartId(): void;
|
|
191
|
+
getItem(key: string): Promise<string | null>;
|
|
192
|
+
setItem(key: string, value: string): Promise<void>;
|
|
193
|
+
removeItem(key: string): Promise<void>;
|
|
194
|
+
getCartId(): Promise<string | null>;
|
|
195
|
+
setCartId(cartId: string): Promise<void>;
|
|
196
|
+
clearCartId(): Promise<void>;
|
|
196
197
|
}
|
|
197
198
|
/**
|
|
198
199
|
* Cookie-based token storage for browser or server environments
|
|
@@ -201,6 +202,7 @@ export declare class CookieTokenStorage implements ClientStorage {
|
|
|
201
202
|
private accessTokenKey;
|
|
202
203
|
private refreshTokenKey;
|
|
203
204
|
private cartIdKey;
|
|
205
|
+
private userDataKey;
|
|
204
206
|
private cookieOptions;
|
|
205
207
|
constructor(prefix?: string, cookieOptions?: {
|
|
206
208
|
path: string;
|
|
@@ -210,43 +212,31 @@ export declare class CookieTokenStorage implements ClientStorage {
|
|
|
210
212
|
maxAge: number;
|
|
211
213
|
});
|
|
212
214
|
/**
|
|
213
|
-
* Get
|
|
214
|
-
* Works in both browser and Next.js server components
|
|
215
|
-
*/
|
|
216
|
-
getAccessToken(): string | null;
|
|
217
|
-
/**
|
|
218
|
-
* Set access token in cookies
|
|
219
|
-
* Works in browser environment
|
|
220
|
-
*/
|
|
221
|
-
setAccessToken(token: string): void;
|
|
222
|
-
/**
|
|
223
|
-
* Get refresh token from cookies
|
|
215
|
+
* Get value from cookies
|
|
224
216
|
* Works in both browser and Next.js server components
|
|
225
217
|
*/
|
|
226
|
-
|
|
218
|
+
getItem(key: string): Promise<string | null>;
|
|
227
219
|
/**
|
|
228
|
-
* Set
|
|
220
|
+
* Set value in cookies
|
|
229
221
|
* Works in browser environment
|
|
230
222
|
*/
|
|
231
|
-
|
|
223
|
+
setItem(key: string, value: string): Promise<void>;
|
|
232
224
|
/**
|
|
233
|
-
*
|
|
225
|
+
* Remove value from cookies
|
|
234
226
|
*/
|
|
235
|
-
|
|
227
|
+
removeItem(key: string): Promise<void>;
|
|
236
228
|
/**
|
|
237
229
|
* Get cart ID from cookies
|
|
238
|
-
* Works in both browser and Next.js server components
|
|
239
230
|
*/
|
|
240
|
-
getCartId(): string | null
|
|
231
|
+
getCartId(): Promise<string | null>;
|
|
241
232
|
/**
|
|
242
233
|
* Set cart ID in cookies
|
|
243
|
-
* Works in browser environment
|
|
244
234
|
*/
|
|
245
|
-
setCartId(cartId: string): void
|
|
235
|
+
setCartId(cartId: string): Promise<void>;
|
|
246
236
|
/**
|
|
247
237
|
* Clear cart ID from cookies
|
|
248
238
|
*/
|
|
249
|
-
clearCartId(): void
|
|
239
|
+
clearCartId(): Promise<void>;
|
|
250
240
|
}
|
|
251
241
|
/**
|
|
252
242
|
* Next.js specific cookie storage implementation
|
|
@@ -256,40 +246,24 @@ export declare class NextCookieTokenStorage implements ClientStorage {
|
|
|
256
246
|
private accessTokenKey;
|
|
257
247
|
private refreshTokenKey;
|
|
258
248
|
private cartIdKey;
|
|
249
|
+
private userDataKey;
|
|
259
250
|
private cookieStore;
|
|
260
251
|
constructor(cookieStore: any, prefix?: string);
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
getAccessToken(): string | null;
|
|
265
|
-
/**
|
|
266
|
-
* Set access token in Next.js cookies
|
|
267
|
-
*/
|
|
268
|
-
setAccessToken(token: string): void;
|
|
269
|
-
/**
|
|
270
|
-
* Get refresh token from Next.js cookies
|
|
271
|
-
*/
|
|
272
|
-
getRefreshToken(): string | null;
|
|
273
|
-
/**
|
|
274
|
-
* Set refresh token in Next.js cookies
|
|
275
|
-
*/
|
|
276
|
-
setRefreshToken(token: string): void;
|
|
277
|
-
/**
|
|
278
|
-
* Clear all tokens from Next.js cookies
|
|
279
|
-
*/
|
|
280
|
-
clearTokens(): void;
|
|
252
|
+
getItem(key: string): Promise<string | null>;
|
|
253
|
+
setItem(key: string, value: string): Promise<void>;
|
|
254
|
+
removeItem(key: string): Promise<void>;
|
|
281
255
|
/**
|
|
282
256
|
* Get cart ID from Next.js cookies
|
|
283
257
|
*/
|
|
284
|
-
getCartId(): string | null
|
|
258
|
+
getCartId(): Promise<string | null>;
|
|
285
259
|
/**
|
|
286
260
|
* Set cart ID in Next.js cookies
|
|
287
261
|
*/
|
|
288
|
-
setCartId(cartId: string): void
|
|
262
|
+
setCartId(cartId: string): Promise<void>;
|
|
289
263
|
/**
|
|
290
264
|
* Clear cart ID from Next.js cookies
|
|
291
265
|
*/
|
|
292
|
-
clearCartId(): void
|
|
266
|
+
clearCartId(): Promise<void>;
|
|
293
267
|
}
|
|
294
268
|
/**
|
|
295
269
|
* Helper to create a token storage instance based on environment
|