@commercengine/storefront-sdk 0.3.11 → 0.3.12

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 (2) hide show
  1. package/README.md +354 -53
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Storefront SDK
2
2
 
3
- TypeScript SDK for the CommerceEngine Storefront API.
3
+ A powerful, type-safe TypeScript SDK for the CommerceEngine Storefront API. Built with modern JavaScript patterns, automatic token management, and comprehensive error handling.
4
+
5
+ **✨ Key Features:**
6
+ - **100% Type Safe**: Every API endpoint is fully typed with TypeScript
7
+ - **Automatic Token Management**: Built-in refresh token logic for seamless authentication
8
+ - **Universal Compatibility**: Works in browser, Node.js, and hybrid rendering environments
9
+ - **Production Ready**: Implements all API best practices out of the box
10
+ - **Zero Configuration**: Works with sensible defaults, extensive customization available
4
11
 
5
12
  ## Installation
6
13
 
@@ -8,104 +15,398 @@ TypeScript SDK for the CommerceEngine Storefront API.
8
15
  npm install @commercengine/storefront-sdk
9
16
  ```
10
17
 
11
- Or with yarn:
12
-
13
18
  ```bash
14
19
  yarn add @commercengine/storefront-sdk
15
20
  ```
16
21
 
17
- Or with pnpm:
18
-
19
22
  ```bash
20
23
  pnpm add @commercengine/storefront-sdk
21
24
  ```
22
25
 
23
- ## Usage
24
-
25
- ### Initializing the SDK
26
-
27
- You can initialize the SDK in just a few lines:
26
+ ## Quick Start
28
27
 
29
28
  ```typescript
30
29
  import StorefrontSDK, { Environment } from "@commercengine/storefront-sdk";
31
30
 
32
- // Initialize with environment and store ID
31
+ // Basic initialization
32
+ const sdk = new StorefrontSDK({
33
+ storeId: "your-store-id",
34
+ environment: Environment.Staging,
35
+ apiKey: "your-api-key", // Required for authentication
36
+ });
37
+
38
+ // Get started with anonymous authentication
39
+ const { data, error } = await sdk.auth.getAnonymousToken();
40
+ if (error) {
41
+ console.log(error)
42
+ } else {
43
+ accessToken = data.accessToken
44
+ }
45
+ ```
46
+
47
+ ## Configuration Options
48
+
49
+ The SDK supports extensive configuration to fit your needs:
50
+
51
+ ### Basic Configuration
52
+
53
+ ```typescript
33
54
  const sdk = new StorefrontSDK({
55
+ // Required
34
56
  storeId: "your-store-id",
57
+
58
+ // Environment (optional, defaults to Production)
35
59
  environment: Environment.Staging, // or Environment.Production
60
+
61
+ // API key for authentication (required for auth endpoints)
62
+ apiKey: "your-api-key",
36
63
  });
64
+ ```
65
+
66
+ ### Advanced Configuration
37
67
 
38
- // Or with a custom base URL (if needed)
39
- const customSdk = new StorefrontSDK({
68
+ ```typescript
69
+ const sdk = new StorefrontSDK({
40
70
  storeId: "your-store-id",
41
- baseUrl: "https://custom-api.example.com",
71
+ environment: Environment.Production,
72
+
73
+ // Token Management
74
+ accessToken: "initial-access-token", // Initial access token
75
+ refreshToken: "initial-refresh-token", // Initial refresh token (for automatic mode)
76
+
77
+ // Custom base URL (optional, overrides environment) - Not needed for most implementations
78
+ baseUrl: "https://your-custom-api.example.com",
79
+
80
+ // Request Configuration
81
+ timeout: 10000, // Request timeout in milliseconds
82
+
83
+ // Default Headers (auto applied to all applicable requests)
84
+ defaultHeaders: {
85
+ customer_group_id: "01JHS28V83KDWTRBXXJQRTEKA0", // For pricing and promotions
86
+ },
87
+
88
+ // Debug and Logging
89
+ debug: true, // Enable detailed request/response logging - Uses console.log by default
90
+ logger: console.log, // Custom logger function - structure your logs any way you want. Also helpful to pipe logs into external services
42
91
  });
