@reactionary/medusa 0.10.50 → 0.10.51

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.
@@ -28,6 +28,7 @@ import {
28
28
  Reactionary,
29
29
  success
30
30
  } from "@reactionary/core";
31
+ import { FetchError } from "@medusajs/js-sdk";
31
32
  import {} from "@medusajs/types";
32
33
  import createDebug from "debug";
33
34
  import {
@@ -233,6 +234,30 @@ class MedusaCartCapability extends CartCapability {
233
234
  }
234
235
  this.medusaApi.setSessionData(sessionData);
235
236
  }
237
+ /**
238
+ * Removes every trace of a cart from the session: the active cart marker
239
+ * (when it points at this cart) and the entry in every owned-carts
240
+ * collection. Used when a cart turns out to be deleted or stale upstream.
241
+ */
242
+ pruneCartFromSession(cartKey) {
243
+ const sessionData = this.medusaApi.getSessionData();
244
+ if (sessionData.activeCartId?.key === cartKey) {
245
+ delete sessionData.activeCartId;
246
+ this.medusaApi.setSessionData({ activeCartId: void 0 });
247
+ }
248
+ if (sessionData.allOwnedCarts) {
249
+ const prunedCollections = Object.fromEntries(
250
+ Object.entries(sessionData.allOwnedCarts).map(([collection, carts]) => [
251
+ collection,
252
+ carts.filter((cart) => cart.key !== cartKey)
253
+ ])
254
+ );
255
+ this.medusaApi.setSessionData({ allOwnedCarts: prunedCollections });
256
+ }
257
+ }
258
+ isNotFoundError(err) {
259
+ return err instanceof FetchError && err.status === 404;
260
+ }
236
261
  removeCartFromOwnedList(cartIdentifier, company) {
237
262
  const sessionData = this.medusaApi.getSessionData();
238
263
  const companyIdToUse = company ? company.taxIdentifier : "_me";
@@ -329,18 +354,23 @@ class MedusaCartCapability extends CartCapability {
329
354
  }
330
355
  }
331
356
  if (activeCartId) {
332
- const response = await client.store.cart.retrieve(activeCartId.key, { fields: "id,region_id" });
333
- if (!response.cart) {
334
- delete sessionData.activeCartId;
335
- this.medusaApi.setSessionData({
336
- activeCartId: void 0
337
- });
357
+ let remoteCart;
358
+ try {
359
+ const response = await client.store.cart.retrieve(activeCartId.key, { fields: "id,region_id" });
360
+ remoteCart = response.cart;
361
+ } catch (retrieveError) {
362
+ if (!this.isNotFoundError(retrieveError)) {
363
+ throw retrieveError;
364
+ }
365
+ }
366
+ if (!remoteCart) {
367
+ this.pruneCartFromSession(activeCartId.key);
338
368
  return error({
339
369
  type: "NotFound",
340
370
  identifier: activeCartId
341
371
  });
342
372
  }
343
- return success(this.factory.parseCartIdentifier(this.context, response.cart));
373
+ return success(this.factory.parseCartIdentifier(this.context, remoteCart));
344
374
  }
345
375
  return error({
346
376
  type: "NotFound",
@@ -359,13 +389,7 @@ class MedusaCartCapability extends CartCapability {
359
389
  const client = await this.getClient();
360
390
  const medusaId = payload.cart;
361
391
  if (medusaId.key) {
362
- const sessionData = this.medusaApi.getSessionData();
363
- if (sessionData.activeCartId) {
364
- delete sessionData.activeCartId;
365
- this.medusaApi.setSessionData({
366
- activeCartId: void 0
367
- });
368
- }
392
+ this.pruneCartFromSession(medusaId.key);
369
393
  }
370
394
  const cartResponse = await client.store.cart.retrieve(medusaId.key);
371
395
  if (cartResponse.cart) {
@@ -374,9 +398,11 @@ class MedusaCartCapability extends CartCapability {
374
398
  }
375
399
  }
376
400
  return success(void 0);
377
- } catch (error2) {
378
- debug("Failed to delete cart:", error2);
379
- return success(void 0);
401
+ } catch (err) {
402
+ if (this.isNotFoundError(err)) {
403
+ return success(void 0);
404
+ }
405
+ handleProviderError("delete cart", err);
380
406
  }
381
407
  }
382
408
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/medusa",
3
- "version": "0.10.50",
3
+ "version": "0.10.51",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@medusajs/js-sdk": "^2.13.3",
13
- "@reactionary/core": "0.10.50",
13
+ "@reactionary/core": "0.10.51",
14
14
  "debug": "^4.4.3",
15
15
  "@medusajs/types": "^2.13.3",
16
16
  "zod": "4.1.9"
@@ -36,6 +36,13 @@ export declare class MedusaCartCapability<TFactory extends CartFactory = MedusaC
36
36
  */
37
37
  protected createCartPayload(payload: CartMutationCreateCart): Promise<StoreCreateCart>;
38
38
  protected addCartToOwnedList(cartIdentifier: MedusaCartIdentifier, companyId?: CompanyIdentifier): void;
39
+ /**
40
+ * Removes every trace of a cart from the session: the active cart marker
41
+ * (when it points at this cart) and the entry in every owned-carts
42
+ * collection. Used when a cart turns out to be deleted or stale upstream.
43
+ */
44
+ protected pruneCartFromSession(cartKey: string): void;
45
+ protected isNotFoundError(err: unknown): boolean;
39
46
  protected removeCartFromOwnedList(cartIdentifier: MedusaCartIdentifier, company?: CompanyIdentifier): void;
40
47
  createCart(payload: CartMutationCreateCart): Promise<Result<CartFactoryCartOutput<TFactory>>>;
41
48
  remove(payload: CartMutationItemRemove): Promise<Result<CartFactoryCartOutput<TFactory>>>;