@commercengine/storefront-sdk-nextjs 0.1.0-alpha.1 → 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 +333 -440
- 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 +142 -92
- package/dist/index.d.cts +29 -94
- package/dist/index.d.ts +29 -94
- package/dist/index.js +142 -82
- 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/client.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
-
}) : x)(function(x) {
|
|
5
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
-
});
|
|
8
2
|
|
|
9
3
|
// src/sdk-manager.ts
|
|
10
4
|
import { cache } from "react";
|
|
@@ -193,21 +187,17 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
193
187
|
}
|
|
194
188
|
async getAccessToken() {
|
|
195
189
|
if (this.access) {
|
|
196
|
-
console.log(`\u{1F535} [BuildCache] Using instance token for key: ${this.cacheKey}`);
|
|
197
190
|
return this.access;
|
|
198
191
|
}
|
|
199
192
|
const cached = getCachedToken(this.cacheKey);
|
|
200
193
|
if (cached?.accessToken) {
|
|
201
|
-
console.log(`\u{1F7E2} [BuildCache] Using cached token for key: ${this.cacheKey}`);
|
|
202
194
|
this.access = cached.accessToken;
|
|
203
195
|
this.refresh = cached.refreshToken ?? null;
|
|
204
196
|
return this.access;
|
|
205
197
|
}
|
|
206
|
-
console.log(`\u{1F7E1} [BuildCache] No cached token found for key: ${this.cacheKey}`);
|
|
207
198
|
return null;
|
|
208
199
|
}
|
|
209
200
|
async setAccessToken(token) {
|
|
210
|
-
console.log(`\u{1F7E0} [BuildCache] Caching new access token for key: ${this.cacheKey}`);
|
|
211
201
|
this.access = token;
|
|
212
202
|
setCachedToken(this.cacheKey, {
|
|
213
203
|
accessToken: token,
|
|
@@ -234,30 +224,63 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
234
224
|
};
|
|
235
225
|
|
|
236
226
|
// src/sdk-manager.ts
|
|
237
|
-
var globalConfig = null;
|
|
238
|
-
function getEnvConfig() {
|
|
239
|
-
return {
|
|
240
|
-
storeId: process.env.NEXT_PUBLIC_STORE_ID || "",
|
|
241
|
-
environment: process.env.NEXT_PUBLIC_ENVIRONMENT === "production" ? Environment.Production : Environment.Staging,
|
|
242
|
-
apiKey: process.env.NEXT_PUBLIC_API_KEY
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
227
|
function getConfig() {
|
|
246
|
-
|
|
247
|
-
|
|
228
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
229
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
230
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
231
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
232
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
233
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
234
|
+
const envDefaultHeaders = {};
|
|
235
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
236
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
248
237
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
238
|
+
const storeId = globalStorefrontConfig?.storeId || envStoreId;
|
|
239
|
+
const apiKey = globalStorefrontConfig?.apiKey || envApiKey;
|
|
240
|
+
const environment = globalStorefrontConfig?.environment || (envEnvironment === "production" ? Environment.Production : Environment.Staging);
|
|
241
|
+
const baseUrl = globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
242
|
+
const timeout = globalStorefrontConfig?.timeout || envTimeout;
|
|
243
|
+
const debug = globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
244
|
+
const defaultHeaders = {
|
|
245
|
+
...envDefaultHeaders,
|
|
246
|
+
...globalStorefrontConfig?.defaultHeaders
|
|
247
|
+
};
|
|
248
|
+
if (!storeId || !apiKey) {
|
|
249
|
+
throw new Error(
|
|
250
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
251
|
+
|
|
252
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
253
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
254
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
255
|
+
|
|
256
|
+
These variables are required for both client and server contexts to work.`
|
|
257
|
+
);
|
|
259
258
|
}
|
|
259
|
+
const config = {
|
|
260
|
+
storeId,
|
|
261
|
+
apiKey,
|
|
262
|
+
environment
|
|
263
|
+
};
|
|
264
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
265
|
+
if (timeout) config.timeout = timeout;
|
|
266
|
+
if (debug) config.debug = debug;
|
|
267
|
+
const logger = globalStorefrontConfig?.logger;
|
|
268
|
+
const accessToken = globalStorefrontConfig?.accessToken;
|
|
269
|
+
const refreshToken = globalStorefrontConfig?.refreshToken;
|
|
270
|
+
const onTokensUpdated = globalStorefrontConfig?.onTokensUpdated;
|
|
271
|
+
const onTokensCleared = globalStorefrontConfig?.onTokensCleared;
|
|
272
|
+
const tokenStorageOptions = globalStorefrontConfig?.tokenStorageOptions;
|
|
273
|
+
if (logger) config.logger = logger;
|
|
274
|
+
if (accessToken) config.accessToken = accessToken;
|
|
275
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
276
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
277
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
278
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
279
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
280
|
+
return config;
|
|
260
281
|
}
|
|
282
|
+
var globalStorefrontConfig = null;
|
|
283
|
+
var clientSDK = null;
|
|
261
284
|
function createTokenStorage(cookieStore, options, config) {
|
|
262
285
|
if (typeof window !== "undefined") {
|
|
263
286
|
return new ClientTokenStorage(options);
|
|
@@ -268,14 +291,12 @@ function createTokenStorage(cookieStore, options, config) {
|
|
|
268
291
|
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
269
292
|
if (shouldCache && config) {
|
|
270
293
|
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
271
|
-
console.log(`\u{1F680} [BuildCache] Using BuildCachingMemoryTokenStorage with key: ${cacheKey}`);
|
|
272
294
|
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
273
295
|
}
|
|
274
|
-
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
275
296
|
return new MemoryTokenStorage();
|
|
276
297
|
}
|
|
277
298
|
var getServerSDKCached = cache((cookieStore) => {
|
|
278
|
-
const config =
|
|
299
|
+
const config = getConfig();
|
|
279
300
|
return new StorefrontSDK({
|
|
280
301
|
...config,
|
|
281
302
|
tokenStorage: createTokenStorage(
|
|
@@ -287,7 +308,7 @@ var getServerSDKCached = cache((cookieStore) => {
|
|
|
287
308
|
});
|
|
288
309
|
var buildTimeSDK = null;
|
|
289
310
|
function getBuildTimeSDK() {
|
|
290
|
-
const config =
|
|
311
|
+
const config = getConfig();
|
|
291
312
|
if (!buildTimeSDK) {
|
|
292
313
|
buildTimeSDK = new StorefrontSDK({
|
|
293
314
|
...config,
|
|
@@ -323,67 +344,27 @@ function getStorefrontSDK(cookieStore) {
|
|
|
323
344
|
if (cookieStore) {
|
|
324
345
|
return getServerSDKCached(cookieStore);
|
|
325
346
|
}
|
|
326
|
-
if (hasRequestContext()) {
|
|
327
|
-
let autoDetectMessage = "";
|
|
328
|
-
try {
|
|
329
|
-
__require.resolve("next/headers");
|
|
330
|
-
autoDetectMessage = `
|
|
331
|
-
|
|
332
|
-
\u{1F50D} Auto-detection attempted but failed. You may be in:
|
|
333
|
-
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
334
|
-
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
335
|
-
- Server Component in App Router (use: const sdk = getStorefrontSDK(cookies()))
|
|
336
|
-
`;
|
|
337
|
-
} catch {
|
|
338
|
-
autoDetectMessage = `
|
|
339
|
-
|
|
340
|
-
\u{1F4A1} Make sure you have Next.js installed and are in a server context.
|
|
341
|
-
`;
|
|
342
|
-
}
|
|
343
|
-
throw new Error(
|
|
344
|
-
`
|
|
345
|
-
\u{1F6A8} Server Environment Detected!
|
|
346
|
-
|
|
347
|
-
You're calling getStorefrontSDK() on the server without cookies.
|
|
348
|
-
Please pass the Next.js cookie store:
|
|
349
|
-
|
|
350
|
-
\u2705 Correct usage:
|
|
351
|
-
import { cookies } from 'next/headers';
|
|
352
|
-
|
|
353
|
-
// Server Actions & Route Handlers
|
|
354
|
-
const sdk = getStorefrontSDK(await cookies());
|
|
355
|
-
|
|
356
|
-
// API Routes & Server Components (App Router)
|
|
357
|
-
const sdk = getStorefrontSDK(cookies());
|
|
358
|
-
|
|
359
|
-
\u274C Your current usage:
|
|
360
|
-
const sdk = getStorefrontSDK(); // Missing cookies!
|
|
361
|
-
${autoDetectMessage}
|
|
362
|
-
This is required for server-side token access.
|
|
363
|
-
`.trim()
|
|
364
|
-
);
|
|
365
|
-
}
|
|
366
347
|
return getBuildTimeSDK();
|
|
367
348
|
}
|
|
368
|
-
function initializeStorefrontSDK(
|
|
369
|
-
globalConfig = config;
|
|
349
|
+
function initializeStorefrontSDK() {
|
|
370
350
|
clientSDK = null;
|
|
371
351
|
buildTimeSDK = null;
|
|
372
352
|
}
|
|
373
353
|
|
|
374
|
-
// src/storefront.ts
|
|
375
|
-
function storefront(cookieStore) {
|
|
376
|
-
return getStorefrontSDK(cookieStore);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
354
|
// src/init-client.ts
|
|
380
355
|
import { useEffect } from "react";
|
|
381
|
-
function
|
|
382
|
-
|
|
383
|
-
|
|
356
|
+
async function bootstrapTokens() {
|
|
357
|
+
const sdk = getStorefrontSDK();
|
|
358
|
+
const accessToken = await sdk.getAccessToken();
|
|
359
|
+
if (!accessToken) {
|
|
360
|
+
await sdk.auth.getAnonymousToken();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function StorefrontSDKInitializer({ runtimeConfig } = {}) {
|
|
384
364
|
useEffect(() => {
|
|
385
|
-
initializeStorefrontSDK(
|
|
386
|
-
|
|
365
|
+
initializeStorefrontSDK();
|
|
366
|
+
bootstrapTokens().catch(console.error);
|
|
367
|
+
}, [runtimeConfig]);
|
|
387
368
|
return null;
|
|
388
369
|
}
|
|
389
370
|
|
|
@@ -393,6 +374,5 @@ export {
|
|
|
393
374
|
ClientTokenStorage,
|
|
394
375
|
StorefrontSDKInitializer,
|
|
395
376
|
getStorefrontSDK,
|
|
396
|
-
initializeStorefrontSDK
|
|
397
|
-
storefront
|
|
377
|
+
initializeStorefrontSDK
|
|
398
378
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,26 +15,33 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
19
|
|
|
31
20
|
// src/index.ts
|
|
32
21
|
var index_exports = {};
|
|
33
22
|
__export(index_exports, {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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,
|
|
39
|
+
createStorefront: () => createStorefront
|
|
38
40
|
});
|
|
39
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");
|
|
40
45
|
|
|
41
46
|
// src/sdk-manager.ts
|
|
42
47
|
var import_react = require("react");
|
|
@@ -221,21 +226,17 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
221
226
|
}
|
|
222
227
|
async getAccessToken() {
|
|
223
228
|
if (this.access) {
|
|
224
|
-
console.log(`\u{1F535} [BuildCache] Using instance token for key: ${this.cacheKey}`);
|
|
225
229
|
return this.access;
|
|
226
230
|
}
|
|
227
231
|
const cached = getCachedToken(this.cacheKey);
|
|
228
232
|
if (cached?.accessToken) {
|
|
229
|
-
console.log(`\u{1F7E2} [BuildCache] Using cached token for key: ${this.cacheKey}`);
|
|
230
233
|
this.access = cached.accessToken;
|
|
231
234
|
this.refresh = cached.refreshToken ?? null;
|
|
232
235
|
return this.access;
|
|
233
236
|
}
|
|
234
|
-
console.log(`\u{1F7E1} [BuildCache] No cached token found for key: ${this.cacheKey}`);
|
|
235
237
|
return null;
|
|
236
238
|
}
|
|
237
239
|
async setAccessToken(token) {
|
|
238
|
-
console.log(`\u{1F7E0} [BuildCache] Caching new access token for key: ${this.cacheKey}`);
|
|
239
240
|
this.access = token;
|
|
240
241
|
setCachedToken(this.cacheKey, {
|
|
241
242
|
accessToken: token,
|
|
@@ -262,30 +263,63 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
262
263
|
};
|
|
263
264
|
|
|
264
265
|
// src/sdk-manager.ts
|
|
265
|
-
var globalConfig = null;
|
|
266
|
-
function getEnvConfig() {
|
|
267
|
-
return {
|
|
268
|
-
storeId: process.env.NEXT_PUBLIC_STORE_ID || "",
|
|
269
|
-
environment: process.env.NEXT_PUBLIC_ENVIRONMENT === "production" ? import_storefront_sdk.Environment.Production : import_storefront_sdk.Environment.Staging,
|
|
270
|
-
apiKey: process.env.NEXT_PUBLIC_API_KEY
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
266
|
function getConfig() {
|
|
274
|
-
|
|
275
|
-
|
|
267
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
268
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
269
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
270
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
271
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
272
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
273
|
+
const envDefaultHeaders = {};
|
|
274
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
275
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
276
276
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
277
|
+
const storeId = globalStorefrontConfig?.storeId || envStoreId;
|
|
278
|
+
const apiKey = globalStorefrontConfig?.apiKey || envApiKey;
|
|
279
|
+
const environment = globalStorefrontConfig?.environment || (envEnvironment === "production" ? import_storefront_sdk.Environment.Production : import_storefront_sdk.Environment.Staging);
|
|
280
|
+
const baseUrl = globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
281
|
+
const timeout = globalStorefrontConfig?.timeout || envTimeout;
|
|
282
|
+
const debug = globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
283
|
+
const defaultHeaders = {
|
|
284
|
+
...envDefaultHeaders,
|
|
285
|
+
...globalStorefrontConfig?.defaultHeaders
|
|
286
|
+
};
|
|
287
|
+
if (!storeId || !apiKey) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
290
|
+
|
|
291
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
292
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
293
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
294
|
+
|
|
295
|
+
These variables are required for both client and server contexts to work.`
|
|
296
|
+
);
|
|
287
297
|
}
|
|
298
|
+
const config = {
|
|
299
|
+
storeId,
|
|
300
|
+
apiKey,
|
|
301
|
+
environment
|
|
302
|
+
};
|
|
303
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
304
|
+
if (timeout) config.timeout = timeout;
|
|
305
|
+
if (debug) config.debug = debug;
|
|
306
|
+
const logger = globalStorefrontConfig?.logger;
|
|
307
|
+
const accessToken = globalStorefrontConfig?.accessToken;
|
|
308
|
+
const refreshToken = globalStorefrontConfig?.refreshToken;
|
|
309
|
+
const onTokensUpdated = globalStorefrontConfig?.onTokensUpdated;
|
|
310
|
+
const onTokensCleared = globalStorefrontConfig?.onTokensCleared;
|
|
311
|
+
const tokenStorageOptions = globalStorefrontConfig?.tokenStorageOptions;
|
|
312
|
+
if (logger) config.logger = logger;
|
|
313
|
+
if (accessToken) config.accessToken = accessToken;
|
|
314
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
315
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
316
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
317
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
318
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
319
|
+
return config;
|
|
288
320
|
}
|
|
321
|
+
var globalStorefrontConfig = null;
|
|
322
|
+
var clientSDK = null;
|
|
289
323
|
function createTokenStorage(cookieStore, options, config) {
|
|
290
324
|
if (typeof window !== "undefined") {
|
|
291
325
|
return new ClientTokenStorage(options);
|
|
@@ -296,14 +330,12 @@ function createTokenStorage(cookieStore, options, config) {
|
|
|
296
330
|
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
297
331
|
if (shouldCache && config) {
|
|
298
332
|
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
299
|
-
console.log(`\u{1F680} [BuildCache] Using BuildCachingMemoryTokenStorage with key: ${cacheKey}`);
|
|
300
333
|
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
301
334
|
}
|
|
302
|
-
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
303
335
|
return new import_storefront_sdk.MemoryTokenStorage();
|
|
304
336
|
}
|
|
305
337
|
var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
306
|
-
const config =
|
|
338
|
+
const config = getConfig();
|
|
307
339
|
return new import_storefront_sdk.StorefrontSDK({
|
|
308
340
|
...config,
|
|
309
341
|
tokenStorage: createTokenStorage(
|
|
@@ -315,7 +347,7 @@ var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
|
315
347
|
});
|
|
316
348
|
var buildTimeSDK = null;
|
|
317
349
|
function getBuildTimeSDK() {
|
|
318
|
-
const config =
|
|
350
|
+
const config = getConfig();
|
|
319
351
|
if (!buildTimeSDK) {
|
|
320
352
|
buildTimeSDK = new import_storefront_sdk.StorefrontSDK({
|
|
321
353
|
...config,
|
|
@@ -351,61 +383,79 @@ function getStorefrontSDK(cookieStore) {
|
|
|
351
383
|
if (cookieStore) {
|
|
352
384
|
return getServerSDKCached(cookieStore);
|
|
353
385
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
362
|
-
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
363
|
-
- Server Component in App Router (use: const sdk = getStorefrontSDK(cookies()))
|
|
364
|
-
`;
|
|
365
|
-
} catch {
|
|
366
|
-
autoDetectMessage = `
|
|
386
|
+
return getBuildTimeSDK();
|
|
387
|
+
}
|
|
388
|
+
function setGlobalStorefrontConfig(config) {
|
|
389
|
+
globalStorefrontConfig = config;
|
|
390
|
+
clientSDK = null;
|
|
391
|
+
buildTimeSDK = null;
|
|
392
|
+
}
|
|
367
393
|
|
|
368
|
-
|
|
369
|
-
|
|
394
|
+
// src/create-storefront.ts
|
|
395
|
+
function createStorefront(config) {
|
|
396
|
+
if (config) {
|
|
397
|
+
setGlobalStorefrontConfig(config);
|
|
398
|
+
}
|
|
399
|
+
function storefront(cookieStoreOrOptions, options) {
|
|
400
|
+
if (typeof window !== "undefined") {
|
|
401
|
+
return getStorefrontSDK();
|
|
402
|
+
}
|
|
403
|
+
let cookieStore;
|
|
404
|
+
let isRootLayout = false;
|
|
405
|
+
if (cookieStoreOrOptions) {
|
|
406
|
+
if ("isRootLayout" in cookieStoreOrOptions) {
|
|
407
|
+
isRootLayout = cookieStoreOrOptions.isRootLayout;
|
|
408
|
+
} else {
|
|
409
|
+
cookieStore = cookieStoreOrOptions;
|
|
410
|
+
isRootLayout = options?.isRootLayout || false;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (cookieStore) {
|
|
414
|
+
return getStorefrontSDK(cookieStore);
|
|
415
|
+
}
|
|
416
|
+
if (process.env.NEXT_IS_BUILD === "true" || process.env.NEXT_BUILD_CACHE_TOKENS === "true") {
|
|
417
|
+
return getStorefrontSDK();
|
|
418
|
+
}
|
|
419
|
+
if (isRootLayout) {
|
|
420
|
+
return getStorefrontSDK();
|
|
370
421
|
}
|
|
371
422
|
throw new Error(
|
|
372
|
-
|
|
373
|
-
\u{1F6A8} Server
|
|
374
|
-
|
|
375
|
-
You're calling
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
\u2705 Correct usage:
|
|
379
|
-
import { cookies } from 'next/headers'
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
\u274C Your current usage:
|
|
388
|
-
const sdk = getStorefrontSDK(); // Missing cookies!
|
|
389
|
-
${autoDetectMessage}
|
|
390
|
-
This is required for server-side token access.
|
|
391
|
-
`.trim()
|
|
423
|
+
[
|
|
424
|
+
"\u{1F6A8} Server context requires cookies for user continuity!",
|
|
425
|
+
"",
|
|
426
|
+
"You're calling storefront() on the server without cookies.",
|
|
427
|
+
"This breaks user session continuity and analytics tracking.",
|
|
428
|
+
"",
|
|
429
|
+
"\u2705 Correct usage:",
|
|
430
|
+
" import { cookies } from 'next/headers'",
|
|
431
|
+
" const sdk = storefront(cookies())",
|
|
432
|
+
"",
|
|
433
|
+
"\u{1F4CD} Root Layout exception:",
|
|
434
|
+
" const sdk = storefront({ isRootLayout: true })",
|
|
435
|
+
"",
|
|
436
|
+
"This is required to maintain consistent user identity across requests."
|
|
437
|
+
].join("\n")
|
|
392
438
|
);
|
|
393
439
|
}
|
|
394
|
-
return
|
|
440
|
+
return storefront;
|
|
395
441
|
}
|
|
396
|
-
|
|
397
|
-
// src/storefront.ts
|
|
398
|
-
function storefront(cookieStore) {
|
|
399
|
-
return getStorefrontSDK(cookieStore);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// src/index.ts
|
|
403
|
-
__reExport(index_exports, require("@commercengine/storefront-sdk"), module.exports);
|
|
404
442
|
// Annotate the CommonJS export names for ESM import in node:
|
|
405
443
|
0 && (module.exports = {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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,
|
|
460
|
+
createStorefront
|
|
411
461
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,107 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StorefrontSDK } from '@commercengine/storefront-sdk';
|
|
2
2
|
export * from '@commercengine/storefront-sdk';
|
|
3
|
-
export { StorefrontSDK,
|
|
4
|
-
|
|
3
|
+
export { AuthClient, BrowserTokenStorage, CartClient, CatalogClient, CookieTokenStorage, CustomerClient, Environment, HelpersClient, MemoryTokenStorage, OrderClient, ResponseUtils, ShippingClient, StoreConfigClient, StorefrontAPIClient, StorefrontSDK, TokenStorage } from '@commercengine/storefront-sdk';
|
|
4
|
+
import { S as StorefrontRuntimeConfig } from './server-CwxgXezP.cjs';
|
|
5
5
|
|
|
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
6
|
type NextCookieStore = {
|
|
85
7
|
get: (name: string) => {
|
|
8
|
+
name: string;
|
|
86
9
|
value: string;
|
|
87
10
|
} | undefined;
|
|
88
|
-
set: (name: string, value: string,
|
|
11
|
+
set: (name: string, value: string, opts?: Record<string, unknown>) => void;
|
|
89
12
|
delete: (name: string) => void;
|
|
90
13
|
};
|
|
91
14
|
/**
|
|
92
|
-
*
|
|
15
|
+
* Creates a configured storefront function that works universally across all Next.js contexts
|
|
93
16
|
*
|
|
94
17
|
* Usage:
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
103
|
-
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // lib/storefront.ts
|
|
20
|
+
* import { createStorefront } from "@commercengine/storefront-sdk-nextjs";
|
|
21
|
+
*
|
|
22
|
+
* export const storefront = createStorefront({
|
|
23
|
+
* debug: true,
|
|
24
|
+
* logger: (msg, ...args) => console.log('[DEBUG]', msg, ...args)
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
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
|
|
104
30
|
*/
|
|
105
|
-
declare function
|
|
31
|
+
declare function createStorefront(config?: StorefrontRuntimeConfig): {
|
|
32
|
+
(): StorefrontSDK;
|
|
33
|
+
(cookieStore: NextCookieStore): StorefrontSDK;
|
|
34
|
+
(options: {
|
|
35
|
+
isRootLayout: true;
|
|
36
|
+
}): StorefrontSDK;
|
|
37
|
+
(cookieStore: NextCookieStore, options: {
|
|
38
|
+
isRootLayout?: boolean;
|
|
39
|
+
}): StorefrontSDK;
|
|
40
|
+
};
|
|
106
41
|
|
|
107
|
-
export {
|
|
42
|
+
export { StorefrontRuntimeConfig, createStorefront };
|