@reactionary/provider-medusa 0.1.13 → 0.2.2
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/core/client.js +2 -23
- package/core/initialize.js +4 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/providers/cart.provider.js +68 -58
- package/providers/category.provider.js +19 -53
- package/providers/checkout.provider.js +18 -40
- package/providers/identity.provider.js +9 -25
- package/providers/inventory.provider.js +7 -22
- package/providers/price.provider.js +8 -13
- package/providers/product-search.provider.js +31 -35
- package/providers/product.provider.js +13 -13
- package/providers/profile.provider.js +323 -0
- package/schema/capabilities.schema.js +2 -1
- package/src/core/client.d.ts +0 -35
- package/src/index.d.ts +1 -0
- package/src/providers/cart.provider.d.ts +10 -10
- package/src/providers/category.provider.d.ts +8 -50
- package/src/providers/checkout.provider.d.ts +10 -10
- package/src/providers/identity.provider.d.ts +5 -5
- package/src/providers/inventory.provider.d.ts +2 -2
- package/src/providers/price.provider.d.ts +3 -3
- package/src/providers/product-search.provider.d.ts +3 -17
- package/src/providers/product.provider.d.ts +4 -4
- package/src/providers/profile.provider.d.ts +30 -0
- package/src/schema/capabilities.schema.d.ts +3 -2
- package/test/cart.provider.spec.js +69 -49
- package/test/category.provider.spec.js +125 -63
- package/test/checkout.spec.js +80 -49
- package/test/identity.provider.spec.js +22 -7
- package/test/inventory.provider.spec.js +35 -24
- package/test/large-cart.provider.spec.js +57 -31
- package/test/price.provider.spec.js +57 -36
- package/test/product.provider.spec.js +78 -49
- package/test/search.provider.spec.js +47 -20
package/core/client.js
CHANGED
|
@@ -207,25 +207,11 @@ class MedusaClient {
|
|
|
207
207
|
id: {
|
|
208
208
|
userId: customerResponse.customer.id
|
|
209
209
|
},
|
|
210
|
-
type: "Registered"
|
|
211
|
-
meta: {
|
|
212
|
-
cache: {
|
|
213
|
-
hit: false,
|
|
214
|
-
key: customerResponse.customer.id
|
|
215
|
-
},
|
|
216
|
-
placeholder: false
|
|
217
|
-
}
|
|
210
|
+
type: "Registered"
|
|
218
211
|
};
|
|
219
212
|
return identity;
|
|
220
213
|
}
|
|
221
214
|
return {
|
|
222
|
-
meta: {
|
|
223
|
-
cache: {
|
|
224
|
-
hit: false,
|
|
225
|
-
key: ""
|
|
226
|
-
},
|
|
227
|
-
placeholder: false
|
|
228
|
-
},
|
|
229
215
|
type: "Anonymous"
|
|
230
216
|
};
|
|
231
217
|
} catch (error) {
|
|
@@ -237,14 +223,7 @@ class MedusaClient {
|
|
|
237
223
|
}
|
|
238
224
|
async logout(reqCtx) {
|
|
239
225
|
const identity = {
|
|
240
|
-
type: "Anonymous"
|
|
241
|
-
meta: {
|
|
242
|
-
cache: {
|
|
243
|
-
hit: false,
|
|
244
|
-
key: ""
|
|
245
|
-
},
|
|
246
|
-
placeholder: false
|
|
247
|
-
}
|
|
226
|
+
type: "Anonymous"
|
|
248
227
|
};
|
|
249
228
|
const client = await this.getClient();
|
|
250
229
|
try {
|
package/core/initialize.js
CHANGED
|
@@ -9,6 +9,7 @@ import { MedusaProductProvider } from "../providers/product.provider.js";
|
|
|
9
9
|
import { MedusaClient } from "./client.js";
|
|
10
10
|
import { MedusaCategoryProvider } from "../providers/category.provider.js";
|
|
11
11
|
import { MedusaCheckoutProvider } from "../providers/checkout.provider.js";
|
|
12
|
+
import { MedusaProfileProvider } from "../providers/profile.provider.js";
|
|
12
13
|
function withMedusaCapabilities(configuration, capabilities) {
|
|
13
14
|
return (cache, context) => {
|
|
14
15
|
const client = {};
|
|
@@ -39,6 +40,9 @@ function withMedusaCapabilities(configuration, capabilities) {
|
|
|
39
40
|
if (caps.identity) {
|
|
40
41
|
client.identity = new MedusaIdentityProvider(configuration, cache, context, medusaClient);
|
|
41
42
|
}
|
|
43
|
+
if (caps.profile) {
|
|
44
|
+
client.profile = new MedusaProfileProvider(configuration, cache, context, medusaClient);
|
|
45
|
+
}
|
|
42
46
|
return client;
|
|
43
47
|
};
|
|
44
48
|
}
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-medusa",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"zod": "4.1.9",
|
|
9
|
-
"@reactionary/core": "0.
|
|
9
|
+
"@reactionary/core": "0.2.2",
|
|
10
10
|
"@medusajs/js-sdk": "^2.0.0",
|
|
11
11
|
"debug": "^4.3.4",
|
|
12
12
|
"@medusajs/types": "^2.11.0",
|
|
@@ -22,13 +22,17 @@ import {
|
|
|
22
22
|
CartQueryByIdSchema,
|
|
23
23
|
CartSchema,
|
|
24
24
|
ProductVariantIdentifierSchema,
|
|
25
|
-
Reactionary
|
|
25
|
+
Reactionary,
|
|
26
|
+
success,
|
|
27
|
+
error
|
|
26
28
|
} from "@reactionary/core";
|
|
27
29
|
import createDebug from "debug";
|
|
30
|
+
import { MedusaCartIdentifierSchema } from "../schema/medusa.schema.js";
|
|
28
31
|
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
handleProviderError,
|
|
33
|
+
parseMedusaCostBreakdown,
|
|
34
|
+
parseMedusaItemPrice
|
|
35
|
+
} from "../utils/medusa-helpers.js";
|
|
32
36
|
const debug = createDebug("reactionary:medusa:cart");
|
|
33
37
|
class MedusaCartProvider extends CartProvider {
|
|
34
38
|
constructor(config, cache, context, client) {
|
|
@@ -55,19 +59,25 @@ class MedusaCartProvider extends CartProvider {
|
|
|
55
59
|
debug("Received cart response:", cartResponse);
|
|
56
60
|
}
|
|
57
61
|
if (cartResponse.cart) {
|
|
58
|
-
return this.parseSingle(cartResponse.cart);
|
|
62
|
+
return success(this.parseSingle(cartResponse.cart));
|
|
59
63
|
}
|
|
60
|
-
return
|
|
61
|
-
|
|
64
|
+
return error({
|
|
65
|
+
type: "NotFound",
|
|
66
|
+
identifier: payload
|
|
67
|
+
});
|
|
68
|
+
} catch (err) {
|
|
62
69
|
debug("Failed to get cart by ID:", error);
|
|
63
|
-
return
|
|
70
|
+
return error({
|
|
71
|
+
type: "NotFound",
|
|
72
|
+
identifier: payload
|
|
73
|
+
});
|
|
64
74
|
}
|
|
65
75
|
}
|
|
66
76
|
async add(payload) {
|
|
67
77
|
try {
|
|
68
78
|
const client = await this.getClient();
|
|
69
79
|
let cartIdentifier = payload.cart;
|
|
70
|
-
if (!cartIdentifier
|
|
80
|
+
if (!cartIdentifier) {
|
|
71
81
|
cartIdentifier = await this.createCart();
|
|
72
82
|
}
|
|
73
83
|
const medusaId = cartIdentifier;
|
|
@@ -96,11 +106,11 @@ class MedusaCartProvider extends CartProvider {
|
|
|
96
106
|
debug("Received add item response:", response);
|
|
97
107
|
}
|
|
98
108
|
if (response.cart) {
|
|
99
|
-
return this.parseSingle(response.cart);
|
|
109
|
+
return success(this.parseSingle(response.cart));
|
|
100
110
|
}
|
|
101
111
|
throw new Error("Failed to add item to cart");
|
|
102
|
-
} catch (
|
|
103
|
-
handleProviderError("add item to cart",
|
|
112
|
+
} catch (error2) {
|
|
113
|
+
handleProviderError("add item to cart", error2);
|
|
104
114
|
}
|
|
105
115
|
}
|
|
106
116
|
async remove(payload) {
|
|
@@ -115,11 +125,11 @@ class MedusaCartProvider extends CartProvider {
|
|
|
115
125
|
}
|
|
116
126
|
);
|
|
117
127
|
if (response.parent) {
|
|
118
|
-
return this.parseSingle(response.parent);
|
|
128
|
+
return success(this.parseSingle(response.parent));
|
|
119
129
|
}
|
|
120
130
|
throw new Error("Failed to remove item from cart");
|
|
121
|
-
} catch (
|
|
122
|
-
handleProviderError("remove item from cart",
|
|
131
|
+
} catch (error2) {
|
|
132
|
+
handleProviderError("remove item from cart", error2);
|
|
123
133
|
}
|
|
124
134
|
}
|
|
125
135
|
async changeQuantity(payload) {
|
|
@@ -142,11 +152,11 @@ class MedusaCartProvider extends CartProvider {
|
|
|
142
152
|
}
|
|
143
153
|
);
|
|
144
154
|
if (response.cart) {
|
|
145
|
-
return this.parseSingle(response.cart);
|
|
155
|
+
return success(this.parseSingle(response.cart));
|
|
146
156
|
}
|
|
147
157
|
throw new Error("Failed to change item quantity");
|
|
148
|
-
} catch (
|
|
149
|
-
handleProviderError("change item quantity",
|
|
158
|
+
} catch (error2) {
|
|
159
|
+
handleProviderError("change item quantity", error2);
|
|
150
160
|
}
|
|
151
161
|
}
|
|
152
162
|
async getActiveCartId() {
|
|
@@ -157,22 +167,24 @@ class MedusaCartProvider extends CartProvider {
|
|
|
157
167
|
if (activeCartId) {
|
|
158
168
|
try {
|
|
159
169
|
await client.store.cart.retrieve(activeCartId);
|
|
160
|
-
return
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
170
|
+
return success(
|
|
171
|
+
MedusaCartIdentifierSchema.parse({
|
|
172
|
+
key: activeCartId,
|
|
173
|
+
region_id: (await this.client.getActiveRegion()).id
|
|
174
|
+
})
|
|
175
|
+
);
|
|
164
176
|
} catch {
|
|
165
177
|
}
|
|
166
178
|
}
|
|
167
|
-
return
|
|
168
|
-
|
|
169
|
-
|
|
179
|
+
return error({
|
|
180
|
+
type: "NotFound",
|
|
181
|
+
identifier: void 0
|
|
170
182
|
});
|
|
171
|
-
} catch (
|
|
183
|
+
} catch (err) {
|
|
172
184
|
debug("Failed to get active cart ID:", error);
|
|
173
|
-
return
|
|
174
|
-
|
|
175
|
-
|
|
185
|
+
return error({
|
|
186
|
+
type: "NotFound",
|
|
187
|
+
identifier: void 0
|
|
176
188
|
});
|
|
177
189
|
}
|
|
178
190
|
}
|
|
@@ -195,10 +207,10 @@ class MedusaCartProvider extends CartProvider {
|
|
|
195
207
|
await client.store.cart.deleteLineItem(medusaId.key, item.id);
|
|
196
208
|
}
|
|
197
209
|
}
|
|
198
|
-
return
|
|
199
|
-
} catch (
|
|
200
|
-
debug("Failed to delete cart:",
|
|
201
|
-
return
|
|
210
|
+
return success(void 0);
|
|
211
|
+
} catch (error2) {
|
|
212
|
+
debug("Failed to delete cart:", error2);
|
|
213
|
+
return success(void 0);
|
|
202
214
|
}
|
|
203
215
|
}
|
|
204
216
|
async applyCouponCode(payload) {
|
|
@@ -215,11 +227,11 @@ class MedusaCartProvider extends CartProvider {
|
|
|
215
227
|
}
|
|
216
228
|
);
|
|
217
229
|
if (response.cart) {
|
|
218
|
-
return this.parseSingle(response.cart);
|
|
230
|
+
return success(this.parseSingle(response.cart));
|
|
219
231
|
}
|
|
220
232
|
throw new Error("Failed to apply coupon code");
|
|
221
|
-
} catch (
|
|
222
|
-
handleProviderError("apply coupon code",
|
|
233
|
+
} catch (error2) {
|
|
234
|
+
handleProviderError("apply coupon code", error2);
|
|
223
235
|
}
|
|
224
236
|
}
|
|
225
237
|
async removeCouponCode(payload) {
|
|
@@ -232,18 +244,22 @@ class MedusaCartProvider extends CartProvider {
|
|
|
232
244
|
(x) => !x.is_automatic && x.code
|
|
233
245
|
);
|
|
234
246
|
const remainingCodes = manualDiscounts.filter((x) => x.code !== payload.couponCode).map((promotion) => promotion.code) || [];
|
|
235
|
-
const response = await client.store.cart.update(
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
247
|
+
const response = await client.store.cart.update(
|
|
248
|
+
medusaId.key,
|
|
249
|
+
{
|
|
250
|
+
promo_codes: remainingCodes || []
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
fields: this.includedFields
|
|
254
|
+
}
|
|
255
|
+
);
|
|
240
256
|
if (response.cart) {
|
|
241
|
-
return this.parseSingle(response.cart);
|
|
257
|
+
return success(this.parseSingle(response.cart));
|
|
242
258
|
}
|
|
243
259
|
}
|
|
244
260
|
throw new Error("Failed to remove coupon code");
|
|
245
|
-
} catch (
|
|
246
|
-
handleProviderError("remove coupon code",
|
|
261
|
+
} catch (error2) {
|
|
262
|
+
handleProviderError("remove coupon code", error2);
|
|
247
263
|
}
|
|
248
264
|
}
|
|
249
265
|
async changeCurrency(payload) {
|
|
@@ -271,11 +287,11 @@ class MedusaCartProvider extends CartProvider {
|
|
|
271
287
|
this.client.setSessionData({
|
|
272
288
|
activeCartId: newCartResponse.cart.id
|
|
273
289
|
});
|
|
274
|
-
return this.parseSingle(newCartResponse.cart);
|
|
290
|
+
return success(this.parseSingle(newCartResponse.cart));
|
|
275
291
|
}
|
|
276
292
|
throw new Error("Failed to change currency");
|
|
277
|
-
} catch (
|
|
278
|
-
handleProviderError("change currency",
|
|
293
|
+
} catch (error2) {
|
|
294
|
+
handleProviderError("change currency", error2);
|
|
279
295
|
}
|
|
280
296
|
}
|
|
281
297
|
async createCart(currency) {
|
|
@@ -300,8 +316,8 @@ class MedusaCartProvider extends CartProvider {
|
|
|
300
316
|
return cartIdentifier;
|
|
301
317
|
}
|
|
302
318
|
throw new Error("Failed to create cart");
|
|
303
|
-
} catch (
|
|
304
|
-
handleProviderError("create cart",
|
|
319
|
+
} catch (error2) {
|
|
320
|
+
handleProviderError("create cart", error2);
|
|
305
321
|
}
|
|
306
322
|
}
|
|
307
323
|
async getClient() {
|
|
@@ -361,24 +377,18 @@ class MedusaCartProvider extends CartProvider {
|
|
|
361
377
|
const price = this.parseCostBreakdown(remote);
|
|
362
378
|
const items = new Array();
|
|
363
379
|
const allItems = remote.items || [];
|
|
364
|
-
allItems.sort(
|
|
380
|
+
allItems.sort(
|
|
381
|
+
(a, b) => a.created_at && b.created_at ? new Date(a.created_at).getTime() - new Date(b.created_at).getTime() : 0
|
|
382
|
+
);
|
|
365
383
|
for (const remoteItem of allItems) {
|
|
366
384
|
items.push(this.parseCartItem(remoteItem, price.grandTotal.currency));
|
|
367
385
|
}
|
|
368
|
-
const meta = {
|
|
369
|
-
cache: {
|
|
370
|
-
hit: false,
|
|
371
|
-
key: this.generateCacheKeySingle(identifier)
|
|
372
|
-
},
|
|
373
|
-
placeholder: false
|
|
374
|
-
};
|
|
375
386
|
const result = {
|
|
376
387
|
identifier,
|
|
377
388
|
name,
|
|
378
389
|
description,
|
|
379
390
|
price,
|
|
380
391
|
items,
|
|
381
|
-
meta,
|
|
382
392
|
userId: {
|
|
383
393
|
userId: "???"
|
|
384
394
|
}
|
|
@@ -20,6 +20,8 @@ import {
|
|
|
20
20
|
CategoryQueryForTopCategoriesSchema,
|
|
21
21
|
createPaginatedResponseSchema,
|
|
22
22
|
Reactionary,
|
|
23
|
+
success,
|
|
24
|
+
error,
|
|
23
25
|
CategoryPaginatedResultSchema
|
|
24
26
|
} from "@reactionary/core";
|
|
25
27
|
import z from "zod";
|
|
@@ -50,9 +52,9 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
50
52
|
break;
|
|
51
53
|
}
|
|
52
54
|
offset += limit;
|
|
53
|
-
} catch (
|
|
55
|
+
} catch (error2) {
|
|
54
56
|
throw new Error(
|
|
55
|
-
"Category not found " + externalId + " due to error: " +
|
|
57
|
+
"Category not found " + externalId + " due to error: " + error2
|
|
56
58
|
);
|
|
57
59
|
break;
|
|
58
60
|
}
|
|
@@ -62,25 +64,12 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
62
64
|
async getById(payload) {
|
|
63
65
|
const candidate = await this.resolveCategoryIdByExternalId(payload.id.key);
|
|
64
66
|
if (!candidate) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
images: [],
|
|
70
|
-
name: "",
|
|
71
|
-
slug: "",
|
|
72
|
-
text: "",
|
|
73
|
-
meta: {
|
|
74
|
-
cache: {
|
|
75
|
-
hit: false,
|
|
76
|
-
key: ""
|
|
77
|
-
},
|
|
78
|
-
placeholder: true
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
return dummyCategory;
|
|
67
|
+
return error({
|
|
68
|
+
type: "NotFound",
|
|
69
|
+
identifier: payload
|
|
70
|
+
});
|
|
82
71
|
}
|
|
83
|
-
return this.parseSingle(candidate);
|
|
72
|
+
return success(this.parseSingle(candidate));
|
|
84
73
|
}
|
|
85
74
|
async getBySlug(payload) {
|
|
86
75
|
const sdk = await this.client.getClient();
|
|
@@ -90,9 +79,12 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
90
79
|
offset: 0
|
|
91
80
|
});
|
|
92
81
|
if (categoryResult.count === 0) {
|
|
93
|
-
return
|
|
82
|
+
return error({
|
|
83
|
+
type: "NotFound",
|
|
84
|
+
identifier: payload
|
|
85
|
+
});
|
|
94
86
|
}
|
|
95
|
-
return this.parseSingle(categoryResult.product_categories[0]);
|
|
87
|
+
return success(this.parseSingle(categoryResult.product_categories[0]));
|
|
96
88
|
}
|
|
97
89
|
async getBreadcrumbPathToCategory(payload) {
|
|
98
90
|
const actualCategoryId = await this.resolveCategoryIdByExternalId(
|
|
@@ -113,7 +105,7 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
113
105
|
current = current.parent_category;
|
|
114
106
|
}
|
|
115
107
|
results = results.reverse();
|
|
116
|
-
return results;
|
|
108
|
+
return success(results);
|
|
117
109
|
}
|
|
118
110
|
async findChildCategories(payload) {
|
|
119
111
|
const sdk = await this.client.getClient();
|
|
@@ -130,14 +122,7 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
130
122
|
offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
|
|
131
123
|
});
|
|
132
124
|
const result = this.parsePaginatedResult(response);
|
|
133
|
-
result
|
|
134
|
-
cache: {
|
|
135
|
-
hit: false,
|
|
136
|
-
key: this.generateCacheKeyPaginatedResult("top", result)
|
|
137
|
-
},
|
|
138
|
-
placeholder: false
|
|
139
|
-
};
|
|
140
|
-
return result;
|
|
125
|
+
return success(result);
|
|
141
126
|
}
|
|
142
127
|
async findTopCategories(payload) {
|
|
143
128
|
const sdk = await this.client.getClient();
|
|
@@ -148,14 +133,7 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
148
133
|
offset: (payload.paginationOptions.pageNumber - 1) * payload.paginationOptions.pageSize
|
|
149
134
|
});
|
|
150
135
|
const result = this.parsePaginatedResult(response);
|
|
151
|
-
result
|
|
152
|
-
cache: {
|
|
153
|
-
hit: false,
|
|
154
|
-
key: this.generateCacheKeyPaginatedResult("top", result)
|
|
155
|
-
},
|
|
156
|
-
placeholder: false
|
|
157
|
-
};
|
|
158
|
-
return result;
|
|
136
|
+
return success(result);
|
|
159
137
|
}
|
|
160
138
|
parseSingle(_body) {
|
|
161
139
|
const identifier = CategoryIdentifierSchema.parse({
|
|
@@ -171,14 +149,7 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
171
149
|
slug,
|
|
172
150
|
text,
|
|
173
151
|
parentCategory,
|
|
174
|
-
images: []
|
|
175
|
-
meta: {
|
|
176
|
-
cache: {
|
|
177
|
-
hit: false,
|
|
178
|
-
key: ""
|
|
179
|
-
},
|
|
180
|
-
placeholder: false
|
|
181
|
-
}
|
|
152
|
+
images: []
|
|
182
153
|
};
|
|
183
154
|
return result;
|
|
184
155
|
}
|
|
@@ -188,17 +159,12 @@ class MedusaCategoryProvider extends CategoryProvider {
|
|
|
188
159
|
(body.count ?? 0) / Math.max(body.product_categories.length, 1)
|
|
189
160
|
);
|
|
190
161
|
const pageNumber = body.count === 0 ? 1 : Math.floor(body.offset / body.product_categories.length) + 1;
|
|
191
|
-
const meta = {
|
|
192
|
-
cache: { hit: false, key: "unknown" },
|
|
193
|
-
placeholder: false
|
|
194
|
-
};
|
|
195
162
|
const result = {
|
|
196
163
|
pageNumber,
|
|
197
164
|
pageSize: Math.max(body.product_categories.length, 1),
|
|
198
165
|
totalCount: body.count,
|
|
199
166
|
totalPages,
|
|
200
|
-
items
|
|
201
|
-
meta
|
|
167
|
+
items
|
|
202
168
|
};
|
|
203
169
|
return result;
|
|
204
170
|
}
|
|
@@ -26,7 +26,9 @@ import {
|
|
|
26
26
|
PaymentMethodSchema,
|
|
27
27
|
Reactionary,
|
|
28
28
|
ShippingMethodIdentifierSchema,
|
|
29
|
-
ShippingMethodSchema
|
|
29
|
+
ShippingMethodSchema,
|
|
30
|
+
success,
|
|
31
|
+
error
|
|
30
32
|
} from "@reactionary/core";
|
|
31
33
|
import createDebug from "debug";
|
|
32
34
|
import z from "zod";
|
|
@@ -77,14 +79,14 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
77
79
|
fields: this.includedFields
|
|
78
80
|
}
|
|
79
81
|
);
|
|
80
|
-
return this.parseSingle(response.cart);
|
|
82
|
+
return success(this.parseSingle(response.cart));
|
|
81
83
|
}
|
|
82
84
|
async getById(payload) {
|
|
83
85
|
const client = await this.client.getClient();
|
|
84
86
|
const response = await client.store.cart.retrieve(payload.identifier.key, {
|
|
85
87
|
fields: this.includedFields
|
|
86
88
|
});
|
|
87
|
-
return this.parseSingle(response.cart);
|
|
89
|
+
return success(this.parseSingle(response.cart));
|
|
88
90
|
}
|
|
89
91
|
async setShippingAddress(payload) {
|
|
90
92
|
const client = await this.client.getClient();
|
|
@@ -99,7 +101,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
99
101
|
fields: this.includedFields
|
|
100
102
|
}
|
|
101
103
|
);
|
|
102
|
-
return this.parseSingle(response.cart);
|
|
104
|
+
return success(this.parseSingle(response.cart));
|
|
103
105
|
}
|
|
104
106
|
async getAvailableShippingMethods(payload) {
|
|
105
107
|
const client = await this.client.getClient();
|
|
@@ -132,7 +134,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
132
134
|
shippingMethods
|
|
133
135
|
);
|
|
134
136
|
}
|
|
135
|
-
return shippingMethods;
|
|
137
|
+
return success(shippingMethods);
|
|
136
138
|
}
|
|
137
139
|
async getAvailablePaymentMethods(payload) {
|
|
138
140
|
const client = await this.client.getClient();
|
|
@@ -156,14 +158,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
156
158
|
},
|
|
157
159
|
logo: void 0,
|
|
158
160
|
description: pm.id,
|
|
159
|
-
isPunchOut: true
|
|
160
|
-
meta: {
|
|
161
|
-
cache: {
|
|
162
|
-
hit: false,
|
|
163
|
-
key: ""
|
|
164
|
-
},
|
|
165
|
-
placeholder: false
|
|
166
|
-
}
|
|
161
|
+
isPunchOut: true
|
|
167
162
|
}
|
|
168
163
|
);
|
|
169
164
|
}
|
|
@@ -173,7 +168,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
173
168
|
paymentMethods
|
|
174
169
|
);
|
|
175
170
|
}
|
|
176
|
-
return paymentMethods;
|
|
171
|
+
return success(paymentMethods);
|
|
177
172
|
}
|
|
178
173
|
async addPaymentInstruction(payload) {
|
|
179
174
|
const client = await this.client.getClient();
|
|
@@ -199,11 +194,11 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
199
194
|
fields: this.includedFields
|
|
200
195
|
}
|
|
201
196
|
);
|
|
202
|
-
return this.parseSingle(updatedCartResponse.cart);
|
|
203
|
-
} catch (
|
|
204
|
-
debug("Failed to add payment instruction: {0}", [
|
|
197
|
+
return success(this.parseSingle(updatedCartResponse.cart));
|
|
198
|
+
} catch (error2) {
|
|
199
|
+
debug("Failed to add payment instruction: {0}", [error2]);
|
|
205
200
|
throw new Error(
|
|
206
|
-
`Failed to add payment instruction: ${
|
|
201
|
+
`Failed to add payment instruction: ${error2 instanceof Error ? error2.message : "Unknown error"}`
|
|
207
202
|
);
|
|
208
203
|
}
|
|
209
204
|
}
|
|
@@ -234,16 +229,16 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
234
229
|
fields: this.includedFields
|
|
235
230
|
});
|
|
236
231
|
if (response.cart) {
|
|
237
|
-
return this.parseSingle(response.cart);
|
|
232
|
+
return success(this.parseSingle(response.cart));
|
|
238
233
|
}
|
|
239
234
|
throw new Error("Failed to set shipping method");
|
|
240
|
-
} catch (
|
|
241
|
-
handleProviderError("set shipping method",
|
|
235
|
+
} catch (error2) {
|
|
236
|
+
handleProviderError("set shipping method", error2);
|
|
242
237
|
}
|
|
243
238
|
}
|
|
244
239
|
async finalizeCheckout(payload) {
|
|
245
240
|
const checkout = await this.getById({ identifier: payload.checkout });
|
|
246
|
-
if (!checkout || !checkout.readyForFinalization) {
|
|
241
|
+
if (!checkout.success || !checkout.value.readyForFinalization) {
|
|
247
242
|
throw new CheckoutNotReadyForFinalizationError(payload.checkout);
|
|
248
243
|
}
|
|
249
244
|
const client = await this.client.getClient();
|
|
@@ -295,13 +290,6 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
295
290
|
city: storeAddress.city || "",
|
|
296
291
|
postalCode: storeAddress.postal_code || "",
|
|
297
292
|
countryCode: storeAddress.country_code || "",
|
|
298
|
-
meta: {
|
|
299
|
-
cache: {
|
|
300
|
-
hit: false,
|
|
301
|
-
key: ""
|
|
302
|
-
},
|
|
303
|
-
placeholder: false
|
|
304
|
-
},
|
|
305
293
|
region: ""
|
|
306
294
|
};
|
|
307
295
|
}
|
|
@@ -361,13 +349,6 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
361
349
|
for (const remoteItem of remote.items || []) {
|
|
362
350
|
items.push(this.parseCheckoutItem(remoteItem, price.grandTotal.currency));
|
|
363
351
|
}
|
|
364
|
-
const meta = {
|
|
365
|
-
cache: {
|
|
366
|
-
hit: false,
|
|
367
|
-
key: this.generateCacheKeySingle(identifier)
|
|
368
|
-
},
|
|
369
|
-
placeholder: false
|
|
370
|
-
};
|
|
371
352
|
const billingAddress = remote.billing_address ? this.composeAddressFromStoreAddress(remote.billing_address) : void 0;
|
|
372
353
|
const shippingAddress = remote.shipping_address ? this.composeAddressFromStoreAddress(remote.shipping_address) : void 0;
|
|
373
354
|
const backupUnattendedDelivery = remote.metadata?.["consent_for_unattended_delivery"] !== void 0 ? remote.metadata?.["consent_for_unattended_delivery"] === "true" : void 0;
|
|
@@ -396,8 +377,7 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
396
377
|
shippingMethod: { key: sm.shipping_option_id },
|
|
397
378
|
consentForUnattendedDelivery,
|
|
398
379
|
instructions,
|
|
399
|
-
pickupPoint
|
|
400
|
-
meta
|
|
380
|
+
pickupPoint
|
|
401
381
|
};
|
|
402
382
|
});
|
|
403
383
|
const paymentInstructions = new Array();
|
|
@@ -415,7 +395,6 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
415
395
|
};
|
|
416
396
|
paymentInstructions.push(
|
|
417
397
|
{
|
|
418
|
-
meta,
|
|
419
398
|
identifier: {
|
|
420
399
|
key: remotePayment.id
|
|
421
400
|
},
|
|
@@ -449,7 +428,6 @@ class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
449
428
|
description,
|
|
450
429
|
price,
|
|
451
430
|
items,
|
|
452
|
-
meta,
|
|
453
431
|
originalCartReference,
|
|
454
432
|
paymentInstructions,
|
|
455
433
|
readyForFinalization: false,
|