medusa-storefront-data 1.0.0 → 2.1.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.
Files changed (110) hide show
  1. package/dist/cookies.d.ts +2 -2
  2. package/dist/cookies.d.ts.map +1 -1
  3. package/dist/edge.d.ts +3 -0
  4. package/dist/edge.d.ts.map +1 -0
  5. package/dist/edge.js +1 -0
  6. package/dist/middleware.d.ts +3 -0
  7. package/dist/middleware.d.ts.map +1 -0
  8. package/dist/middleware.js +1 -0
  9. package/dist/server/cart.d.ts +9 -5
  10. package/dist/server/cart.d.ts.map +1 -1
  11. package/dist/server/cart.js +164 -194
  12. package/dist/server/categories.d.ts +3 -2
  13. package/dist/server/categories.d.ts.map +1 -1
  14. package/dist/server/categories.js +14 -51
  15. package/dist/server/collections.d.ts.map +1 -1
  16. package/dist/server/collections.js +16 -61
  17. package/dist/server/contact.d.ts +34 -0
  18. package/dist/server/contact.d.ts.map +1 -0
  19. package/dist/server/contact.js +57 -0
  20. package/dist/server/customer.d.ts +7 -7
  21. package/dist/server/customer.d.ts.map +1 -1
  22. package/dist/server/customer.js +96 -145
  23. package/dist/server/dynamic-config.d.ts.map +1 -1
  24. package/dist/server/dynamic-config.js +38 -12
  25. package/dist/server/fulfillment.d.ts +4 -3
  26. package/dist/server/fulfillment.d.ts.map +1 -1
  27. package/dist/server/fulfillment.js +16 -41
  28. package/dist/server/guest.d.ts +35 -63
  29. package/dist/server/guest.d.ts.map +1 -1
  30. package/dist/server/guest.js +81 -202
  31. package/dist/server/home.d.ts +15 -0
  32. package/dist/server/home.d.ts.map +1 -0
  33. package/dist/server/home.js +45 -0
  34. package/dist/server/index.d.ts +2 -0
  35. package/dist/server/index.d.ts.map +1 -1
  36. package/dist/server/index.js +2 -0
  37. package/dist/server/locale-actions.d.ts +1 -1
  38. package/dist/server/locale-actions.d.ts.map +1 -1
  39. package/dist/server/locale-actions.js +8 -13
  40. package/dist/server/locales.d.ts +2 -4
  41. package/dist/server/locales.d.ts.map +1 -1
  42. package/dist/server/locales.js +5 -13
  43. package/dist/server/orders.d.ts +5 -11
  44. package/dist/server/orders.d.ts.map +1 -1
  45. package/dist/server/orders.js +126 -267
  46. package/dist/server/payment-details.d.ts +4 -4
  47. package/dist/server/payment-details.d.ts.map +1 -1
  48. package/dist/server/payment-details.js +17 -42
  49. package/dist/server/payment.d.ts +2 -1
  50. package/dist/server/payment.d.ts.map +1 -1
  51. package/dist/server/payment.js +9 -21
  52. package/dist/server/pincode.d.ts +7 -0
  53. package/dist/server/pincode.d.ts.map +1 -0
  54. package/dist/server/pincode.js +30 -0
  55. package/dist/server/products.d.ts +15 -19
  56. package/dist/server/products.d.ts.map +1 -1
  57. package/dist/server/products.js +47 -178
  58. package/dist/server/regions.d.ts +1 -1
  59. package/dist/server/regions.d.ts.map +1 -1
  60. package/dist/server/regions.js +6 -3
  61. package/dist/server/returns.d.ts +4 -4
  62. package/dist/server/returns.d.ts.map +1 -1
  63. package/dist/server/returns.js +50 -154
  64. package/dist/server/swaps.d.ts +7 -6
  65. package/dist/server/swaps.d.ts.map +1 -1
  66. package/dist/server/swaps.js +23 -57
  67. package/dist/server/variants.d.ts.map +1 -1
  68. package/dist/server/variants.js +11 -22
  69. package/dist/server/wishlist.d.ts +11 -0
  70. package/dist/server/wishlist.d.ts.map +1 -0
  71. package/dist/server/wishlist.js +49 -0
  72. package/dist/util/get-locale-header.d.ts +1 -1
  73. package/dist/util/revalidate-cart.d.ts +2 -0
  74. package/dist/util/revalidate-cart.d.ts.map +1 -0
  75. package/dist/util/revalidate-cart.js +8 -0
  76. package/dist/util/sort-products.d.ts +3 -0
  77. package/dist/util/sort-products.d.ts.map +1 -0
  78. package/dist/util/sort-products.js +1 -0
  79. package/dist/util/store-client.d.ts +13 -0
  80. package/dist/util/store-client.d.ts.map +1 -0
  81. package/dist/util/store-client.js +77 -0
  82. package/package.json +95 -37
  83. package/src/edge.ts +2 -0
  84. package/src/middleware.ts +2 -2
  85. package/src/server/cart.ts +214 -267
  86. package/src/server/categories.ts +19 -72
  87. package/src/server/collections.ts +25 -82
  88. package/src/server/contact.ts +92 -0
  89. package/src/server/customer.ts +146 -190
  90. package/src/server/dynamic-config.ts +38 -12
  91. package/src/server/fulfillment.ts +27 -53
  92. package/src/server/guest.ts +159 -276
  93. package/src/server/home.ts +68 -0
  94. package/src/server/index.ts +1 -0
  95. package/src/server/locale-actions.ts +8 -15
  96. package/src/server/locales.ts +6 -18
  97. package/src/server/orders.ts +167 -337
  98. package/src/server/payment-details.ts +24 -52
  99. package/src/server/payment.ts +8 -28
  100. package/src/server/pincode.ts +49 -0
  101. package/src/server/products.ts +72 -235
  102. package/src/server/regions.ts +10 -6
  103. package/src/server/returns.ts +75 -189
  104. package/src/server/swaps.ts +94 -123
  105. package/src/server/variants.ts +9 -28
  106. package/src/server/wishlist.ts +1 -1
  107. package/src/util/revalidate-cart.ts +10 -0
  108. package/src/util/sort-products.ts +2 -47
  109. package/src/util/store-client.ts +93 -0
  110. package/src/services/middleware.ts +0 -54