43
92
  ```
44
93
 
45
- ### Authentication
94
+ ## Token Management
95
+
96
+ The SDK offers two approaches to token management:
97
+
98
+ ### 1. Manual Token Management (Simple)
46
99
 
47
- The SDK supports various authentication methods:
100
+ For basic use cases where you manage tokens yourself:
48
101
 
49
102
  ```typescript
50
- // Anonymous authentication
51
- const { access_token, refresh_token, user } =
52
- await sdk.auth.getAnonymousToken();
103
+ const sdk = new StorefrontSDK({
104
+ storeId: "your-store-id",
105
+ apiKey: "your-api-key",
106
+ });
53
107
 
54
- // Phone login
55
- const { otp_token, otp_action } = await sdk.auth.loginWithPhone("9876543210");
108
+ // Login and set tokens manually
109
+ const { data: loginData } = await sdk.auth.loginWithPassword({
110
+ email: "user@example.com",
111
+ password: "password",
112
+ });
56
113
 
57
- // Email login
58
- const { otp_token, otp_action } = await sdk.auth.loginWithEmail(
59
- "user@example.com"
60
- );
114
+ if (loginData) {
115
+ await sdk.setTokens(loginData.access_token);
116
+ }
117
+ ```
61
118
 
62
- // Verify OTP
63
- const { user, access_token, refresh_token } = await sdk.auth.verifyOtp(
64
- "123456",
65
- otp_token,
66
- "login"
67
- );
119
+ ### 2. Automatic Token Management (Recommended)
68
120
 
69
- // Password login
70
- const { user, access_token, refresh_token } = await sdk.auth.loginWithPassword({
71
- email: "user@example.com",
72
- password: "your-password",
121
+ For production applications with automatic token refresh and persistence:
122
+
123
+ ```typescript
124
+ import StorefrontSDK, { BrowserTokenStorage } from "@commercengine/storefront-sdk";
125
+
126
+ const sdk = new StorefrontSDK({
127
+ storeId: "your-store-id",
128
+ apiKey: "your-api-key",
129
+
130
+ // Enable automatic token management
131
+ tokenStorage: new BrowserTokenStorage("myapp_"), // Prefix for localStorage keys
132
+
133
+ // Optional callbacks
134
+ onTokensUpdated: (accessToken, refreshToken) => {
135
+ console.log("Tokens updated!");
136
+ },
137
+ onTokensCleared: () => {
138
+ console.log("User logged out");
139
+ },
140
+ });
141
+ ```
142
+
143
+ ### Token Storage Options
144
+
145
+ Choose the storage method that fits your environment:
146
+
147
+ #### Browser localStorage
148
+ ```typescript
149
+ import { BrowserTokenStorage } from "@commercengine/storefront-sdk";
150
+
151
+ const tokenStorage = new BrowserTokenStorage("myapp_"); // Optional prefix
152
+ ```
153
+
154
+ #### Cookies (for SSR or cross-tab sync)
155
+ ```typescript
156
+ import { CookieTokenStorage } from "@commercengine/storefront-sdk";
157
+
158
+ const tokenStorage = new CookieTokenStorage({
159
+ prefix: "myapp_",
160
+ maxAge: 7 * 24 * 60 * 60, // 7 days
161
+ secure: true,
162
+ sameSite: "Lax",
73
163
  });
