@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/server.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,64 +342,17 @@ function getStorefrontSDK(cookieStore) {
|
|
|
322
342
|
if (cookieStore) {
|
|
323
343
|
return getServerSDKCached(cookieStore);
|
|
324
344
|
}
|
|
325
|
-
if (hasRequestContext()) {
|
|
326
|
-
let autoDetectMessage = "";
|
|
327
|
-
try {
|
|
328
|
-
__require.resolve("next/headers");
|
|
329
|
-
autoDetectMessage = `
|
|
330
|
-
|
|
331
|
-
\u{1F50D} Auto-detection attempted but failed. You may be in:
|
|
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 = `
|
|
338
|
-
|
|
339
|
-
\u{1F4A1} Make sure you have Next.js installed and are in a server context.
|
|
340
|
-
`;
|
|
341
|
-
}
|
|
342
|
-
throw new Error(
|
|
343
|
-
`
|
|
344
|
-
\u{1F6A8} Server Environment Detected!
|
|
345
|
-
|
|
346
|
-
You're calling getStorefrontSDK() on the server without cookies.
|
|
347
|
-
Please pass the Next.js cookie store:
|
|
348
|
-
|
|
349
|
-
\u2705 Correct usage:
|
|
350
|
-
import { cookies } from 'next/headers';
|
|
351
|
-
|
|
352
|
-
// Server Actions & Route Handlers
|
|
353
|
-
const sdk = getStorefrontSDK(await cookies());
|
|
354
|
-
|
|
355
|
-
// API Routes & Server Components (App Router)
|
|
356
|
-
const sdk = getStorefrontSDK(cookies());
|
|
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()
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
345
|
return getBuildTimeSDK();
|
|
366
346
|
}
|
|
367
|
-
function initializeStorefrontSDK(
|
|
368
|
-
globalConfig = config;
|
|
347
|
+
function initializeStorefrontSDK() {
|
|
369
348
|
clientSDK = null;
|
|
370
349
|
buildTimeSDK = null;
|
|
371
350
|
}
|
|
372
351
|
|
|
373
|
-
// src/storefront.ts
|
|
374
|
-
function storefront(cookieStore) {
|
|
375
|
-
return getStorefrontSDK(cookieStore);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
352
|
// src/server.ts
|
|
379
353
|
export * from "@commercengine/storefront-sdk";
|
|
380
354
|
export {
|
|
381
355
|
ServerTokenStorage,
|
|
382
356
|
getStorefrontSDK,
|
|
383
|
-
initializeStorefrontSDK
|
|
384
|
-
storefront
|
|
357
|
+
initializeStorefrontSDK
|
|
385
358
|
};
|
package/dist/storefront.cjs
CHANGED
|
@@ -259,20 +259,64 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
259
259
|
};
|
|
260
260
|
|
|
261
261
|
// src/sdk-manager.ts
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
262
|
+
function getConfig(requestConfig) {
|
|
263
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
264
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
265
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
266
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
267
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
268
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
269
|
+
const envDefaultHeaders = {};
|
|
270
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
271
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
272
|
+
}
|
|
273
|
+
const storeId = requestConfig?.storeId || globalStorefrontConfig?.storeId || envStoreId;
|
|
274
|
+
const apiKey = requestConfig?.apiKey || globalStorefrontConfig?.apiKey || envApiKey;
|
|
275
|
+
const environment = requestConfig?.environment || globalStorefrontConfig?.environment || (envEnvironment === "production" ? import_storefront_sdk.Environment.Production : import_storefront_sdk.Environment.Staging);
|
|
276
|
+
const baseUrl = requestConfig?.baseUrl || globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
277
|
+
const timeout = requestConfig?.timeout || globalStorefrontConfig?.timeout || envTimeout;
|
|
278
|
+
const debug = requestConfig?.debug !== void 0 ? requestConfig.debug : globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
279
|
+
const defaultHeaders = {
|
|
280
|
+
...envDefaultHeaders,
|
|
281
|
+
...globalStorefrontConfig?.defaultHeaders,
|
|
282
|
+
...requestConfig?.defaultHeaders
|
|
268
283
|
};
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
284
|
+
if (!storeId || !apiKey) {
|
|
285
|
+
throw new Error(
|
|
286
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
287
|
+
|
|
288
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
289
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
290
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
291
|
+
|
|
292
|
+
These variables are required for both client and server contexts to work.
|
|
293
|
+
Alternatively, you can pass them via the storefront() function config parameter.`
|
|
294
|
+
);
|
|
273
295
|
}
|
|
274
|
-
|
|
296
|
+
const config = {
|
|
297
|
+
storeId,
|
|
298
|
+
apiKey,
|
|
299
|
+
environment
|
|
300
|
+
};
|
|
301
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
302
|
+
if (timeout) config.timeout = timeout;
|
|
303
|
+
if (debug) config.debug = debug;
|
|
304
|
+
const logger = requestConfig?.logger || globalStorefrontConfig?.logger;
|
|
305
|
+
const accessToken = requestConfig?.accessToken || globalStorefrontConfig?.accessToken;
|
|
306
|
+
const refreshToken = requestConfig?.refreshToken || globalStorefrontConfig?.refreshToken;
|
|
307
|
+
const onTokensUpdated = requestConfig?.onTokensUpdated || globalStorefrontConfig?.onTokensUpdated;
|
|
308
|
+
const onTokensCleared = requestConfig?.onTokensCleared || globalStorefrontConfig?.onTokensCleared;
|
|
309
|
+
const tokenStorageOptions = requestConfig?.tokenStorageOptions || globalStorefrontConfig?.tokenStorageOptions;
|
|
310
|
+
if (logger) config.logger = logger;
|
|
311
|
+
if (accessToken) config.accessToken = accessToken;
|
|
312
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
313
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
314
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
315
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
316
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
317
|
+
return config;
|
|
275
318
|
}
|
|
319
|
+
var globalStorefrontConfig = null;
|
|
276
320
|
var clientSDK = null;
|
|
277
321
|
function hasRequestContext() {
|
|
278
322
|
try {
|
|
@@ -299,8 +343,8 @@ function createTokenStorage(cookieStore, options, config) {
|
|
|
299
343
|
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
300
344
|
return new import_storefront_sdk.MemoryTokenStorage();
|
|
301
345
|
}
|
|
302
|
-
var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
303
|
-
const config =
|
|
346
|
+
var getServerSDKCached = (0, import_react.cache)((cookieStore, requestConfig) => {
|
|
347
|
+
const config = getConfig(requestConfig);
|
|
304
348
|
return new import_storefront_sdk.StorefrontSDK({
|
|
305
349
|
...config,
|
|
306
350
|
tokenStorage: createTokenStorage(
|
|
@@ -311,8 +355,18 @@ var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
|
311
355
|
});
|
|
312
356
|
});
|
|
313
357
|
var buildTimeSDK = null;
|
|
314
|
-
function getBuildTimeSDK() {
|
|
315
|
-
const config =
|
|
358
|
+
function getBuildTimeSDK(requestConfig) {
|
|
359
|
+
const config = getConfig(requestConfig);
|
|
360
|
+
if (requestConfig) {
|
|
361
|
+
return new import_storefront_sdk.StorefrontSDK({
|
|
362
|
+
...config,
|
|
363
|
+
tokenStorage: createTokenStorage(
|
|
364
|
+
void 0,
|
|
365
|
+
config.tokenStorageOptions,
|
|
366
|
+
config
|
|
367
|
+
)
|
|
368
|
+
});
|
|
369
|
+
}
|
|
316
370
|
if (!buildTimeSDK) {
|
|
317
371
|
buildTimeSDK = new import_storefront_sdk.StorefrontSDK({
|
|
318
372
|
...config,
|
|
@@ -325,14 +379,24 @@ function getBuildTimeSDK() {
|
|
|
325
379
|
}
|
|
326
380
|
return buildTimeSDK;
|
|
327
381
|
}
|
|
328
|
-
function getStorefrontSDK(cookieStore) {
|
|
382
|
+
function getStorefrontSDK(cookieStore, requestConfig) {
|
|
329
383
|
if (typeof window !== "undefined") {
|
|
330
384
|
if (cookieStore) {
|
|
331
385
|
console.warn(
|
|
332
386
|
"Cookie store passed in client environment - this will be ignored"
|
|
333
387
|
);
|
|
334
388
|
}
|
|
335
|
-
const config = getConfig();
|
|
389
|
+
const config = getConfig(requestConfig);
|
|
390
|
+
if (requestConfig) {
|
|
391
|
+
return new import_storefront_sdk.StorefrontSDK({
|
|
392
|
+
...config,
|
|
393
|
+
tokenStorage: createTokenStorage(
|
|
394
|
+
void 0,
|
|
395
|
+
config.tokenStorageOptions,
|
|
396
|
+
config
|
|
397
|
+
)
|
|
398
|
+
});
|
|
399
|
+
}
|
|
336
400
|
if (!clientSDK) {
|
|
337
401
|
clientSDK = new import_storefront_sdk.StorefrontSDK({
|
|
338
402
|
...config,
|
|
@@ -346,7 +410,7 @@ function getStorefrontSDK(cookieStore) {
|
|
|
346
410
|
return clientSDK;
|
|
347
411
|
}
|
|
348
412
|
if (cookieStore) {
|
|
349
|
-
return getServerSDKCached(cookieStore);
|
|
413
|
+
return getServerSDKCached(cookieStore, requestConfig);
|
|
350
414
|
}
|
|
351
415
|
if (hasRequestContext()) {
|
|
352
416
|
let autoDetectMessage = "";
|
|
@@ -388,12 +452,20 @@ This is required for server-side token access.
|
|
|
388
452
|
`.trim()
|
|
389
453
|
);
|
|
390
454
|
}
|
|
391
|
-
return getBuildTimeSDK();
|
|
455
|
+
return getBuildTimeSDK(requestConfig);
|
|
392
456
|
}
|
|
393
457
|
|
|
394
458
|
// src/storefront.ts
|
|
395
|
-
function storefront(
|
|
396
|
-
|
|
459
|
+
function storefront(cookieStoreOrConfig, config) {
|
|
460
|
+
let cookieStore;
|
|
461
|
+
let requestConfig;
|
|
462
|
+
if (cookieStoreOrConfig && typeof cookieStoreOrConfig === "object" && !("get" in cookieStoreOrConfig)) {
|
|
463
|
+
requestConfig = cookieStoreOrConfig;
|
|
464
|
+
} else {
|
|
465
|
+
cookieStore = cookieStoreOrConfig;
|
|
466
|
+
requestConfig = config;
|
|
467
|
+
}
|
|
468
|
+
return getStorefrontSDK(cookieStore, requestConfig);
|
|
397
469
|
}
|
|
398
470
|
var storefront_default = storefront;
|
|
399
471
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/storefront.d.cts
CHANGED
|
@@ -1,40 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Universal storefront SDK export
|
|
5
|
-
*
|
|
6
|
-
* This provides a simple unified interface that works in all Next.js contexts:
|
|
7
|
-
* - Client components: automatically uses client token storage
|
|
8
|
-
* - Server components: requires cookies() to be passed
|
|
9
|
-
* - SSG/ISR: automatically uses memory storage with optional build caching
|
|
10
|
-
*
|
|
11
|
-
* Usage:
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // Client-side
|
|
14
|
-
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
15
|
-
* const products = await storefront().catalog.listProducts()
|
|
16
|
-
*
|
|
17
|
-
* // Server-side
|
|
18
|
-
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
19
|
-
* import { cookies } from 'next/headers'
|
|
20
|
-
* const products = await storefront(cookies()).catalog.listProducts()
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
type NextCookieStore = {
|
|
25
|
-
get: (name: string) => {
|
|
26
|
-
value: string;
|
|
27
|
-
} | undefined;
|
|
28
|
-
set: (name: string, value: string, options?: any) => void;
|
|
29
|
-
delete: (name: string) => void;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Universal storefront SDK accessor
|
|
33
|
-
*
|
|
34
|
-
* @param cookieStore - Next.js cookie store (required on server-side)
|
|
35
|
-
* @returns StorefrontSDK instance configured for the current environment
|
|
36
|
-
*/
|
|
37
|
-
declare function storefront(): StorefrontSDK;
|
|
38
|
-
declare function storefront(cookieStore: NextCookieStore): StorefrontSDK;
|
|
39
|
-
|
|
40
|
-
export { storefront as default, storefront };
|
|
1
|
+
import '@commercengine/storefront-sdk';
|
|
2
|
+
export { S as StorefrontRuntimeConfig, b as default, b as storefront } from './server-ByBPoXJG.cjs';
|
package/dist/storefront.d.ts
CHANGED
|
@@ -1,40 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Universal storefront SDK export
|
|
5
|
-
*
|
|
6
|
-
* This provides a simple unified interface that works in all Next.js contexts:
|
|
7
|
-
* - Client components: automatically uses client token storage
|
|
8
|
-
* - Server components: requires cookies() to be passed
|
|
9
|
-
* - SSG/ISR: automatically uses memory storage with optional build caching
|
|
10
|
-
*
|
|
11
|
-
* Usage:
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // Client-side
|
|
14
|
-
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
15
|
-
* const products = await storefront().catalog.listProducts()
|
|
16
|
-
*
|
|
17
|
-
* // Server-side
|
|
18
|
-
* import { storefront } from '@commercengine/storefront-sdk-nextjs/storefront'
|
|
19
|
-
* import { cookies } from 'next/headers'
|
|
20
|
-
* const products = await storefront(cookies()).catalog.listProducts()
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
type NextCookieStore = {
|
|
25
|
-
get: (name: string) => {
|
|
26
|
-
value: string;
|
|
27
|
-
} | undefined;
|
|
28
|
-
set: (name: string, value: string, options?: any) => void;
|
|
29
|
-
delete: (name: string) => void;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Universal storefront SDK accessor
|
|
33
|
-
*
|
|
34
|
-
* @param cookieStore - Next.js cookie store (required on server-side)
|
|
35
|
-
* @returns StorefrontSDK instance configured for the current environment
|
|
36
|
-
*/
|
|
37
|
-
declare function storefront(): StorefrontSDK;
|
|
38
|
-
declare function storefront(cookieStore: NextCookieStore): StorefrontSDK;
|
|
39
|
-
|
|
40
|
-
export { storefront as default, storefront };
|
|
1
|
+
import '@commercengine/storefront-sdk';
|
|
2
|
+
export { S as StorefrontRuntimeConfig, b as default, b as storefront } from './server-ByBPoXJG.js';
|
package/dist/storefront.js
CHANGED
|
@@ -233,20 +233,64 @@ var BuildCachingMemoryTokenStorage = class {
|
|
|
233
233
|
};
|
|
234
234
|
|
|
235
235
|
// src/sdk-manager.ts
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
236
|
+
function getConfig(requestConfig) {
|
|
237
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
238
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
239
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
240
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
241
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
242
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
243
|
+
const envDefaultHeaders = {};
|
|
244
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
245
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
246
|
+
}
|
|
247
|
+
const storeId = requestConfig?.storeId || globalStorefrontConfig?.storeId || envStoreId;
|
|
248
|
+
const apiKey = requestConfig?.apiKey || globalStorefrontConfig?.apiKey || envApiKey;
|
|
249
|
+
const environment = requestConfig?.environment || globalStorefrontConfig?.environment || (envEnvironment === "production" ? Environment.Production : Environment.Staging);
|
|
250
|
+
const baseUrl = requestConfig?.baseUrl || globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
251
|
+
const timeout = requestConfig?.timeout || globalStorefrontConfig?.timeout || envTimeout;
|
|
252
|
+
const debug = requestConfig?.debug !== void 0 ? requestConfig.debug : globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
253
|
+
const defaultHeaders = {
|
|
254
|
+
...envDefaultHeaders,
|
|
255
|
+
...globalStorefrontConfig?.defaultHeaders,
|
|
256
|
+
...requestConfig?.defaultHeaders
|
|
242
257
|
};
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
258
|
+
if (!storeId || !apiKey) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
261
|
+
|
|
262
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
263
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
264
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
265
|
+
|
|
266
|
+
These variables are required for both client and server contexts to work.
|
|
267
|
+
Alternatively, you can pass them via the storefront() function config parameter.`
|
|
268
|
+
);
|
|
247
269
|
}
|
|
248
|
-
|
|
270
|
+
const config = {
|
|
271
|
+
storeId,
|
|
272
|
+
apiKey,
|
|
273
|
+
environment
|
|
274
|
+
};
|
|
275
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
276
|
+
if (timeout) config.timeout = timeout;
|
|
277
|
+
if (debug) config.debug = debug;
|
|
278
|
+
const logger = requestConfig?.logger || globalStorefrontConfig?.logger;
|
|
279
|
+
const accessToken = requestConfig?.accessToken || globalStorefrontConfig?.accessToken;
|
|
280
|
+
const refreshToken = requestConfig?.refreshToken || globalStorefrontConfig?.refreshToken;
|
|
281
|
+
const onTokensUpdated = requestConfig?.onTokensUpdated || globalStorefrontConfig?.onTokensUpdated;
|
|
282
|
+
const onTokensCleared = requestConfig?.onTokensCleared || globalStorefrontConfig?.onTokensCleared;
|
|
283
|
+
const tokenStorageOptions = requestConfig?.tokenStorageOptions || globalStorefrontConfig?.tokenStorageOptions;
|
|
284
|
+
if (logger) config.logger = logger;
|
|
285
|
+
if (accessToken) config.accessToken = accessToken;
|
|
286
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
287
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
288
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
289
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
290
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
291
|
+
return config;
|
|
249
292
|
}
|
|
293
|
+
var globalStorefrontConfig = null;
|
|
250
294
|
var clientSDK = null;
|
|
251
295
|
function hasRequestContext() {
|
|
252
296
|
try {
|
|
@@ -273,8 +317,8 @@ function createTokenStorage(cookieStore, options, config) {
|
|
|
273
317
|
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
274
318
|
return new MemoryTokenStorage();
|
|
275
319
|
}
|
|
276
|
-
var getServerSDKCached = cache((cookieStore) => {
|
|
277
|
-
const config =
|
|
320
|
+
var getServerSDKCached = cache((cookieStore, requestConfig) => {
|
|
321
|
+
const config = getConfig(requestConfig);
|
|
278
322
|
return new StorefrontSDK({
|
|
279
323
|
...config,
|
|
280
324
|
tokenStorage: createTokenStorage(
|
|
@@ -285,8 +329,18 @@ var getServerSDKCached = cache((cookieStore) => {
|
|
|
285
329
|
});
|
|
286
330
|
});
|
|
287
331
|
var buildTimeSDK = null;
|
|
288
|
-
function getBuildTimeSDK() {
|
|
289
|
-
const config =
|
|
332
|
+
function getBuildTimeSDK(requestConfig) {
|
|
333
|
+
const config = getConfig(requestConfig);
|
|
334
|
+
if (requestConfig) {
|
|
335
|
+
return new StorefrontSDK({
|
|
336
|
+
...config,
|
|
337
|
+
tokenStorage: createTokenStorage(
|
|
338
|
+
void 0,
|
|
339
|
+
config.tokenStorageOptions,
|
|
340
|
+
config
|
|
341
|
+
)
|
|
342
|
+
});
|
|
343
|
+
}
|
|
290
344
|
if (!buildTimeSDK) {
|
|
291
345
|
buildTimeSDK = new StorefrontSDK({
|
|
292
346
|
...config,
|
|
@@ -299,14 +353,24 @@ function getBuildTimeSDK() {
|
|
|
299
353
|
}
|
|
300
354
|
return buildTimeSDK;
|
|
301
355
|
}
|
|
302
|
-
function getStorefrontSDK(cookieStore) {
|
|
356
|
+
function getStorefrontSDK(cookieStore, requestConfig) {
|
|
303
357
|
if (typeof window !== "undefined") {
|
|
304
358
|
if (cookieStore) {
|
|
305
359
|
console.warn(
|
|
306
360
|
"Cookie store passed in client environment - this will be ignored"
|
|
307
361
|
);
|
|
308
362
|
}
|
|
309
|
-
const config = getConfig();
|
|
363
|
+
const config = getConfig(requestConfig);
|
|
364
|
+
if (requestConfig) {
|
|
365
|
+
return new StorefrontSDK({
|
|
366
|
+
...config,
|
|
367
|
+
tokenStorage: createTokenStorage(
|
|
368
|
+
void 0,
|
|
369
|
+
config.tokenStorageOptions,
|
|
370
|
+
config
|
|
371
|
+
)
|
|
372
|
+
});
|
|
373
|
+
}
|
|
310
374
|
if (!clientSDK) {
|
|
311
375
|
clientSDK = new StorefrontSDK({
|
|
312
376
|
...config,
|
|
@@ -320,7 +384,7 @@ function getStorefrontSDK(cookieStore) {
|
|
|
320
384
|
return clientSDK;
|
|
321
385
|
}
|
|
322
386
|
if (cookieStore) {
|
|
323
|
-
return getServerSDKCached(cookieStore);
|
|
387
|
+
return getServerSDKCached(cookieStore, requestConfig);
|
|
324
388
|
}
|
|
325
389
|
if (hasRequestContext()) {
|
|
326
390
|
let autoDetectMessage = "";
|
|
@@ -362,12 +426,20 @@ This is required for server-side token access.
|
|
|
362
426
|
`.trim()
|
|
363
427
|
);
|
|
364
428
|
}
|
|
365
|
-
return getBuildTimeSDK();
|
|
429
|
+
return getBuildTimeSDK(requestConfig);
|
|
366
430
|
}
|
|
367
431
|
|
|
368
432
|
// src/storefront.ts
|
|
369
|
-
function storefront(
|
|
370
|
-
|
|
433
|
+
function storefront(cookieStoreOrConfig, config) {
|
|
434
|
+
let cookieStore;
|
|
435
|
+
let requestConfig;
|
|
436
|
+
if (cookieStoreOrConfig && typeof cookieStoreOrConfig === "object" && !("get" in cookieStoreOrConfig)) {
|
|
437
|
+
requestConfig = cookieStoreOrConfig;
|
|
438
|
+
} else {
|
|
439
|
+
cookieStore = cookieStoreOrConfig;
|
|
440
|
+
requestConfig = config;
|
|
441
|
+
}
|
|
442
|
+
return getStorefrontSDK(cookieStore, requestConfig);
|
|
371
443
|
}
|
|
372
444
|
var storefront_default = storefront;
|
|
373
445
|
export {
|