@commercengine/storefront-sdk-nextjs 0.1.0-alpha.0 → 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 +515 -139
- package/dist/client.cjs +405 -0
- package/dist/client.d.cts +121 -0
- package/dist/client.d.ts +121 -0
- package/dist/client.js +378 -0
- package/dist/index.cjs +229 -140
- package/dist/index.d.cts +27 -102
- package/dist/index.d.ts +27 -102
- package/dist/index.js +230 -132
- 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-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-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 +385 -0
- package/dist/server.d.cts +3 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +358 -0
- package/dist/storefront.cjs +474 -0
- package/dist/storefront.d.cts +2 -0
- package/dist/storefront.d.ts +2 -0
- package/dist/storefront.js +448 -0
- package/package.json +18 -8
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/storefront.ts
|
|
31
|
+
var storefront_exports = {};
|
|
32
|
+
__export(storefront_exports, {
|
|
33
|
+
default: () => storefront_default,
|
|
34
|
+
storefront: () => storefront
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(storefront_exports);
|
|
37
|
+
|
|
38
|
+
// src/sdk-manager.ts
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
var import_storefront_sdk = require("@commercengine/storefront-sdk");
|
|
41
|
+
|
|
42
|
+
// src/token-storage.ts
|
|
43
|
+
var ClientTokenStorage = class {
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
const prefix = options.prefix || "ce_";
|
|
46
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
47
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
48
|
+
this.options = {
|
|
49
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
50
|
+
// 30 days default
|
|
51
|
+
path: options.path || "/",
|
|
52
|
+
domain: options.domain,
|
|
53
|
+
secure: options.secure ?? (typeof window !== "undefined" && window.location?.protocol === "https:"),
|
|
54
|
+
sameSite: options.sameSite || "Lax"
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async getAccessToken() {
|
|
58
|
+
return this.getCookie(this.accessTokenKey);
|
|
59
|
+
}
|
|
60
|
+
async setAccessToken(token) {
|
|
61
|
+
this.setCookie(this.accessTokenKey, token);
|
|
62
|
+
}
|
|
63
|
+
async getRefreshToken() {
|
|
64
|
+
return this.getCookie(this.refreshTokenKey);
|
|
65
|
+
}
|
|
66
|
+
async setRefreshToken(token) {
|
|
67
|
+
this.setCookie(this.refreshTokenKey, token);
|
|
68
|
+
}
|
|
69
|
+
async clearTokens() {
|
|
70
|
+
this.deleteCookie(this.accessTokenKey);
|
|
71
|
+
this.deleteCookie(this.refreshTokenKey);
|
|
72
|
+
}
|
|
73
|
+
getCookie(name) {
|
|
74
|
+
if (typeof document === "undefined") return null;
|
|
75
|
+
const value = `; ${document.cookie}`;
|
|
76
|
+
const parts = value.split(`; ${name}=`);
|
|
77
|
+
if (parts.length === 2) {
|
|
78
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
79
|
+
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
setCookie(name, value) {
|
|
84
|
+
if (typeof document === "undefined") return;
|
|
85
|
+
const encodedValue = encodeURIComponent(value);
|
|
86
|
+
let cookieString = `${name}=${encodedValue}`;
|
|
87
|
+
if (this.options.maxAge) {
|
|
88
|
+
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
89
|
+
}
|
|
90
|
+
if (this.options.path) {
|
|
91
|
+
cookieString += `; Path=${this.options.path}`;
|
|
92
|
+
}
|
|
93
|
+
if (this.options.domain) {
|
|
94
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
95
|
+
}
|
|
96
|
+
if (this.options.secure) {
|
|
97
|
+
cookieString += `; Secure`;
|
|
98
|
+
}
|
|
99
|
+
if (this.options.sameSite) {
|
|
100
|
+
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
101
|
+
}
|
|
102
|
+
document.cookie = cookieString;
|
|
103
|
+
}
|
|
104
|
+
deleteCookie(name) {
|
|
105
|
+
if (typeof document === "undefined") return;
|
|
106
|
+
let cookieString = `${name}=; Max-Age=0`;
|
|
107
|
+
if (this.options.path) {
|
|
108
|
+
cookieString += `; Path=${this.options.path}`;
|
|
109
|
+
}
|
|
110
|
+
if (this.options.domain) {
|
|
111
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
112
|
+
}
|
|
113
|
+
document.cookie = cookieString;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var ServerTokenStorage = class {
|
|
117
|
+
constructor(cookieStore, options = {}) {
|
|
118
|
+
const prefix = options.prefix || "ce_";
|
|
119
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
120
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
121
|
+
this.cookieStore = cookieStore;
|
|
122
|
+
this.options = {
|
|
123
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
124
|
+
// 30 days default
|
|
125
|
+
path: options.path || "/",
|
|
126
|
+
domain: options.domain,
|
|
127
|
+
secure: options.secure ?? process.env.NODE_ENV === "production",
|
|
128
|
+
sameSite: options.sameSite || "Lax"
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
async getAccessToken() {
|
|
132
|
+
try {
|
|
133
|
+
return this.cookieStore.get(this.accessTokenKey)?.value || null;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.warn(`Could not get access token from server cookies:`, error);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async setAccessToken(token) {
|
|
140
|
+
try {
|
|
141
|
+
this.cookieStore.set(this.accessTokenKey, token, {
|
|
142
|
+
maxAge: this.options.maxAge,
|
|
143
|
+
path: this.options.path,
|
|
144
|
+
domain: this.options.domain,
|
|
145
|
+
secure: this.options.secure,
|
|
146
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
147
|
+
httpOnly: false
|
|
148
|
+
// Allow client-side access for SDK flexibility
|
|
149
|
+
});
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.warn(`Could not set access token on server:`, error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async getRefreshToken() {
|
|
155
|
+
try {
|
|
156
|
+
return this.cookieStore.get(this.refreshTokenKey)?.value || null;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
console.warn(`Could not get refresh token from server cookies:`, error);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async setRefreshToken(token) {
|
|
163
|
+
try {
|
|
164
|
+
this.cookieStore.set(this.refreshTokenKey, token, {
|
|
165
|
+
maxAge: this.options.maxAge,
|
|
166
|
+
path: this.options.path,
|
|
167
|
+
domain: this.options.domain,
|
|
168
|
+
secure: this.options.secure,
|
|
169
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
170
|
+
httpOnly: false
|
|
171
|
+
// Allow client-side access for SDK flexibility
|
|
172
|
+
});
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.warn(`Could not set refresh token on server:`, error);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async clearTokens() {
|
|
178
|
+
try {
|
|
179
|
+
this.cookieStore.delete(this.accessTokenKey);
|
|
180
|
+
this.cookieStore.delete(this.refreshTokenKey);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.warn(`Could not clear tokens on server:`, error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/build-token-cache.ts
|
|
188
|
+
var store = /* @__PURE__ */ new Map();
|
|
189
|
+
function isExpired(token) {
|
|
190
|
+
if (!token) return true;
|
|
191
|
+
if (!token.expiresAt) return false;
|
|
192
|
+
return Date.now() > token.expiresAt - 3e4;
|
|
193
|
+
}
|
|
194
|
+
function getCachedToken(key) {
|
|
195
|
+
const token = store.get(key);
|
|
196
|
+
return isExpired(token) ? null : token;
|
|
197
|
+
}
|
|
198
|
+
function setCachedToken(key, token) {
|
|
199
|
+
const expiresAt = token.ttlSeconds != null ? Date.now() + token.ttlSeconds * 1e3 : void 0;
|
|
200
|
+
store.set(key, {
|
|
201
|
+
accessToken: token.accessToken,
|
|
202
|
+
refreshToken: token.refreshToken ?? null,
|
|
203
|
+
expiresAt
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
function clearCachedToken(key) {
|
|
207
|
+
store.delete(key);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/build-caching-memory-storage.ts
|
|
211
|
+
var DEFAULT_TTL_SECONDS = 5 * 60;
|
|
212
|
+
var BuildCachingMemoryTokenStorage = class {
|
|
213
|
+
constructor(cacheKey, ttlSeconds = DEFAULT_TTL_SECONDS) {
|
|
214
|
+
this.cacheKey = cacheKey;
|
|
215
|
+
this.ttlSeconds = ttlSeconds;
|
|
216
|
+
this.access = null;
|
|
217
|
+
this.refresh = null;
|
|
218
|
+
}
|
|
219
|
+
async getAccessToken() {
|
|
220
|
+
if (this.access) {
|
|
221
|
+
console.log(`\u{1F535} [BuildCache] Using instance token for key: ${this.cacheKey}`);
|
|
222
|
+
return this.access;
|
|
223
|
+
}
|
|
224
|
+
const cached = getCachedToken(this.cacheKey);
|
|
225
|
+
if (cached?.accessToken) {
|
|
226
|
+
console.log(`\u{1F7E2} [BuildCache] Using cached token for key: ${this.cacheKey}`);
|
|
227
|
+
this.access = cached.accessToken;
|
|
228
|
+
this.refresh = cached.refreshToken ?? null;
|
|
229
|
+
return this.access;
|
|
230
|
+
}
|
|
231
|
+
console.log(`\u{1F7E1} [BuildCache] No cached token found for key: ${this.cacheKey}`);
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
async setAccessToken(token) {
|
|
235
|
+
console.log(`\u{1F7E0} [BuildCache] Caching new access token for key: ${this.cacheKey}`);
|
|
236
|
+
this.access = token;
|
|
237
|
+
setCachedToken(this.cacheKey, {
|
|
238
|
+
accessToken: token,
|
|
239
|
+
refreshToken: this.refresh,
|
|
240
|
+
ttlSeconds: this.ttlSeconds
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async getRefreshToken() {
|
|
244
|
+
return this.refresh;
|
|
245
|
+
}
|
|
246
|
+
async setRefreshToken(token) {
|
|
247
|
+
this.refresh = token;
|
|
248
|
+
setCachedToken(this.cacheKey, {
|
|
249
|
+
accessToken: this.access ?? "",
|
|
250
|
+
refreshToken: token,
|
|
251
|
+
ttlSeconds: this.ttlSeconds
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async clearTokens() {
|
|
255
|
+
this.access = null;
|
|
256
|
+
this.refresh = null;
|
|
257
|
+
clearCachedToken(this.cacheKey);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// src/sdk-manager.ts
|
|
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
|
|
283
|
+
};
|
|
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
|
+
);
|
|
295
|
+
}
|
|
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;
|
|
318
|
+
}
|
|
319
|
+
var globalStorefrontConfig = null;
|
|
320
|
+
var clientSDK = null;
|
|
321
|
+
function hasRequestContext() {
|
|
322
|
+
try {
|
|
323
|
+
const { cookies } = require("next/headers");
|
|
324
|
+
cookies();
|
|
325
|
+
return true;
|
|
326
|
+
} catch {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function createTokenStorage(cookieStore, options, config) {
|
|
331
|
+
if (typeof window !== "undefined") {
|
|
332
|
+
return new ClientTokenStorage(options);
|
|
333
|
+
}
|
|
334
|
+
if (cookieStore) {
|
|
335
|
+
return new ServerTokenStorage(cookieStore, options);
|
|
336
|
+
}
|
|
337
|
+
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
338
|
+
if (shouldCache && config) {
|
|
339
|
+
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
340
|
+
console.log(`\u{1F680} [BuildCache] Using BuildCachingMemoryTokenStorage with key: ${cacheKey}`);
|
|
341
|
+
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
342
|
+
}
|
|
343
|
+
console.log(`\u{1F504} [Build] Using standard MemoryTokenStorage (caching disabled)`);
|
|
344
|
+
return new import_storefront_sdk.MemoryTokenStorage();
|
|
345
|
+
}
|
|
346
|
+
var getServerSDKCached = (0, import_react.cache)((cookieStore, requestConfig) => {
|
|
347
|
+
const config = getConfig(requestConfig);
|
|
348
|
+
return new import_storefront_sdk.StorefrontSDK({
|
|
349
|
+
...config,
|
|
350
|
+
tokenStorage: createTokenStorage(
|
|
351
|
+
cookieStore,
|
|
352
|
+
config.tokenStorageOptions,
|
|
353
|
+
config
|
|
354
|
+
)
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
var buildTimeSDK = null;
|
|
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
|
+
}
|
|
370
|
+
if (!buildTimeSDK) {
|
|
371
|
+
buildTimeSDK = new import_storefront_sdk.StorefrontSDK({
|
|
372
|
+
...config,
|
|
373
|
+
tokenStorage: createTokenStorage(
|
|
374
|
+
void 0,
|
|
375
|
+
config.tokenStorageOptions,
|
|
376
|
+
config
|
|
377
|
+
)
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return buildTimeSDK;
|
|
381
|
+
}
|
|
382
|
+
function getStorefrontSDK(cookieStore, requestConfig) {
|
|
383
|
+
if (typeof window !== "undefined") {
|
|
384
|
+
if (cookieStore) {
|
|
385
|
+
console.warn(
|
|
386
|
+
"Cookie store passed in client environment - this will be ignored"
|
|
387
|
+
);
|
|
388
|
+
}
|
|
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
|
+
}
|
|
400
|
+
if (!clientSDK) {
|
|
401
|
+
clientSDK = new import_storefront_sdk.StorefrontSDK({
|
|
402
|
+
...config,
|
|
403
|
+
tokenStorage: createTokenStorage(
|
|
404
|
+
void 0,
|
|
405
|
+
config.tokenStorageOptions,
|
|
406
|
+
config
|
|
407
|
+
)
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return clientSDK;
|
|
411
|
+
}
|
|
412
|
+
if (cookieStore) {
|
|
413
|
+
return getServerSDKCached(cookieStore, requestConfig);
|
|
414
|
+
}
|
|
415
|
+
if (hasRequestContext()) {
|
|
416
|
+
let autoDetectMessage = "";
|
|
417
|
+
try {
|
|
418
|
+
require.resolve("next/headers");
|
|
419
|
+
autoDetectMessage = `
|
|
420
|
+
|
|
421
|
+
\u{1F50D} Auto-detection attempted but failed. You may be in:
|
|
422
|
+
- Server Action (use: const sdk = getStorefrontSDK(await cookies()))
|
|
423
|
+
- API Route (use: const sdk = getStorefrontSDK(cookies()))
|
|
424
|
+
- Server Component in App Router (use: const sdk = getStorefrontSDK(cookies()))
|
|
425
|
+
`;
|
|
426
|
+
} catch {
|
|
427
|
+
autoDetectMessage = `
|
|
428
|
+
|
|
429
|
+
\u{1F4A1} Make sure you have Next.js installed and are in a server context.
|
|
430
|
+
`;
|
|
431
|
+
}
|
|
432
|
+
throw new Error(
|
|
433
|
+
`
|
|
434
|
+
\u{1F6A8} Server Environment Detected!
|
|
435
|
+
|
|
436
|
+
You're calling getStorefrontSDK() on the server without cookies.
|
|
437
|
+
Please pass the Next.js cookie store:
|
|
438
|
+
|
|
439
|
+
\u2705 Correct usage:
|
|
440
|
+
import { cookies } from 'next/headers';
|
|
441
|
+
|
|
442
|
+
// Server Actions & Route Handlers
|
|
443
|
+
const sdk = getStorefrontSDK(await cookies());
|
|
444
|
+
|
|
445
|
+
// API Routes & Server Components (App Router)
|
|
446
|
+
const sdk = getStorefrontSDK(cookies());
|
|
447
|
+
|
|
448
|
+
\u274C Your current usage:
|
|
449
|
+
const sdk = getStorefrontSDK(); // Missing cookies!
|
|
450
|
+
${autoDetectMessage}
|
|
451
|
+
This is required for server-side token access.
|
|
452
|
+
`.trim()
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return getBuildTimeSDK(requestConfig);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// src/storefront.ts
|
|
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);
|
|
469
|
+
}
|
|
470
|
+
var storefront_default = storefront;
|
|
471
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
472
|
+
0 && (module.exports = {
|
|
473
|
+
storefront
|
|
474
|
+
});
|