164
+ ```
165
+
166
+ #### Memory (for server-side or temporary storage)
167
+ ```typescript
168
+ import { MemoryTokenStorage } from "@commercengine/storefront-sdk";
169
+
170
+ const tokenStorage = new MemoryTokenStorage();
171
+ ```
172
+
173
+ #### Custom Storage
174
+ ```typescript
175
+ class CustomTokenStorage implements TokenStorage {
176
+ async getAccessToken(): Promise<string | null> {
177
+ // Your implementation
178
+ }
179
+
180
+ async setAccessToken(token: string): Promise<void> {
181
+ // Your implementation
182
+ }
183
+
184
+ // ... implement other required methods
185
+ }
186
+ ```
74
187
 
75
- // Registration
76
- const { user, access_token, refresh_token } = await sdk.auth.registerWithPhone({
188
+ ## Authentication
189
+
190
+ ### Anonymous Authentication
191
+ ```typescript
192
+ // Get anonymous token for guest users
193
+ const { data } = await sdk.auth.getAnonymousToken();
194
+ if (data) {
195
+ await sdk.setTokens(data.access_token, data.refresh_token);
196
+ }
197
+ ```
198
+
199
+ ### Phone/Email Authentication
200
+ ```typescript
201
+ // Step 1: Initiate login
202
+ const { data: otpData } = await sdk.auth.loginWithPhone({
77
203
  phone: "9876543210",
204
+ country_code: "+91",
205
+ register_if_not_exists: true,
206
+ });
207
+
208
+ // Step 2: Verify OTP
209
+ if (otpData) {
210
+ const { data: authData } = await sdk.auth.verifyOtp({
211
+ otp: "123456",
212
+ otp_token: otpData.otp_token,
213
+ otp_action: otpData.otp_action,
214
+ });
215
+
216
+ if (authData) {
217
+ await sdk.setTokens(authData.access_token, authData.refresh_token);
218
+ }
219
+ }
220
+ ```
221
+
222
+ ### Password Authentication
223
+ ```typescript
224
+ const { data } = await sdk.auth.loginWithPassword({
78
225
  email: "user@example.com",
79
- first_name: "John",
80
- last_name: "Doe",
226
+ password: "your-password",
227
+ });
228
+
229
+ if (data) {
230
+ await sdk.setTokens(data.access_token, data.refresh_token);
231
+ }
232
+ ```
233
+
234
+ ## API Clients & Complete Type Safety
235
+
236
+ The SDK provides **complete access to every CommerceEngine API endpoint** with full TypeScript support. All clients are automatically generated from the OpenAPI specification, ensuring 100% accuracy and type safety.
237
+
238
+ ### Available Clients
239
+
240
+ ```typescript
241
+ // Authentication & User Management
242
+ sdk.auth.* // Login, registration, OTP, password management
243
+ sdk.customer.* // Customer profiles, addresses, preferences
244
+
245
+ // E-commerce Core
246
+ sdk.catalog.* // Products, categories, search, variants
247
+ sdk.cart.* // Cart management, coupons, promotions
248
+ sdk.order.* // Order creation, tracking, history
249
+
250
+ // Supporting Services
251
+ sdk.shipping.* // Shipping methods, rates, tracking
252
+ sdk.helpers.* // Countries, currencies, utilities
253
+ ```
254
+
255
+ ### Example Usage
256
+
257
+ ```typescript
258
+ // Every method is fully typed - IntelliSense shows all available parameters
259
+ const { data: products } = await sdk.catalog.listProducts({
260
+ query: {
261
+ page: 1,
262
+ limit: 20,
263
+ category_id: "electronics",
264
+ sort: "price_asc",
265
+ }
266
+ });
267
+
268
+ // Get product details
269
+ const { data: product } = await sdk.catalog.getProduct({
270
+ product_id_or_slug: "product-id"
81
271
  });
82
272
 
83
- // Token management
84
- sdk.setToken(access_token); // Set token for all clients
85
- sdk.clearToken(); // Clear token from all clients
273
+ // Create a cart with full type checking
274
+ const { data: cart } = await sdk.cart.createCart({
275
+ items: [
276
+ {
277
+ product_id: "product-id",
278
+ quantity: 2,
279
+ variant_id: "variant-id",
280
+ }
281
+ ]
282
+ });
283
+ ```
284
+
285
+ > **📚 API Reference**: For complete endpoint documentation, parameters, and response schemas, visit [docs.commercengine.io/api-reference](https://docs.commercengine.io/api-reference)
286
+
287
+ ## User Information & JWT Utilities
288
+
289
+ Extract user information from tokens with built-in utilities:
290
+
291
+ ```typescript
292
+ // Get current user info
293
+ const userInfo = await sdk.getUserInfo();
294
+ console.log(userInfo?.userId, userInfo?.email, userInfo?.customerId);
295
+
296
+ // Check authentication status
297
+ const isLoggedIn = await sdk.isLoggedIn();
298
+ const isAnonymous = await sdk.isAnonymous();
299
+
300
+ // Get specific user data
301
+ const userId = await sdk.getUserId();
302
+ const customerId = await sdk.getCustomerId();
303
+ const customerGroupId = await sdk.getCustomerGroupId();
86
304
  ```
87
305
 
88
- ### Using the API Clients
306
+ ## Error Handling
307
+
308
+ All API calls return a consistent `ApiResult<T>` structure with full error information:
309
+
310
+ ```typescript
311
+ const { data, error, response } = await sdk.catalog.listProducts();
89
312
 
