@commercengine/storefront-sdk-nextjs 1.0.0-alpha.2 → 1.0.0-alpha.3
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/README.md +59 -40
- package/dist/index.cjs +35 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +37 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,24 +28,8 @@ Add your store configuration to `.env.local`:
|
|
|
28
28
|
```bash
|
|
29
29
|
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
30
30
|
NEXT_PUBLIC_API_KEY=your-api-key
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Optional environment variables:**
|
|
34
|
-
```bash
|
|
35
31
|
# Environment (defaults to "staging")
|
|
36
32
|
NEXT_PUBLIC_ENVIRONMENT=staging # or "production"
|
|
37
|
-
|
|
38
|
-
# Custom API endpoint (overrides environment default)
|
|
39
|
-
NEXT_PUBLIC_API_BASE_URL=https://your-custom-api.example.com
|
|
40
|
-
|
|
41
|
-
# Request timeout in milliseconds
|
|
42
|
-
NEXT_PUBLIC_API_TIMEOUT=10000
|
|
43
|
-
|
|
44
|
-
# Debug mode (defaults to false)
|
|
45
|
-
NEXT_PUBLIC_DEBUG_MODE=true
|
|
46
|
-
|
|
47
|
-
# Default customer group for pricing and promotions
|
|
48
|
-
NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID=01JHS28V83KDWTRBXXJQRTEKA0
|
|
49
33
|
```
|
|
50
34
|
|
|
51
35
|
### 2. Create Your Storefront Configuration
|
|
@@ -76,13 +60,32 @@ const storefrontConfig: StorefrontRuntimeConfig = {
|
|
|
76
60
|
},
|
|
77
61
|
};
|
|
78
62
|
|
|
79
|
-
// Create the configured storefront function
|
|
63
|
+
// Create the configured storefront function (storefrontConfig is OPTIONAL)
|
|
80
64
|
export const storefront = createStorefront(storefrontConfig);
|
|
81
65
|
|
|
82
66
|
// Re-export types for convenience
|
|
83
67
|
export type { StorefrontRuntimeConfig };
|
|
84
68
|
```
|
|
85
69
|
|
|
70
|
+
**All Core SDK Types Available:**
|
|
71
|
+
The Next.js SDK re-exports all types from the core SDK, so you can import them directly:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Import any core SDK types you need
|
|
75
|
+
import type {
|
|
76
|
+
UserInfo,
|
|
77
|
+
SupportedDefaultHeaders,
|
|
78
|
+
StorefrontSDKOptions,
|
|
79
|
+
components,
|
|
80
|
+
operations
|
|
81
|
+
} from "@commercengine/storefront-sdk-nextjs";
|
|
82
|
+
|
|
83
|
+
// Use imported types
|
|
84
|
+
const headers: SupportedDefaultHeaders = {
|
|
85
|
+
customer_group_id: "01ABC123..."
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
86
89
|
### 3. Initialize in Root Layout
|
|
87
90
|
|
|
88
91
|
```typescript
|
|
@@ -240,7 +243,7 @@ export async function GET() {
|
|
|
240
243
|
|
|
241
244
|
### Root Layout (Special Case)
|
|
242
245
|
|
|
243
|
-
Root Layout requires explicit flag since it's outside request context
|
|
246
|
+
Root Layout requires explicit flag since it's outside request context. Passing this flag will instruct the SDK to fallback to MemoryStore when rendering the root layout as the root layout runs on both server and client before cookie context is available.
|
|
244
247
|
|
|
245
248
|
```typescript
|
|
246
249
|
// app/layout.tsx
|
|
@@ -268,11 +271,32 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
|
|
268
271
|
|
|
269
272
|
The SDK provides powerful build-time optimizations through intelligent token caching.
|
|
270
273
|
|
|
271
|
-
###
|
|
274
|
+
### Configure Next.js for Build-Time Optimization
|
|
272
275
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
+
Create or update your `next.config.ts` (or `next.config.js`) to enable automatic token caching during builds:
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
// next.config.ts
|
|
280
|
+
import type { NextConfig } from "next";
|
|
281
|
+
import { PHASE_PRODUCTION_BUILD } from 'next/constants';
|
|
282
|
+
|
|
283
|
+
const nextConfig = (phase: string): NextConfig => {
|
|
284
|
+
const isBuild = phase === PHASE_PRODUCTION_BUILD;
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
env: {
|
|
288
|
+
// Enable build-time token caching during production builds and when explicitly set
|
|
289
|
+
NEXT_BUILD_CACHE_TOKENS: process.env.NEXT_BUILD_CACHE_TOKENS ?? (isBuild ? 'true' : 'false'),
|
|
290
|
+
// Critical: tells SDK to use MemoryStore during SSG/Build/ISR to avoid cookie context failures
|
|
291
|
+
NEXT_IS_BUILD: isBuild ? 'true' : 'false',
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
// Ensure static generation is enabled
|
|
295
|
+
output: undefined, // Let Next.js decide based on usage
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export default nextConfig;
|
|
276
300
|
```
|
|
277
301
|
|
|
278
302
|
### SSG with generateStaticParams
|
|
@@ -334,9 +358,17 @@ With token caching enabled:
|
|
|
334
358
|
|
|
335
359
|
## Authentication Patterns
|
|
336
360
|
|
|
361
|
+
> **⚠️ Important:** Any authentication endpoint that returns tokens (like `loginWithPassword`, `verifyOtp`, `register`, etc.) **must** be called in contexts where cookies can be set and managed:
|
|
362
|
+
> - ✅ **Server Actions** (recommended for authentication flows)
|
|
363
|
+
> - ✅ **API Routes** (`/api` directory)
|
|
364
|
+
> - ✅ **Client Components** (browser environment)
|
|
365
|
+
> - ❌ **Server Components** (cannot set cookies, tokens won't persist)
|
|
366
|
+
>
|
|
367
|
+
> This ensures the SDK can automatically handle token storage and user session continuity.
|
|
368
|
+
|
|
337
369
|
### Anonymous Users
|
|
338
370
|
|
|
339
|
-
The SDK automatically creates anonymous tokens
|
|
371
|
+
The SDK automatically creates anonymous tokens via the `StorefrontSDKInitializer` component imported in your root layout. If this component is not imported (not recommended), a new token will be minted for each request as a fallback. **It is highly recommended** to use the `StorefrontSDKInitializer` and ensure all token-returning endpoints are called from request contexts that can set cookies. [Authentication Patterns](#authentication-patterns).
|
|
340
372
|
|
|
341
373
|
```typescript
|
|
342
374
|
// This works everywhere - creates anonymous token automatically
|
|
@@ -346,7 +378,7 @@ const { data: products } = await storefront(cookies()).catalog.listProducts();
|
|
|
346
378
|
### Email/Password Login
|
|
347
379
|
|
|
348
380
|
```typescript
|
|
349
|
-
// Server Action
|
|
381
|
+
// Server Action (✅ Recommended - can set cookies for token persistence)
|
|
350
382
|
"use server";
|
|
351
383
|
export async function loginUser(email: string, password: string) {
|
|
352
384
|
const sdk = storefront(cookies());
|
|
@@ -365,7 +397,7 @@ export async function loginUser(email: string, password: string) {
|
|
|
365
397
|
### Phone/OTP Login
|
|
366
398
|
|
|
367
399
|
```typescript
|
|
368
|
-
// Server Action - Step 1: Send OTP
|
|
400
|
+
// Server Action - Step 1: Send OTP (✅ Recommended context)
|
|
369
401
|
export async function sendOTP(phone: string, country_code: string) {
|
|
370
402
|
const sdk = storefront(await cookies());
|
|
371
403
|
|
|
@@ -376,7 +408,7 @@ export async function sendOTP(phone: string, country_code: string) {
|
|
|
376
408
|
});
|
|
377
409
|
}
|
|
378
410
|
|
|
379
|
-
// Server Action - Step 2: Verify OTP
|
|
411
|
+
// Server Action - Step 2: Verify OTP (✅ Recommended - can set cookies for token persistence)
|
|
380
412
|
export async function verifyOTP(otp: string, otp_token: string, otp_action: string) {
|
|
381
413
|
const sdk = storefront(await cookies());
|
|
382
414
|
|
|
@@ -571,19 +603,6 @@ import { storefront } from "@/lib/storefront";
|
|
|
571
603
|
const sdk = storefront(cookies());
|
|
572
604
|
```
|
|
573
605
|
|
|
574
|
-
### From Previous SDK Versions
|
|
575
|
-
|
|
576
|
-
```typescript
|
|
577
|
-
// Before (old pattern)
|
|
578
|
-
import { getStorefrontSDK, storefront } from "@commercengine/storefront-sdk-nextjs";
|
|
579
|
-
const sdk = getStorefrontSDK(cookies());
|
|
580
|
-
const sdk2 = storefront(cookies(), { debug: true });
|
|
581
|
-
|
|
582
|
-
// After (createStorefront pattern)
|
|
583
|
-
import { storefront } from "@/lib/storefront"; // Your configured function
|
|
584
|
-
const sdk = storefront(cookies());
|
|
585
|
-
```
|
|
586
|
-
|
|
587
606
|
## Why This Pattern?
|
|
588
607
|
|
|
589
608
|
**The Perfect DX Pattern provides:**
|
|
@@ -598,7 +617,7 @@ const sdk = storefront(cookies());
|
|
|
598
617
|
|
|
599
618
|
## API Reference
|
|
600
619
|
|
|
601
|
-
For complete API documentation of all available endpoints, visit [
|
|
620
|
+
For complete API documentation of all available endpoints, visit [SDK DOCS REFERENCE](https://sdk-docs.commercengine.io/sdk/)
|
|
602
621
|
|
|
603
622
|
The Next.js SDK provides access to all the same endpoints as the core SDK:
|
|
604
623
|
|
package/dist/index.cjs
CHANGED
|
@@ -20,9 +20,28 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
AuthClient: () => import_storefront_sdk3.AuthClient,
|
|
24
|
+
BrowserTokenStorage: () => import_storefront_sdk4.BrowserTokenStorage,
|
|
25
|
+
CartClient: () => import_storefront_sdk3.CartClient,
|
|
26
|
+
CatalogClient: () => import_storefront_sdk3.CatalogClient,
|
|
27
|
+
CookieTokenStorage: () => import_storefront_sdk4.CookieTokenStorage,
|
|
28
|
+
CustomerClient: () => import_storefront_sdk3.CustomerClient,
|
|
29
|
+
Environment: () => import_storefront_sdk2.Environment,
|
|
30
|
+
HelpersClient: () => import_storefront_sdk3.HelpersClient,
|
|
31
|
+
MemoryTokenStorage: () => import_storefront_sdk4.MemoryTokenStorage,
|
|
32
|
+
OrderClient: () => import_storefront_sdk3.OrderClient,
|
|
33
|
+
ResponseUtils: () => import_storefront_sdk3.ResponseUtils,
|
|
34
|
+
ShippingClient: () => import_storefront_sdk3.ShippingClient,
|
|
35
|
+
StoreConfigClient: () => import_storefront_sdk3.StoreConfigClient,
|
|
36
|
+
StorefrontAPIClient: () => import_storefront_sdk3.StorefrontAPIClient,
|
|
37
|
+
StorefrontSDK: () => import_storefront_sdk2.StorefrontSDK,
|
|
38
|
+
TokenStorage: () => import_storefront_sdk4.TokenStorage,
|
|
23
39
|
createStorefront: () => createStorefront
|
|
24
40
|
});
|
|
25
41
|
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
var import_storefront_sdk2 = require("@commercengine/storefront-sdk");
|
|
43
|
+
var import_storefront_sdk3 = require("@commercengine/storefront-sdk");
|
|
44
|
+
var import_storefront_sdk4 = require("@commercengine/storefront-sdk");
|
|
26
45
|
|
|
27
46
|
// src/sdk-manager.ts
|
|
28
47
|
var import_react = require("react");
|
|
@@ -422,5 +441,21 @@ function createStorefront(config) {
|
|
|
422
441
|
}
|
|
423
442
|
// Annotate the CommonJS export names for ESM import in node:
|
|
424
443
|
0 && (module.exports = {
|
|
444
|
+
AuthClient,
|
|
445
|
+
BrowserTokenStorage,
|
|
446
|
+
CartClient,
|
|
447
|
+
CatalogClient,
|
|
448
|
+
CookieTokenStorage,
|
|
449
|
+
CustomerClient,
|
|
450
|
+
Environment,
|
|
451
|
+
HelpersClient,
|
|
452
|
+
MemoryTokenStorage,
|
|
453
|
+
OrderClient,
|
|
454
|
+
ResponseUtils,
|
|
455
|
+
ShippingClient,
|
|
456
|
+
StoreConfigClient,
|
|
457
|
+
StorefrontAPIClient,
|
|
458
|
+
StorefrontSDK,
|
|
459
|
+
TokenStorage,
|
|
425
460
|
createStorefront
|
|
426
461
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { StorefrontSDK } from '@commercengine/storefront-sdk';
|
|
2
|
-
export
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK, TokenStorage } from '@commercengine/storefront-sdk';
|
|
3
4
|
import { S as StorefrontRuntimeConfig } from './server-CwxgXezP.cjs';
|
|
4
5
|
|
|
5
6
|
type NextCookieStore = {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { StorefrontSDK } from '@commercengine/storefront-sdk';
|
|
2
|
-
export
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK, TokenStorage } from '@commercengine/storefront-sdk';
|
|
3
4
|
import { S as StorefrontRuntimeConfig } from './server-CwxgXezP.js';
|
|
4
5
|
|
|
5
6
|
type NextCookieStore = {
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { StorefrontSDK as StorefrontSDK2, Environment as Environment2 } from "@commercengine/storefront-sdk";
|
|
3
|
+
import {
|
|
4
|
+
StorefrontAPIClient,
|
|
5
|
+
AuthClient,
|
|
6
|
+
CartClient,
|
|
7
|
+
CatalogClient,
|
|
8
|
+
CustomerClient,
|
|
9
|
+
HelpersClient,
|
|
10
|
+
ShippingClient,
|
|
11
|
+
OrderClient,
|
|
12
|
+
StoreConfigClient,
|
|
13
|
+
ResponseUtils
|
|
14
|
+
} from "@commercengine/storefront-sdk";
|
|
15
|
+
import {
|
|
16
|
+
TokenStorage,
|
|
17
|
+
MemoryTokenStorage as MemoryTokenStorage2,
|
|
18
|
+
BrowserTokenStorage,
|
|
19
|
+
CookieTokenStorage
|
|
20
|
+
} from "@commercengine/storefront-sdk";
|
|
21
|
+
|
|
1
22
|
// src/sdk-manager.ts
|
|
2
23
|
import { cache } from "react";
|
|
3
24
|
import {
|
|
@@ -399,5 +420,21 @@ function createStorefront(config) {
|
|
|
399
420
|
return storefront;
|
|
400
421
|
}
|
|
401
422
|
export {
|
|
423
|
+
AuthClient,
|
|
424
|
+
BrowserTokenStorage,
|
|
425
|
+
CartClient,
|
|
426
|
+
CatalogClient,
|
|
427
|
+
CookieTokenStorage,
|
|
428
|
+
CustomerClient,
|
|
429
|
+
Environment2 as Environment,
|
|
430
|
+
HelpersClient,
|
|
431
|
+
MemoryTokenStorage2 as MemoryTokenStorage,
|
|
432
|
+
OrderClient,
|
|
433
|
+
ResponseUtils,
|
|
434
|
+
ShippingClient,
|
|
435
|
+
StoreConfigClient,
|
|
436
|
+
StorefrontAPIClient,
|
|
437
|
+
StorefrontSDK2 as StorefrontSDK,
|
|
438
|
+
TokenStorage,
|
|
402
439
|
createStorefront
|
|
403
440
|
};
|