@commercengine/storefront-sdk 0.5.0 → 0.6.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/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/lib/client.ts
2
9
  import createClient from "openapi-fetch";
3
10
 
@@ -209,6 +216,115 @@ var CookieTokenStorage = class {
209
216
  document.cookie = cookieString;
210
217
  }
211
218
  };
219
+ var NextJSTokenStorage = class {
220
+ accessTokenKey;
221
+ refreshTokenKey;
222
+ options;
223
+ constructor(options = {}) {
224
+ const prefix = options.prefix || "storefront_";
225
+ this.accessTokenKey = `${prefix}access_token`;
226
+ this.refreshTokenKey = `${prefix}refresh_token`;
227
+ this.options = {
228
+ maxAge: options.maxAge || 30 * 24 * 60 * 60,
229
+ // 30 days default
230
+ path: options.path || "/",
231
+ domain: options.domain,
232
+ secure: options.secure ?? (typeof window !== "undefined" && window.location?.protocol === "https:"),
233
+ sameSite: options.sameSite || "Lax"
234
+ };
235
+ }
236
+ async getAccessToken() {
237
+ return await this.getCookie(this.accessTokenKey);
238
+ }
239
+ async setAccessToken(token) {
240
+ await this.setCookie(this.accessTokenKey, token);
241
+ }
242
+ async getRefreshToken() {
243
+ return await this.getCookie(this.refreshTokenKey);
244
+ }
245
+ async setRefreshToken(token) {
246
+ await this.setCookie(this.refreshTokenKey, token);
247
+ }
248
+ async clearTokens() {
249
+ await this.deleteCookie(this.accessTokenKey);
250
+ await this.deleteCookie(this.refreshTokenKey);
251
+ }
252
+ async getCookie(name) {
253
+ if (typeof window !== "undefined") {
254
+ const value = `; ${document.cookie}`;
255
+ const parts = value.split(`; ${name}=`);
256
+ if (parts.length === 2) {
257
+ const cookieValue = parts.pop()?.split(";").shift();
258
+ return cookieValue ? decodeURIComponent(cookieValue) : null;
259
+ }
260
+ return null;
261
+ } else {
262
+ try {
263
+ const { cookies } = __require("next/headers");
264
+ const cookieStore = await cookies();
265
+ return cookieStore.get(name)?.value || null;
266
+ } catch {
267
+ return null;
268
+ }
269
+ }
270
+ }
271
+ async setCookie(name, value) {
272
+ if (typeof window !== "undefined") {
273
+ const encodedValue = encodeURIComponent(value);
274
+ let cookieString = `${name}=${encodedValue}`;
275
+ if (this.options.maxAge) {
276
+ cookieString += `; Max-Age=${this.options.maxAge}`;
277
+ }
278
+ if (this.options.path) {
279
+ cookieString += `; Path=${this.options.path}`;
280
+ }
281
+ if (this.options.domain) {
282
+ cookieString += `; Domain=${this.options.domain}`;
283
+ }
284
+ if (this.options.secure) {
285
+ cookieString += `; Secure`;
286
+ }
287
+ if (this.options.sameSite) {
288
+ cookieString += `; SameSite=${this.options.sameSite}`;
289
+ }
290
+ document.cookie = cookieString;
291
+ } else {
292
+ try {
293
+ const { cookies } = __require("next/headers");
294
+ const cookieStore = await cookies();
295
+ cookieStore.set(name, value, {
296
+ maxAge: this.options.maxAge,
297
+ path: this.options.path,
298
+ domain: this.options.domain,
299
+ secure: this.options.secure,
300
+ sameSite: this.options.sameSite?.toLowerCase()
301
+ });
302
+ } catch {
303
+ console.warn(`Could not set cookie ${name} on server side`);
304
+ }
305
+ }
306
+ }
307
+ async deleteCookie(name) {
308
+ if (typeof window !== "undefined") {
309
+ let cookieString = `${name}=; Max-Age=0`;
310
+ if (this.options.path) {
311
+ cookieString += `; Path=${this.options.path}`;
312
+ }
313
+ if (this.options.domain) {
314
+ cookieString += `; Domain=${this.options.domain}`;
315
+ }
316
+ document.cookie = cookieString;
317
+ } else {
318
+ try {
319
+ const { cookies } = __require("next/headers");
320
+ const cookieStore = await cookies();
321
+ cookieStore.delete(name);
322
+ } catch {
323
+ console.warn(`Could not remove cookie ${name} on server side`);
324
+ }
325
+ }
326
+ }
327
+ };
212
328
  function createAuthMiddleware(config) {
213
329
  let isRefreshing = false;
214
330
  let refreshPromise = null;
@@ -1444,23 +1560,6 @@ var CartClient = class extends StorefrontAPIClient {
1444
1560
  })
1445
1561
  );
1446
1562
  }
1447
- /**
1448
- * Update cart customer
1449
- *
1450
- * @param cartId - The ID of the cart
1451
- * @param body - The body of the request
1452
- * @returns Promise with updated cart
1453
- */
1454
- async updateCartCustomer(cartId, body) {
1455
- return this.executeRequest(
1456
- () => this.client.POST("/carts/{id}/update-customer", {
1457
- params: {
1458
- path: cartId
1459
- },
1460
- body
1461
- })
1462
- );
1463
- }
1464
1563
  };
1465
1564
 
1466
1565
  // src/lib/auth.ts
@@ -2466,6 +2565,7 @@ export {
2466
2565
  Environment,
2467
2566
  HelpersClient,
2468
2567
  MemoryTokenStorage,
2568
+ NextJSTokenStorage,
2469
2569
  OrderClient,
2470
2570
  ResponseUtils,
2471
2571
  ShippingClient,