@commercengine/storefront-sdk-nextjs 0.1.0-alpha.0 → 0.1.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/README.md +534 -139
- package/dist/client.cjs +136 -0
- package/dist/client.d.cts +17 -0
- package/dist/client.d.cts.map +1 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +44 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +128 -319
- package/dist/index.d.cts +34 -107
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +34 -107
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -303
- package/dist/index.js.map +1 -0
- package/dist/sdk-manager-B9xDQQuv.d.ts +141 -0
- package/dist/sdk-manager-B9xDQQuv.d.ts.map +1 -0
- package/dist/sdk-manager-CdgrfSVp.js +343 -0
- package/dist/sdk-manager-CdgrfSVp.js.map +1 -0
- package/dist/sdk-manager-D2ktqPrp.d.cts +141 -0
- package/dist/sdk-manager-D2ktqPrp.d.cts.map +1 -0
- package/dist/sdk-manager-NWADjRFW.cjs +400 -0
- package/dist/server.cjs +102 -0
- package/dist/server.d.cts +4 -0
- package/dist/server.d.ts +4 -0
- package/dist/server.js +6 -0
- package/package.json +21 -11
package/dist/index.d.ts
CHANGED
|
@@ -1,116 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { StorefrontRuntimeConfig } from "./sdk-manager-B9xDQQuv.js";
|
|
2
|
+
import { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK, StorefrontSDK as StorefrontSDK$1 } from "@commercengine/storefront-sdk";
|
|
3
|
+
export * from "@commercengine/storefront-sdk";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
* Configuration options for NextJSTokenStorage
|
|
6
|
-
*/
|
|
7
|
-
interface NextJSTokenStorageOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Prefix for cookie names (default: "ce_")
|
|
10
|
-
*/
|
|
11
|
-
prefix?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Maximum age of cookies in seconds (default: 30 days)
|
|
14
|
-
*/
|
|
15
|
-
maxAge?: number;
|
|
16
|
-
/**
|
|
17
|
-
* Cookie path (default: "/")
|
|
18
|
-
*/
|
|
19
|
-
path?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Cookie domain (default: current domain)
|
|
22
|
-
*/
|
|
23
|
-
domain?: string;
|
|
24
|
-
/**
|
|
25
|
-
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
26
|
-
*/
|
|
27
|
-
secure?: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* SameSite cookie attribute (default: "Lax")
|
|
30
|
-
*/
|
|
31
|
-
sameSite?: "Strict" | "Lax" | "None";
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Client-side token storage that uses document.cookie
|
|
35
|
-
*/
|
|
36
|
-
declare class ClientTokenStorage implements TokenStorage {
|
|
37
|
-
private accessTokenKey;
|
|
38
|
-
private refreshTokenKey;
|
|
39
|
-
private options;
|
|
40
|
-
constructor(options?: NextJSTokenStorageOptions);
|
|
41
|
-
getAccessToken(): Promise<string | null>;
|
|
42
|
-
setAccessToken(token: string): Promise<void>;
|
|
43
|
-
getRefreshToken(): Promise<string | null>;
|
|
44
|
-
setRefreshToken(token: string): Promise<void>;
|
|
45
|
-
clearTokens(): Promise<void>;
|
|
46
|
-
private getCookie;
|
|
47
|
-
private setCookie;
|
|
48
|
-
private deleteCookie;
|
|
49
|
-
}
|
|
50
|
-
type NextCookieStore$1 = {
|
|
51
|
-
get: (name: string) => {
|
|
52
|
-
value: string;
|
|
53
|
-
} | undefined;
|
|
54
|
-
set: (name: string, value: string, options?: any) => void;
|
|
55
|
-
delete: (name: string) => void;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Server-side token storage that uses Next.js cookies API
|
|
59
|
-
*/
|
|
60
|
-
declare class ServerTokenStorage implements TokenStorage {
|
|
61
|
-
private accessTokenKey;
|
|
62
|
-
private refreshTokenKey;
|
|
63
|
-
private options;
|
|
64
|
-
private cookieStore;
|
|
65
|
-
constructor(cookieStore: NextCookieStore$1, options?: NextJSTokenStorageOptions);
|
|
66
|
-
getAccessToken(): Promise<string | null>;
|
|
67
|
-
setAccessToken(token: string): Promise<void>;
|
|
68
|
-
getRefreshToken(): Promise<string | null>;
|
|
69
|
-
setRefreshToken(token: string): Promise<void>;
|
|
70
|
-
clearTokens(): Promise<void>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Configuration for the NextJS SDK wrapper
|
|
75
|
-
*/
|
|
76
|
-
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
77
|
-
/**
|
|
78
|
-
* Token storage configuration options
|
|
79
|
-
*/
|
|
80
|
-
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
81
|
-
}
|
|
5
|
+
//#region src/create-storefront.d.ts
|
|
82
6
|
type NextCookieStore = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
7
|
+
get: (name: string) => {
|
|
8
|
+
name: string;
|
|
9
|
+
value: string;
|
|
10
|
+
} | undefined;
|
|
11
|
+
set: (name: string, value: string, opts?: Record<string, unknown>) => void;
|
|
12
|
+
delete: (name: string) => void;
|
|
88
13
|
};
|
|
89
14
|
/**
|
|
90
|
-
*
|
|
15
|
+
* Creates a configured storefront function that works universally across all Next.js contexts
|
|
91
16
|
*
|
|
92
17
|
* Usage:
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
declare function getStorefrontSDK(): StorefrontSDK;
|
|
97
|
-
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
98
|
-
|
|
99
|
-
interface StorefrontSDKInitializerProps {
|
|
100
|
-
config: NextJSSDKConfig;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Client-side initialization component
|
|
104
|
-
* Use this in your root layout to initialize the SDK once
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // lib/storefront.ts
|
|
20
|
+
* import { createStorefront } from "@commercengine/storefront-sdk-nextjs";
|
|
105
21
|
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
22
|
+
* export const storefront = createStorefront({
|
|
23
|
+
* debug: true,
|
|
24
|
+
* logger: (msg, ...args) => console.log('[DEBUG]', msg, ...args)
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
111
27
|
*
|
|
112
|
-
*
|
|
28
|
+
* @param config Optional configuration for advanced options (defaults to empty if not provided)
|
|
29
|
+
* @returns A storefront function that works in all Next.js contexts
|
|
113
30
|
*/
|
|
114
|
-
declare function
|
|
115
|
-
|
|
116
|
-
|
|
31
|
+
declare function createStorefront(config?: StorefrontRuntimeConfig): {
|
|
32
|
+
(): StorefrontSDK$1;
|
|
33
|
+
(cookieStore: NextCookieStore): StorefrontSDK$1;
|
|
34
|
+
(options: {
|
|
35
|
+
isRootLayout: true;
|
|
36
|
+
}): StorefrontSDK$1;
|
|
37
|
+
(cookieStore: NextCookieStore, options: {
|
|
38
|
+
isRootLayout?: boolean;
|
|
39
|
+
}): StorefrontSDK$1;
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
export { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, type StorefrontRuntimeConfig, StorefrontSDK, createStorefront };
|
|
43
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/create-storefront.ts"],"sourcesContent":[],"mappings":";;;;;KAQK,eAAA;;;;EAAA,CAAA,GAAA,SAAA;EAwBW,GAAA,EAAA,CAAA,IAAA,EAAA,MAAA,EAAgB,KAAA,EAAA,MAAA,EAAA,IAAA,CAAA,EAtBY,MAsBZ,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,GAAA,IAAA;EAAA,MAAA,EAAA,CAAA,IAAA,EAAA,MAAA,EAAA,GAAA,IAAA;;;;;;;;;;;;;;;;;;;iBAAhB,gBAAA,UAA0B;MAKjB;gBACU,kBAAkB;;;MACG;gBACrB;;MAAuD"}
|
package/dist/index.js
CHANGED
|
@@ -1,305 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "@commercengine/storefront-sdk";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
setCookie(name, value) {
|
|
55
|
-
if (typeof document === "undefined") return;
|
|
56
|
-
const encodedValue = encodeURIComponent(value);
|
|
57
|
-
let cookieString = `${name}=${encodedValue}`;
|
|
58
|
-
if (this.options.maxAge) {
|
|
59
|
-
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
60
|
-
}
|
|
61
|
-
if (this.options.path) {
|
|
62
|
-
cookieString += `; Path=${this.options.path}`;
|
|
63
|
-
}
|
|
64
|
-
if (this.options.domain) {
|
|
65
|
-
cookieString += `; Domain=${this.options.domain}`;
|
|
66
|
-
}
|
|
67
|
-
if (this.options.secure) {
|
|
68
|
-
cookieString += `; Secure`;
|
|
69
|
-
}
|
|
70
|
-
if (this.options.sameSite) {
|
|
71
|
-
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
72
|
-
}
|
|
73
|
-
document.cookie = cookieString;
|
|
74
|
-
}
|
|
75
|
-
deleteCookie(name) {
|
|
76
|
-
if (typeof document === "undefined") return;
|
|
77
|
-
let cookieString = `${name}=; Max-Age=0`;
|
|
78
|
-
if (this.options.path) {
|
|
79
|
-
cookieString += `; Path=${this.options.path}`;
|
|
80
|
-
}
|
|
81
|
-
if (this.options.domain) {
|
|
82
|
-
cookieString += `; Domain=${this.options.domain}`;
|
|
83
|
-
}
|
|
84
|
-
document.cookie = cookieString;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
var ServerTokenStorage = class {
|
|
88
|
-
constructor(cookieStore, options = {}) {
|
|
89
|
-
const prefix = options.prefix || "ce_";
|
|
90
|
-
this.accessTokenKey = `${prefix}access_token`;
|
|
91
|
-
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
92
|
-
this.cookieStore = cookieStore;
|
|
93
|
-
this.options = {
|
|
94
|
-
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
95
|
-
// 30 days default
|
|
96
|
-
path: options.path || "/",
|
|
97
|
-
domain: options.domain,
|
|
98
|
-
secure: options.secure ?? process.env.NODE_ENV === "production",
|
|
99
|
-
sameSite: options.sameSite || "Lax"
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
async getAccessToken() {
|
|
103
|
-
try {
|
|
104
|
-
return this.cookieStore.get(this.accessTokenKey)?.value || null;
|
|
105
|
-
} catch (error) {
|
|
106
|
-
console.warn(`Could not get access token from server cookies:`, error);
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
async setAccessToken(token) {
|
|
111
|
-
try {
|
|
112
|
-
this.cookieStore.set(this.accessTokenKey, token, {
|
|
113
|
-
maxAge: this.options.maxAge,
|
|
114
|
-
path: this.options.path,
|
|
115
|
-
domain: this.options.domain,
|
|
116
|
-
secure: this.options.secure,
|
|
117
|
-
sameSite: this.options.sameSite?.toLowerCase(),
|
|
118
|
-
httpOnly: false
|
|
119
|
-
// Must be false for client-side access
|
|
120
|
-
});
|
|
121
|
-
} catch (error) {
|
|
122
|
-
console.warn(`Could not set access token on server:`, error);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
async getRefreshToken() {
|
|
126
|
-
try {
|
|
127
|
-
return this.cookieStore.get(this.refreshTokenKey)?.value || null;
|
|
128
|
-
} catch (error) {
|
|
129
|
-
console.warn(`Could not get refresh token from server cookies:`, error);
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
async setRefreshToken(token) {
|
|
134
|
-
try {
|
|
135
|
-
this.cookieStore.set(this.refreshTokenKey, token, {
|
|
136
|
-
maxAge: this.options.maxAge,
|
|
137
|
-
path: this.options.path,
|
|
138
|
-
domain: this.options.domain,
|
|
139
|
-
secure: this.options.secure,
|
|
140
|
-
sameSite: this.options.sameSite?.toLowerCase(),
|
|
141
|
-
httpOnly: false
|
|
142
|
-
// Must be false for client-side access
|
|
143
|
-
});
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.warn(`Could not set refresh token on server:`, error);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
async clearTokens() {
|
|
149
|
-
try {
|
|
150
|
-
this.cookieStore.delete(this.accessTokenKey);
|
|
151
|
-
this.cookieStore.delete(this.refreshTokenKey);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
console.warn(`Could not clear tokens on server:`, error);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
// src/sdk-manager.ts
|
|
159
|
-
var NextJSSDKManager = class _NextJSSDKManager {
|
|
160
|
-
constructor() {
|
|
161
|
-
this.clientSDK = null;
|
|
162
|
-
this.config = null;
|
|
163
|
-
}
|
|
164
|
-
static getInstance() {
|
|
165
|
-
if (!_NextJSSDKManager.instance) {
|
|
166
|
-
_NextJSSDKManager.instance = new _NextJSSDKManager();
|
|
167
|
-
}
|
|
168
|
-
return _NextJSSDKManager.instance;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Initialize the SDK with configuration (should be called once)
|
|
172
|
-
*/
|
|
173
|
-
initialize(config) {
|
|
174
|
-
this.config = config;
|
|
175
|
-
if (typeof window !== "undefined" && !this.clientSDK) {
|
|
176
|
-
this.clientSDK = this.createClientSDK();
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Get SDK instance for client-side usage
|
|
181
|
-
*/
|
|
182
|
-
getClientSDK() {
|
|
183
|
-
if (!this.config) {
|
|
184
|
-
throw new Error("SDK not initialized. Call initialize() first.");
|
|
185
|
-
}
|
|
186
|
-
if (!this.clientSDK) {
|
|
187
|
-
this.clientSDK = this.createClientSDK();
|
|
188
|
-
}
|
|
189
|
-
return this.clientSDK;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Get SDK instance for server-side usage
|
|
193
|
-
*/
|
|
194
|
-
getServerSDK(cookieStore) {
|
|
195
|
-
if (!this.config) {
|
|
196
|
-
throw new Error("SDK not initialized. Call initialize() first.");
|
|
197
|
-
}
|
|
198
|
-
return new StorefrontSDK({
|
|
199
|
-
...this.config,
|
|
200
|
-
tokenStorage: new ServerTokenStorage(
|
|
201
|
-
cookieStore,
|
|
202
|
-
this.config.tokenStorageOptions
|
|
203
|
-
)
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
createClientSDK() {
|
|
207
|
-
if (!this.config) {
|
|
208
|
-
throw new Error("SDK not initialized. Call initialize() first.");
|
|
209
|
-
}
|
|
210
|
-
return new StorefrontSDK({
|
|
211
|
-
...this.config,
|
|
212
|
-
tokenStorage: new ClientTokenStorage(this.config.tokenStorageOptions)
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Check if SDK is initialized
|
|
217
|
-
*/
|
|
218
|
-
isInitialized() {
|
|
219
|
-
return this.config !== null;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Reset the SDK (useful for testing)
|
|
223
|
-
*/
|
|
224
|
-
reset() {
|
|
225
|
-
this.clientSDK = null;
|
|
226
|
-
this.config = null;
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
function getStorefrontSDK(cookieStore) {
|
|
230
|
-
const manager = NextJSSDKManager.getInstance();
|
|
231
|
-
if (typeof window !== "undefined") {
|
|
232
|
-
if (cookieStore) {
|
|
233
|
-
console.warn(
|
|
234
|
-
"Cookie store passed in client environment - this will be ignored"
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
return manager.getClientSDK();
|
|
238
|
-
}
|
|
239
|
-
if (!cookieStore) {
|
|
240
|
-
let autoDetectMessage = "";
|
|
241
|
-
try {
|
|
242
|
-
__require.resolve("next/headers");
|
|
243
|
-
autoDetectMessage = `
|
|
244
|
-
|
|
245
|
-
\u{1F50D} Auto-detection attempted but failed. You may be in:
|
|
246
|
-
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
247
|
-
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
248
|
-
- Middleware (token access limited)
|
|
249
|
-
`;
|
|
250
|
-
} catch {
|
|
251
|
-
autoDetectMessage = `
|
|
252
|
-
|
|
253
|
-
\u{1F4A1} Make sure you have Next.js installed and are in a server context.
|
|
254
|
-
`;
|
|
255
|
-
}
|
|
256
|
-
throw new Error(
|
|
257
|
-
`
|
|
258
|
-
\u{1F6A8} Server Environment Detected!
|
|
259
|
-
|
|
260
|
-
You're calling getStorefrontSDK() on the server without cookies.
|
|
261
|
-
Please pass the Next.js cookie store:
|
|
262
|
-
|
|
263
|
-
\u2705 Correct usage:
|
|
264
|
-
import { cookies } from 'next/headers';
|
|
265
|
-
|
|
266
|
-
// Server Actions & Route Handlers
|
|
267
|
-
const sdk = getStorefrontSDK(await cookies());
|
|
268
|
-
|
|
269
|
-
// API Routes (Next.js 12)
|
|
270
|
-
const sdk = getStorefrontSDK(cookies());
|
|
271
|
-
|
|
272
|
-
\u274C Your current usage:
|
|
273
|
-
const sdk = getStorefrontSDK(); // Missing cookies!
|
|
274
|
-
${autoDetectMessage}
|
|
275
|
-
This is required for server-side token access.
|
|
276
|
-
`.trim()
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
return manager.getServerSDK(cookieStore);
|
|
280
|
-
}
|
|
281
|
-
function initializeStorefrontSDK(config) {
|
|
282
|
-
const manager = NextJSSDKManager.getInstance();
|
|
283
|
-
manager.initialize(config);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// src/init-client.ts
|
|
287
|
-
import { useEffect } from "react";
|
|
288
|
-
function StorefrontSDKInitializer({
|
|
289
|
-
config
|
|
290
|
-
}) {
|
|
291
|
-
useEffect(() => {
|
|
292
|
-
initializeStorefrontSDK(config);
|
|
293
|
-
}, [config]);
|
|
294
|
-
return null;
|
|
1
|
+
import { getStorefrontSDK, setGlobalStorefrontConfig } from "./sdk-manager-CdgrfSVp.js";
|
|
2
|
+
import { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK } from "@commercengine/storefront-sdk";
|
|
3
|
+
|
|
4
|
+
//#region src/create-storefront.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a configured storefront function that works universally across all Next.js contexts
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // lib/storefront.ts
|
|
11
|
+
* import { createStorefront } from "@commercengine/storefront-sdk-nextjs";
|
|
12
|
+
*
|
|
13
|
+
* export const storefront = createStorefront({
|
|
14
|
+
* debug: true,
|
|
15
|
+
* logger: (msg, ...args) => console.log('[DEBUG]', msg, ...args)
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param config Optional configuration for advanced options (defaults to empty if not provided)
|
|
20
|
+
* @returns A storefront function that works in all Next.js contexts
|
|
21
|
+
*/
|
|
22
|
+
function createStorefront(config) {
|
|
23
|
+
if (config) setGlobalStorefrontConfig(config);
|
|
24
|
+
function storefront(cookieStoreOrOptions, options) {
|
|
25
|
+
if (typeof window !== "undefined") return getStorefrontSDK();
|
|
26
|
+
let cookieStore;
|
|
27
|
+
let isRootLayout = false;
|
|
28
|
+
if (cookieStoreOrOptions) if ("isRootLayout" in cookieStoreOrOptions) isRootLayout = cookieStoreOrOptions.isRootLayout;
|
|
29
|
+
else {
|
|
30
|
+
cookieStore = cookieStoreOrOptions;
|
|
31
|
+
isRootLayout = options?.isRootLayout || false;
|
|
32
|
+
}
|
|
33
|
+
if (cookieStore) return getStorefrontSDK(cookieStore);
|
|
34
|
+
if (process.env.NEXT_IS_BUILD === "true" || process.env.NEXT_BUILD_CACHE_TOKENS === "true") return getStorefrontSDK();
|
|
35
|
+
if (isRootLayout) return getStorefrontSDK();
|
|
36
|
+
throw new Error([
|
|
37
|
+
"🚨 Server context requires cookies for user continuity!",
|
|
38
|
+
"",
|
|
39
|
+
"You're calling storefront() on the server without cookies.",
|
|
40
|
+
"This breaks user session continuity and analytics tracking.",
|
|
41
|
+
"",
|
|
42
|
+
"✅ Correct usage:",
|
|
43
|
+
" import { cookies } from 'next/headers'",
|
|
44
|
+
" const sdk = storefront(cookies())",
|
|
45
|
+
"",
|
|
46
|
+
"📍 Root Layout exception:",
|
|
47
|
+
" const sdk = storefront({ isRootLayout: true })",
|
|
48
|
+
"",
|
|
49
|
+
"This is required to maintain consistent user identity across requests."
|
|
50
|
+
].join("\n"));
|
|
51
|
+
}
|
|
52
|
+
return storefront;
|
|
295
53
|
}
|
|
296
54
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
ClientTokenStorage,
|
|
301
|
-
Environment,
|
|
302
|
-
ServerTokenStorage,
|
|
303
|
-
StorefrontSDKInitializer,
|
|
304
|
-
getStorefrontSDK
|
|
305
|
-
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK, createStorefront };
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["cookieStore: NextCookieStore | undefined"],"sources":["../src/create-storefront.ts"],"sourcesContent":["import {\n getStorefrontSDK,\n setGlobalStorefrontConfig,\n type StorefrontRuntimeConfig,\n} from \"./sdk-manager\";\nimport type { StorefrontSDK } from \"@commercengine/storefront-sdk\";\n\n// Structural type so we don't import next/headers at module scope\ntype NextCookieStore = {\n get: (name: string) => { name: string; value: string } | undefined;\n set: (name: string, value: string, opts?: Record<string, unknown>) => void;\n delete: (name: string) => void;\n};\n\n\n/**\n * Creates a configured storefront function that works universally across all Next.js contexts\n *\n * Usage:\n * ```typescript\n * // lib/storefront.ts\n * import { createStorefront } from \"@commercengine/storefront-sdk-nextjs\";\n *\n * export const storefront = createStorefront({\n * debug: true,\n * logger: (msg, ...args) => console.log('[DEBUG]', msg, ...args)\n * });\n * ```\n *\n * @param config Optional configuration for advanced options (defaults to empty if not provided)\n * @returns A storefront function that works in all Next.js contexts\n */\nexport function createStorefront(config?: StorefrontRuntimeConfig) {\n if (config) {\n setGlobalStorefrontConfig(config);\n }\n\n function storefront(): StorefrontSDK;\n function storefront(cookieStore: NextCookieStore): StorefrontSDK;\n function storefront(options: { isRootLayout: true }): StorefrontSDK;\n function storefront(cookieStore: NextCookieStore, options: { isRootLayout?: boolean }): StorefrontSDK;\n function storefront(\n cookieStoreOrOptions?: NextCookieStore | { isRootLayout: true }, \n options?: { isRootLayout?: boolean }\n ): StorefrontSDK {\n // Client path\n if (typeof window !== \"undefined\") {\n return getStorefrontSDK();\n }\n\n // Parse parameters to handle different overloads\n let cookieStore: NextCookieStore | undefined;\n let isRootLayout = false;\n\n if (cookieStoreOrOptions) {\n if ('isRootLayout' in cookieStoreOrOptions) {\n // First parameter is options object\n isRootLayout = cookieStoreOrOptions.isRootLayout;\n } else {\n // First parameter is cookie store\n cookieStore = cookieStoreOrOptions;\n isRootLayout = options?.isRootLayout || false;\n }\n }\n\n // Server path with explicit cookies\n if (cookieStore) {\n return getStorefrontSDK(cookieStore);\n }\n\n // Server path without cookies:\n // 1. Always allow build contexts (no request context available)\n if (\n process.env.NEXT_IS_BUILD === \"true\" ||\n process.env.NEXT_BUILD_CACHE_TOKENS === \"true\"\n ) {\n return getStorefrontSDK();\n }\n\n // 2. Allow root layout with explicit flag\n if (isRootLayout) {\n return getStorefrontSDK();\n }\n\n // 3. All other server contexts must pass cookies - throw helpful error\n throw new Error(\n [\n \"🚨 Server context requires cookies for user continuity!\",\n \"\",\n \"You're calling storefront() on the server without cookies.\",\n \"This breaks user session continuity and analytics tracking.\",\n \"\",\n \"✅ Correct usage:\",\n \" import { cookies } from 'next/headers'\",\n \" const sdk = storefront(cookies())\",\n \"\",\n \"📍 Root Layout exception:\",\n \" const sdk = storefront({ isRootLayout: true })\",\n \"\",\n \"This is required to maintain consistent user identity across requests.\",\n ].join(\"\\n\")\n );\n }\n\n return storefront;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,iBAAiB,QAAkC;AACjE,KAAI,OACF,2BAA0B;CAO5B,SAAS,WACP,sBACA,SACe;AAEf,MAAI,OAAO,WAAW,YACpB,QAAO;EAIT,IAAIA;EACJ,IAAI,eAAe;AAEnB,MAAI,qBACF,KAAI,kBAAkB,qBAEpB,gBAAe,qBAAqB;OAC/B;AAEL,iBAAc;AACd,kBAAe,SAAS,gBAAgB;;AAK5C,MAAI,YACF,QAAO,iBAAiB;AAK1B,MACE,QAAQ,IAAI,kBAAkB,UAC9B,QAAQ,IAAI,4BAA4B,OAExC,QAAO;AAIT,MAAI,aACF,QAAO;AAIT,QAAM,IAAI,MACR;GACE;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;IACA,KAAK;;AAIX,QAAO"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { DebugLoggerFn, Environment, StorefrontSDK, StorefrontSDKOptions, SupportedDefaultHeaders, TokenStorage } from "@commercengine/storefront-sdk";
|
|
2
|
+
|
|
3
|
+
//#region src/token-storage.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for NextJSTokenStorage
|
|
7
|
+
*/
|
|
8
|
+
interface NextJSTokenStorageOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Prefix for cookie names (default: "ce_")
|
|
11
|
+
*/
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
15
|
+
*/
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Cookie path (default: "/")
|
|
19
|
+
*/
|
|
20
|
+
path?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Cookie domain (default: current domain)
|
|
23
|
+
*/
|
|
24
|
+
domain?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
27
|
+
*/
|
|
28
|
+
secure?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* SameSite cookie attribute (default: "Lax")
|
|
31
|
+
*/
|
|
32
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Client-side token storage that uses document.cookie
|
|
36
|
+
*/
|
|
37
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
38
|
+
private accessTokenKey;
|
|
39
|
+
private refreshTokenKey;
|
|
40
|
+
private options;
|
|
41
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
42
|
+
getAccessToken(): Promise<string | null>;
|
|
43
|
+
setAccessToken(token: string): Promise<void>;
|
|
44
|
+
getRefreshToken(): Promise<string | null>;
|
|
45
|
+
setRefreshToken(token: string): Promise<void>;
|
|
46
|
+
clearTokens(): Promise<void>;
|
|
47
|
+
private getCookie;
|
|
48
|
+
private setCookie;
|
|
49
|
+
private deleteCookie;
|
|
50
|
+
}
|
|
51
|
+
type NextCookieStore$1 = {
|
|
52
|
+
get: (name: string) => {
|
|
53
|
+
value: string;
|
|
54
|
+
} | undefined;
|
|
55
|
+
set: (name: string, value: string, options?: any) => void;
|
|
56
|
+
delete: (name: string) => void;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Server-side token storage that uses Next.js cookies API
|
|
60
|
+
*/
|
|
61
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
62
|
+
private accessTokenKey;
|
|
63
|
+
private refreshTokenKey;
|
|
64
|
+
private options;
|
|
65
|
+
private cookieStore;
|
|
66
|
+
constructor(cookieStore: NextCookieStore$1, options?: NextJSTokenStorageOptions);
|
|
67
|
+
getAccessToken(): Promise<string | null>;
|
|
68
|
+
setAccessToken(token: string): Promise<void>;
|
|
69
|
+
getRefreshToken(): Promise<string | null>;
|
|
70
|
+
setRefreshToken(token: string): Promise<void>;
|
|
71
|
+
clearTokens(): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/sdk-manager.d.ts
|
|
75
|
+
/**
|
|
76
|
+
* Configuration for the NextJS SDK wrapper
|
|
77
|
+
*/
|
|
78
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
79
|
+
/**
|
|
80
|
+
* Token storage configuration options
|
|
81
|
+
*/
|
|
82
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
86
|
+
* These override environment variables for that specific request
|
|
87
|
+
*/
|
|
88
|
+
interface StorefrontRuntimeConfig {
|
|
89
|
+
/**
|
|
90
|
+
* Override environment variables
|
|
91
|
+
*/
|
|
92
|
+
storeId?: string;
|
|
93
|
+
apiKey?: string;
|
|
94
|
+
environment?: Environment;
|
|
95
|
+
baseUrl?: string;
|
|
96
|
+
timeout?: number;
|
|
97
|
+
debug?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Advanced options not available via environment variables
|
|
100
|
+
*/
|
|
101
|
+
accessToken?: string;
|
|
102
|
+
refreshToken?: string;
|
|
103
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
104
|
+
logger?: DebugLoggerFn;
|
|
105
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
106
|
+
onTokensCleared?: () => void;
|
|
107
|
+
/**
|
|
108
|
+
* Token storage configuration
|
|
109
|
+
*/
|
|
110
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
111
|
+
}
|
|
112
|
+
type NextCookieStore = {
|
|
113
|
+
get: (name: string) => {
|
|
114
|
+
value: string;
|
|
115
|
+
} | undefined;
|
|
116
|
+
set: (name: string, value: string, options?: any) => void;
|
|
117
|
+
delete: (name: string) => void;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Smart SDK getter that automatically detects environment
|
|
121
|
+
*
|
|
122
|
+
* Usage:
|
|
123
|
+
* - Client-side: getStorefrontSDK()
|
|
124
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
125
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
126
|
+
*/
|
|
127
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
128
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
129
|
+
/**
|
|
130
|
+
* Set global storefront configuration that applies to all SDK instances
|
|
131
|
+
* This gets bundled into both client and server contexts automatically
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Initialize the SDK (internal use)
|
|
136
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
137
|
+
*/
|
|
138
|
+
declare function initializeStorefrontSDK(): void;
|
|
139
|
+
//#endregion
|
|
140
|
+
export { ClientTokenStorage, NextJSSDKConfig, NextJSTokenStorageOptions, ServerTokenStorage, StorefrontRuntimeConfig, getStorefrontSDK, initializeStorefrontSDK };
|
|
141
|
+
//# sourceMappingURL=sdk-manager-B9xDQQuv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk-manager-B9xDQQuv.d.ts","names":[],"sources":["../src/token-storage.ts","../src/sdk-manager.ts"],"sourcesContent":[],"mappings":";;;;;;AAKA;AA8Ca,UA9CI,yBAAA,CA8Ce;EAAA;;;QAuBO,CAAA,EAAA,MAAA;;;;QAvBI,CAAA,EAAA,MAAA;;AAkG1C;AAYD;EAAgC,IAAA,CAAA,EAAA,MAAA;;;;QA8BO,CAAA,EAAA,MAAA;;;;QA9BI,CAAA,EAAA,OAAA;;;;;AC9I3C;;;;AACU,cD+BG,kBAAA,YAA8B,YC/BjC,CAAA;;EAWO,QAAA,eAAA;EAAuB,QAAA,OAAA;aAMxB,CAAA,OAAA,CAAA,EDmBO,yBCnBP;gBAUG,CAAA,CAAA,EDuBO,OCvBP,CAAA,MAAA,GAAA,IAAA,CAAA;gBACR,CAAA,KAAA,EAAA,MAAA,CAAA,ED0B4B,OC1B5B,CAAA,IAAA,CAAA;iBAOa,CAAA,CAAA,EDuBG,OCvBH,CAAA,MAAA,GAAA,IAAA,CAAA;kCD2BgB;ECuInC,WAAA,CAAA,CAAA,EDnIkB,OCmIH,CAAA,IAAA,CAAA;EAcJ,QAAA,SAAA;EACA,QAAA,SAAA;EAAgB,QAAA,YAAA;;KDhF3B,iBAAA,GCgF2D;;IA+DhD,KAAA,EAAA,MAAA;;;;;;;;cDtIH,kBAAA,YAA8B;;;;;2BAMhB,6BAA0B;oBAe3B;iCASa;qBAeZ;kCASa;iBAejB;;;;;AAjOvB;AA8CA;AAAgC,UChCf,eAAA,SACP,ID+BsB,CC/BjB,oBD+BiB,EAAA,cAAA,CAAA,CAAA;;;;qBA2BL,CAAA,ECtDH,yBDsDG;;;;;AAuE1B;AAYY,UClII,uBAAA,CDkIe;EAAA;;;SAqBN,CAAA,EAAA,MAAA;QASa,CAAA,EAAA,MAAA;aAeZ,CAAA,ECzKX,WDyKW;SASa,CAAA,EAAA,MAAA;SAejB,CAAA,EAAA,MAAA;OArEoB,CAAA,EAAA,OAAA;;;;;EC9I1B,YAAA,CAAA,EAAA,MACf;EAAA,cAAA,CAAA,EA2BiB,uBA3BjB;QAAa,CAAA,EA4BJ,aA5BI;iBAIS,CAAA,EAAA,CAAA,WAAA,EAAA,MAAA,EAAA,YAAA,EAAA,MAAA,EAAA,GAAA,IAAA;iBAJd,CAAA,EAAA,GAAA,GAAA,IAAA;;AAWV;;qBAMgB,CAAA,EAkBQ,yBAlBR;;KAoLX,eAAA,GAzKM;KAOa,EAAA,CAAA,IAAA,EAAA,MAAA,EAAA,GAAA;;EAkKnB,CAAA,GAAA,SAAA;EAcW,GAAA,EAAA,CAAA,IAAA,EAAA,MAAA,EAAgB,KAAA,EAAA,MAAI,EAAA,OAAA,CAAA,EAAA,GAAA,EAAA,GAAA,IAAA;EACpB,MAAA,EAAA,CAAA,IAAA,EAAA,MAAgB,EAAA,GAAA,IAAA;CAAA;;;;AA+DhC;;;;;iBAhEgB,gBAAA,CAAA,GAAoB;iBACpB,gBAAA,cAA8B,kBAAkB;;;;;;;;;;iBA+DhD,uBAAA,CAAA"}
|