90
- The SDK provides dedicated clients for different API sections:
313
+ if (error) {
314
+ console.error("API Error:", error.message, error.code);
315
+ console.log("Status:", response.status);
316
+ } else {
317
+ console.log("Success:", data);
318
+ }
319
+ ```
91
320
 
321
+ ### Network Error Handling
92
322
  ```typescript
93
- // Catalog client example (product listing)
94
- const products = await sdk.catalog.listProducts();
323
+ const result = await sdk.catalog.getProduct({ product_id_or_slug: "invalid-id" });
95
324
 
96
- // Cart client example (add to cart)
97
- const cart = await sdk.cart.addToCart("product-id", 1);
325
+ if (result.error) {
326
+ switch (result.error.code) {
327
+ case "NETWORK_ERROR":
328
+ console.log("Network connection failed");
329
+ break;
330
+ case "UNAUTHORIZED":
331
+ console.log("Authentication required");
332
+ break;
333
+ default:
334
+ console.log("API Error:", result.error.message);
335
+ }
336
+ }
98
337
  ```
99
338
 
100
- ## Advanced Configuration
339
+ ## Debug Mode
101
340
 
102
- You can configure timeout and other options:
341
+ Enable detailed logging for development:
103
342
 
104
343
  ```typescript
105
344
  const sdk = new StorefrontSDK({
106
345
  storeId: "your-store-id",
107
- environment: Environment.Production,
108
- timeout: 5000, // 5 second timeout
109
- token: "existing-token", // Initialize with an existing token
346
+ debug: true,
347
+ // Optional: Pass a custom logger
348
+ logger: (message, data) => {
349
+ console.log(`[SDK Debug] ${message}`, data);
350
+ },
110
351
  });
111
352
  ```
353
+
354
+ Debug mode logs:
355
+ - Request URLs, methods, headers, and bodies
356
+ - Response status, headers, and bodies
357
+ - Token refresh attempts and results
358
+ - Network errors and retry attempts
359
+
360
+ ## TypeScript Support
361
+
362
+ The SDK is built with TypeScript-first design:
363
+
364
+ ```typescript
365
+ import type {
366
+ ApiResult,
367
+ UserInfo,
368
+ DebugLoggerFn,
369
+ SupportedDefaultHeaders
370
+ } from "@commercengine/storefront-sdk";
371
+
372
+ // All API responses are properly typed
373
+ const { data }: ApiResult<ProductListResponse> = await sdk.catalog.listProducts();
374
+
375
+ // IntelliSense works for all nested properties
376
+ console.log(data?.products[0].name);
377
+ ```
378
+
379
+ ## Universal Compatibility
380
+
381
+ The SDK works seamlessly across all JavaScript environments:
382
+
383
+ ### Client-Side Applications
384
+ - **React, Vue, Angular**: Use `BrowserTokenStorage` for persistent sessions
385
+ - **Automatic token refresh**: Handles token expiry transparently
386
+ - **Cross-tab synchronization**: With `CookieTokenStorage`
387
+
388
+ ### Server-Side Applications
389
+ - **Node.js, Bun, Deno**: Use `MemoryTokenStorage` or custom storage
390
+ - **API routes**: Perfect for Next.js API routes, Express middleware
391
+ - **Background jobs**: Reliable token management for long-running processes
392
+
393
+ ### Hybrid Rendering (SSR/SSG)
394
+ - **Next.js, Nuxt, SvelteKit**: Seamless client/server token handoff
395
+ - **Cookie-based storage**: Maintains sessions across server/client boundaries
396
+ - **Hydration-safe**: No client/server state mismatches
397
+
398
+ ## Best Practices Built-In
399
+
400
+ The SDK implements CommerceEngine API best practices automatically:
401
+
402
+ - ✅ **Automatic token refresh** before expiry
403
+ - ✅ **Proper error handling** for all edge cases
404
+ - ✅ **Request retries** for transient failures
405
+ - ✅ **Rate limiting compliance** with proper backoff
406
+ - ✅ **Security headers** and CSRF protection
407
+ - ✅ **Timeout handling** with configurable limits
408
+ - ✅ **Memory leak prevention** with proper cleanup
409
+
410
+ ## License
411
+
412
+ This project is licensed under the MIT License.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercengine/storefront-sdk",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "description": "TypeScript SDK for the Storefront API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",