/ src / lib / data / fulfillment.ts
fulfillment.ts
 1  "use server"
 2  
 3  import { sdk } from "@lib/config"
 4  import { HttpTypes } from "@medusajs/types"
 5  import { getAuthHeaders, getCacheOptions } from "./cookies"
 6  
 7  export const listCartShippingMethods = async (cartId: string) => {
 8    const headers = {
 9      ...(await getAuthHeaders()),
10    }
11  
12    const next = {
13      ...(await getCacheOptions("fulfillment")),
14    }
15  
16    return sdk.client
17      .fetch<HttpTypes.StoreShippingOptionListResponse>(
18        `/store/shipping-options`,
19        {
20          method: "GET",
21          query: {
22            cart_id: cartId,
23            fields:
24              "+service_zone.fulfllment_set.type,*service_zone.fulfillment_set.location.address",
25          },
26          headers,
27          next,
28          cache: "force-cache",
29        }
30      )
31      .then(({ shipping_options }) => shipping_options)
32      .catch(() => {
33        return null
34      })
35  }
36  
37  export const calculatePriceForShippingOption = async (
38    optionId: string,
39    cartId: string,
40    data?: Record<string, unknown>
41  ) => {
42    const headers = {
43      ...(await getAuthHeaders()),
44    }
45  
46    const next = {
47      ...(await getCacheOptions("fulfillment")),
48    }
49  
50    const body = { cart_id: cartId, data }
51  
52    if (data) {
53      body.data = data
54    }
55  
56    return sdk.client
57      .fetch<{ shipping_option: HttpTypes.StoreCartShippingOption }>(
58        `/store/shipping-options/${optionId}/calculate`,
59        {
60          method: "POST",
61          body,
62          headers,
63          next,
64        }
65      )
66      .then(({ shipping_option }) => shipping_option)
67      .catch((e) => {
68        return null
69      })
70  }