|
| 1 | +import type Medusa from "@medusajs/js-sdk" |
| 2 | +import type { HttpTypes } from "@medusajs/types" |
| 3 | +import type { CartService } from "./types" |
| 4 | + |
| 5 | +export type MedusaCartServiceConfig = { |
| 6 | + isNotFoundError?: (error: unknown) => boolean |
| 7 | +} |
| 8 | + |
| 9 | +export type MedusaCartCreateParams = HttpTypes.StoreCreateCart |
| 10 | +export type MedusaCartUpdateParams = HttpTypes.StoreUpdateCart |
| 11 | +export type MedusaCartAddItemParams = HttpTypes.StoreAddCartLineItem |
| 12 | +export type MedusaCartUpdateItemParams = HttpTypes.StoreUpdateCartLineItem |
| 13 | + |
| 14 | +export type MedusaCompleteCartResult = |
| 15 | + | { type: "order"; order: HttpTypes.StoreOrder } |
| 16 | + | { |
| 17 | + type: "cart" |
| 18 | + cart: HttpTypes.StoreCart |
| 19 | + error: { message: string; name: string; type: string } |
| 20 | + } |
| 21 | + |
| 22 | +/** |
| 23 | + * Creates a CartService for Medusa SDK |
| 24 | + * |
| 25 | + * @example |
| 26 | + * ```typescript |
| 27 | + * import { createCartHooks, createMedusaCartService } from "@techsio/storefront-data" |
| 28 | + * import { sdk } from "@/lib/medusa-client" |
| 29 | + * |
| 30 | + * const cartHooks = createCartHooks({ |
| 31 | + * service: createMedusaCartService(sdk), |
| 32 | + * queryKeys: cartQueryKeys, |
| 33 | + * cartStorage: { |
| 34 | + * getCartId: () => localStorage.getItem("cart_id"), |
| 35 | + * setCartId: (id) => localStorage.setItem("cart_id", id), |
| 36 | + * clearCartId: () => localStorage.removeItem("cart_id"), |
| 37 | + * }, |
| 38 | + * }) |
| 39 | + * ``` |
| 40 | + */ |
| 41 | +export function createMedusaCartService( |
| 42 | + sdk: Medusa, |
| 43 | + _config?: MedusaCartServiceConfig |
| 44 | +): CartService< |
| 45 | + HttpTypes.StoreCart, |
| 46 | + MedusaCartCreateParams, |
| 47 | + MedusaCartUpdateParams, |
| 48 | + MedusaCartAddItemParams, |
| 49 | + MedusaCartUpdateItemParams, |
| 50 | + MedusaCompleteCartResult |
| 51 | +> { |
| 52 | + return { |
| 53 | + async retrieveCart( |
| 54 | + cartId: string, |
| 55 | + _signal?: AbortSignal |
| 56 | + ): Promise<HttpTypes.StoreCart | null> { |
| 57 | + try { |
| 58 | + const { cart } = await sdk.store.cart.retrieve(cartId) |
| 59 | + return cart ?? null |
| 60 | + } catch (error: unknown) { |
| 61 | + // Handle 404 as null (cart not found) |
| 62 | + if ( |
| 63 | + error && |
| 64 | + typeof error === "object" && |
| 65 | + "status" in error && |
| 66 | + (error as { status: number }).status === 404 |
| 67 | + ) { |
| 68 | + return null |
| 69 | + } |
| 70 | + throw error |
| 71 | + } |
| 72 | + }, |
| 73 | + |
| 74 | + async createCart( |
| 75 | + params: MedusaCartCreateParams |
| 76 | + ): Promise<HttpTypes.StoreCart> { |
| 77 | + const { cart } = await sdk.store.cart.create(params) |
| 78 | + if (!cart) { |
| 79 | + throw new Error("Failed to create cart") |
| 80 | + } |
| 81 | + return cart |
| 82 | + }, |
| 83 | + |
| 84 | + async updateCart( |
| 85 | + cartId: string, |
| 86 | + params: MedusaCartUpdateParams |
| 87 | + ): Promise<HttpTypes.StoreCart> { |
| 88 | + const { cart } = await sdk.store.cart.update(cartId, params) |
| 89 | + if (!cart) { |
| 90 | + throw new Error("Failed to update cart") |
| 91 | + } |
| 92 | + return cart |
| 93 | + }, |
| 94 | + |
| 95 | + async addLineItem( |
| 96 | + cartId: string, |
| 97 | + params: MedusaCartAddItemParams |
| 98 | + ): Promise<HttpTypes.StoreCart> { |
| 99 | + const { cart } = await sdk.store.cart.createLineItem(cartId, params) |
| 100 | + if (!cart) { |
| 101 | + throw new Error("Failed to add item to cart") |
| 102 | + } |
| 103 | + return cart |
| 104 | + }, |
| 105 | + |
| 106 | + async updateLineItem( |
| 107 | + cartId: string, |
| 108 | + lineItemId: string, |
| 109 | + params: MedusaCartUpdateItemParams |
| 110 | + ): Promise<HttpTypes.StoreCart> { |
| 111 | + const { cart } = await sdk.store.cart.updateLineItem( |
| 112 | + cartId, |
| 113 | + lineItemId, |
| 114 | + params |
| 115 | + ) |
| 116 | + if (!cart) { |
| 117 | + throw new Error("Failed to update line item") |
| 118 | + } |
| 119 | + return cart |
| 120 | + }, |
| 121 | + |
| 122 | + async removeLineItem( |
| 123 | + cartId: string, |
| 124 | + lineItemId: string |
| 125 | + ): Promise<HttpTypes.StoreCart> { |
| 126 | + const { parent } = await sdk.store.cart.deleteLineItem(cartId, lineItemId) |
| 127 | + if (!parent) { |
| 128 | + throw new Error("Failed to remove line item") |
| 129 | + } |
| 130 | + return parent |
| 131 | + }, |
| 132 | + |
| 133 | + async transferCart(cartId: string): Promise<HttpTypes.StoreCart> { |
| 134 | + const { cart } = await sdk.store.cart.transferCart(cartId) |
| 135 | + if (!cart) { |
| 136 | + throw new Error("Failed to transfer cart") |
| 137 | + } |
| 138 | + return cart |
| 139 | + }, |
| 140 | + |
| 141 | + async completeCart(cartId: string): Promise<MedusaCompleteCartResult> { |
| 142 | + const result = await sdk.store.cart.complete(cartId) |
| 143 | + return result |
| 144 | + }, |
| 145 | + } |
| 146 | +} |
0 commit comments