1 import { HttpTypes, SelectParams } from "@medusajs/types"
2
3 import { Client } from "../client.js"
4 import { ClientHeaders } from "../types.js"
5
6 export class Exchange {
7 /**
8 * @ignore
9 */
10 private client: Client
11 /**
12 * @ignore
13 */
14 constructor(client: Client) {
15 this.client = client
16 }
17
18 /**
19 * This method retrieves a paginated list of exchanges. It sends a request to the
20 * [List Exchanges](https://docs.medusajs.com/api/admin/exchanges/list-exchanges)
21 * API route.
22 *
23 * @param query - Filters and pagination configurations.
24 * @param headers - Headers to pass in the request.
25 * @returns The paginated list of exchanges.
26 *
27 * @example
28 * To retrieve the list of exchanges:
29 *
30 * ```ts
31 * sdk.admin.exchange.list()
32 * .then(({ exchanges, count, limit, offset }) => {
33 * console.log(exchanges)
34 * })
35 * ```
36 *
37 * To configure the pagination, pass the `limit` and `offset` query parameters.
38 *
39 * For example, to retrieve only 10 items and skip 10 items:
40 *
41 * ```ts
42 * sdk.admin.exchange.list({
43 * limit: 10,
44 * offset: 10
45 * })
46 * .then(({ exchanges, count, limit, offset }) => {
47 * console.log(exchanges)
48 * })
49 * ```
50 *
51 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
52 * in each exchange:
53 *
54 * ```ts
55 * sdk.admin.exchange.list({
56 * fields: "id,*order"
57 * })
58 * .then(({ exchanges, count, limit, offset }) => {
59 * console.log(exchanges)
60 * })
61 * ```
62 *
63 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
64 */
65 async list(
66 query?: HttpTypes.AdminExchangeListParams,
67 headers?: ClientHeaders
68 ) {
69 return await this.client.fetch<HttpTypes.AdminExchangeListResponse>(
70 `/admin/exchanges`,
71 {
72 query,
73 headers,
74 }
75 )
76 }
77
78 /**
79 * This method retrieves an exchange by its ID. It sends a request to the
80 * [Get Exchange](https://docs.medusajs.com/api/admin/exchanges/get-an-exchange)
81 * API route.
82 *
83 * @param id - The exchange's ID.
84 * @param query - Configure the fields to retrieve in the exchange.
85 * @param headers - Headers to pass in the request.
86 * @returns The exchange's details.
87 *
88 * @example
89 * To retrieve an exchange by its ID:
90 *
91 * ```ts
92 * sdk.admin.exchange.retrieve("exchange_123")
93 * .then(({ exchange }) => {
94 * console.log(exchange)
95 * })
96 * ```
97 *
98 * To specify the fields and relations to retrieve:
99 *
100 * ```ts
101 * sdk.admin.exchange.retrieve("exchange_123", {
102 * fields: "id,*order"
103 * })
104 * .then(({ exchange }) => {
105 * console.log(exchange)
106 * })
107 * ```
108 *
109 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
110 */
111 async retrieve(
112 id: string,
113 query?: SelectParams,
114 headers?: ClientHeaders
115 ) {
116 return await this.client.fetch<HttpTypes.AdminExchangeResponse>(
117 `/admin/exchanges/${id}`,
118 {
119 query,
120 headers,
121 }
122 )
123 }
124
125 /**
126 * This method creates an admin exchange. It sends a request to the
127 * [Create Exchange](https://docs.medusajs.com/api/admin/exchanges/create-exchange) API route.
128 *
129 * @param body - The exchange's details.
130 * @param query - Configure the fields to retrieve in the exchange.
131 * @param headers - Headers to pass in the request.
132 * @returns The exchange's details.
133 *
134 * @example
135 * sdk.admin.exchange.create({
136 * order_id: "order_123"
137 * })
138 * .then(({ exchange }) => {
139 * console.log(exchange)
140 * })
141 */
142 async create(
143 body: HttpTypes.AdminCreateExchange,
144 query?: HttpTypes.SelectParams,
145 headers?: ClientHeaders
146 ) {
147 return await this.client.fetch<HttpTypes.AdminExchangeResponse>(
148 `/admin/exchanges`,
149 {
150 method: "POST",
151 headers,
152 body,
153 query,
154 }
155 )
156 }
157
158 /**
159 * This method cancels an exchange. It sends a request to the
160 * [Cancel Exchange](https://docs.medusajs.com/api/admin/exchanges/cancel-an-exchange) API route.
161 *
162 * @param id - The exchange's ID.
163 * @param query - Configure the fields to retrieve in the exchange.
164 * @param headers - Headers to pass in the request.
165 * @returns The exchange's details.
166 *
167 * @example
168 * sdk.admin.exchange.cancel("exchange_123")
169 * .then(({ exchange }) => {
170 * console.log(exchange)
171 * })
172 */
173 async cancel(
174 id: string,
175 query?: HttpTypes.SelectParams,
176 headers?: ClientHeaders
177 ) {
178 return await this.client.fetch<HttpTypes.AdminExchangeResponse>(
179 `/admin/exchanges/${id}/cancel`,
180 {
181 method: "POST",
182 headers,
183 query,
184 }
185 )
186 }
187
188 /**
189 * This method adds inbound (or return) items to an exchange. These inbound items will
190 * have the action `RETURN_ITEM`.
191 *
192 * This method sends a request to the [Add Inbound Items](https://docs.medusajs.com/api/admin/exchanges/add-inbound-items-to-an-exchange)
193 * API route.
194 *
195 * @param id - The exchange's ID.
196 * @param body - The items to add.
197 * @param query - Configure the fields to retrieve in the return.
198 * @param headers - Headers to pass in the request.
199 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
200 *
201 * @example
202 * sdk.admin.exchange.addInboundItems("exchange_123", {
203 * items: [{
204 * id: "orli_123",
205 * quantity: 1
206 * }]
207 * })
208 * .then(({ return: returnData }) => {
209 * console.log(returnData)
210 * })
211 */
212 async addInboundItems(
213 id: string,
214 body: HttpTypes.AdminAddExchangeInboundItems,
215 query?: HttpTypes.SelectParams,
216 headers?: ClientHeaders
217 ) {
218 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
219 `/admin/exchanges/${id}/inbound/items`,
220 {
221 method: "POST",
222 headers,
223 body,
224 query,
225 }
226 )
227 }
228
229 /**
230 * This method updates an inbound (or return) item from an exchange using the ID of
231 * the item's `RETURN_ITEM` action.
232 *
233 * Every item has an `actions` property, whose value is an array of actions. You can
234 * check the action's name using its `action` property, and use the value of the `id` property.
235 *
236 * This method sends a request to the [Update Inbound Item](https://docs.medusajs.com/api/admin/exchanges/update-inbound-item)
237 * API route.
238 *
239 * @param id - The exchange's ID.
240 * @param actionId - The id of the return item's `RETURN_ITEM` action.
241 * @param body - The details to update.
242 * @param query - Configure the fields to retrieve in the return.
243 * @param headers - Headers to pass in the request.
244 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
245 *
246 * @example
247 * sdk.admin.exchange.updateInboundItem(
248 * "exchange_123",
249 * "ordchact_123",
250 * {
251 * quantity: 1
252 * }
253 * )
254 * .then(({ return: returnData }) => {
255 * console.log(returnData)
256 * })
257 */
258 async updateInboundItem(
259 id: string,
260 actionId: string,
261 body: HttpTypes.AdminUpdateExchangeInboundItem,
262 query?: HttpTypes.SelectParams,
263 headers?: ClientHeaders
264 ) {
265 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
266 `/admin/exchanges/${id}/inbound/items/${actionId}`,
267 {
268 method: "POST",
269 headers,
270 body,
271 query,
272 }
273 )
274 }
275
276 /**
277 * This method removes an inbound (or return) item from an exchange using the ID of the
278 * item's `RETURN_ITEM` action.
279 *
280 * Every item has an `actions` property, whose value is an array of actions.
281 * You can check the action's name using its `action` property, and use the value of the `id` property.
282 *
283 * This method sends a request to the [Remove Inbound Item](https://docs.medusajs.com/api/admin/exchanges/remove-inbound-item)
284 * API route.
285 *
286 * @param id - The exchange's ID.
287 * @param actionId - The id of the return item's `RETURN_ITEM` action.
288 * @param query - Configure the fields to retrieve in the return.
289 * @param headers - Headers to pass in the request.
290 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
291 *
292 * @example
293 * sdk.admin.exchange.removeInboundItem(
294 * "exchange_123",
295 * "ordchact_123",
296 * )
297 * .then(({ return: returnData }) => {
298 * console.log(returnData)
299 * })
300 */
301 async removeInboundItem(
302 id: string,
303 actionId: string,
304 query?: HttpTypes.SelectParams,
305 headers?: ClientHeaders
306 ) {
307 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
308 `/admin/exchanges/${id}/inbound/items/${actionId}`,
309 {
310 method: "DELETE",
311 headers,
312 query,
313 }
314 )
315 }
316
317 /**
318 * This method adds an inbound (or return) shipping method to an exchange.
319 * The inbound shipping method will have a `SHIPPING_ADD` action.
320 *
321 * This method sends a request to the [Add Inbound Shipping](https://docs.medusajs.com/api/admin/exchanges/add-inbound-shipping)
322 * API route.
323 *
324 * This method sends a request to the [Add Inbound Shipping](https://docs.medusajs.com/api/admin/exchanges/add-inbound-shipping)
325 * API route.
326 *
327 * @param id - The exchange's ID.
328 * @param body - The shipping method's details.
329 * @param query - Configure the fields to retrieve in the return.
330 * @param headers - Headers to pass in the request.
331 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
332 *
333 * @example
334 * sdk.admin.exchange.addInboundShipping("exchange_123", {
335 * shipping_option_id: "so_123"
336 * })
337 * .then(({ return: returnData }) => {
338 * console.log(returnData)
339 * })
340 */
341 async addInboundShipping(
342 id: string,
343 body: HttpTypes.AdminExchangeAddInboundShipping,
344 query?: HttpTypes.SelectParams,
345 headers?: ClientHeaders
346 ) {
347 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
348 `/admin/exchanges/${id}/inbound/shipping-method`,
349 {
350 method: "POST",
351 headers,
352 body,
353 query,
354 }
355 )
356 }
357
358 /**
359 * This method updates the shipping method for returning items in the exchange using the ID
360 * of the method's `SHIPPING_ADD` action.
361 *
362 * Every shipping method has an `actions` property, whose value is an array of actions.
363 * You can check the action's name using its `action` property, and use the value of the `id` property.
364 *
365 * This method sends a request to the [Update Inbound Shipping](https://docs.medusajs.com/api/admin/exchanges/update-inbound-shipping)
366 * API route.
367 *
368 * @param id - The exchange's ID.
369 * @param actionId - The id of the shipping method's `SHIPPING_ADD` action.
370 * @param body - The details to update.
371 * @param query - Configure the fields to retrieve in the return.
372 * @param headers - Headers to pass in the request.
373 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
374 *
375 * @example
376 * sdk.admin.exchange.updateInboundShipping(
377 * "exchange_123",
378 * "ordchact_123",
379 * {
380 * custom_amount: 10
381 * }
382 * )
383 * .then(({ return: returnData }) => {
384 * console.log(returnData)
385 * })
386 */
387 async updateInboundShipping(
388 id: string,
389 actionId: string,
390 body: HttpTypes.AdminExchangeUpdateInboundShipping,
391 query?: HttpTypes.SelectParams,
392 headers?: ClientHeaders
393 ) {
394 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
395 `/admin/exchanges/${id}/inbound/shipping-method/${actionId}`,
396 {
397 method: "POST",
398 headers,
399 body,
400 query,
401 }
402 )
403 }
404
405 /**
406 * This method removes the shipping method for returning items in the exchange using the ID
407 * of the method's `SHIPPING_ADD` action.
408 *
409 * Every shipping method has an `actions` property, whose value is an array of actions.
410 * You can check the action's name using its `action` property, and use the value of the `id` property.
411 *
412 * This method sends a request to the [Remove Inbound Shipping](https://docs.medusajs.com/api/admin/exchanges/remove-inbound-shipping-method)
413 * API route.
414 *
415 * @param id - The exchange's ID.
416 * @param actionId - The id of the shipping method's `SHIPPING_ADD` action.
417 * @param query - Configure the fields to retrieve in the return.
418 * @param headers - Headers to pass in the request.
419 * @returns The details of the return associated with the exchange, with a preview of the order when the exchange is applied.
420 *
421 * @example
422 * sdk.admin.exchange.deleteInboundShipping(
423 * "exchange_123",
424 * "ordchact_123",
425 * )
426 * .then(({ return: returnData }) => {
427 * console.log(returnData)
428 * })
429 */
430 async deleteInboundShipping(
431 id: string,
432 actionId: string,
433 query?: HttpTypes.SelectParams,
434 headers?: ClientHeaders
435 ) {
436 return await this.client.fetch<HttpTypes.AdminExchangeReturnResponse>(
437 `/admin/exchanges/${id}/inbound/shipping-method/${actionId}`,
438 {
439 method: "DELETE",
440 headers,
441 query,
442 }
443 )
444 }
445
446 /**
447 * This method adds outbound (or new) items to an exchange.
448 * These outbound items will have the action `ITEM_ADD`.
449 *
450 * This method sends a request to the [Add Outbound Items](https://docs.medusajs.com/api/admin/exchanges/add-outbound-items-to-exchange)
451 * API route.
452 *
453 * @param id - The exchange's ID.
454 * @param body - The items to add.
455 * @param query - Configure the fields to retrieve in the exchange.
456 * @param headers - Headers to pass in the request
457 * @returns The exchange's details with a preview of the order when the exchange is applied.
458 *
459 * @example
460 * sdk.admin.exchange.addOutboundItems("exchange_123", {
461 * items: [{
462 * id: "variant_123",
463 * quantity: 1
464 * }]
465 * })
466 * .then(({ exchange }) => {
467 * console.log(exchange)
468 * })
469 */
470 async addOutboundItems(
471 id: string,
472 body: HttpTypes.AdminAddExchangeOutboundItems,
473 query?: HttpTypes.SelectParams,
474 headers?: ClientHeaders
475 ) {
476 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
477 `/admin/exchanges/${id}/outbound/items`,
478 {
479 method: "POST",
480 headers,
481 body,
482 query,
483 }
484 )
485 }
486
487 /**
488 * This method updates an outbound (or new) item from an exchange using the ID
489 * of the item's `ITEM_ADD` action.
490 *
491 * Every item has an `actions` property, whose value is an array of actions.
492 * You can check the action's name using its `action` property, and use the value of the `id` property.
493 *
494 * This method sends a request to the [Update Inbound Item](https://docs.medusajs.com/api/admin/exchanges/update-outbound-item)
495 * API route.
496 *
497 * @param id - The exchange's ID.
498 * @param actionId - The id of the new exchange item's `ITEM_ADD` action.
499 * @param body - The item's details to update.
500 * @param query - Configure the fields to retrieve in the exchange.
501 * @param headers - Headers to pass in the request
502 * @returns The exchange's details with a preview of the order when the exchange is applied.
503 *
504 * @example
505 * sdk.admin.exchange.updateOutboundItem(
506 * "exchange_123",
507 * "ordchact_123",
508 * {
509 * quantity: 1
510 * }
511 * )
512 * .then(({ exchange }) => {
513 * console.log(exchange)
514 * })
515 */
516 async updateOutboundItem(
517 id: string,
518 actionId: string,
519 body: HttpTypes.AdminUpdateExchangeOutboundItem,
520 query?: HttpTypes.SelectParams,
521 headers?: ClientHeaders
522 ) {
523 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
524 `/admin/exchanges/${id}/outbound/items/${actionId}`,
525 {
526 method: "POST",
527 headers,
528 body,
529 query,
530 }
531 )
532 }
533
534 /**
535 * This method removes an outbound (or new) item from an exchange using the ID
536 * of the item's `ITEM_ADD` action.
537 *
538 * Every item has an `actions` property, whose value is an array of actions.
539 * You can check the action's name using its `action` property, and use the value of the `id` property.
540 *
541 * This method sends a request to the [Update Outbound Item](https://docs.medusajs.com/api/admin/exchanges/remove-outbound-item)
542 * API route.
543 *
544 * @param id - The exchange's ID.
545 * @param actionId - The id of the new exchange item's `ITEM_ADD` action.
546 * @param query - Configure the fields to retrieve in the exchange.
547 * @param headers - Headers to pass in the request
548 * @returns The exchange's details with a preview of the order when the exchange is applied.
549 *
550 * @example
551 * sdk.admin.exchange.removeOutboundItem(
552 * "exchange_123",
553 * "ordchact_123",
554 * )
555 * .then(({ exchange }) => {
556 * console.log(exchange)
557 * })
558 */
559 async removeOutboundItem(
560 id: string,
561 actionId: string,
562 query?: HttpTypes.SelectParams,
563 headers?: ClientHeaders
564 ) {
565 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
566 `/admin/exchanges/${id}/outbound/items/${actionId}`,
567 {
568 method: "DELETE",
569 headers,
570 query,
571 }
572 )
573 }
574
575 /**
576 * This method adds an outbound shipping method to an exchange. The outbound shipping method
577 * will have a `SHIPPING_ADD` action.
578 *
579 * This method sends a request to the [Add Outbound Shipping](https://docs.medusajs.com/api/admin/exchanges/add-outbound-shipping)
580 * API route.
581 *
582 * @param id - The exchange's ID.
583 * @param body - The shipping method's details.
584 * @param query - Configure the fields to retrieve in the exchange.
585 * @param headers - Headers to pass in the request
586 * @returns The exchange's details with a preview of the order when the exchange is applied.
587 *
588 * @example
589 * sdk.admin.exchange.addOutboundShipping("exchange_123", {
590 * shipping_option_id: "so_123"
591 * })
592 * .then(({ exchange }) => {
593 * console.log(exchange)
594 * })
595 */
596 async addOutboundShipping(
597 id: string,
598 body: HttpTypes.AdminExchangeAddOutboundShipping,
599 query?: HttpTypes.SelectParams,
600 headers?: ClientHeaders
601 ) {
602 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
603 `/admin/exchanges/${id}/outbound/shipping-method`,
604 {
605 method: "POST",
606 headers,
607 body,
608 query,
609 }
610 )
611 }
612
613 /**
614 * This method updates the shipping method for delivering outbound items in the exchange using
615 * the ID of the method's `SHIPPING_ADD` action.
616 *
617 * Every shipping method has an `actions` property, whose value is an array of actions.
618 * You can check the action's name using its `action` property, and use the value of the `id` property.
619 *
620 * This method sends a request to the [Update Outbound Shipping](https://docs.medusajs.com/api/admin/exchanges/update-outbound-shipping)
621 * API route.
622 *
623 * @param id - The exchange's ID.
624 * @param actionId - The id of the shipping method's `SHIPPING_ADD` action.
625 * @param body - The details to update.
626 * @param query - Configure the fields to retrieve in the exchange.
627 * @param headers - Headers to pass in the request
628 * @returns The exchange's details with a preview of the order when the exchange is applied.
629 *
630 * @example
631 * sdk.admin.exchange.updateOutboundShipping(
632 * "exchange_123",
633 * "ordchact_123",
634 * {
635 * custom_amount: 10
636 * }
637 * )
638 * .then(({ exchange }) => {
639 * console.log(exchange)
640 * })
641 */
642 async updateOutboundShipping(
643 id: string,
644 actionId: string,
645 body: HttpTypes.AdminExchangeUpdateOutboundShipping,
646 query?: HttpTypes.SelectParams,
647 headers?: ClientHeaders
648 ) {
649 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
650 `/admin/exchanges/${id}/outbound/shipping-method/${actionId}`,
651 {
652 method: "POST",
653 headers,
654 body,
655 query,
656 }
657 )
658 }
659
660 /**
661 * This method removes the shipping method for delivering outbound items in the exchange using
662 * the ID of the method's `SHIPPING_ADD` action.
663 *
664 * Every shipping method has an `actions` property, whose value is an array of actions.
665 * You can check the action's name using its `action` property, and use the value of the `id` property.
666 *
667 * This method sends a request to the [Remove Outbound Shipping](https://docs.medusajs.com/api/admin/exchanges/remove-outbound-shipping-method)
668 * API route.
669 *
670 * @param id - The exchange's ID.
671 * @param actionId - The id of the shipping method's `SHIPPING_ADD` action.
672 * @param query - Configure the fields to retrieve in the exchange.
673 * @param headers - Headers to pass in the request
674 * @returns The exchange's details with a preview of the order when the exchange is applied.
675 *
676 * @example
677 * sdk.admin.exchange.deleteOutboundShipping(
678 * "exchange_123",
679 * "ordchact_123",
680 * )
681 * .then(({ exchange }) => {
682 * console.log(exchange)
683 * })
684 */
685 async deleteOutboundShipping(
686 id: string,
687 actionId: string,
688 query?: HttpTypes.SelectParams,
689 headers?: ClientHeaders
690 ) {
691 return await this.client.fetch<HttpTypes.AdminExchangePreviewResponse>(
692 `/admin/exchanges/${id}/outbound/shipping-method/${actionId}`,
693 {
694 method: "DELETE",
695 headers,
696 query,
697 }
698 )
699 }
700
701 /**
702 * This method confirms an exchange request, applying its changes on the associated order.
703 *
704 * This method sends a request to the [Confirm Exchange](https://docs.medusajs.com/api/admin/exchanges/confirm-an-exchange)
705 * API route.
706 *
707 * @param id - The exchange's ID.
708 * @param body - The confirmation's details.
709 * @param query - Configure the fields to retrieve in the exchange.
710 * @param headers - Headers to pass in the request
711 * @returns The exchange and associated return's details with a preview of the order when the exchange is applied.
712 *
713 * @example
714 * sdk.admin.exchange.request("exchange_123", {})
715 * .then(({ exchange }) => {
716 * console.log(exchange)
717 * })
718 */
719 async request(
720 id: string,
721 body: HttpTypes.AdminRequestExchange,
722 query?: HttpTypes.SelectParams,
723 headers?: ClientHeaders
724 ) {
725 return await this.client.fetch<HttpTypes.AdminExchangeRequestResponse>(
726 `/admin/exchanges/${id}/request`,
727 {
728 method: "POST",
729 headers,
730 body,
731 query,
732 }
733 )
734 }
735
736 /**
737 * This method cancels an exchange request. It sends a request to the
738 * [Cancel Exchange Request](https://docs.medusajs.com/api/admin/exchanges/cancel-exchange-request)
739 * API route.
740 *
741 * @param id - The exchange's ID.
742 * @param query - Configure the fields to retrieve in the exchange.
743 * @param headers - Headers to pass in the request
744 * @returns The cancelation's details.
745 *
746 * @example
747 * sdk.admin.exchange.cancel("exchange_123")
748 * .then(({ deleted }) => {
749 * console.log(deleted)
750 * })
751 */
752 async cancelRequest(
753 id: string,
754 query?: HttpTypes.SelectParams,
755 headers?: ClientHeaders
756 ) {
757 return await this.client.fetch<HttpTypes.AdminExchangeDeleteResponse>(
758 `/admin/exchanges/${id}/request`,
759 {
760 method: "DELETE",
761 headers,
762 query,
763 }
764 )
765 }
766 }