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.
- package/dist/cookies.d.ts +2 -2
- package/dist/cookies.d.ts.map +1 -1
- package/dist/edge.d.ts +3 -0
- package/dist/edge.d.ts.map +1 -0
- package/dist/edge.js +1 -0
- package/dist/middleware.d.ts +3 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +1 -0
- package/dist/server/cart.d.ts +9 -5
- package/dist/server/cart.d.ts.map +1 -1
- package/dist/server/cart.js +164 -194
- package/dist/server/categories.d.ts +3 -2
- package/dist/server/categories.d.ts.map +1 -1
- package/dist/server/categories.js +14 -51
- package/dist/server/collections.d.ts.map +1 -1
- package/dist/server/collections.js +16 -61
- package/dist/server/contact.d.ts +34 -0
- package/dist/server/contact.d.ts.map +1 -0
- package/dist/server/contact.js +57 -0
- package/dist/server/customer.d.ts +7 -7
- package/dist/server/customer.d.ts.map +1 -1
- package/dist/server/customer.js +96 -145
- package/dist/server/dynamic-config.d.ts.map +1 -1
- package/dist/server/dynamic-config.js +38 -12
- package/dist/server/fulfillment.d.ts +4 -3
- package/dist/server/fulfillment.d.ts.map +1 -1
- package/dist/server/fulfillment.js +16 -41
- package/dist/server/guest.d.ts +35 -63
- package/dist/server/guest.d.ts.map +1 -1
- package/dist/server/guest.js +81 -202
- package/dist/server/home.d.ts +15 -0
- package/dist/server/home.d.ts.map +1 -0
- package/dist/server/home.js +45 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -0
- package/dist/server/locale-actions.d.ts +1 -1
- package/dist/server/locale-actions.d.ts.map +1 -1
- package/dist/server/locale-actions.js +8 -13
- package/dist/server/locales.d.ts +2 -4
- package/dist/server/locales.d.ts.map +1 -1
- package/dist/server/locales.js +5 -13
- package/dist/server/orders.d.ts +5 -11
- package/dist/server/orders.d.ts.map +1 -1
- package/dist/server/orders.js +126 -267
- package/dist/server/payment-details.d.ts +4 -4
- package/dist/server/payment-details.d.ts.map +1 -1
- package/dist/server/payment-details.js +17 -42
- package/dist/server/payment.d.ts +2 -1
- package/dist/server/payment.d.ts.map +1 -1
- package/dist/server/payment.js +9 -21
- package/dist/server/pincode.d.ts +7 -0
- package/dist/server/pincode.d.ts.map +1 -0
- package/dist/server/pincode.js +30 -0
- package/dist/server/products.d.ts +15 -19
- package/dist/server/products.d.ts.map +1 -1
- package/dist/server/products.js +47 -178
- package/dist/server/regions.d.ts +1 -1
- package/dist/server/regions.d.ts.map +1 -1
- package/dist/server/regions.js +6 -3
- package/dist/server/returns.d.ts +4 -4
- package/dist/server/returns.d.ts.map +1 -1
- package/dist/server/returns.js +50 -154
- package/dist/server/swaps.d.ts +7 -6
- package/dist/server/swaps.d.ts.map +1 -1
- package/dist/server/swaps.js +23 -57
- package/dist/server/variants.d.ts.map +1 -1
- package/dist/server/variants.js +11 -22
- package/dist/server/wishlist.d.ts +11 -0
- package/dist/server/wishlist.d.ts.map +1 -0
- package/dist/server/wishlist.js +49 -0
- package/dist/util/get-locale-header.d.ts +1 -1
- package/dist/util/revalidate-cart.d.ts +2 -0
- package/dist/util/revalidate-cart.d.ts.map +1 -0
- package/dist/util/revalidate-cart.js +8 -0
- package/dist/util/sort-products.d.ts +3 -0
- package/dist/util/sort-products.d.ts.map +1 -0
- package/dist/util/sort-products.js +1 -0
- package/dist/util/store-client.d.ts +13 -0
- package/dist/util/store-client.d.ts.map +1 -0
- package/dist/util/store-client.js +77 -0
- package/package.json +95 -37
- package/src/edge.ts +2 -0
- package/src/middleware.ts +2 -2
- package/src/server/cart.ts +214 -267
- package/src/server/categories.ts +19 -72
- package/src/server/collections.ts +25 -82
- package/src/server/contact.ts +92 -0
- package/src/server/customer.ts +146 -190
- package/src/server/dynamic-config.ts +38 -12
- package/src/server/fulfillment.ts +27 -53
- package/src/server/guest.ts +159 -276
- package/src/server/home.ts +68 -0
- package/src/server/index.ts +1 -0
- package/src/server/locale-actions.ts +8 -15
- package/src/server/locales.ts +6 -18
- package/src/server/orders.ts +167 -337
- package/src/server/payment-details.ts +24 -52
- package/src/server/payment.ts +8 -28
- package/src/server/pincode.ts +49 -0
- package/src/server/products.ts +72 -235
- package/src/server/regions.ts +10 -6
- package/src/server/returns.ts +75 -189
- package/src/server/swaps.ts +94 -123
- package/src/server/variants.ts +9 -28
- package/src/server/wishlist.ts +1 -1
- package/src/util/revalidate-cart.ts +10 -0
- package/src/util/sort-products.ts +2 -47
- package/src/util/store-client.ts +93 -0
- package/src/services/middleware.ts +0 -54
package/dist/server/returns.js
CHANGED
|
@@ -1,78 +1,19 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import {
|
|
2
|
+
import { medusaReturnCreate, medusaReturnCreateGuest, medusaReturnList, medusaReturnListGuest, medusaReturnReasonList, medusaReturnShippingOptionsResolve, medusaReturnUpdatePayment, } from "medusa-services/returns";
|
|
3
3
|
import { cookies } from "next/headers";
|
|
4
|
-
import { getAuthHeaders, getCacheOptions } from "../cookies";
|
|
5
4
|
import { revalidateTag } from "next/cache";
|
|
5
|
+
import { getStoreClientOptions, getStoreClientOptionsWithToken } from "../util/store-client";
|
|
6
6
|
export const listReturnReasons = async () => {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
const next = {
|
|
11
|
-
...(await getCacheOptions("return_reasons")),
|
|
12
|
-
};
|
|
13
|
-
return sdk.client
|
|
14
|
-
.fetch(`/store/return-reasons`, {
|
|
15
|
-
method: "GET",
|
|
16
|
-
headers,
|
|
17
|
-
next,
|
|
18
|
-
cache: "force-cache", // Reasons change rarely
|
|
19
|
-
})
|
|
20
|
-
.then(({ return_reasons }) => return_reasons)
|
|
21
|
-
.catch(() => []);
|
|
7
|
+
const options = await getStoreClientOptions();
|
|
8
|
+
return medusaReturnReasonList(options);
|
|
22
9
|
};
|
|
23
10
|
export const listReturnShippingOptions = async (cartId, regionId, productIds) => {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// Strategy 1: Try with cart_id (preferred as it has context)
|
|
31
|
-
if (cartId) {
|
|
32
|
-
try {
|
|
33
|
-
const { shipping_options } = await sdk.client.fetch(`/store/shipping-options`, {
|
|
34
|
-
method: "GET",
|
|
35
|
-
query: {
|
|
36
|
-
cart_id: cartId,
|
|
37
|
-
is_return: true,
|
|
38
|
-
},
|
|
39
|
-
headers,
|
|
40
|
-
next,
|
|
41
|
-
cache: "no-store",
|
|
42
|
-
});
|
|
43
|
-
if (shipping_options.length > 0) {
|
|
44
|
-
return shipping_options;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch (e) {
|
|
48
|
-
// Fallback strategy
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// Strategy 2: Fallback - Create ephemeral cart if region_id is available
|
|
52
|
-
// This is required because /store/shipping-options STRICTLY requires a cart_id for return options
|
|
53
|
-
if (regionId) {
|
|
54
|
-
try {
|
|
55
|
-
// Create a temporary cart for this region to get valid shipping options
|
|
56
|
-
const { cart } = await sdk.store.cart.create({ region_id: regionId }, {}, headers);
|
|
57
|
-
if (cart?.id) {
|
|
58
|
-
const { shipping_options } = await sdk.client.fetch(`/store/shipping-options`, {
|
|
59
|
-
method: "GET",
|
|
60
|
-
query: {
|
|
61
|
-
cart_id: cart.id,
|
|
62
|
-
is_return: true,
|
|
63
|
-
},
|
|
64
|
-
headers,
|
|
65
|
-
next,
|
|
66
|
-
cache: "no-store",
|
|
67
|
-
});
|
|
68
|
-
return shipping_options;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
catch (e) {
|
|
72
|
-
// Silence error
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return [];
|
|
11
|
+
const options = await getStoreClientOptions();
|
|
12
|
+
const shippingOptions = await medusaReturnShippingOptionsResolve(options, {
|
|
13
|
+
cartId,
|
|
14
|
+
regionId,
|
|
15
|
+
});
|
|
16
|
+
return shippingOptions;
|
|
76
17
|
};
|
|
77
18
|
export const createReturnRequest = async (state, formData) => {
|
|
78
19
|
const orderId = formData.get("order_id");
|
|
@@ -88,146 +29,101 @@ export const createReturnRequest = async (state, formData) => {
|
|
|
88
29
|
try {
|
|
89
30
|
items = JSON.parse(itemsJson);
|
|
90
31
|
}
|
|
91
|
-
catch
|
|
32
|
+
catch {
|
|
92
33
|
return { success: false, error: "Invalid items data", return: null };
|
|
93
34
|
}
|
|
94
35
|
if (items.length === 0) {
|
|
95
36
|
return { success: false, error: "At least one item must be selected", return: null };
|
|
96
37
|
}
|
|
97
|
-
const
|
|
98
|
-
...(await getAuthHeaders()),
|
|
99
|
-
};
|
|
38
|
+
const options = await getStoreClientOptions();
|
|
100
39
|
try {
|
|
101
|
-
// Standard Medusa v2 payload
|
|
102
40
|
const payload = {
|
|
103
41
|
order_id: orderId,
|
|
104
42
|
items: items.map((item) => ({
|
|
105
43
|
id: item.id,
|
|
106
44
|
quantity: item.quantity,
|
|
107
45
|
reason_id: item.return_reason_id || undefined,
|
|
108
|
-
note:
|
|
46
|
+
note: note && note.trim().length > 0 ? note : undefined,
|
|
109
47
|
})),
|
|
110
|
-
...(returnShippingOptionId
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
48
|
+
...(returnShippingOptionId
|
|
49
|
+
? {
|
|
50
|
+
return_shipping: {
|
|
51
|
+
option_id: returnShippingOptionId,
|
|
52
|
+
location_id: locationId || items[0]?.location_id || "default_location",
|
|
53
|
+
},
|
|
114
54
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
note: (note && note.trim().length > 0) ? note : "Return request",
|
|
55
|
+
: {}),
|
|
56
|
+
note: note && note.trim().length > 0 ? note : "Return request",
|
|
118
57
|
};
|
|
119
|
-
// Double check location_id is NOT nested incorrectly if already set above
|
|
120
|
-
// The plugin expects return_shipping.location_id
|
|
121
|
-
// Check for any available guest tokens
|
|
122
58
|
const cookieStore = await cookies();
|
|
123
|
-
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value
|
|
124
|
-
|
|
125
|
-
|
|
59
|
+
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
60
|
+
cookieStore.get("_medusa_guest_token")?.value ||
|
|
61
|
+
cookieStore.get("guest_id")?.value;
|
|
126
62
|
const token = cookieStore.get("_medusa_jwt")?.value;
|
|
127
63
|
let returnData;
|
|
128
|
-
// If we have a guest token, try the guest specific endpoint
|
|
129
64
|
if (guestToken && !token) {
|
|
130
65
|
try {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
body: payload,
|
|
134
|
-
headers: {
|
|
135
|
-
...headers,
|
|
136
|
-
"Authorization": `Bearer ${guestToken}`
|
|
137
|
-
},
|
|
138
|
-
cache: "no-store",
|
|
139
|
-
});
|
|
140
|
-
returnData = response.return || response;
|
|
66
|
+
const guestOptions = await getStoreClientOptionsWithToken(guestToken);
|
|
67
|
+
returnData = (await medusaReturnCreateGuest(orderId, payload, guestOptions));
|
|
141
68
|
}
|
|
142
|
-
catch
|
|
143
|
-
|
|
144
|
-
const response = await sdk.client.fetch(`/store/returns`, {
|
|
145
|
-
method: "POST",
|
|
146
|
-
body: payload,
|
|
147
|
-
headers,
|
|
148
|
-
cache: "no-store",
|
|
149
|
-
});
|
|
150
|
-
returnData = response.return;
|
|
69
|
+
catch {
|
|
70
|
+
returnData = (await medusaReturnCreate(payload, options));
|
|
151
71
|
}
|
|
152
72
|
}
|
|
153
73
|
else {
|
|
154
|
-
|
|
155
|
-
const response = await sdk.client.fetch(`/store/returns`, {
|
|
156
|
-
method: "POST",
|
|
157
|
-
body: payload,
|
|
158
|
-
headers,
|
|
159
|
-
cache: "no-store",
|
|
160
|
-
});
|
|
161
|
-
returnData = response.return;
|
|
74
|
+
returnData = (await medusaReturnCreate(payload, options));
|
|
162
75
|
}
|
|
163
76
|
revalidateTag("orders");
|
|
164
77
|
return { success: true, error: null, return: returnData };
|
|
165
78
|
}
|
|
166
79
|
catch (error) {
|
|
167
|
-
return {
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
error: error instanceof Error ? error.message : "Failed to create return request",
|
|
83
|
+
return: null,
|
|
84
|
+
};
|
|
168
85
|
}
|
|
169
86
|
};
|
|
170
87
|
/**
|
|
171
88
|
* Link a payment method to a return (for Refund Destination)
|
|
172
89
|
*/
|
|
173
90
|
export const updateReturnPayment = async (returnId, paymentId) => {
|
|
174
|
-
const
|
|
175
|
-
...(await getAuthHeaders()),
|
|
176
|
-
};
|
|
91
|
+
const options = await getStoreClientOptions();
|
|
177
92
|
try {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
body: { payment_id: paymentId },
|
|
181
|
-
headers,
|
|
182
|
-
cache: "no-store",
|
|
183
|
-
});
|
|
184
|
-
return { success: true, data: response };
|
|
93
|
+
const data = await medusaReturnUpdatePayment(returnId, paymentId, options);
|
|
94
|
+
return { success: true, data };
|
|
185
95
|
}
|
|
186
96
|
catch (error) {
|
|
187
97
|
console.error("Update return payment fail:", error);
|
|
188
|
-
return {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: error instanceof Error ? error.message : String(error),
|
|
101
|
+
};
|
|
189
102
|
}
|
|
190
103
|
};
|
|
191
104
|
/**
|
|
192
105
|
* List returns for an order or customer
|
|
193
106
|
*/
|
|
194
107
|
export const listReturns = async (orderId) => {
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
};
|
|
198
|
-
// Authenticated flow
|
|
199
|
-
const authHeaders = headers;
|
|
200
|
-
if (authHeaders.authorization || authHeaders.Authorization) {
|
|
108
|
+
const options = await getStoreClientOptions();
|
|
109
|
+
if (options.authorization) {
|
|
201
110
|
try {
|
|
202
|
-
|
|
203
|
-
method: "GET",
|
|
204
|
-
query: orderId ? { order_id: orderId } : {},
|
|
205
|
-
headers,
|
|
206
|
-
cache: "no-store",
|
|
207
|
-
});
|
|
208
|
-
return response;
|
|
111
|
+
return await medusaReturnList(options, orderId ? { order_id: orderId } : undefined);
|
|
209
112
|
}
|
|
210
|
-
catch
|
|
113
|
+
catch {
|
|
211
114
|
return { returns: [], count: 0 };
|
|
212
115
|
}
|
|
213
116
|
}
|
|
214
|
-
// Guest flow
|
|
215
117
|
if (orderId) {
|
|
216
118
|
const cookieStore = await cookies();
|
|
217
|
-
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
119
|
+
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
120
|
+
cookieStore.get("_medusa_guest_token")?.value;
|
|
218
121
|
if (guestToken) {
|
|
219
122
|
try {
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
headers: {
|
|
223
|
-
...headers,
|
|
224
|
-
"Authorization": `Bearer ${guestToken}`
|
|
225
|
-
},
|
|
226
|
-
cache: "no-store",
|
|
227
|
-
});
|
|
228
|
-
return response;
|
|
123
|
+
const guestOptions = await getStoreClientOptionsWithToken(guestToken);
|
|
124
|
+
return await medusaReturnListGuest(orderId, guestOptions);
|
|
229
125
|
}
|
|
230
|
-
catch
|
|
126
|
+
catch {
|
|
231
127
|
return { returns: [], count: 0 };
|
|
232
128
|
}
|
|
233
129
|
}
|
package/dist/server/swaps.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
export type SwapActionState = {
|
|
2
|
+
success: boolean;
|
|
3
|
+
error: string | null;
|
|
4
|
+
swap: Record<string, unknown> | null;
|
|
5
|
+
};
|
|
1
6
|
/**
|
|
2
7
|
* Create an exchange (swap) request.
|
|
3
8
|
* Supports both authenticated customers and guest users.
|
|
4
9
|
*/
|
|
5
|
-
export declare const createSwapRequest: (
|
|
6
|
-
success: boolean;
|
|
7
|
-
error: string | null;
|
|
8
|
-
swap: any | null;
|
|
9
|
-
}>;
|
|
10
|
+
export declare const createSwapRequest: (_prevState: SwapActionState, formData: FormData) => Promise<SwapActionState>;
|
|
10
11
|
/**
|
|
11
12
|
* List swaps for an order or customer
|
|
12
13
|
*/
|
|
13
|
-
export declare const listSwaps: (orderId?: string) => Promise<
|
|
14
|
+
export declare const listSwaps: (orderId?: string) => Promise<import("medusa-services/swaps").StoreSwapListResponse>;
|
|
14
15
|
//# sourceMappingURL=swaps.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swaps.d.ts","sourceRoot":"","sources":["../../src/server/swaps.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"swaps.d.ts","sourceRoot":"","sources":["../../src/server/swaps.ts"],"names":[],"mappings":"AAYA,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAA;CACrC,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAC5B,YAAY,eAAe,EAC3B,UAAU,QAAQ,KACjB,OAAO,CAAC,eAAe,CAsEzB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,GAAU,UAAU,MAAM,mEAoB/C,CAAA"}
|
package/dist/server/swaps.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import {
|
|
2
|
+
import { medusaSwapCreate, medusaSwapCreateGuest, medusaSwapList, medusaSwapListGuest, } from "medusa-services/swaps";
|
|
3
3
|
import { cookies } from "next/headers";
|
|
4
|
-
import { getAuthHeaders } from "../cookies";
|
|
5
4
|
import { revalidateTag } from "next/cache";
|
|
5
|
+
import { getStoreClientOptions, getStoreClientOptionsWithToken } from "../util/store-client";
|
|
6
6
|
/**
|
|
7
7
|
* Create an exchange (swap) request.
|
|
8
8
|
* Supports both authenticated customers and guest users.
|
|
9
9
|
*/
|
|
10
|
-
export const createSwapRequest = async (
|
|
10
|
+
export const createSwapRequest = async (_prevState, formData) => {
|
|
11
11
|
const orderId = formData.get("order_id");
|
|
12
12
|
const returnItemsJson = formData.get("return_items");
|
|
13
13
|
const newItemsJson = formData.get("new_items");
|
|
@@ -25,15 +25,13 @@ export const createSwapRequest = async (prevState, formData) => {
|
|
|
25
25
|
returnItems = JSON.parse(returnItemsJson);
|
|
26
26
|
newItems = JSON.parse(newItemsJson);
|
|
27
27
|
}
|
|
28
|
-
catch
|
|
28
|
+
catch {
|
|
29
29
|
return { success: false, error: "Invalid items data format", swap: null };
|
|
30
30
|
}
|
|
31
31
|
if (returnItems.length === 0) {
|
|
32
32
|
return { success: false, error: "At least one item must be selected for return", swap: null };
|
|
33
33
|
}
|
|
34
|
-
const
|
|
35
|
-
...(await getAuthHeaders()),
|
|
36
|
-
};
|
|
34
|
+
const options = await getStoreClientOptions();
|
|
37
35
|
try {
|
|
38
36
|
const payload = {
|
|
39
37
|
order_id: orderId,
|
|
@@ -43,46 +41,28 @@ export const createSwapRequest = async (prevState, formData) => {
|
|
|
43
41
|
note: note || "Exchange requested from storefront",
|
|
44
42
|
};
|
|
45
43
|
const cookieStore = await cookies();
|
|
46
|
-
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
44
|
+
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
45
|
+
cookieStore.get("_medusa_guest_token")?.value;
|
|
47
46
|
const token = cookieStore.get("_medusa_jwt")?.value;
|
|
48
47
|
let swapData;
|
|
49
|
-
// Check if we should use the guest endpoint
|
|
50
48
|
if (guestToken && !token) {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
},
|
|
59
|
-
headers: {
|
|
60
|
-
...headers,
|
|
61
|
-
"Authorization": `Bearer ${guestToken}`
|
|
62
|
-
},
|
|
63
|
-
cache: "no-store",
|
|
64
|
-
});
|
|
65
|
-
swapData = response.swap || response;
|
|
49
|
+
const guestOptions = await getStoreClientOptionsWithToken(guestToken);
|
|
50
|
+
swapData = await medusaSwapCreateGuest(orderId, {
|
|
51
|
+
return_items: payload.return_items,
|
|
52
|
+
new_items: payload.new_items,
|
|
53
|
+
reason: payload.reason,
|
|
54
|
+
note: payload.note,
|
|
55
|
+
}, guestOptions);
|
|
66
56
|
}
|
|
67
57
|
else {
|
|
68
|
-
|
|
69
|
-
method: "POST",
|
|
70
|
-
body: payload,
|
|
71
|
-
headers,
|
|
72
|
-
cache: "no-store",
|
|
73
|
-
});
|
|
74
|
-
swapData = response.swap || response;
|
|
58
|
+
swapData = await medusaSwapCreate(payload, options);
|
|
75
59
|
}
|
|
76
60
|
revalidateTag("orders");
|
|
77
61
|
return { success: true, error: null, swap: swapData };
|
|
78
62
|
}
|
|
79
63
|
catch (error) {
|
|
80
|
-
// Extract error message from response if possible
|
|
81
64
|
let errorMsg = "Failed to create exchange request";
|
|
82
|
-
if (error.
|
|
83
|
-
errorMsg = error.response.data.message;
|
|
84
|
-
}
|
|
85
|
-
else if (error.message) {
|
|
65
|
+
if (error instanceof Error && error.message) {
|
|
86
66
|
errorMsg = error.message;
|
|
87
67
|
}
|
|
88
68
|
return { success: false, error: errorMsg, swap: null };
|
|
@@ -92,31 +72,17 @@ export const createSwapRequest = async (prevState, formData) => {
|
|
|
92
72
|
* List swaps for an order or customer
|
|
93
73
|
*/
|
|
94
74
|
export const listSwaps = async (orderId) => {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// Authenticated flow
|
|
99
|
-
if (headers.authorization) {
|
|
100
|
-
return sdk.client.fetch(`/store/swaps`, {
|
|
101
|
-
method: "GET",
|
|
102
|
-
query: orderId ? { order_id: orderId } : {},
|
|
103
|
-
headers,
|
|
104
|
-
cache: "no-store",
|
|
105
|
-
});
|
|
75
|
+
const options = await getStoreClientOptions();
|
|
76
|
+
if (options.authorization) {
|
|
77
|
+
return medusaSwapList(options, orderId ? { order_id: orderId } : undefined);
|
|
106
78
|
}
|
|
107
|
-
// Guest flow
|
|
108
79
|
if (orderId) {
|
|
109
80
|
const cookieStore = await cookies();
|
|
110
|
-
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
81
|
+
const guestToken = cookieStore.get("_medusa_guest_jwt")?.value ||
|
|
82
|
+
cookieStore.get("_medusa_guest_token")?.value;
|
|
111
83
|
if (guestToken) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
headers: {
|
|
115
|
-
...headers,
|
|
116
|
-
"Authorization": `Bearer ${guestToken}`
|
|
117
|
-
},
|
|
118
|
-
cache: "no-store",
|
|
119
|
-
});
|
|
84
|
+
const guestOptions = await getStoreClientOptionsWithToken(guestToken);
|
|
85
|
+
return medusaSwapListGuest(orderId, guestOptions);
|
|
120
86
|
}
|
|
121
87
|
}
|
|
122
88
|
return { swaps: [], count: 0 };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../src/server/variants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../src/server/variants.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,eAAO,MAAM,eAAe,GAC1B,YAAY,MAAM,KACjB,OAAO,CAAC,SAAS,CAAC,mBAAmB,GAAG,IAAI,CAU9C,CAAA"}
|
package/dist/server/variants.js
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { medusaVariantRetrieve } from "medusa-services/variants";
|
|
3
|
+
import { getStoreClientOptions } from "../util/store-client";
|
|
4
4
|
export const retrieveVariant = async (variant_id) => {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
try {
|
|
6
|
+
const options = await getStoreClientOptions();
|
|
7
|
+
if (!options.authorization)
|
|
8
|
+
return null;
|
|
9
|
+
const { variant } = await medusaVariantRetrieve(variant_id, options, { fields: "*images" });
|
|
10
|
+
return variant;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
7
13
|
return null;
|
|
8
|
-
|
|
9
|
-
...authHeaders,
|
|
10
|
-
};
|
|
11
|
-
const next = {
|
|
12
|
-
...(await getCacheOptions("variants")),
|
|
13
|
-
};
|
|
14
|
-
return await sdk.client
|
|
15
|
-
.fetch(`/store/product-variants/${variant_id}`, {
|
|
16
|
-
method: "GET",
|
|
17
|
-
query: {
|
|
18
|
-
fields: "*images",
|
|
19
|
-
},
|
|
20
|
-
headers,
|
|
21
|
-
next,
|
|
22
|
-
cache: "force-cache",
|
|
23
|
-
})
|
|
24
|
-
.then(({ variant }) => variant)
|
|
25
|
-
.catch(() => null);
|
|
14
|
+
}
|
|
26
15
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Wishlist entries enriched with full Medusa product payloads. */
|
|
2
|
+
export declare function getWishlist(includeDetails?: boolean, countryCode?: string): Promise<{
|
|
3
|
+
success: false;
|
|
4
|
+
error: string;
|
|
5
|
+
data: unknown[];
|
|
6
|
+
} | {
|
|
7
|
+
success: true;
|
|
8
|
+
data: Record<string, unknown>[];
|
|
9
|
+
error?: undefined;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=wishlist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wishlist.d.ts","sourceRoot":"","sources":["../../src/server/wishlist.ts"],"names":[],"mappings":"AAOA,mEAAmE;AACnE,wBAAsB,WAAW,CAAC,cAAc,UAAO,EAAE,WAAW,CAAC,EAAE,MAAM;;;UAGF,OAAO,EAAE;;;;;GAoDnF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
import { sdk } from "../config";
|
|
3
|
+
import { getRegion } from "./regions";
|
|
4
|
+
import { fetchWishlist } from "medusa-wishlist-logic/server";
|
|
5
|
+
/** Wishlist entries enriched with full Medusa product payloads. */
|
|
6
|
+
export async function getWishlist(includeDetails = true, countryCode) {
|
|
7
|
+
const result = await fetchWishlist(includeDetails);
|
|
8
|
+
if (!result.success) {
|
|
9
|
+
return { success: false, error: result.error, data: [] };
|
|
10
|
+
}
|
|
11
|
+
let wishlistItems = result.data;
|
|
12
|
+
if (!includeDetails || wishlistItems.length === 0) {
|
|
13
|
+
return { success: true, data: wishlistItems };
|
|
14
|
+
}
|
|
15
|
+
const productIds = wishlistItems
|
|
16
|
+
.map((item) => {
|
|
17
|
+
const id = item.product_id ?? item.id;
|
|
18
|
+
return typeof id === "string" ? id : null;
|
|
19
|
+
})
|
|
20
|
+
.filter((id) => Boolean(id));
|
|
21
|
+
if (productIds.length === 0) {
|
|
22
|
+
return { success: true, data: wishlistItems };
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
let regionId;
|
|
26
|
+
if (countryCode) {
|
|
27
|
+
const region = await getRegion(countryCode);
|
|
28
|
+
regionId = region?.id;
|
|
29
|
+
}
|
|
30
|
+
const productsResponse = await sdk.store.product.list({
|
|
31
|
+
id: productIds,
|
|
32
|
+
fields: "*thumbnail,*images,*variants,*variants.options,*options,*options.values,*variants.calculated_price,*variants.inventory_quantity,*variants.manage_inventory,*variants.allow_backorder",
|
|
33
|
+
...(regionId ? { region_id: regionId } : {}),
|
|
34
|
+
}, {});
|
|
35
|
+
const fullProducts = productsResponse.products;
|
|
36
|
+
wishlistItems = wishlistItems.map((item) => {
|
|
37
|
+
const itemId = (item.product_id ?? item.id);
|
|
38
|
+
const fullProduct = fullProducts.find((p) => p.id === itemId);
|
|
39
|
+
if (fullProduct) {
|
|
40
|
+
return { ...item, product: fullProduct };
|
|
41
|
+
}
|
|
42
|
+
return item;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Return API wishlist without enrichment
|
|
47
|
+
}
|
|
48
|
+
return { success: true, data: wishlistItems };
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"revalidate-cart.d.ts","sourceRoot":"","sources":["../../src/util/revalidate-cart.ts"],"names":[],"mappings":"AAGA,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAMxD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { revalidateTag } from "next/cache";
|
|
2
|
+
import { getCacheTag } from "../cookies";
|
|
3
|
+
export async function revalidateCartTags() {
|
|
4
|
+
const cartCacheTag = await getCacheTag("carts");
|
|
5
|
+
revalidateTag(cartCacheTag);
|
|
6
|
+
const fulfillmentCacheTag = await getCacheTag("fulfillment");
|
|
7
|
+
revalidateTag(fulfillmentCacheTag);
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sort-products.d.ts","sourceRoot":"","sources":["../../src/util/sort-products.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sortProducts } from "medusa-services/product-listing";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MedusaStoreClientOptions } from "medusa-services/store-api";
|
|
2
|
+
import { type MedusaAuthClientOptions } from "medusa-services/auth";
|
|
3
|
+
/**
|
|
4
|
+
* Builds portable Medusa store client options from Next.js cookies and env.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getStoreClientOptions(): Promise<MedusaStoreClientOptions>;
|
|
7
|
+
/** @deprecated Use getStoreClientOptions */
|
|
8
|
+
export declare const getStoreCartClientOptions: typeof getStoreClientOptions;
|
|
9
|
+
export declare function getMedusaBackendUrl(): string;
|
|
10
|
+
export declare function getGuestStoreClientOptions(): Promise<MedusaStoreClientOptions | null>;
|
|
11
|
+
export declare function getStoreClientOptionsWithToken(token: string): Promise<MedusaStoreClientOptions>;
|
|
12
|
+
export declare function getAuthClientOptions(authorization?: string): Promise<MedusaAuthClientOptions>;
|
|
13
|
+
//# sourceMappingURL=store-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-client.d.ts","sourceRoot":"","sources":["../../src/util/store-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAA;AACzE,OAAO,EAAuB,KAAK,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAqBxF;;GAEG;AACH,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,wBAAwB,CAAC,CAY/E;AAED,4CAA4C;AAC5C,eAAO,MAAM,yBAAyB,8BAAwB,CAAA;AAE9D,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C;AAED,wBAAsB,0BAA0B,IAAI,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC,CAuB3F;AAED,wBAAsB,8BAA8B,CAClD,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,wBAAwB,CAAC,CAMnC;AAED,wBAAsB,oBAAoB,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAWnG"}
|