@commercengine/storefront-sdk-nextjs 0.1.0-alpha.0 → 0.1.0-alpha.1
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 +610 -108
- package/dist/client.cjs +430 -0
- package/dist/client.d.cts +138 -0
- package/dist/client.d.ts +138 -0
- package/dist/client.js +398 -0
- package/dist/index.cjs +163 -89
- package/dist/index.d.cts +9 -18
- package/dist/index.d.ts +9 -18
- package/dist/index.js +162 -87
- package/dist/middleware.cjs +66 -0
- package/dist/middleware.d.cts +38 -0
- package/dist/middleware.d.ts +38 -0
- package/dist/middleware.js +39 -0
- package/dist/server-D-pFrx8J.d.cts +105 -0
- package/dist/server-DaDfTjsO.d.cts +103 -0
- package/dist/server-DaDfTjsO.d.ts +103 -0
- package/dist/server-DjlQVC11.d.ts +105 -0
- package/dist/server.cjs +416 -0
- package/dist/server.d.cts +4 -0
- package/dist/server.d.ts +4 -0
- package/dist/server.js +385 -0
- package/dist/storefront.cjs +402 -0
- package/dist/storefront.d.cts +40 -0
- package/dist/storefront.d.ts +40 -0
- package/dist/storefront.js +376 -0
- package/package.json +18 -8
package/dist/index.cjs
CHANGED
|
@@ -17,6 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
21
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
22
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
23
|
// file that has been converted to a CommonJS file using a Babel-
|
|
@@ -31,14 +32,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
32
|
var index_exports = {};
|
|
32
33
|
__export(index_exports, {
|
|
33
34
|
ClientTokenStorage: () => ClientTokenStorage,
|
|
34
|
-
Environment: () => import_storefront_sdk2.Environment,
|
|
35
35
|
ServerTokenStorage: () => ServerTokenStorage,
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
getStorefrontSDK: () => getStorefrontSDK,
|
|
37
|
+
storefront: () => storefront
|
|
38
38
|
});
|
|
39
39
|
module.exports = __toCommonJS(index_exports);
|
|
40
40
|
|
|
41
41
|
// src/sdk-manager.ts
|
|
42
|
+
var import_react = require("react");
|
|
42
43
|
var import_storefront_sdk = require("@commercengine/storefront-sdk");
|
|
43
44
|
|
|
44
45
|
// src/token-storage.ts
|
|
@@ -147,7 +148,7 @@ var ServerTokenStorage = class {
|
|
|
147
148
|
secure: this.options.secure,
|
|
148
149
|
sameSite: this.options.sameSite?.toLowerCase(),
|
|
149
150
|
httpOnly: false
|
|
150
|
-
//
|
|
151
|
+
// Allow client-side access for SDK flexibility
|
|
151
152
|
});
|
|
152
153
|
} catch (error) {
|
|
153
154
|
console.warn(`Could not set access token on server:`, error);
|
|
@@ -170,7 +171,7 @@ var ServerTokenStorage = class {
|
|
|
170
171
|
secure: this.options.secure,
|
|
171
172
|
sameSite: this.options.sameSite?.toLowerCase(),
|
|
172
173
|
httpOnly: false
|
|
173
|
-
//
|
|
174
|
+
// Allow client-side access for SDK flexibility
|
|
174
175
|
});
|
|
175
176
|
} catch (error) {
|
|
176
177
|
console.warn(`Could not set refresh token on server:`, error);
|
|
@@ -186,88 +187,171 @@ var ServerTokenStorage = class {
|
|
|
186
187
|
}
|
|
187
188
|
};
|
|
188
189
|
|
|
189
|
-
// src/
|
|
190
|
-
var
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
190
|
+
// src/build-token-cache.ts
|
|
191
|
+
var store = /* @__PURE__ */ new Map();
|
|
192
|
+
function isExpired(token) {
|
|
193
|
+
if (!token) return true;
|
|
194
|
+
if (!token.expiresAt) return false;
|
|
195
|
+
return Date.now() > token.expiresAt - 3e4;
|
|
196
|
+
}
|
|
197
|
+
function getCachedToken(key) {
|
|
198
|
+
const token = store.get(key);
|
|
199
|
+
return isExpired(token) ? null : token;
|
|
200
|
+
}
|
|
201
|
+
function setCachedToken(key, token) {
|
|
202
|
+
const expiresAt = token.ttlSeconds != null ? Date.now() + token.ttlSeconds * 1e3 : void 0;
|
|
203
|
+
store.set(key, {
|
|
204
|
+
accessToken: token.accessToken,
|
|
205
|
+
refreshToken: token.refreshToken ?? null,
|
|
206
|
+
expiresAt
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function clearCachedToken(key) {
|
|
210
|
+
store.delete(key);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/build-caching-memory-storage.ts
|
|
214
|
+
var DEFAULT_TTL_SECONDS = 5 * 60;
|
|
215
|
+
var BuildCachingMemoryTokenStorage = class {
|
|
216
|
+
constructor(cacheKey, ttlSeconds = DEFAULT_TTL_SECONDS) {
|
|
217
|
+
this.cacheKey = cacheKey;
|
|
218
|
+
this.ttlSeconds = ttlSeconds;
|
|
219
|
+
this.access = null;
|
|
220
|
+
this.refresh = null;
|
|
209
221
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (!this.config) {
|
|
215
|
-
throw new Error("SDK not initialized. Call initialize() first.");
|
|
222
|
+
async getAccessToken() {
|
|
223
|
+
if (this.access) {
|
|
224
|
+
console.log(`\u{1F535} [BuildCache] Using instance token for key: ${this.cacheKey}`);
|
|
225
|
+
return this.access;
|
|
216
226
|
}
|
|
217
|
-
|
|
218
|
-
|
|
227
|
+
const cached = getCachedToken(this.cacheKey);
|
|
228
|
+
if (cached?.accessToken) {
|
|
229
|
+
console.log(`\u{1F7E2} [BuildCache] Using cached token for key: ${this.cacheKey}`);
|
|
230
|
+
this.access = cached.accessToken;
|
|
231
|
+
this.refresh = cached.refreshToken ?? null;
|
|
232
|
+
return this.access;
|
|
219
233
|
}
|
|
220
|
-
|
|
234
|
+
console.log(`\u{1F7E1} [BuildCache] No cached token found for key: ${this.cacheKey}`);
|
|
235
|
+
return null;
|
|
221
236
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return new import_storefront_sdk.StorefrontSDK({
|
|
230
|
-
...this.config,
|
|
231
|
-
tokenStorage: new ServerTokenStorage(
|
|
232
|
-
cookieStore,
|
|
233
|
-
this.config.tokenStorageOptions
|
|
234
|
-
)
|
|
237
|
+
async setAccessToken(token) {
|
|
238
|
+
console.log(`\u{1F7E0} [BuildCache] Caching new access token for key: ${this.cacheKey}`);
|
|
239
|
+
this.access = token;
|
|
240
|
+
setCachedToken(this.cacheKey, {
|
|
241
|
+
accessToken: token,
|
|
242
|
+
refreshToken: this.refresh,
|
|
243
|
+
ttlSeconds: this.ttlSeconds
|
|
235
244
|
});
|
|
236
245
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
throw new Error("SDK not initialized. Call initialize() first.");
|
|
240
|
-
}
|
|
241
|
-
return new import_storefront_sdk.StorefrontSDK({
|
|
242
|
-
...this.config,
|
|
243
|
-
tokenStorage: new ClientTokenStorage(this.config.tokenStorageOptions)
|
|
244
|
-
});
|
|
246
|
+
async getRefreshToken() {
|
|
247
|
+
return this.refresh;
|
|
245
248
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
249
|
+
async setRefreshToken(token) {
|
|
250
|
+
this.refresh = token;
|
|
251
|
+
setCachedToken(this.cacheKey, {
|
|
252
|
+
accessToken: this.access ?? "",
|
|
253
|
+
refreshToken: token,
|
|
254
|
+
ttlSeconds: this.ttlSeconds
|
|
255
|
+
});
|
|
251
256
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
this.clientSDK = null;
|
|
257
|
-
this.config = null;
|
|
257
|
+
async clearTokens() {
|
|
258
|
+
this.access = null;
|
|
259
|
+
this.refresh = null;
|
|
260
|
+
clearCachedToken(this.cacheKey);
|
|
258
261
|
}
|
|
259
262
|
};
|
|
263
|
+
|
|
264
|
+
// 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
|
+
function getConfig() {
|
|
274
|
+
if (globalConfig) {
|
|
275
|
+
return globalConfig;
|
|
276
|
+
}
|
|
277
|
+
return getEnvConfig();
|
|
278
|
+
}
|
|
279
|
+
var clientSDK = null;
|
|
280
|
+
function hasRequestContext() {
|
|
281
|
+
try {
|
|
282
|
+
const { cookies } = require("next/headers");
|
|
283
|
+
cookies();
|
|
284
|
+
return true;
|
|
285
|
+
} catch {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function createTokenStorage(cookieStore, options, config) {
|
|
290
|
+
if (typeof window !== "undefined") {
|
|
291
|
+
return new ClientTokenStorage(options);
|
|
292
|
+
}
|
|
293
|
+
if (cookieStore) {
|
|
294
|
+
return new ServerTokenStorage(cookieStore, options);
|
|
295
|
+
}
|
|
296
|
+
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
297
|
+
if (shouldCache && config) {
|
|
298
|
+
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
299
|
+
console.log(`\u{1F680} [BuildCache] Using BuildCachingMemoryTokenStorage with key: ${cacheKey}`);
|
|
300
|
+
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
301
|
+
}
|
|
302
|
+
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
303
|
+
return new import_storefront_sdk.MemoryTokenStorage();
|
|
304
|
+
}
|
|
305
|
+
var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
306
|
+
const config = getEnvConfig();
|
|
307
|
+
return new import_storefront_sdk.StorefrontSDK({
|
|
308
|
+
...config,
|
|
309
|
+
tokenStorage: createTokenStorage(
|
|
310
|
+
cookieStore,
|
|
311
|
+
config.tokenStorageOptions,
|
|
312
|
+
config
|
|
313
|
+
)
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
var buildTimeSDK = null;
|
|
317
|
+
function getBuildTimeSDK() {
|
|
318
|
+
const config = getEnvConfig();
|
|
319
|
+
if (!buildTimeSDK) {
|
|
320
|
+
buildTimeSDK = new import_storefront_sdk.StorefrontSDK({
|
|
321
|
+
...config,
|
|
322
|
+
tokenStorage: createTokenStorage(
|
|
323
|
+
void 0,
|
|
324
|
+
config.tokenStorageOptions,
|
|
325
|
+
config
|
|
326
|
+
)
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return buildTimeSDK;
|
|
330
|
+
}
|
|
260
331
|
function getStorefrontSDK(cookieStore) {
|
|
261
|
-
const manager = NextJSSDKManager.getInstance();
|
|
262
332
|
if (typeof window !== "undefined") {
|
|
263
333
|
if (cookieStore) {
|
|
264
334
|
console.warn(
|
|
265
335
|
"Cookie store passed in client environment - this will be ignored"
|
|
266
336
|
);
|
|
267
337
|
}
|
|
268
|
-
|
|
338
|
+
const config = getConfig();
|
|
339
|
+
if (!clientSDK) {
|
|
340
|
+
clientSDK = new import_storefront_sdk.StorefrontSDK({
|
|
341
|
+
...config,
|
|
342
|
+
tokenStorage: createTokenStorage(
|
|
343
|
+
void 0,
|
|
344
|
+
config.tokenStorageOptions,
|
|
345
|
+
config
|
|
346
|
+
)
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return clientSDK;
|
|
350
|
+
}
|
|
351
|
+
if (cookieStore) {
|
|
352
|
+
return getServerSDKCached(cookieStore);
|
|
269
353
|
}
|
|
270
|
-
if (
|
|
354
|
+
if (hasRequestContext()) {
|
|
271
355
|
let autoDetectMessage = "";
|
|
272
356
|
try {
|
|
273
357
|
require.resolve("next/headers");
|
|
@@ -276,7 +360,7 @@ function getStorefrontSDK(cookieStore) {
|
|
|
276
360
|
\u{1F50D} Auto-detection attempted but failed. You may be in:
|
|
277
361
|
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
278
362
|
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
279
|
-
-
|
|
363
|
+
- Server Component in App Router (use: const sdk = getStorefrontSDK(cookies()))
|
|
280
364
|
`;
|
|
281
365
|
} catch {
|
|
282
366
|
autoDetectMessage = `
|
|
@@ -297,7 +381,7 @@ import { cookies } from 'next/headers';
|
|
|
297
381
|
// Server Actions & Route Handlers
|
|
298
382
|
const sdk = getStorefrontSDK(await cookies());
|
|
299
383
|
|
|
300
|
-
// API Routes (
|
|
384
|
+
// API Routes & Server Components (App Router)
|
|
301
385
|
const sdk = getStorefrontSDK(cookies());
|
|
302
386
|
|
|
303
387
|
\u274C Your current usage:
|
|
@@ -307,31 +391,21 @@ This is required for server-side token access.
|
|
|
307
391
|
`.trim()
|
|
308
392
|
);
|
|
309
393
|
}
|
|
310
|
-
return
|
|
311
|
-
}
|
|
312
|
-
function initializeStorefrontSDK(config) {
|
|
313
|
-
const manager = NextJSSDKManager.getInstance();
|
|
314
|
-
manager.initialize(config);
|
|
394
|
+
return getBuildTimeSDK();
|
|
315
395
|
}
|
|
316
396
|
|
|
317
|
-
// src/
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
config
|
|
321
|
-
}) {
|
|
322
|
-
(0, import_react.useEffect)(() => {
|
|
323
|
-
initializeStorefrontSDK(config);
|
|
324
|
-
}, [config]);
|
|
325
|
-
return null;
|
|
397
|
+
// src/storefront.ts
|
|
398
|
+
function storefront(cookieStore) {
|
|
399
|
+
return getStorefrontSDK(cookieStore);
|
|
326
400
|
}
|
|
327
401
|
|
|
328
402
|
// src/index.ts
|
|
329
|
-
|
|
403
|
+
__reExport(index_exports, require("@commercengine/storefront-sdk"), module.exports);
|
|
330
404
|
// Annotate the CommonJS export names for ESM import in node:
|
|
331
405
|
0 && (module.exports = {
|
|
332
406
|
ClientTokenStorage,
|
|
333
|
-
Environment,
|
|
334
407
|
ServerTokenStorage,
|
|
335
|
-
|
|
336
|
-
|
|
408
|
+
getStorefrontSDK,
|
|
409
|
+
storefront,
|
|
410
|
+
...require("@commercengine/storefront-sdk")
|
|
337
411
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { TokenStorage, StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
-
export
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
4
|
+
export { default as storefront } from './storefront.cjs';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Configuration options for NextJSTokenStorage
|
|
@@ -91,26 +93,15 @@ type NextCookieStore = {
|
|
|
91
93
|
*
|
|
92
94
|
* Usage:
|
|
93
95
|
* - Client-side: getStorefrontSDK()
|
|
94
|
-
* - Server-side: getStorefrontSDK(await cookies())
|
|
96
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
97
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
95
98
|
*/
|
|
96
99
|
declare function getStorefrontSDK(): StorefrontSDK;
|
|
97
100
|
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
98
|
-
|
|
99
|
-
interface StorefrontSDKInitializerProps {
|
|
100
|
-
config: NextJSSDKConfig;
|
|
101
|
-
}
|
|
102
101
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* The core SDK middleware now automatically handles everything:
|
|
107
|
-
* - Creates anonymous tokens automatically on first API request (if no tokens exist)
|
|
108
|
-
* - Token state assessment and cleanup on first request per page load
|
|
109
|
-
* - Expired token refresh with automatic anonymous fallback
|
|
110
|
-
* - Graceful handling of partial token states
|
|
111
|
-
*
|
|
112
|
-
* No manual token creation needed - just make API calls and everything works!
|
|
102
|
+
* Initialize the SDK with configuration (internal use)
|
|
103
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
113
104
|
*/
|
|
114
|
-
declare function
|
|
105
|
+
declare function initializeStorefrontSDK(config: NextJSSDKConfig): void;
|
|
115
106
|
|
|
116
|
-
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, ServerTokenStorage,
|
|
107
|
+
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, ServerTokenStorage, getStorefrontSDK, initializeStorefrontSDK as i };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { TokenStorage, StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
2
|
-
export
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
4
|
+
export { default as storefront } from './storefront.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Configuration options for NextJSTokenStorage
|
|
@@ -91,26 +93,15 @@ type NextCookieStore = {
|
|
|
91
93
|
*
|
|
92
94
|
* Usage:
|
|
93
95
|
* - Client-side: getStorefrontSDK()
|
|
94
|
-
* - Server-side: getStorefrontSDK(await cookies())
|
|
96
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
97
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
95
98
|
*/
|
|
96
99
|
declare function getStorefrontSDK(): StorefrontSDK;
|
|
97
100
|
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
98
|
-
|
|
99
|
-
interface StorefrontSDKInitializerProps {
|
|
100
|
-
config: NextJSSDKConfig;
|
|
101
|
-
}
|
|
102
101
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* The core SDK middleware now automatically handles everything:
|
|
107
|
-
* - Creates anonymous tokens automatically on first API request (if no tokens exist)
|
|
108
|
-
* - Token state assessment and cleanup on first request per page load
|
|
109
|
-
* - Expired token refresh with automatic anonymous fallback
|
|
110
|
-
* - Graceful handling of partial token states
|
|
111
|
-
*
|
|
112
|
-
* No manual token creation needed - just make API calls and everything works!
|
|
102
|
+
* Initialize the SDK with configuration (internal use)
|
|
103
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
113
104
|
*/
|
|
114
|
-
declare function
|
|
105
|
+
declare function initializeStorefrontSDK(config: NextJSSDKConfig): void;
|
|
115
106
|
|
|
116
|
-
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, ServerTokenStorage,
|
|
107
|
+
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, ServerTokenStorage, getStorefrontSDK, initializeStorefrontSDK as i };
|