@commercengine/storefront-sdk 0.5.0 → 0.6.0
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 +14 -1
- package/dist/index.cjs +111 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -9
- package/dist/index.d.ts +48 -9
- package/dist/index.iife.js +116 -17
- package/dist/index.iife.js.map +1 -1
- package/dist/index.js +117 -17
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -170,6 +170,18 @@ import { MemoryTokenStorage } from "@commercengine/storefront-sdk";
|
|
|
170
170
|
const tokenStorage = new MemoryTokenStorage();
|
|
171
171
|
```
|
|
172
172
|
|
|
173
|
+
#### Next.js Universal (recommended for Next.js apps)
|
|
174
|
+
```typescript
|
|
175
|
+
import { NextJSTokenStorage } from "@commercengine/storefront-sdk";
|
|
176
|
+
|
|
177
|
+
const tokenStorage = new NextJSTokenStorage({
|
|
178
|
+
prefix: "myapp_",
|
|
179
|
+
maxAge: 30 * 24 * 60 * 60, // 30 days
|
|
180
|
+
secure: true, // Auto-detects HTTPS in browser
|
|
181
|
+
sameSite: "Lax",
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
173
185
|
#### Custom Storage
|
|
174
186
|
```typescript
|
|
175
187
|
class CustomTokenStorage implements TokenStorage {
|
|
@@ -391,7 +403,8 @@ The SDK works seamlessly across all JavaScript environments:
|
|
|
391
403
|
- **Background jobs**: Reliable token management for long-running processes
|
|
392
404
|
|
|
393
405
|
### Hybrid Rendering (SSR/SSG)
|
|
394
|
-
- **Next.js
|
|
406
|
+
- **Next.js**: Use `NextJSTokenStorage` for universal client/server token access
|
|
407
|
+
- **Nuxt, SvelteKit**: Use `CookieTokenStorage` for seamless client/server token handoff
|
|
395
408
|
- **Cookie-based storage**: Maintains sessions across server/client boundaries
|
|
396
409
|
- **Hydration-safe**: No client/server state mismatches
|
|
397
410
|
|
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,7 @@ __export(index_exports, {
|
|
|
39
39
|
Environment: () => Environment,
|
|
40
40
|
HelpersClient: () => HelpersClient,
|
|
41
41
|
MemoryTokenStorage: () => MemoryTokenStorage,
|
|
42
|
+
NextJSTokenStorage: () => NextJSTokenStorage,
|
|
42
43
|
OrderClient: () => OrderClient,
|
|
43
44
|
ResponseUtils: () => ResponseUtils,
|
|
44
45
|
ShippingClient: () => ShippingClient,
|
|
@@ -260,6 +261,115 @@ var CookieTokenStorage = class {
|
|
|
260
261
|
document.cookie = cookieString;
|
|
261
262
|
}
|
|
262
263
|
};
|
|
264
|
+
var NextJSTokenStorage = class {
|
|
265
|
+
accessTokenKey;
|
|
266
|
+
refreshTokenKey;
|
|
267
|
+
options;
|
|
268
|
+
constructor(options = {}) {
|
|
269
|
+
const prefix = options.prefix || "storefront_";
|
|
270
|
+
this.accessTokenKey = `${prefix}access_token`;
|
|
271
|
+
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
272
|
+
this.options = {
|
|
273
|
+
maxAge: options.maxAge || 30 * 24 * 60 * 60,
|
|
274
|
+
// 30 days default
|
|
275
|
+
path: options.path || "/",
|
|
276
|
+
domain: options.domain,
|
|
277
|
+
secure: options.secure ?? (typeof window !== "undefined" && window.location?.protocol === "https:"),
|
|
278
|
+
sameSite: options.sameSite || "Lax"
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
async getAccessToken() {
|
|
282
|
+
return this.getCookie(this.accessTokenKey);
|
|
283
|
+
}
|
|
284
|
+
async setAccessToken(token) {
|
|
285
|
+
this.setCookie(this.accessTokenKey, token);
|
|
286
|
+
}
|
|
287
|
+
async getRefreshToken() {
|
|
288
|
+
return this.getCookie(this.refreshTokenKey);
|
|
289
|
+
}
|
|
290
|
+
async setRefreshToken(token) {
|
|
291
|
+
this.setCookie(this.refreshTokenKey, token);
|
|
292
|
+
}
|
|
293
|
+
async clearTokens() {
|
|
294
|
+
this.deleteCookie(this.accessTokenKey);
|
|
295
|
+
this.deleteCookie(this.refreshTokenKey);
|
|
296
|
+
}
|
|
297
|
+
getCookie(name) {
|
|
298
|
+
if (typeof window !== "undefined") {
|
|
299
|
+
const value = `; ${document.cookie}`;
|
|
300
|
+
const parts = value.split(`; ${name}=`);
|
|
301
|
+
if (parts.length === 2) {
|
|
302
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
303
|
+
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
304
|
+
}
|
|
305
|
+
return null;
|
|
306
|
+
} else {
|
|
307
|
+
try {
|
|
308
|
+
const { cookies } = require("next/headers");
|
|
309
|
+
const cookieStore = cookies();
|
|
310
|
+
return cookieStore.get(name)?.value || null;
|
|
311
|
+
} catch {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
setCookie(name, value) {
|
|
317
|
+
if (typeof window !== "undefined") {
|
|
318
|
+
const encodedValue = encodeURIComponent(value);
|
|
319
|
+
let cookieString = `${name}=${encodedValue}`;
|
|
320
|
+
if (this.options.maxAge) {
|
|
321
|
+
cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
322
|
+
}
|
|
323
|
+
if (this.options.path) {
|
|
324
|
+
cookieString += `; Path=${this.options.path}`;
|
|
325
|
+
}
|
|
326
|
+
if (this.options.domain) {
|
|
327
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
328
|
+
}
|
|
329
|
+
if (this.options.secure) {
|
|
330
|
+
cookieString += `; Secure`;
|
|
331
|
+
}
|
|
332
|
+
if (this.options.sameSite) {
|
|
333
|
+
cookieString += `; SameSite=${this.options.sameSite}`;
|
|
334
|
+
}
|
|
335
|
+
document.cookie = cookieString;
|
|
336
|
+
} else {
|
|
337
|
+
try {
|
|
338
|
+
const { cookies } = require("next/headers");
|
|
339
|
+
const cookieStore = cookies();
|
|
340
|
+
cookieStore.set(name, value, {
|
|
341
|
+
maxAge: this.options.maxAge,
|
|
342
|
+
path: this.options.path,
|
|
343
|
+
domain: this.options.domain,
|
|
344
|
+
secure: this.options.secure,
|
|
345
|
+
sameSite: this.options.sameSite?.toLowerCase()
|
|
346
|
+
});
|
|
347
|
+
} catch {
|
|
348
|
+
console.warn(`Could not set cookie ${name} on server side`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
deleteCookie(name) {
|
|
353
|
+
if (typeof window !== "undefined") {
|
|
354
|
+
let cookieString = `${name}=; Max-Age=0`;
|
|
355
|
+
if (this.options.path) {
|
|
356
|
+
cookieString += `; Path=${this.options.path}`;
|
|
357
|
+
}
|
|
358
|
+
if (this.options.domain) {
|
|
359
|
+
cookieString += `; Domain=${this.options.domain}`;
|
|
360
|
+
}
|
|
361
|
+
document.cookie = cookieString;
|
|
362
|
+
} else {
|
|
363
|
+
try {
|
|
364
|
+
const { cookies } = require("next/headers");
|
|
365
|
+
const cookieStore = cookies();
|
|
366
|
+
cookieStore.delete(name);
|
|
367
|
+
} catch {
|
|
368
|
+
console.warn(`Could not remove cookie ${name} on server side`);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
};
|
|
263
373
|
function createAuthMiddleware(config) {
|
|
264
374
|
let isRefreshing = false;
|
|
265
375
|
let refreshPromise = null;
|
|
@@ -1495,23 +1605,6 @@ var CartClient = class extends StorefrontAPIClient {
|
|
|
1495
1605
|
})
|
|
1496
1606
|
);
|
|
1497
1607
|
}
|
|
1498
|
-
/**
|
|
1499
|
-
* Update cart customer
|
|
1500
|
-
*
|
|
1501
|
-
* @param cartId - The ID of the cart
|
|
1502
|
-
* @param body - The body of the request
|
|
1503
|
-
* @returns Promise with updated cart
|
|
1504
|
-
*/
|
|
1505
|
-
async updateCartCustomer(cartId, body) {
|
|
1506
|
-
return this.executeRequest(
|
|
1507
|
-
() => this.client.POST("/carts/{id}/update-customer", {
|
|
1508
|
-
params: {
|
|
1509
|
-
path: cartId
|
|
1510
|
-
},
|
|
1511
|
-
body
|
|
1512
|
-
})
|
|
1513
|
-
);
|
|
1514
|
-
}
|
|
1515
1608
|
};
|
|
1516
1609
|
|
|
1517
1610
|
// src/lib/auth.ts
|
|
@@ -2518,6 +2611,7 @@ var index_default = StorefrontSDK;
|
|
|
2518
2611
|
Environment,
|
|
2519
2612
|
HelpersClient,
|
|
2520
2613
|
MemoryTokenStorage,
|
|
2614
|
+
NextJSTokenStorage,
|
|
2521
2615
|
OrderClient,
|
|
2522
2616
|
ResponseUtils,
|
|
2523
2617
|
ShippingClient,
|