@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/client.cjs
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// src/client.ts
|
|
23
|
+
var client_exports = {};
|
|
24
|
+
__export(client_exports, {
|
|
25
|
+
ClientTokenStorage: () => ClientTokenStorage,
|
|
26
|
+
StorefrontSDKInitializer: () => StorefrontSDKInitializer,
|
|
27
|
+
getStorefrontSDK: () => getStorefrontSDK,
|
|
28
|
+
initializeStorefrontSDK: () => initializeStorefrontSDK
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(client_exports);
|
|
31
|
+
|
|
32
|
+
// src/sdk-manager.ts
|
|
33
|
+
var import_react = require("react");
|
|
34
|
+
var import_storefront_sdk = require("@commercengine/storefront-sdk");
|
|
35
|
+
|
|
36
|
+
// src/token-storage.ts
|
|
37
|
+
var ClientTokenStorage = class {
|
|
38
|
+
constructor(options = {}) {
|
|
39
|
+
const prefix = options.prefix || "ce_";
|
|
40
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
41
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
42
|
+
this.options = {
|
|
43
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
44
|
+
// 30 days default
|
|
45
|
+
path: options.path || "/",
|
|
46
|
+
domain: options.domain,
|
|
47
|
+
secure: options.secure ?? (typeof window !== "undefined" && window.location?.protocol === "https:"),
|
|
48
|
+
sameSite: options.sameSite || "Lax"
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async getAccessToken() {
|
|
52
|
+
return this.getCookie(this.accessTokenKey);
|
|
53
|
+
}
|
|
54
|
+
async setAccessToken(token) {
|
|
55
|
+
this.setCookie(this.accessTokenKey, token);
|
|
56
|
+
}
|
|
57
|
+
async getRefreshToken() {
|
|
58
|
+
return this.getCookie(this.refreshTokenKey);
|
|
59
|
+
}
|
|
60
|
+
async setRefreshToken(token) {
|
|
61
|
+
this.setCookie(this.refreshTokenKey, token);
|
|
62
|
+
}
|
|
63
|
+
async clearTokens() {
|
|
64
|
+
this.deleteCookie(this.accessTokenKey);
|
|
65
|
+
this.deleteCookie(this.refreshTokenKey);
|
|
66
|
+
}
|
|
67
|
+
getCookie(name) {
|
|
68
|
+
if (typeof document === "undefined") return null;
|
|
69
|
+
const value = `; ${document.cookie}`;
|
|
70
|
+
const parts = value.split(`; ${name}=`);
|
|
71
|
+
if (parts.length === 2) {
|
|
72
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
73
|
+
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
setCookie(name, value) {
|
|
78
|
+
if (typeof document === "undefined") return;
|
|
79
|
+
const encodedValue = encodeURIComponent(value);
|
|
80
|
+
let cookieString = `${name}=${encodedValue}`;
|
|
81
|
+
if (this.options.maxAge) {
|
|
82
|
+
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
83
|
+
}
|
|
84
|
+
if (this.options.path) {
|
|
85
|
+
cookieString += `; Path=${this.options.path}`;
|
|
86
|
+
}
|
|
87
|
+
if (this.options.domain) {
|
|
88
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
89
|
+
}
|
|
90
|
+
if (this.options.secure) {
|
|
91
|
+
cookieString += `; Secure`;
|
|
92
|
+
}
|
|
93
|
+
if (this.options.sameSite) {
|
|
94
|
+
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
95
|
+
}
|
|
96
|
+
document.cookie = cookieString;
|
|
97
|
+
}
|
|
98
|
+
deleteCookie(name) {
|
|
99
|
+
if (typeof document === "undefined") return;
|
|
100
|
+
let cookieString = `${name}=; Max-Age=0`;
|
|
101
|
+
if (this.options.path) {
|
|
102
|
+
cookieString += `; Path=${this.options.path}`;
|
|
103
|
+
}
|
|
104
|
+
if (this.options.domain) {
|
|
105
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
106
|
+
}
|
|
107
|
+
document.cookie = cookieString;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var ServerTokenStorage = class {
|
|
111
|
+
constructor(cookieStore, options = {}) {
|
|
112
|
+
const prefix = options.prefix || "ce_";
|
|
113
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
114
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
115
|
+
this.cookieStore = cookieStore;
|
|
116
|
+
this.options = {
|
|
117
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
118
|
+
// 30 days default
|
|
119
|
+
path: options.path || "/",
|
|
120
|
+
domain: options.domain,
|
|
121
|
+
secure: options.secure ?? process.env.NODE_ENV === "production",
|
|
122
|
+
sameSite: options.sameSite || "Lax"
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async getAccessToken() {
|
|
126
|
+
try {
|
|
127
|
+
return this.cookieStore.get(this.accessTokenKey)?.value || null;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.warn(`Could not get access token from server cookies:`, error);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async setAccessToken(token) {
|
|
134
|
+
try {
|
|
135
|
+
this.cookieStore.set(this.accessTokenKey, token, {
|
|
136
|
+
maxAge: this.options.maxAge,
|
|
137
|
+
path: this.options.path,
|
|
138
|
+
domain: this.options.domain,
|
|
139
|
+
secure: this.options.secure,
|
|
140
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
141
|
+
httpOnly: false
|
|
142
|
+
// Allow client-side access for SDK flexibility
|
|
143
|
+
});
|
|
144
|
+
} catch (error) {
|
|
145
|
+
console.warn(`Could not set access token on server:`, error);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async getRefreshToken() {
|
|
149
|
+
try {
|
|
150
|
+
return this.cookieStore.get(this.refreshTokenKey)?.value || null;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.warn(`Could not get refresh token from server cookies:`, error);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async setRefreshToken(token) {
|
|
157
|
+
try {
|
|
158
|
+
this.cookieStore.set(this.refreshTokenKey, token, {
|
|
159
|
+
maxAge: this.options.maxAge,
|
|
160
|
+
path: this.options.path,
|
|
161
|
+
domain: this.options.domain,
|
|
162
|
+
secure: this.options.secure,
|
|
163
|
+
sameSite: this.options.sameSite?.toLowerCase(),
|
|
164
|
+
httpOnly: false
|
|
165
|
+
// Allow client-side access for SDK flexibility
|
|
166
|
+
});
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.warn(`Could not set refresh token on server:`, error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async clearTokens() {
|
|
172
|
+
try {
|
|
173
|
+
this.cookieStore.delete(this.accessTokenKey);
|
|
174
|
+
this.cookieStore.delete(this.refreshTokenKey);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.warn(`Could not clear tokens on server:`, error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// src/build-token-cache.ts
|
|
182
|
+
var store = /* @__PURE__ */ new Map();
|
|
183
|
+
function isExpired(token) {
|
|
184
|
+
if (!token) return true;
|
|
185
|
+
if (!token.expiresAt) return false;
|
|
186
|
+
return Date.now() > token.expiresAt - 3e4;
|
|
187
|
+
}
|
|
188
|
+
function getCachedToken(key) {
|
|
189
|
+
const token = store.get(key);
|
|
190
|
+
return isExpired(token) ? null : token;
|
|
191
|
+
}
|
|
192
|
+
function setCachedToken(key, token) {
|
|
193
|
+
const expiresAt = token.ttlSeconds != null ? Date.now() + token.ttlSeconds * 1e3 : void 0;
|
|
194
|
+
store.set(key, {
|
|
195
|
+
accessToken: token.accessToken,
|
|
196
|
+
refreshToken: token.refreshToken ?? null,
|
|
197
|
+
expiresAt
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
function clearCachedToken(key) {
|
|
201
|
+
store.delete(key);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/build-caching-memory-storage.ts
|
|
205
|
+
var DEFAULT_TTL_SECONDS = 5 * 60;
|
|
206
|
+
var BuildCachingMemoryTokenStorage = class {
|
|
207
|
+
constructor(cacheKey, ttlSeconds = DEFAULT_TTL_SECONDS) {
|
|
208
|
+
this.cacheKey = cacheKey;
|
|
209
|
+
this.ttlSeconds = ttlSeconds;
|
|
210
|
+
this.access = null;
|
|
211
|
+
this.refresh = null;
|
|
212
|
+
}
|
|
213
|
+
async getAccessToken() {
|
|
214
|
+
if (this.access) {
|
|
215
|
+
return this.access;
|
|
216
|
+
}
|
|
217
|
+
const cached = getCachedToken(this.cacheKey);
|
|
218
|
+
if (cached?.accessToken) {
|
|
219
|
+
this.access = cached.accessToken;
|
|
220
|
+
this.refresh = cached.refreshToken ?? null;
|
|
221
|
+
return this.access;
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
async setAccessToken(token) {
|
|
226
|
+
this.access = token;
|
|
227
|
+
setCachedToken(this.cacheKey, {
|
|
228
|
+
accessToken: token,
|
|
229
|
+
refreshToken: this.refresh,
|
|
230
|
+
ttlSeconds: this.ttlSeconds
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
async getRefreshToken() {
|
|
234
|
+
return this.refresh;
|
|
235
|
+
}
|
|
236
|
+
async setRefreshToken(token) {
|
|
237
|
+
this.refresh = token;
|
|
238
|
+
setCachedToken(this.cacheKey, {
|
|
239
|
+
accessToken: this.access ?? "",
|
|
240
|
+
refreshToken: token,
|
|
241
|
+
ttlSeconds: this.ttlSeconds
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
async clearTokens() {
|
|
245
|
+
this.access = null;
|
|
246
|
+
this.refresh = null;
|
|
247
|
+
clearCachedToken(this.cacheKey);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// src/sdk-manager.ts
|
|
252
|
+
function getConfig() {
|
|
253
|
+
const envStoreId = process.env.NEXT_PUBLIC_STORE_ID;
|
|
254
|
+
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
|
|
255
|
+
const envEnvironment = process.env.NEXT_PUBLIC_ENVIRONMENT;
|
|
256
|
+
const envBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
|
|
257
|
+
const envTimeout = process.env.NEXT_PUBLIC_API_TIMEOUT ? parseInt(process.env.NEXT_PUBLIC_API_TIMEOUT, 10) : void 0;
|
|
258
|
+
const envDebug = process.env.NEXT_PUBLIC_DEBUG_MODE === "true";
|
|
259
|
+
const envDefaultHeaders = {};
|
|
260
|
+
if (process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID) {
|
|
261
|
+
envDefaultHeaders.customer_group_id = process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_GROUP_ID;
|
|
262
|
+
}
|
|
263
|
+
const storeId = globalStorefrontConfig?.storeId || envStoreId;
|
|
264
|
+
const apiKey = globalStorefrontConfig?.apiKey || envApiKey;
|
|
265
|
+
const environment = globalStorefrontConfig?.environment || (envEnvironment === "production" ? import_storefront_sdk.Environment.Production : import_storefront_sdk.Environment.Staging);
|
|
266
|
+
const baseUrl = globalStorefrontConfig?.baseUrl || envBaseUrl;
|
|
267
|
+
const timeout = globalStorefrontConfig?.timeout || envTimeout;
|
|
268
|
+
const debug = globalStorefrontConfig?.debug !== void 0 ? globalStorefrontConfig.debug : envDebug;
|
|
269
|
+
const defaultHeaders = {
|
|
270
|
+
...envDefaultHeaders,
|
|
271
|
+
...globalStorefrontConfig?.defaultHeaders
|
|
272
|
+
};
|
|
273
|
+
if (!storeId || !apiKey) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`StorefrontSDK configuration missing! Please set the following environment variables:
|
|
276
|
+
|
|
277
|
+
NEXT_PUBLIC_STORE_ID=your-store-id
|
|
278
|
+
NEXT_PUBLIC_API_KEY=your-api-key
|
|
279
|
+
NEXT_PUBLIC_ENVIRONMENT=staging (or production)
|
|
280
|
+
|
|
281
|
+
These variables are required for both client and server contexts to work.`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const config = {
|
|
285
|
+
storeId,
|
|
286
|
+
apiKey,
|
|
287
|
+
environment
|
|
288
|
+
};
|
|
289
|
+
if (baseUrl) config.baseUrl = baseUrl;
|
|
290
|
+
if (timeout) config.timeout = timeout;
|
|
291
|
+
if (debug) config.debug = debug;
|
|
292
|
+
const logger = globalStorefrontConfig?.logger;
|
|
293
|
+
const accessToken = globalStorefrontConfig?.accessToken;
|
|
294
|
+
const refreshToken = globalStorefrontConfig?.refreshToken;
|
|
295
|
+
const onTokensUpdated = globalStorefrontConfig?.onTokensUpdated;
|
|
296
|
+
const onTokensCleared = globalStorefrontConfig?.onTokensCleared;
|
|
297
|
+
const tokenStorageOptions = globalStorefrontConfig?.tokenStorageOptions;
|
|
298
|
+
if (logger) config.logger = logger;
|
|
299
|
+
if (accessToken) config.accessToken = accessToken;
|
|
300
|
+
if (refreshToken) config.refreshToken = refreshToken;
|
|
301
|
+
if (onTokensUpdated) config.onTokensUpdated = onTokensUpdated;
|
|
302
|
+
if (onTokensCleared) config.onTokensCleared = onTokensCleared;
|
|
303
|
+
if (Object.keys(defaultHeaders).length > 0) config.defaultHeaders = defaultHeaders;
|
|
304
|
+
if (tokenStorageOptions) config.tokenStorageOptions = tokenStorageOptions;
|
|
305
|
+
return config;
|
|
306
|
+
}
|
|
307
|
+
var globalStorefrontConfig = null;
|
|
308
|
+
var clientSDK = null;
|
|
309
|
+
function createTokenStorage(cookieStore, options, config) {
|
|
310
|
+
if (typeof window !== "undefined") {
|
|
311
|
+
return new ClientTokenStorage(options);
|
|
312
|
+
}
|
|
313
|
+
if (cookieStore) {
|
|
314
|
+
return new ServerTokenStorage(cookieStore, options);
|
|
315
|
+
}
|
|
316
|
+
const shouldCache = process.env.NEXT_BUILD_CACHE_TOKENS === "true";
|
|
317
|
+
if (shouldCache && config) {
|
|
318
|
+
const cacheKey = `${config.storeId}:${config.environment || "production"}`;
|
|
319
|
+
return new BuildCachingMemoryTokenStorage(cacheKey);
|
|
320
|
+
}
|
|
321
|
+
return new import_storefront_sdk.MemoryTokenStorage();
|
|
322
|
+
}
|
|
323
|
+
var getServerSDKCached = (0, import_react.cache)((cookieStore) => {
|
|
324
|
+
const config = getConfig();
|
|
325
|
+
return new import_storefront_sdk.StorefrontSDK({
|
|
326
|
+
...config,
|
|
327
|
+
tokenStorage: createTokenStorage(
|
|
328
|
+
cookieStore,
|
|
329
|
+
config.tokenStorageOptions,
|
|
330
|
+
config
|
|
331
|
+
)
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
var buildTimeSDK = null;
|
|
335
|
+
function getBuildTimeSDK() {
|
|
336
|
+
const config = getConfig();
|
|
337
|
+
if (!buildTimeSDK) {
|
|
338
|
+
buildTimeSDK = new import_storefront_sdk.StorefrontSDK({
|
|
339
|
+
...config,
|
|
340
|
+
tokenStorage: createTokenStorage(
|
|
341
|
+
void 0,
|
|
342
|
+
config.tokenStorageOptions,
|
|
343
|
+
config
|
|
344
|
+
)
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
return buildTimeSDK;
|
|
348
|
+
}
|
|
349
|
+
function getStorefrontSDK(cookieStore) {
|
|
350
|
+
if (typeof window !== "undefined") {
|
|
351
|
+
if (cookieStore) {
|
|
352
|
+
console.warn(
|
|
353
|
+
"Cookie store passed in client environment - this will be ignored"
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
const config = getConfig();
|
|
357
|
+
if (!clientSDK) {
|
|
358
|
+
clientSDK = new import_storefront_sdk.StorefrontSDK({
|
|
359
|
+
...config,
|
|
360
|
+
tokenStorage: createTokenStorage(
|
|
361
|
+
void 0,
|
|
362
|
+
config.tokenStorageOptions,
|
|
363
|
+
config
|
|
364
|
+
)
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return clientSDK;
|
|
368
|
+
}
|
|
369
|
+
if (cookieStore) {
|
|
370
|
+
return getServerSDKCached(cookieStore);
|
|
371
|
+
}
|
|
372
|
+
return getBuildTimeSDK();
|
|
373
|
+
}
|
|
374
|
+
function initializeStorefrontSDK() {
|
|
375
|
+
clientSDK = null;
|
|
376
|
+
buildTimeSDK = null;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// src/init-client.ts
|
|
380
|
+
var import_react2 = require("react");
|
|
381
|
+
async function bootstrapTokens() {
|
|
382
|
+
const sdk = getStorefrontSDK();
|
|
383
|
+
const accessToken = await sdk.getAccessToken();
|
|
384
|
+
if (!accessToken) {
|
|
385
|
+
await sdk.auth.getAnonymousToken();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
function StorefrontSDKInitializer({ runtimeConfig } = {}) {
|
|
389
|
+
(0, import_react2.useEffect)(() => {
|
|
390
|
+
initializeStorefrontSDK();
|
|
391
|
+
bootstrapTokens().catch(console.error);
|
|
392
|
+
}, [runtimeConfig]);
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/client.ts
|
|
397
|
+
__reExport(client_exports, require("@commercengine/storefront-sdk"), module.exports);
|
|
398
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
399
|
+
0 && (module.exports = {
|
|
400
|
+
ClientTokenStorage,
|
|
401
|
+
StorefrontSDKInitializer,
|
|
402
|
+
getStorefrontSDK,
|
|
403
|
+
initializeStorefrontSDK,
|
|
404
|
+
...require("@commercengine/storefront-sdk")
|
|
405
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { TokenStorage, StorefrontSDK, StorefrontSDKOptions, Environment, SupportedDefaultHeaders, DebugLoggerFn } from '@commercengine/storefront-sdk';
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for NextJSTokenStorage
|
|
7
|
+
*/
|
|
8
|
+
interface NextJSTokenStorageOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Prefix for cookie names (default: "ce_")
|
|
11
|
+
*/
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
15
|
+
*/
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Cookie path (default: "/")
|
|
19
|
+
*/
|
|
20
|
+
path?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Cookie domain (default: current domain)
|
|
23
|
+
*/
|
|
24
|
+
domain?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
27
|
+
*/
|
|
28
|
+
secure?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* SameSite cookie attribute (default: "Lax")
|
|
31
|
+
*/
|
|
32
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Client-side token storage that uses document.cookie
|
|
36
|
+
*/
|
|
37
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
38
|
+
private accessTokenKey;
|
|
39
|
+
private refreshTokenKey;
|
|
40
|
+
private options;
|
|
41
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
42
|
+
getAccessToken(): Promise<string | null>;
|
|
43
|
+
setAccessToken(token: string): Promise<void>;
|
|
44
|
+
getRefreshToken(): Promise<string | null>;
|
|
45
|
+
setRefreshToken(token: string): Promise<void>;
|
|
46
|
+
clearTokens(): Promise<void>;
|
|
47
|
+
private getCookie;
|
|
48
|
+
private setCookie;
|
|
49
|
+
private deleteCookie;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Configuration for the NextJS SDK wrapper
|
|
54
|
+
*/
|
|
55
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
56
|
+
/**
|
|
57
|
+
* Token storage configuration options
|
|
58
|
+
*/
|
|
59
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
63
|
+
* These override environment variables for that specific request
|
|
64
|
+
*/
|
|
65
|
+
interface StorefrontRuntimeConfig {
|
|
66
|
+
/**
|
|
67
|
+
* Override environment variables
|
|
68
|
+
*/
|
|
69
|
+
storeId?: string;
|
|
70
|
+
apiKey?: string;
|
|
71
|
+
environment?: Environment;
|
|
72
|
+
baseUrl?: string;
|
|
73
|
+
timeout?: number;
|
|
74
|
+
debug?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Advanced options not available via environment variables
|
|
77
|
+
*/
|
|
78
|
+
accessToken?: string;
|
|
79
|
+
refreshToken?: string;
|
|
80
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
81
|
+
logger?: DebugLoggerFn;
|
|
82
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
83
|
+
onTokensCleared?: () => void;
|
|
84
|
+
/**
|
|
85
|
+
* Token storage configuration
|
|
86
|
+
*/
|
|
87
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
88
|
+
}
|
|
89
|
+
type NextCookieStore = {
|
|
90
|
+
get: (name: string) => {
|
|
91
|
+
value: string;
|
|
92
|
+
} | undefined;
|
|
93
|
+
set: (name: string, value: string, options?: any) => void;
|
|
94
|
+
delete: (name: string) => void;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Smart SDK getter that automatically detects environment
|
|
98
|
+
*
|
|
99
|
+
* Usage:
|
|
100
|
+
* - Client-side: getStorefrontSDK()
|
|
101
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
102
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
103
|
+
*/
|
|
104
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
105
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
106
|
+
/**
|
|
107
|
+
* Initialize the SDK (internal use)
|
|
108
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
109
|
+
*/
|
|
110
|
+
declare function initializeStorefrontSDK(): void;
|
|
111
|
+
|
|
112
|
+
interface StorefrontSDKInitializerProps {
|
|
113
|
+
/**
|
|
114
|
+
* Optional runtime configuration overrides for client-side API calls
|
|
115
|
+
* These settings will apply to all client-side storefront() calls
|
|
116
|
+
*/
|
|
117
|
+
runtimeConfig?: StorefrontRuntimeConfig;
|
|
118
|
+
}
|
|
119
|
+
declare function StorefrontSDKInitializer({ runtimeConfig }?: StorefrontSDKInitializerProps): null;
|
|
120
|
+
|
|
121
|
+
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, StorefrontSDKInitializer, getStorefrontSDK, initializeStorefrontSDK };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { TokenStorage, StorefrontSDK, StorefrontSDKOptions, Environment, SupportedDefaultHeaders, DebugLoggerFn } from '@commercengine/storefront-sdk';
|
|
2
|
+
export * from '@commercengine/storefront-sdk';
|
|
3
|
+
export { StorefrontSDK, StorefrontSDKOptions } from '@commercengine/storefront-sdk';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for NextJSTokenStorage
|
|
7
|
+
*/
|
|
8
|
+
interface NextJSTokenStorageOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Prefix for cookie names (default: "ce_")
|
|
11
|
+
*/
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Maximum age of cookies in seconds (default: 30 days)
|
|
15
|
+
*/
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Cookie path (default: "/")
|
|
19
|
+
*/
|
|
20
|
+
path?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Cookie domain (default: current domain)
|
|
23
|
+
*/
|
|
24
|
+
domain?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Whether cookies should be secure (default: auto-detect based on environment)
|
|
27
|
+
*/
|
|
28
|
+
secure?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* SameSite cookie attribute (default: "Lax")
|
|
31
|
+
*/
|
|
32
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Client-side token storage that uses document.cookie
|
|
36
|
+
*/
|
|
37
|
+
declare class ClientTokenStorage implements TokenStorage {
|
|
38
|
+
private accessTokenKey;
|
|
39
|
+
private refreshTokenKey;
|
|
40
|
+
private options;
|
|
41
|
+
constructor(options?: NextJSTokenStorageOptions);
|
|
42
|
+
getAccessToken(): Promise<string | null>;
|
|
43
|
+
setAccessToken(token: string): Promise<void>;
|
|
44
|
+
getRefreshToken(): Promise<string | null>;
|
|
45
|
+
setRefreshToken(token: string): Promise<void>;
|
|
46
|
+
clearTokens(): Promise<void>;
|
|
47
|
+
private getCookie;
|
|
48
|
+
private setCookie;
|
|
49
|
+
private deleteCookie;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Configuration for the NextJS SDK wrapper
|
|
54
|
+
*/
|
|
55
|
+
interface NextJSSDKConfig extends Omit<StorefrontSDKOptions, "tokenStorage"> {
|
|
56
|
+
/**
|
|
57
|
+
* Token storage configuration options
|
|
58
|
+
*/
|
|
59
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Runtime configuration overrides that can be passed to storefront() function
|
|
63
|
+
* These override environment variables for that specific request
|
|
64
|
+
*/
|
|
65
|
+
interface StorefrontRuntimeConfig {
|
|
66
|
+
/**
|
|
67
|
+
* Override environment variables
|
|
68
|
+
*/
|
|
69
|
+
storeId?: string;
|
|
70
|
+
apiKey?: string;
|
|
71
|
+
environment?: Environment;
|
|
72
|
+
baseUrl?: string;
|
|
73
|
+
timeout?: number;
|
|
74
|
+
debug?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Advanced options not available via environment variables
|
|
77
|
+
*/
|
|
78
|
+
accessToken?: string;
|
|
79
|
+
refreshToken?: string;
|
|
80
|
+
defaultHeaders?: SupportedDefaultHeaders;
|
|
81
|
+
logger?: DebugLoggerFn;
|
|
82
|
+
onTokensUpdated?: (accessToken: string, refreshToken: string) => void;
|
|
83
|
+
onTokensCleared?: () => void;
|
|
84
|
+
/**
|
|
85
|
+
* Token storage configuration
|
|
86
|
+
*/
|
|
87
|
+
tokenStorageOptions?: NextJSTokenStorageOptions;
|
|
88
|
+
}
|
|
89
|
+
type NextCookieStore = {
|
|
90
|
+
get: (name: string) => {
|
|
91
|
+
value: string;
|
|
92
|
+
} | undefined;
|
|
93
|
+
set: (name: string, value: string, options?: any) => void;
|
|
94
|
+
delete: (name: string) => void;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Smart SDK getter that automatically detects environment
|
|
98
|
+
*
|
|
99
|
+
* Usage:
|
|
100
|
+
* - Client-side: getStorefrontSDK()
|
|
101
|
+
* - Server-side with request context: getStorefrontSDK(await cookies())
|
|
102
|
+
* - SSG/ISR (no request context): getStorefrontSDK() (uses memory storage)
|
|
103
|
+
*/
|
|
104
|
+
declare function getStorefrontSDK(): StorefrontSDK;
|
|
105
|
+
declare function getStorefrontSDK(cookieStore: NextCookieStore): StorefrontSDK;
|
|
106
|
+
/**
|
|
107
|
+
* Initialize the SDK (internal use)
|
|
108
|
+
* This should be called once in your app via StorefrontSDKInitializer
|
|
109
|
+
*/
|
|
110
|
+
declare function initializeStorefrontSDK(): void;
|
|
111
|
+
|
|
112
|
+
interface StorefrontSDKInitializerProps {
|
|
113
|
+
/**
|
|
114
|
+
* Optional runtime configuration overrides for client-side API calls
|
|
115
|
+
* These settings will apply to all client-side storefront() calls
|
|
116
|
+
*/
|
|
117
|
+
runtimeConfig?: StorefrontRuntimeConfig;
|
|
118
|
+
}
|
|
119
|
+
declare function StorefrontSDKInitializer({ runtimeConfig }?: StorefrontSDKInitializerProps): null;
|
|
120
|
+
|
|
121
|
+
export { ClientTokenStorage, type NextJSSDKConfig, type NextJSTokenStorageOptions, StorefrontSDKInitializer, getStorefrontSDK, initializeStorefrontSDK };
|