@commercengine/storefront-sdk-nextjs 0.1.0-alpha.1 → 1.0.0-alpha.2
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 +312 -438
- package/dist/client.cjs +68 -93
- package/dist/client.d.cts +40 -57
- package/dist/client.d.ts +40 -57
- package/dist/client.js +68 -88
- package/dist/index.cjs +107 -92
- package/dist/index.d.cts +29 -95
- package/dist/index.d.ts +29 -95
- package/dist/index.js +106 -83
- package/dist/server-ByBPoXJG.d.cts +182 -0
- package/dist/server-ByBPoXJG.d.ts +182 -0
- package/dist/server-CwxgXezP.d.cts +115 -0
- package/dist/server-CwxgXezP.d.ts +115 -0
- package/dist/server.cjs +57 -88
- package/dist/server.d.cts +1 -2
- package/dist/server.d.ts +1 -2
- package/dist/server.js +57 -84
- package/dist/storefront.cjs +93 -21
- package/dist/storefront.d.cts +2 -40
- package/dist/storefront.d.ts +2 -40
- package/dist/storefront.js +93 -21
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,107 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
export { default as storefront } from './storefront.js';
|
|
1
|
+
import { StorefrontSDK } from '@commercengine/storefront-sdk';
|
|
2
|
+
export { StorefrontSDK } from '@commercengine/storefront-sdk';
|
|
3
|
+
import { S as StorefrontRuntimeConfig } from './server-CwxgXezP.js';
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* Configuration options for NextJSTokenStorage
|
|
8
|
-
*/
|
|
9
|
-
interface NextJSTokenStorageOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Prefix for cookie names (default: "ce_")
|
|
12
|
-
*/
|
|
13
|
-
prefix?: string;
|
|
14
|
-
/**
|
|
15
|
-
* Maximum age of cookies in seconds (default: 30 days)
|
|
16
|
-
*/
|
|
17
|
-
maxAge?: number;
|
|
18
|
-
/**
|
|
19
|
-
* Cookie path (default: "/")
|
|
20
|
-
*/
|
|
21
|
-
path?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Cookie domain (default: current domain)
|
|
24
|
-
*/
|
|
25
|
-
domain?: string;
|
|
26
|
-
/**
|
|
27
|
-
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
28
|
-
*/
|
|
29
|
-
secure?: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* SameSite cookie attribute (default: "Lax")
|
|
32
|
-
*/
|
|
33
|
-
sameSite?: "Strict" | "Lax" | "None";
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Client-side token storage that uses document.cookie
|
|
37
|
-
*/
|
|
38
|
-
declare class ClientTokenStorage implements TokenStorage {
|
|
39
|
-
private accessTokenKey;
|
|
40
|
-
private refreshTokenKey;
|
|
41
|
-
private options;
|
|
42
|
-
constructor(options?: NextJSTokenStorageOptions);
|
|
43
|
-
getAccessToken(): Promise<string | null>;
|
|
44
|
-
setAccessToken(token: string): Promise<void>;
|
|
45
|
-
getRefreshToken(): Promise<string | null>;
|
|
46
|
-
setRefreshToken(token: string): Promise<void>;
|
|
47
|
-
clearTokens(): Promise<void>;
|
|
48
|
-
private getCookie;
|
|
49
|
-
private setCookie;
|
|
50
|
-
private deleteCookie;
|
|
51
|
-
}
|
|
52
|
-
type NextCookieStore$1 = {
|
|
53
|
-
get: (name: string) => {
|
|
54
|
-
value: string;
|
|
55
|
-
} | undefined;
|
|
56
|
-
set: (name: string, value: string, options?: any) => void;
|
|
57
|
-
delete: (name: string) => void;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Server-side token storage that uses Next.js cookies API
|
|
61
|
-
*/
|
|
62
|
-
declare class ServerTokenStorage implements TokenStorage {
|
|
63
|
-
private accessTokenKey;
|
|
64
|
-
private refreshTokenKey;
|
|
65
|
-
private options;
|
|
66
|
-
private cookieStore;
|
|
67
|
-
constructor(cookieStore: NextCookieStore$1, options?: NextJSTokenStorageOptions);
|
|
68
|
-
getAccessToken(): Promise<string | null>;
|
|
69
|
-
setAccessToken(token: string): Promise<void>;
|
|
70
|
-
getRefreshToken(): Promise<string | null>;
|
|
71
|
-
setRefreshToken(token: string): Promise<void>;
|
|
72
|
-
clearTokens(): Promise<void>;
|
|
73
|
-
}
|
|
74
|
-
|
|
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
5
|
type NextCookieStore = {
|
|
85
6
|
get: (name: string) => {
|
|
7
|
+
name: string;
|
|
86
8
|
value: string;
|
|
87
9
|
} | undefined;
|
|
88
|
-
set: (name: string, value: string,
|
|
10
|
+
set: (name: string, value: string, opts?: Record<string, unknown>) => void;
|
|
89
11
|
delete: (name: string) => void;
|
|
90
12
|
};
|
|
91
13
|
/**
|
|
92
|
-
*
|
|
14
|
+
* Creates a configured storefront function that works universally across all Next.js contexts
|
|
93
15
|
*
|
|
94
16
|
* Usage:
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
103
|
-
*
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // lib/storefront.ts
|
|
19
|
+
* import { createStorefront } from "@commercengine/storefront-sdk-nextjs";
|
|
20
|
+
*
|
|
21
|
+
* export const storefront = createStorefront({
|
|
22
|
+
* debug: true,
|
|
23
|
+
* logger: (msg, ...args) => console.log('[DEBUG]', msg, ...args)
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param config Optional configuration for advanced options (defaults to empty if not provided)
|
|
28
|
+
* @returns A storefront function that works in all Next.js contexts
|
|
104
29
|
*/
|
|
105
|
-
declare function
|
|
30
|
+
declare function createStorefront(config?: StorefrontRuntimeConfig): {
|
|
31
|
+
(): StorefrontSDK;
|
|
32
|
+
(cookieStore: NextCookieStore): StorefrontSDK;
|
|
33
|
+
(options: {
|
|
34
|
+
isRootLayout: true;
|
|
35
|
+
}): StorefrontSDK;
|
|
36
|
+
(cookieStore: NextCookieStore, options: {
|
|
37
|
+
isRootLayout?: boolean;
|
|
38
|
+
}): StorefrontSDK;
|
|
39
|
+
};
|
|
106
40
|
|
|
107
|
-
export {
|
|
41
|
+
export { StorefrontRuntimeConfig, createStorefront };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
1
|
// src/sdk-manager.ts
|
|
9
2
|
import { cache } from "react";
|
|
10
3
|
import {
|
|
@@ -192,21 +185,17 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
192
185
|
}
|
|
193
186
|
async getAccessToken() {
|
|
194
187
|
if (this.access) {
|
|
195
|
-
console.log(`\u{1F535} [BuildCache] Using instance token for key: ${this.cacheKey}`);
|
|
196
188
|
return this.access;
|
|
197
189
|
}
|
|
198
190
|
const cached = getCachedToken(this.cacheKey);
|
|
199
191
|
if (cached?.accessToken) {
|
|
200
|
-
console.log(`\u{1F7E2} [BuildCache] Using cached token for key: ${this.cacheKey}`);
|
|
201
192
|
this.access = cached.accessToken;
|
|
202
193
|
this.refresh = cached.refreshToken ?? null;
|
|
203
194
|
return this.access;
|
|
204
195
|
}
|
|
205
|
-
console.log(`\u{1F7E1} [BuildCache] No cached token found for key: ${this.cacheKey}`);
|
|
206
196
|
return null;
|
|
207
197
|
}
|
|
208
198
|
async setAccessToken(token) {
|
|
209
|
-
console.log(`\u{1F7E0} [BuildCache] Caching new access token for key: ${this.cacheKey}`);
|
|
210
199
|
this.access = token;
|
|
211
200
|
setCachedToken(this.cacheKey, {
|
|
212
201
|
accessToken: token,
|
|
@@ -233,30 +222,63 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
233
222
|
};
|
|
234
223
|
|
|
235
224
|
// src/sdk-manager.ts
|
|
236
|
-
var globalConfig = null;
|
|
237
|
-
function getEnvConfig() {
|
|
238
|
-
return {
|
|
239
|
-
storeId: process.env.NEXT_PUBLIC_STORE_ID || "",
|
|
240
|
-
environment: process.env.NEXT_PUBLIC_ENVIRONMENT === "production" ? Environment.Production : Environment.Staging,
|
|
241
|
-
apiKey: process.env.NEXT_PUBLIC_API_KEY
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
225
|
function getConfig() {
|
|
245
|
-
|
|
246
|
-
|
|
226
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
227
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
228
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
229
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
230
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
231
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
232
|
+
const envDefaultHeaders = {};
|
|
233
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
234
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
247
235
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
236
|
+
const storeId = globalStorefrontConfig?.storeId || envStoreId;
|
|
237
|
+
const apiKey = globalStorefrontConfig?.apiKey || envApiKey;
|
|
238
|
+
const environment = globalStorefrontConfig?.environment || (envEnvironment === "production" ? Environment.Production : Environment.Staging);
|
|
239
|
+
const baseUrl = globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
240
|
+
const timeout = globalStorefrontConfig?.timeout || envTimeout;
|
|
241
|
+
const debug = globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
242
|
+
const defaultHeaders = {
|
|
243
|
+
...envDefaultHeaders,
|
|
244
|
+
...globalStorefrontConfig?.defaultHeaders
|
|
245
|
+
};
|
|
246
|
+
if (!storeId || !apiKey) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
249
|
+
|
|
250
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
251
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
252
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
253
|
+
|
|
254
|
+
These variables are required for both client and server contexts to work.`
|
|
255
|
+
);
|
|
258
256
|
}
|
|
257
|
+
const config = {
|
|
258
|
+
storeId,
|
|
259
|
+
apiKey,
|
|
260
|
+
environment
|
|
261
|
+
};
|
|
262
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
263
|
+
if (timeout) config.timeout = timeout;
|
|
264
|
+
if (debug) config.debug = debug;
|
|
265
|
+
const logger = globalStorefrontConfig?.logger;
|
|
266
|
+
const accessToken = globalStorefrontConfig?.accessToken;
|
|
267
|
+
const refreshToken = globalStorefrontConfig?.refreshToken;
|
|
268
|
+
const onTokensUpdated = globalStorefrontConfig?.onTokensUpdated;
|
|
269
|
+
const onTokensCleared = globalStorefrontConfig?.onTokensCleared;
|
|
270
|
+
const tokenStorageOptions = globalStorefrontConfig?.tokenStorageOptions;
|
|
271
|
+
if (logger) config.logger = logger;
|
|
272
|
+
if (accessToken) config.accessToken = accessToken;
|
|
273
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
274
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
275
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
276
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
277
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
278
|
+
return config;
|
|
259
279
|
}
|
|
280
|
+
var globalStorefrontConfig = null;
|
|
281
|
+
var clientSDK = null;
|
|
260
282
|
function createTokenStorage(cookieStore, options, config) {
|
|
261
283
|
if (typeof window !== "undefined") {
|
|
262
284
|
return new ClientTokenStorage(options);
|
|
@@ -267,14 +289,12 @@ function createTokenStorage(cookieStore, options, config) {
|
|
|
267
289
|
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
268
290
|
if (shouldCache && config) {
|
|
269
291
|
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
270
|
-
console.log(`\u{1F680} [BuildCache] Using BuildCachingMemoryTokenStorage with key: ${cacheKey}`);
|
|
271
292
|
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
272
293
|
}
|
|
273
|
-
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
274
294
|
return new MemoryTokenStorage();
|
|
275
295
|
}
|
|
276
296
|
var getServerSDKCached = cache((cookieStore) => {
|
|
277
|
-
const config =
|
|
297
|
+
const config = getConfig();
|
|
278
298
|
return new StorefrontSDK({
|
|
279
299
|
...config,
|
|
280
300
|
tokenStorage: createTokenStorage(
|
|
@@ -286,7 +306,7 @@ var getServerSDKCached = cache((cookieStore) => {
|
|
|
286
306
|
});
|
|
287
307
|
var buildTimeSDK = null;
|
|
288
308
|
function getBuildTimeSDK() {
|
|
289
|
-
const config =
|
|
309
|
+
const config = getConfig();
|
|
290
310
|
if (!buildTimeSDK) {
|
|
291
311
|
buildTimeSDK = new StorefrontSDK({
|
|
292
312
|
...config,
|
|
@@ -322,59 +342,62 @@ function getStorefrontSDK(cookieStore) {
|
|
|
322
342
|
if (cookieStore) {
|
|
323
343
|
return getServerSDKCached(cookieStore);
|
|
324
344
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
333
|
-
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
334
|
-
- Server Component in App Router (use: const sdk = getStorefrontSDK(cookies()))
|
|
335
|
-
`;
|
|
336
|
-
} catch {
|
|
337
|
-
autoDetectMessage = `
|
|
345
|
+
return getBuildTimeSDK();
|
|
346
|
+
}
|
|
347
|
+
function setGlobalStorefrontConfig(config) {
|
|
348
|
+
globalStorefrontConfig = config;
|
|
349
|
+
clientSDK = null;
|
|
350
|
+
buildTimeSDK = null;
|
|
351
|
+
}
|
|
338
352
|
|
|
339
|
-
|
|
340
|
-
|
|
353
|
+
// src/create-storefront.ts
|
|
354
|
+
function createStorefront(config) {
|
|
355
|
+
if (config) {
|
|
356
|
+
setGlobalStorefrontConfig(config);
|
|
357
|
+
}
|
|
358
|
+
function storefront(cookieStoreOrOptions, options) {
|
|
359
|
+
if (typeof window !== "undefined") {
|
|
360
|
+
return getStorefrontSDK();
|
|
361
|
+
}
|
|
362
|
+
let cookieStore;
|
|
363
|
+
let isRootLayout = false;
|
|
364
|
+
if (cookieStoreOrOptions) {
|
|
365
|
+
if ("isRootLayout" in cookieStoreOrOptions) {
|
|
366
|
+
isRootLayout = cookieStoreOrOptions.isRootLayout;
|
|
367
|
+
} else {
|
|
368
|
+
cookieStore = cookieStoreOrOptions;
|
|
369
|
+
isRootLayout = options?.isRootLayout || false;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (cookieStore) {
|
|
373
|
+
return getStorefrontSDK(cookieStore);
|
|
374
|
+
}
|
|
375
|
+
if (process.env.NEXT_IS_BUILD === "true" || process.env.NEXT_BUILD_CACHE_TOKENS === "true") {
|
|
376
|
+
return getStorefrontSDK();
|
|
377
|
+
}
|
|
378
|
+
if (isRootLayout) {
|
|
379
|
+
return getStorefrontSDK();
|
|
341
380
|
}
|
|
342
381
|
throw new Error(
|
|
343
|
-
|
|
344
|
-
\u{1F6A8} Server
|
|
345
|
-
|
|
346
|
-
You're calling
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
\u2705 Correct usage:
|
|
350
|
-
import { cookies } from 'next/headers'
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
\u274C Your current usage:
|
|
359
|
-
const sdk = getStorefrontSDK(); // Missing cookies!
|
|
360
|
-
${autoDetectMessage}
|
|
361
|
-
This is required for server-side token access.
|
|
362
|
-
`.trim()
|
|
382
|
+
[
|
|
383
|
+
"\u{1F6A8} Server context requires cookies for user continuity!",
|
|
384
|
+
"",
|
|
385
|
+
"You're calling storefront() on the server without cookies.",
|
|
386
|
+
"This breaks user session continuity and analytics tracking.",
|
|
387
|
+
"",
|
|
388
|
+
"\u2705 Correct usage:",
|
|
389
|
+
" import { cookies } from 'next/headers'",
|
|
390
|
+
" const sdk = storefront(cookies())",
|
|
391
|
+
"",
|
|
392
|
+
"\u{1F4CD} Root Layout exception:",
|
|
393
|
+
" const sdk = storefront({ isRootLayout: true })",
|
|
394
|
+
"",
|
|
395
|
+
"This is required to maintain consistent user identity across requests."
|
|
396
|
+
].join("\n")
|
|
363
397
|
);
|
|
364
398
|
}
|
|
365
|
-
return
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// src/storefront.ts
|
|
369
|
-
function storefront(cookieStore) {
|
|
370
|
-
return getStorefrontSDK(cookieStore);
|
|
399
|
+
return storefront;
|
|
371
400
|
}
|
|
372
|
-
|
|
373
|
-
// src/index.ts
|
|
374
|
-
export * from "@commercengine/storefront-sdk";
|
|
375
401
|
export {
|
|
376
|
-
|
|
377
|
-
ServerTokenStorage,
|
|
378
|
-
getStorefrontSDK,
|
|
379
|
-
storefront
|
|
402
|
+
createStorefront
|
|
380
403
|
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { TokenStorage, StorefrontSDK, Environment, SupportedDefaultHeaders, DebugLoggerFn, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for NextJSTokenStorage
|
|
5
|
+
*/
|
|
6
|
+
interface NextJSTokenStorageOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix for cookie names (default: "ce_")
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
13
|
+
*/
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Cookie path (default: "/")
|
|
17
|
+
*/
|
|
18
|
+
path?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Cookie domain (default: current domain)
|
|
21
|
+
*/
|
|
22
|
+
domain?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
25
|
+
*/
|
|
26
|
+
secure?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* SameSite cookie attribute (default: "Lax")
|
|
29
|
+
*/
|
|
30
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Client-side token storage that uses document.cookie
|
|
34
|
+
*/
|
|
35
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
36
|
+
private accessTokenKey;
|
|
37
|
+
private refreshTokenKey;
|
|
38
|
+
private options;
|
|
39
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
40
|
+
getAccessToken(): Promise<string | null>;
|
|
41
|
+
setAccessToken(token: string): Promise<void>;
|
|
42
|
+
getRefreshToken(): Promise<string | null>;
|
|
43
|
+
setRefreshToken(token: string): Promise<void>;
|
|
44
|
+
clearTokens(): Promise<void>;
|
|
45
|
+
private getCookie;
|
|
46
|
+
private setCookie;
|
|
47
|
+
private deleteCookie;
|
|
48
|
+
}
|
|
49
|
+
type NextCookieStore$2 = {
|
|
50
|
+
get: (name: string) => {
|
|
51
|
+
value: string;
|
|
52
|
+
} | undefined;
|
|
53
|
+
set: (name: string, value: string, options?: any) => void;
|
|
54
|
+
delete: (name: string) => void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Server-side token storage that uses Next.js cookies API
|
|
58
|
+
*/
|
|
59
|
+
declare class ServerTokenStorage implements TokenStorage {
|
|
60
|
+
private accessTokenKey;
|
|
61
|
+
private refreshTokenKey;
|
|
62
|
+
private options;
|
|
63
|
+
private cookieStore;
|
|
64
|
+
constructor(cookieStore: NextCookieStore$2, options?: NextJSTokenStorageOptions);
|
|
65
|
+
getAccessToken(): Promise<string | null>;
|
|
66
|
+
setAccessToken(token: string): Promise<void>;
|
|
67
|
+
getRefreshToken(): Promise<string | null>;
|
|
68
|
+
setRefreshToken(token: string): Promise<void>;
|
|
69
|
+
clearTokens(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Configuration for the NextJS SDK wrapper
|
|
74
|
+
*/
|
|
75
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
76
|
+
/**
|
|
77
|
+
* Token storage configuration options
|
|
78
|
+
*/
|
|
79
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
83
|
+
* These override environment variables for that specific request
|
|
84
|
+
*/
|
|
85
|
+
interface StorefrontRuntimeConfig {
|
|
86
|
+
/**
|
|
87
|
+
* Override environment variables
|
|
88
|
+
*/
|
|
89
|
+
storeId?: string;
|
|
90
|
+
apiKey?: string;
|
|
91
|
+
environment?: Environment;
|
|
92
|
+
baseUrl?: string;
|
|
93
|
+
timeout?: number;
|
|
94
|
+
debug?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Advanced options not available via environment variables
|
|
97
|
+
*/
|
|
98
|
+
accessToken?: string;
|
|
99
|
+
refreshToken?: string;
|
|
100
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
101
|
+
logger?: DebugLoggerFn;
|
|
102
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
103
|
+
onTokensCleared?: () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Token storage configuration
|
|
106
|
+
*/
|
|
107
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
108
|
+
}
|
|
109
|
+
type NextCookieStore$1 = {
|
|
110
|
+
get: (name: string) => {
|
|
111
|
+
value: string;
|
|
112
|
+
} | undefined;
|
|
113
|
+
set: (name: string, value: string, options?: any) => void;
|
|
114
|
+
delete: (name: string) => void;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Smart SDK getter that automatically detects environment
|
|
118
|
+
*
|
|
119
|
+
* Usage:
|
|
120
|
+
* - Client-side: getStorefrontSDK()
|
|
121
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
122
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
123
|
+
*/
|
|
124
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
125
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1): StorefrontSDK;
|
|
126
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore$1, requestConfig?: StorefrontRuntimeConfig): StorefrontSDK;
|
|
127
|
+
/**
|
|
128
|
+
* Set global storefront configuration that applies to all SDK instances
|
|
129
|
+
* This gets bundled into both client and server contexts automatically
|
|
130
|
+
*/
|
|
131
|
+
declare function setGlobalStorefrontConfig(config: StorefrontRuntimeConfig | null): void;
|
|
132
|
+
/**
|
|
133
|
+
* Get the current global storefront configuration
|
|
134
|
+
*/
|
|
135
|
+
declare function getGlobalStorefrontConfig(): StorefrontRuntimeConfig | null;
|
|
136
|
+
/**
|
|
137
|
+
* Initialize the SDK (internal use)
|
|
138
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
139
|
+
*/
|
|
140
|
+
declare function initializeStorefrontSDK(): void;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Universal storefront SDK export
|
|
144
|
+
*
|
|
145
|
+
* This provides a simple unified interface that works in all Next.js contexts:
|
|
146
|
+
* - Client components: automatically uses client token storage
|
|
147
|
+
* - Server components: requires cookies() to be passed
|
|
148
|
+
* - SSG/ISR: automatically uses memory storage with optional build caching
|
|
149
|
+
*
|
|
150
|
+
* Usage:
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Client-side
|
|
153
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
154
|
+
* const products = await storefront().catalog.listProducts()
|
|
155
|
+
*
|
|
156
|
+
* // Server-side
|
|
157
|
+
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
158
|
+
* import { cookies } from 'next/headers'
|
|
159
|
+
* const products = await storefront(cookies()).catalog.listProducts()
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
type NextCookieStore = {
|
|
164
|
+
get: (name: string) => {
|
|
165
|
+
value: string;
|
|
166
|
+
} | undefined;
|
|
167
|
+
set: (name: string, value: string, options?: any) => void;
|
|
168
|
+
delete: (name: string) => void;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Universal storefront SDK accessor
|
|
172
|
+
*
|
|
173
|
+
* @param cookieStore - Next.js cookie store (required on server-side)
|
|
174
|
+
* @param config - Optional per-request configuration overrides
|
|
175
|
+
* @returns StorefrontSDK instance configured for the current environment
|
|
176
|
+
*/
|
|
177
|
+
declare function storefront(): StorefrontSDK;
|
|
178
|
+
declare function storefront(cookieStore: NextCookieStore): StorefrontSDK;
|
|
179
|
+
declare function storefront(config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
180
|
+
declare function storefront(cookieStore: NextCookieStore, config: StorefrontRuntimeConfig): StorefrontSDK;
|
|
181
|
+
|
|
182
|
+
export { ClientTokenStorage as C, type NextJSSDKConfig as N, type StorefrontRuntimeConfig as S, getGlobalStorefrontConfig as a, storefront as b, ServerTokenStorage as c, type NextJSTokenStorageOptions as d, getStorefrontSDK as g, initializeStorefrontSDK as i, setGlobalStorefrontConfig as s };
|