@@ -1,29 +1,22 @@
1
1
  "use server"
2
2
 
3
- import { sdk } from "../config"
3
+ import {
4
+ medusaReturnCreate,
5
+ medusaReturnCreateGuest,
6
+ medusaReturnList,
7
+ medusaReturnListGuest,
8
+ medusaReturnReasonList,
9
+ medusaReturnShippingOptionsResolve,
10
+ medusaReturnUpdatePayment,
11
+ } from "medusa-services/returns"
4
12
  import { HttpTypes } from "@medusajs/types"
5
13
  import { cookies } from "next/headers"
6
- import { getAuthHeaders, getCacheOptions } from "../cookies"
7
14
  import { revalidateTag } from "next/cache"
15
+ import { getStoreClientOptions, getStoreClientOptionsWithToken } from "../util/store-client"
8
16
 
9
17
  export const listReturnReasons = async () => {
10
- const headers = {
11
- ...(await getAuthHeaders()),
12
- }
13
-
14
- const next = {
15
- ...(await getCacheOptions("return_reasons")),
16
- }
17
-
18
- return sdk.client
19
- .fetch<HttpTypes.StoreReturnReasonListResponse>(`/store/return-reasons`, {
20
- method: "GET",
21
- headers,
22
- next,
23
- cache: "force-cache", // Reasons change rarely
24
- })
25
- .then(({ return_reasons }) => return_reasons)
26
- .catch(() => [])
18
+ const options = await getStoreClientOptions()
19
+ return medusaReturnReasonList(options)
27
20
  }
28
21
 
