@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
package/dist/server.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// src/sdk-manager.ts
|
|
2
|
+
import { cache } from "react";
|
|
3
|
+
import {
|
|
4
|
+
StorefrontSDK,
|
|
5
|
+
MemoryTokenStorage,
|
|
6
|
+
Environment
|
|
7
|
+
} from "@commercengine/storefront-sdk";
|
|
8
|
+
|
|
9
|
+
// src/token-storage.ts
|
|
10
|
+
var ClientTokenStorage = class {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
const prefix = options.prefix || "ce_";
|
|
13
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
14
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
15
|
+
this.options = {
|
|
16
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
17
|
+
// 30 days default
|
|
18
|
+
path: options.path || "/",
|
|
19
|
+
domain: options.domain,
|
|
20
|
+
secure: options.secure ?? (typeof window !== "undefined" && window.location?.protocol === "https:"),
|
|
21
|
+
sameSite: options.sameSite || "Lax"
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
async getAccessToken() {
|
|
25
|
+
return this.getCookie(this.accessTokenKey);
|
|
26
|
+
}
|
|
27
|
+
async setAccessToken(token) {
|
|
28
|
+
this.setCookie(this.accessTokenKey, token);
|
|
29
|
+
}
|
|
30
|
+
async getRefreshToken() {
|
|
31
|
+
return this.getCookie(this.refreshTokenKey);
|
|
32
|
+
}
|
|
33
|
+
async setRefreshToken(token) {
|
|
34
|
+
this.setCookie(this.refreshTokenKey, token);
|
|
35
|
+
}
|
|
36
|
+
async clearTokens() {
|
|
37
|
+
this.deleteCookie(this.accessTokenKey);
|
|
38
|
+
this.deleteCookie(this.refreshTokenKey);
|
|
39
|
+
}
|
|
40
|
+
getCookie(name) {
|
|
41
|
+
if (typeof document === "undefined") return null;
|
|
42
|
+
const value = `; ${document.cookie}`;
|
|
43
|
+
const parts = value.split(`; ${name}=`);
|
|
44
|
+
if (parts.length === 2) {
|
|
45
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
46
|
+
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
setCookie(name, value) {
|
|
51
|
+
if (typeof document === "undefined") return;
|
|
52
|
+
const encodedValue = encodeURIComponent(value);
|
|
53
|
+
let cookieString = `${name}=${encodedValue}`;
|
|
54
|
+
if (this.options.maxAge) {
|
|
55
|
+
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
56
|
+
}
|
|
57
|
+
if (this.options.path) {
|
|
58
|
+
cookieString += `; Path=${this.options.path}`;
|
|
59
|
+
}
|
|
60
|
+
if (this.options.domain) {
|
|
61
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
62
|
+
}
|
|
63
|
+
if (this.options.secure) {
|
|
64
|
+
cookieString += `; Secure`;
|
|
65
|
+
}
|
|
66
|
+
if (this.options.sameSite) {
|
|
67
|
+
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
68
|
+
}
|
|
69
|
+
document.cookie = cookieString;
|
|
70
|
+
}
|
|
71
|
+
deleteCookie(name) {
|
|
72
|
+
if (typeof document === "undefined") return;
|
|
73
|
+
let cookieString = `${name}=; Max-Age=0`;
|
|
74
|
+
if (this.options.path) {
|
|
75
|
+
cookieString += `; Path=${this.options.path}`;
|
|
76
|
+
}
|
|
77
|
+
if (this.options.domain) {
|
|
78
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
79
|
+
}
|
|
80
|
+
document.cookie = cookieString;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var ServerTokenStorage = class {
|
|
84
|
+
constructor(cookieStore, options = {}) {
|
|
85
|
+
const prefix = options.prefix || "ce_";
|
|
86
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
87
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
88
|
+
this.cookieStore = cookieStore;
|
|
89
|
+
this.options = {
|
|
90
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
91
|
+
// 30 days default
|
|
92
|
+
path: options.path || "/",
|
|
93
|
+
domain: options.domain,
|
|
94
|
+
secure: options.secure ?? process.env.NODE_ENV === "production",
|
|
95
|
+
sameSite: options.sameSite || "Lax"
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
async getAccessToken() {
|
|
99
|
+
try {
|
|
100
|
+
return this.cookieStore.get(this.accessTokenKey)?.value || null;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.warn(`Could not get access token from server cookies:`, error);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async setAccessToken(token) {
|
|
107
|
+
try {
|
|
108
|
+
this.cookieStore.set(this.accessTokenKey, token, {
|
|
109
|
+
maxAge: this.options.maxAge,
|
|
110
|
+
path: this.options.path,
|
|
111
|
+
domain: this.options.domain,
|
|
112
|
+
secure: this.options.secure,
|
|
113
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
114
|
+
httpOnly: false
|
|
115
|
+
// Allow client-side access for SDK flexibility
|
|
116
|
+
});
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.warn(`Could not set access token on server:`, error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async getRefreshToken() {
|
|
122
|
+
try {
|
|
123
|
+
return this.cookieStore.get(this.refreshTokenKey)?.value || null;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.warn(`Could not get refresh token from server cookies:`, error);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async setRefreshToken(token) {
|
|
130
|
+
try {
|
|
131
|
+
this.cookieStore.set(this.refreshTokenKey, token, {
|
|
132
|
+
maxAge: this.options.maxAge,
|
|
133
|
+
path: this.options.path,
|
|
134
|
+
domain: this.options.domain,
|
|
135
|
+
secure: this.options.secure,
|
|
136
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
137
|
+
httpOnly: false
|
|
138
|
+
// Allow client-side access for SDK flexibility
|
|
139
|
+
});
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.warn(`Could not set refresh token on server:`, error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async clearTokens() {
|
|
145
|
+
try {
|
|
146
|
+
this.cookieStore.delete(this.accessTokenKey);
|
|
147
|
+
this.cookieStore.delete(this.refreshTokenKey);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.warn(`Could not clear tokens on server:`, error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// src/build-token-cache.ts
|
|
155
|
+
var store = /* @__PURE__ */ new Map();
|
|
156
|
+
function isExpired(token) {
|
|
157
|
+
if (!token) return true;
|
|
158
|
+
if (!token.expiresAt) return false;
|
|
159
|
+
return Date.now() > token.expiresAt - 3e4;
|
|
160
|
+
}
|
|
161
|
+
function getCachedToken(key) {
|
|
162
|
+
const token = store.get(key);
|
|
163
|
+
return isExpired(token) ? null : token;
|
|
164
|
+
}
|
|
165
|
+
function setCachedToken(key, token) {
|
|
166
|
+
const expiresAt = token.ttlSeconds != null ? Date.now() + token.ttlSeconds * 1e3 : void 0;
|
|
167
|
+
store.set(key, {
|
|
168
|
+
accessToken: token.accessToken,
|
|
169
|
+
refreshToken: token.refreshToken ?? null,
|
|
170
|
+
expiresAt
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function clearCachedToken(key) {
|
|
174
|
+
store.delete(key);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/build-caching-memory-storage.ts
|
|
178
|
+
var DEFAULT_TTL_SECONDS = 5 * 60;
|
|
179
|
+
var BuildCachingMemoryTokenStorage = class {
|
|
180
|
+
constructor(cacheKey, ttlSeconds = DEFAULT_TTL_SECONDS) {
|
|
181
|
+
this.cacheKey = cacheKey;
|
|
182
|
+
this.ttlSeconds = ttlSeconds;
|
|
183
|
+
this.access = null;
|
|
184
|
+
this.refresh = null;
|
|
185
|
+
}
|
|
186
|
+
async getAccessToken() {
|
|
187
|
+
if (this.access) {
|
|
188
|
+
return this.access;
|
|
189
|
+
}
|
|
190
|
+
const cached = getCachedToken(this.cacheKey);
|
|
191
|
+
if (cached?.accessToken) {
|
|
192
|
+
this.access = cached.accessToken;
|
|
193
|
+
this.refresh = cached.refreshToken ?? null;
|
|
194
|
+
return this.access;
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
async setAccessToken(token) {
|
|
199
|
+
this.access = token;
|
|
200
|
+
setCachedToken(this.cacheKey, {
|
|
201
|
+
accessToken: token,
|
|
202
|
+
refreshToken: this.refresh,
|
|
203
|
+
ttlSeconds: this.ttlSeconds
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async getRefreshToken() {
|
|
207
|
+
return this.refresh;
|
|
208
|
+
}
|
|
209
|
+
async setRefreshToken(token) {
|
|
210
|
+
this.refresh = token;
|
|
211
|
+
setCachedToken(this.cacheKey, {
|
|
212
|
+
accessToken: this.access ?? "",
|
|
213
|
+
refreshToken: token,
|
|
214
|
+
ttlSeconds: this.ttlSeconds
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
async clearTokens() {
|
|
218
|
+
this.access = null;
|
|
219
|
+
this.refresh = null;
|
|
220
|
+
clearCachedToken(this.cacheKey);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// src/sdk-manager.ts
|
|
225
|
+
function getConfig() {
|
|
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;
|
|
235
|
+
}
|
|
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
|
+
);
|
|
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;
|
|
279
|
+
}
|
|
280
|
+
var globalStorefrontConfig = null;
|
|
281
|
+
var clientSDK = null;
|
|
282
|
+
function createTokenStorage(cookieStore, options, config) {
|
|
283
|
+
if (typeof window !== "undefined") {
|
|
284
|
+
return new ClientTokenStorage(options);
|
|
285
|
+
}
|
|
286
|
+
if (cookieStore) {
|
|
287
|
+
return new ServerTokenStorage(cookieStore, options);
|
|
288
|
+
}
|
|
289
|
+
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
290
|
+
if (shouldCache && config) {
|
|
291
|
+
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
292
|
+
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
293
|
+
}
|
|
294
|
+
return new MemoryTokenStorage();
|
|
295
|
+
}
|
|
296
|
+
var getServerSDKCached = cache((cookieStore) => {
|
|
297
|
+
const config = getConfig();
|
|
298
|
+
return new StorefrontSDK({
|
|
299
|
+
...config,
|
|
300
|
+
tokenStorage: createTokenStorage(
|
|
301
|
+
cookieStore,
|
|
302
|
+
config.tokenStorageOptions,
|
|
303
|
+
config
|
|
304
|
+
)
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
var buildTimeSDK = null;
|
|
308
|
+
function getBuildTimeSDK() {
|
|
309
|
+
const config = getConfig();
|
|
310
|
+
if (!buildTimeSDK) {
|
|
311
|
+
buildTimeSDK = new StorefrontSDK({
|
|
312
|
+
...config,
|
|
313
|
+
tokenStorage: createTokenStorage(
|
|
314
|
+
void 0,
|
|
315
|
+
config.tokenStorageOptions,
|
|
316
|
+
config
|
|
317
|
+
)
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return buildTimeSDK;
|
|
321
|
+
}
|
|
322
|
+
function getStorefrontSDK(cookieStore) {
|
|
323
|
+
if (typeof window !== "undefined") {
|
|
324
|
+
if (cookieStore) {
|
|
325
|
+
console.warn(
|
|
326
|
+
"Cookie store passed in client environment - this will be ignored"
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
const config = getConfig();
|
|
330
|
+
if (!clientSDK) {
|
|
331
|
+
clientSDK = new StorefrontSDK({
|
|
332
|
+
...config,
|
|
333
|
+
tokenStorage: createTokenStorage(
|
|
334
|
+
void 0,
|
|
335
|
+
config.tokenStorageOptions,
|
|
336
|
+
config
|
|
337
|
+
)
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return clientSDK;
|
|
341
|
+
}
|
|
342
|
+
if (cookieStore) {
|
|
343
|
+
return getServerSDKCached(cookieStore);
|
|
344
|
+
}
|
|
345
|
+
return getBuildTimeSDK();
|
|
346
|
+
}
|
|
347
|
+
function initializeStorefrontSDK() {
|
|
348
|
+
clientSDK = null;
|
|
349
|
+
buildTimeSDK = null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/server.ts
|
|
353
|
+
export * from "@commercengine/storefront-sdk";
|
|
354
|
+
export {
|
|
355
|
+
ServerTokenStorage,
|
|
356
|
+
getStorefrontSDK,
|
|
357
|
+
initializeStorefrontSDK
|
|
358
|
+
};
|