29
22
  export const listReturnShippingOptions = async (
@@ -31,72 +24,12 @@ export const listReturnShippingOptions = async (
31
24
  regionId?: string,
32
25
  productIds?: string[]
33
26
  ): Promise<HttpTypes.StoreShippingOption[]> => {
34
- const headers = {
35
- ...(await getAuthHeaders()),
36
- }
37
-
38
- const next = {
39
- ...(await getCacheOptions("fulfillment")),
40
- }
41
-
42
- // Strategy 1: Try with cart_id (preferred as it has context)
43
- if (cartId) {
44
- try {
45
- const { shipping_options } = await sdk.client.fetch<HttpTypes.StoreShippingOptionListResponse>(
46
- `/store/shipping-options`,
47
- {
48
- method: "GET",
49
- query: {
50
- cart_id: cartId,
51
- is_return: true,
52
- },
53
- headers,
54
- next,
55
- cache: "no-store",
56
- }
57
- )
58
-
59
- if (shipping_options.length > 0) {
60
- return shipping_options as unknown as HttpTypes.StoreShippingOption[]
61
- }
62
- } catch (e) {
63
- // Fallback strategy
64
- }
65
- }
66
-
67
- // Strategy 2: Fallback - Create ephemeral cart if region_id is available
68
- // This is required because /store/shipping-options STRICTLY requires a cart_id for return options
69
- if (regionId) {
70
- try {
71
- // Create a temporary cart for this region to get valid shipping options
72
- const { cart } = await sdk.store.cart.create(
73
- { region_id: regionId },
74
- {},
75
- headers
76
- )
77
-
78
- if (cart?.id) {
79
- const { shipping_options } = await sdk.client.fetch<HttpTypes.StoreShippingOptionListResponse>(
80
- `/store/shipping-options`,
81
- {
82
- method: "GET",
83
- query: {
84
- cart_id: cart.id,
85
- is_return: true,
86
- },
87
- headers,
88
- next,
89
- cache: "no-store",
90
- }
91
- )
92
- return shipping_options as unknown as HttpTypes.StoreShippingOption[]
93
- }
94
- } catch (e) {
95
- // Silence error
96
- }
97
- }
98
-
99
- return []
27
+ const options = await getStoreClientOptions()
28
+ const shippingOptions = await medusaReturnShippingOptionsResolve(options, {
29
+ cartId,
30
+ regionId,
31
+ })
32
+ return shippingOptions as unknown as HttpTypes.StoreShippingOption[]
100
33
  }
101
34
 
102
35
  export const createReturnRequest = async (
@@ -120,10 +53,10 @@ export const createReturnRequest = async (
120
53
  if (!orderId) return { success: false, error: "Order ID is required", return: null }
121
54
  if (!itemsJson) return { success: false, error: "Items are required", return: null }
122
55
 
123
- let items: any[] = []
56
+ let items: Array<{ id: string; quantity: number; return_reason_id?: string; location_id?: string }> = []
124
57
  try {
125
58
  items = JSON.parse(itemsJson)
126
- } catch (e) {
59
+ } catch {
127
60
  return { success: false, error: "Invalid items data", return: null }
128
61
  }
129
62
 
@@ -131,91 +64,67 @@ export const createReturnRequest = async (
131
64
  return { success: false, error: "At least one item must be selected", return: null }
132
65
  }
133
66
 
134
- const headers = {
135
- ...(await getAuthHeaders()),
136
- }
67
+ const options = await getStoreClientOptions()
137
68
 
138
69
  try {
139
- // Standard Medusa v2 payload
140
- const payload: any = {
70
+ const payload = {
141
71
  order_id: orderId,
142
72
  items: items.map((item) => ({
143
73
  id: item.id,
144
74
  quantity: item.quantity,
145
75
  reason_id: item.return_reason_id || undefined,
146
- note: (note && note.trim().length > 0) ? note : undefined,
76
+ note: note && note.trim().length > 0 ? note : undefined,
147
77
  })),
148
- ...(returnShippingOptionId ? {
149
- return_shipping: {
150
- option_id: returnShippingOptionId,
151
- location_id: locationId || (items[0] as any).location_id || "default_location"
152
- }
153
- } : {}),
154
- // Optional: keep global note if supported
155
- note: (note && note.trim().length > 0) ? note : "Return request",
78
+ ...(returnShippingOptionId
79
+ ? {
80
+ return_shipping: {
81
+ option_id: returnShippingOptionId,
82
+ location_id: locationId || items[0]?.location_id || "default_location",
83
+ },
84
+ }
85
+ : {}),
86
+ note: note && note.trim().length > 0 ? note : "Return request",
156
87
  }
157
88
 
158
- // Double check location_id is NOT nested incorrectly if already set above
159
- // The plugin expects return_shipping.location_id
160
-
161
- // Check for any available guest tokens
162
89
  const cookieStore = await cookies()
163
- const guestToken = cookieStore.get("_medusa_guest_jwt")?.value
164
- || cookieStore.get("_medusa_guest_token")?.value
165
- || cookieStore.get("guest_id")?.value
166
-
90
+ const guestToken =
91
+ cookieStore.get("_medusa_guest_jwt")?.value ||
92
+ cookieStore.get("_medusa_guest_token")?.value ||
93
+ cookieStore.get("guest_id")?.value
167
94
  const token = cookieStore.get("_medusa_jwt")?.value
168
95
 
169
96
  let returnData: HttpTypes.StoreReturn
170
97
 
171
- // If we have a guest token, try the guest specific endpoint
172
98
  if (guestToken && !token) {
173
99
  try {
174
- const response = await sdk.client.fetch<any>(
175
- `/store/guest-orders/${orderId}/returns`,
176
- {
177
- method: "POST",
178
- body: payload,
179
- headers: {
180
- ...headers,
181
- "Authorization": `Bearer ${guestToken}`
182
- },
183
- cache: "no-store",
184
- }
185
- )
186
- returnData = response.return || response
187
- } catch (e) {
188
- // Fallback to standard if guest endpoint fails
189
- const response = await sdk.client.fetch<{ return: HttpTypes.StoreReturn }>(
190
- `/store/returns`,
191
- {
192
- method: "POST",
193
- body: payload,
194
- headers,
195
- cache: "no-store",
196
- }
197
- )
198
- returnData = response.return
100
+ const guestOptions = await getStoreClientOptionsWithToken(guestToken)
101
+ returnData = (await medusaReturnCreateGuest(
102
+ orderId,
103
+ payload,
104
+ guestOptions
105
+ )) as unknown as HttpTypes.StoreReturn
106
+ } catch {
107
+ returnData = (await medusaReturnCreate(
108
+ payload,
109
+ options
110
+ )) as unknown as HttpTypes.StoreReturn
199
111
  }
200
112
  } else {
201
- // Standard flow
202
- const response = await sdk.client.fetch<{ return: HttpTypes.StoreReturn }>(
203
- `/store/returns`,
204
- {
205
- method: "POST",
206
- body: payload,
207
- headers,
208
- cache: "no-store",
209
- }
210
- )
211
- returnData = response.return
113
+ returnData = (await medusaReturnCreate(
114
+ payload,
115
+ options
116
+ )) as unknown as HttpTypes.StoreReturn
212
117
  }
213
118
 
214
119
  revalidateTag("orders")
215
120
 
216
121
  return { success: true, error: null, return: returnData }
217
- } catch (error: any) {
218
- return { success: false, error: error.message || "Failed to create return request", return: null }
122
+ } catch (error) {
123
+ return {
124
+ success: false,
125
+ error: error instanceof Error ? error.message : "Failed to create return request",
126
+ return: null,
127
+ }
219
128
  }
220
129
  }
221
130
 
@@ -223,24 +132,17 @@ export const createReturnRequest = async (
223
132
  * Link a payment method to a return (for Refund Destination)
224
133
  */
225
134
  export const updateReturnPayment = async (returnId: string, paymentId: string) => {
226
- const headers = {
227
- ...(await getAuthHeaders()),
228
- }
135
+ const options = await getStoreClientOptions()
229
136
 
230
137
  try {
231
- const response = await sdk.client.fetch<any>(
232
- `/store/refund-payment-mapping/${returnId}`,
233
- {
234
- method: "PUT",
235
- body: { payment_id: paymentId },
236
- headers,
237
- cache: "no-store",
238
- }
239
- )
240
- return { success: true, data: response }
241
- } catch (error: any) {
138
+ const data = await medusaReturnUpdatePayment(returnId, paymentId, options)
139
+ return { success: true, data }
140
+ } catch (error) {
242
141
  console.error("Update return payment fail:", error)
243
- return { success: false, error: error.message }
142
+ return {
143
+ success: false,
144
+ error: error instanceof Error ? error.message : String(error),
145
+ }
244
146
  }
245
147
  }
246
148
 
@@ -248,43 +150,27 @@ export const updateReturnPayment = async (returnId: string, paymentId: string) =
248
150
  * List returns for an order or customer
249
151
  */
250
152
  export const listReturns = async (orderId?: string) => {
251
- const headers = {
252
- ...(await getAuthHeaders()),
253
- }
153
+ const options = await getStoreClientOptions()
254
154
 
255
- // Authenticated flow
256
- const authHeaders = headers as any
257
- if (authHeaders.authorization || authHeaders.Authorization) {
155
+ if (options.authorization) {
258
156
  try {
259
- const response = await sdk.client.fetch<any>(`/store/returns`, {
260
- method: "GET",
261
- query: orderId ? { order_id: orderId } : {},
262
- headers,
263
- cache: "no-store",
264
- })
265
- return response
266
- } catch (e) {
157
+ return await medusaReturnList(options, orderId ? { order_id: orderId } : undefined)
158
+ } catch {
267
159
  return { returns: [], count: 0 }
268
160
  }
269
161
  }
270
162
 
271
- // Guest flow
272
163
  if (orderId) {
273
164
  const cookieStore = await cookies()
274
- const guestToken = cookieStore.get("_medusa_guest_jwt")?.value || cookieStore.get("_medusa_guest_token")?.value
165
+ const guestToken =
166
+ cookieStore.get("_medusa_guest_jwt")?.value ||
167
+ cookieStore.get("_medusa_guest_token")?.value
275
168
 
276
169
  if (guestToken) {
277
170
  try {
278
- const response = await sdk.client.fetch<any>(`/store/guest-orders/${orderId}/returns`, {
279
- method: "GET",
280
- headers: {
281
- ...headers,
282
- "Authorization": `Bearer ${guestToken}`
283
- },
284
- cache: "no-store",
285
- })
286
- return response
287
- } catch (e) {
171
+ const guestOptions = await getStoreClientOptionsWithToken(guestToken)
172
+ return await medusaReturnListGuest(orderId, guestOptions)
173
+ } catch {
288
174
  return { returns: [], count: 0 }
289
175
  }
290
176
  }
@@ -1,150 +1,121 @@
1
-
2
1
  "use server"
3
2
 
4
- import { sdk } from "../config"
5
- import { HttpTypes } from "@medusajs/types"
3
+ import {
4
+ medusaSwapCreate,
5
+ medusaSwapCreateGuest,
6
+ medusaSwapList,
7
+ medusaSwapListGuest,
8
+ } from "medusa-services/swaps"
6
9
  import { cookies } from "next/headers"
7
- import { getAuthHeaders } from "../cookies"
8
10
  import { revalidateTag } from "next/cache"
11
+ import { getStoreClientOptions, getStoreClientOptionsWithToken } from "../util/store-client"
12
+
13
+ export type SwapActionState = {
14
+ success: boolean
15
+ error: string | null
16
+ swap: Record<string, unknown> | null
17
+ }
9
18
 
10
19
  /**
11
20
  * Create an exchange (swap) request.
12
21
  * Supports both authenticated customers and guest users.
13
22
  */
14
23
  export const createSwapRequest = async (
15
- prevState: any,
16
- formData: FormData
17
- ): Promise<{
18
- success: boolean
19
- error: string | null
20
- swap: any | null
21
- }> => {
22
- const orderId = formData.get("order_id") as string
23
- const returnItemsJson = formData.get("return_items") as string
24
- const newItemsJson = formData.get("new_items") as string
25
- const reason = formData.get("reason") as string
26
- const note = formData.get("note") as string
27
-
28
- if (!orderId) return { success: false, error: "Order ID is required", swap: null }
29
- if (!returnItemsJson) return { success: false, error: "Items to return are required", swap: null }
30
- if (!newItemsJson) return { success: false, error: "New items are required", swap: null }
31
-
32
- let returnItems: any[] = []
33
- let newItems: any[] = []
34
- try {
35
- returnItems = JSON.parse(returnItemsJson)
36
- newItems = JSON.parse(newItemsJson)
37
- } catch (e) {
38
- return { success: false, error: "Invalid items data format", swap: null }
24
+ _prevState: SwapActionState,
25
+ formData: FormData
26
+ ): Promise<SwapActionState> => {
27
+ const orderId = formData.get("order_id") as string
28
+ const returnItemsJson = formData.get("return_items") as string
29
+ const newItemsJson = formData.get("new_items") as string
30
+ const reason = formData.get("reason") as string
31
+ const note = formData.get("note") as string
32
+
33
+ if (!orderId) return { success: false, error: "Order ID is required", swap: null }
34
+ if (!returnItemsJson) return { success: false, error: "Items to return are required", swap: null }
35
+ if (!newItemsJson) return { success: false, error: "New items are required", swap: null }
36
+
37
+ let returnItems: Array<Record<string, unknown>> = []
38
+ let newItems: Array<Record<string, unknown>> = []
39
+ try {
40
+ returnItems = JSON.parse(returnItemsJson)
41
+ newItems = JSON.parse(newItemsJson)
42
+ } catch {
43
+ return { success: false, error: "Invalid items data format", swap: null }
44
+ }
45
+
46
+ if (returnItems.length === 0) {
47
+ return { success: false, error: "At least one item must be selected for return", swap: null }
48
+ }
49
+
50
+ const options = await getStoreClientOptions()
51
+
52
+ try {
53
+ const payload = {
54
+ order_id: orderId,
55
+ return_items: returnItems,
56
+ new_items: newItems,
57
+ reason: reason || "Size exchange",
58
+ note: note || "Exchange requested from storefront",
39
59
  }
40
60
 
41
- if (returnItems.length === 0) {
42
- return { success: false, error: "At least one item must be selected for return", swap: null }
61
+ const cookieStore = await cookies()
62
+ const guestToken =
63
+ cookieStore.get("_medusa_guest_jwt")?.value ||
64
+ cookieStore.get("_medusa_guest_token")?.value
65
+ const token = cookieStore.get("_medusa_jwt")?.value
66
+
67
+ let swapData: Record<string, unknown>
68
+
69
+ if (guestToken && !token) {
70
+ const guestOptions = await getStoreClientOptionsWithToken(guestToken)
71
+ swapData = await medusaSwapCreateGuest(
72
+ orderId,
73
+ {
74
+ return_items: payload.return_items,
75
+ new_items: payload.new_items,
76
+ reason: payload.reason,
77
+ note: payload.note,
78
+ },
79
+ guestOptions
80
+ )
81
+ } else {
82
+ swapData = await medusaSwapCreate(payload, options)
43
83
  }
44
84
 
45
- const headers = {
46
- ...(await getAuthHeaders()),
47
- }
85
+ revalidateTag("orders")
48
86
 
49
- try {
50
- const payload = {
51
- order_id: orderId,
52
- return_items: returnItems,
53
- new_items: newItems,
54
- reason: reason || "Size exchange",
55
- note: note || "Exchange requested from storefront",
56
- }
57
-
58
- const cookieStore = await cookies()
59
- const guestToken = cookieStore.get("_medusa_guest_jwt")?.value || cookieStore.get("_medusa_guest_token")?.value
60
- const token = cookieStore.get("_medusa_jwt")?.value
61
-
62
- let swapData: any
63
-
64
- // Check if we should use the guest endpoint
65
- if (guestToken && !token) {
66
-
67
- const response = await sdk.client.fetch<any>(
68
- `/store/guest-orders/${orderId}/swaps`,
69
- {
70
- method: "POST",
71
- body: {
72
- return_items: payload.return_items,
73
- new_items: payload.new_items,
74
- reason: payload.reason,
75
- note: payload.note,
76
- },
77
- headers: {
78
- ...headers,
79
- "Authorization": `Bearer ${guestToken}`
80
- },
81
- cache: "no-store",
82
- }
83
- )
84
- swapData = response.swap || response
85
- } else {
86
-
87
- const response = await sdk.client.fetch<any>(
88
- `/store/swaps`,
89
- {
90
- method: "POST",
91
- body: payload,
92
- headers,
93
- cache: "no-store",
94
- }
95
- )
96
- swapData = response.swap || response
97
- }
98
-
99
- revalidateTag("orders")
100
-
101
- return { success: true, error: null, swap: swapData }
102
- } catch (error: any) {
103
- // Extract error message from response if possible
104
- let errorMsg = "Failed to create exchange request"
105
- if (error.response?.data?.message) {
106
- errorMsg = error.response.data.message
107
- } else if (error.message) {
108
- errorMsg = error.message
109
- }
110
-
111
- return { success: false, error: errorMsg, swap: null }
87
+ return { success: true, error: null, swap: swapData }
88
+ } catch (error) {
89
+ let errorMsg = "Failed to create exchange request"
90
+ if (error instanceof Error && error.message) {
91
+ errorMsg = error.message
112
92
  }
93
+
94
+ return { success: false, error: errorMsg, swap: null }
95
+ }
113
96
  }
114
97
 
115
98
  /**
116
99
  * List swaps for an order or customer
117
100
  */
118
101
  export const listSwaps = async (orderId?: string) => {
119
- const headers = {
120
- ...(await getAuthHeaders()),
121
- }
102
+ const options = await getStoreClientOptions()
122
103
 
123
- // Authenticated flow
124
- if ((headers as any).authorization) {
125
- return sdk.client.fetch<any>(`/store/swaps`, {
126
- method: "GET",
127
- query: orderId ? { order_id: orderId } : {},
128
- headers,
129
- cache: "no-store",
130
- })
131
- }
104
+ if (options.authorization) {
105
+ return medusaSwapList(options, orderId ? { order_id: orderId } : undefined)
106
+ }
107
+
108
+ if (orderId) {
109
+ const cookieStore = await cookies()
110
+ const guestToken =
111
+ cookieStore.get("_medusa_guest_jwt")?.value ||
112
+ cookieStore.get("_medusa_guest_token")?.value
132
113
 
133
- // Guest flow
134
- if (orderId) {
135
- const cookieStore = await cookies()
136
- const guestToken = cookieStore.get("_medusa_guest_jwt")?.value || cookieStore.get("_medusa_guest_token")?.value
137
- if (guestToken) {
138
- return sdk.client.fetch<any>(`/store/guest-orders/${orderId}/swaps`, {
139
- method: "GET",
140
- headers: {
141
- ...headers,
142
- "Authorization": `Bearer ${guestToken}`
143
- },
144
- cache: "no-store",
145
- })
146
- }
114
+ if (guestToken) {
115
+ const guestOptions = await getStoreClientOptionsWithToken(guestToken)
116
+ return medusaSwapListGuest(orderId, guestOptions)
147
117
  }
118
+ }
148
119
 
149
- return { swaps: [], count: 0 }
120
+ return { swaps: [], count: 0 }
150
121
  }
@@ -1,38 +1,19 @@
1
1
  "use server"
2
2
 
3
- import { sdk } from "../config"
4
3
  import { HttpTypes } from "@medusajs/types"
5
-
6
- import { getAuthHeaders, getCacheOptions } from "../cookies"
4
+ import { medusaVariantRetrieve } from "medusa-services/variants"
5
+ import { getStoreClientOptions } from "../util/store-client"
7
6
 
8
7
  export const retrieveVariant = async (
9
8
  variant_id: string
10
9
  ): Promise<HttpTypes.StoreProductVariant | null> => {
11
- const authHeaders = await getAuthHeaders()
12
-
13
- if (!authHeaders) return null
10
+ try {
11
+ const options = await getStoreClientOptions()
12
+ if (!options.authorization) return null
14
13
 
15
- const headers = {
16
- ...authHeaders,
14
+ const { variant } = await medusaVariantRetrieve(variant_id, options, { fields: "*images" })
15
+ return variant as unknown as HttpTypes.StoreProductVariant
16
+ } catch {
17
+ return null
17
18
  }
18
-
19
- const next = {
20
- ...(await getCacheOptions("variants")),
21
- }
22
-
23
- return await sdk.client
24
- .fetch<{ variant: HttpTypes.StoreProductVariant }>(
25
- `/store/product-variants/${variant_id}`,
26
- {
27
- method: "GET",
28
- query: {
29
- fields: "*images",
30
- },
31
- headers,
32
- next,
33
- cache: "force-cache",
34
- }
35
- )
36
- .then(({ variant }) => variant)
37
- .catch(() => null)
38
19
  }
@@ -43,7 +43,7 @@ export async function getWishlist(includeDetails = true, countryCode?: string) {
43
43
  "*thumbnail,*images,*variants,*variants.options,*options,*options.values,*variants.calculated_price,*variants.inventory_quantity,*variants.manage_inventory,*variants.allow_backorder",
44
44
  ...(regionId ? { region_id: regionId } : {}),
45
45
  },
46
- { next: { tags: ["products"] } }
46
+ {}
47
47
  );
48
48
 
49
49
  const fullProducts = productsResponse.products as HttpTypes.StoreProduct[];
@@ -0,0 +1,10 @@
1
+ import { revalidateTag } from "next/cache"
2
+ import { getCacheTag } from "../cookies"
3
+
4
+ export async function revalidateCartTags(): Promise<void> {
5
+ const cartCacheTag = await getCacheTag("carts")
6
+ revalidateTag(cartCacheTag)
7
+
8
+ const fulfillmentCacheTag = await getCacheTag("fulfillment")
9
+ revalidateTag(fulfillmentCacheTag)
10
+ }