1 import { HttpTypes } from "@medusajs/types"
2
3 import { Client } from "../client.js"
4 import { ClientHeaders } from "../types.js"
5
6 export class DraftOrder {
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 draft order by its ID. It sends a request to the
20 * [Get Draft Order](https://docs.medusajs.com/api/admin/draft-orders/get-a-draft-order)
21 * API route.
22 *
23 * @param id - The draft order's ID.
24 * @param query - Configure the fields to retrieve in the draft order.
25 * @param headers - Headers to pass in the request
26 * @returns The draft order's details.
27 *
28 * @example
29 * To retrieve a draft order by its ID:
30 *
31 * ```ts
32 * sdk.admin.draftOrder.retrieve("order_123")
33 * .then(({ draft_order }) => {
34 * console.log(draft_order)
35 * })
36 * ```
37 *
38 * To specify the fields and relations to retrieve:
39 *
40 * ```ts
41 * sdk.admin.draftOrder.retrieve("order_123", {
42 * fields: "id,*items"
43 * })
44 * .then(({ draft_order }) => {
45 * console.log(draft_order)
46 * })
47 * ```
48 *
49 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
50 */
51 async retrieve(
52 id: string,
53 query?: HttpTypes.AdminDraftOrderParams,
54 headers?: ClientHeaders
55 ) {
56 return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
57 `/admin/draft-orders/${id}`,
58 {
59 query,
60 headers,
61 }
62 )
63 }
64
65 /**
66 * This method retrieves a paginated list of draft orders. It sends a request to the
67 * [List Draft Orders](https://docs.medusajs.com/api/admin/draft-orders/list-draft-orders) API route.
68 *
69 * @param queryParams - Filters and pagination configurations.
70 * @param headers - Headers to pass in the request.
71 * @returns The paginated list of draft orders.
72 *
73 * @example
74 * To retrieve the list of draft orders:
75 *
76 * ```ts
77 * sdk.admin.draftOrder.list()
78 * .then(({ draft_orders, count, limit, offset }) => {
79 * console.log(draft_orders)
80 * })
81 * ```
82 *
83 * To configure the pagination, pass the `limit` and `offset` query parameters.
84 *
85 * For example, to retrieve only 10 items and skip 10 items:
86 *
87 * ```ts
88 * sdk.admin.draftOrder.list({
89 * limit: 10,
90 * offset: 10
91 * })
92 * .then(({ draft_orders, count, limit, offset }) => {
93 * console.log(draft_orders)
94 * })
95 * ```
96 *
97 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
98 * in each draft order:
99 *
100 * ```ts
101 * sdk.admin.draftOrder.list({
102 * fields: "id,*items"
103 * })
104 * .then(({ draft_orders, count, limit, offset }) => {
105 * console.log(draft_orders)
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 list(
112 queryParams?: HttpTypes.AdminDraftOrderListParams,
113 headers?: ClientHeaders
114 ) {
115 return await this.client.fetch<HttpTypes.AdminDraftOrderListResponse>(
116 `/admin/draft-orders`,
117 {
118 query: queryParams,
119 headers,
120 }
121 )
122 }
123
124 /**
125 * This method creates a draft order. It sends a request to the
126 * [Create Draft Order](https://docs.medusajs.com/api/admin/draft-orders/create-draft-order) API route.
127 *
128 * @param body - The data to create the draft order.
129 * @param query - Configure the fields to retrieve in the draft order.
130 * @param headers - Headers to pass in the request.
131 * @returns The draft order's details.
132 *
133 * @example
134 * sdk.admin.draftOrder.create({
135 * email: "test@test.com",
136 * items: [
137 * {
138 * variant_id: "variant_123",
139 * quantity: 1,
140 * },
141 * ],
142 * region_id: "region_123",
143 * sales_channel_id: "sc_123",
144 * })
145 * .then(({ draft_order }) => {
146 * console.log(draft_order)
147 * })
148 */
149 async create(
150 body: HttpTypes.AdminCreateDraftOrder,
151 query?: HttpTypes.AdminDraftOrderParams,
152 headers?: ClientHeaders
153 ) {
154 return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
155 `/admin/draft-orders`,
156 {
157 method: "POST",
158 body,
159 query,
160 headers,
161 }
162 )
163 }
164
165 /**
166 * This method deletes a draft order. It sends a request to the
167 * [Delete Draft Order](https://docs.medusajs.com/api/admin/draft-orders/delete-a-draft-order) API route.
168 *
169 * @param id - The draft order's ID.
170 * @param headers - Headers to pass in the request.
171 *
172 * @example
173 * sdk.admin.draftOrder.delete("order_123")
174 * .then(({ id, object, deleted }) => {
175 * console.log(id, object, deleted)
176 * })
177 */
178 async delete(id: string, headers?: ClientHeaders) {
179 return await this.client.fetch<HttpTypes.DeleteResponse<"draft-order">>(
180 `/admin/draft-orders/${id}`,
181 {
182 method: "DELETE",
183 headers,
184 }
185 )
186 }
187
188 /**
189 * This method updates a draft order. It sends a request to the
190 * [Update Draft Order](https://docs.medusajs.com/api/admin/draft-orders/update-a-draft-order) API route.
191 *
192 * @param id - The draft order's ID.
193 * @param body - The data to update the draft order.
194 * @param query - Configure the fields to retrieve in the draft order.
195 * @param headers - Headers to pass in the request.
196 * @returns The draft order's details.
197 *
198 * @example
199 * sdk.admin.draftOrder.update("order_123", {
200 * email: "test@test.com",
201 * })
202 * .then(({ draft_order }) => {
203 * console.log(draft_order)
204 * })
205 */
206 async update(
207 id: string,
208 body: HttpTypes.AdminUpdateDraftOrder,
209 query?: HttpTypes.AdminDraftOrderParams,
210 headers?: ClientHeaders
211 ) {
212 return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
213 `/admin/draft-orders/${id}`,
214 {
215 method: "POST",
216 body,
217 query,
218 headers,
219 }
220 )
221 }
222
223 /**
224 * This method converts a draft order to an order. It sends a request to the
225 * [Convert Draft Order to Order](https://docs.medusajs.com/api/admin/draft-orders/convert-to-order) API route.
226 *
227 * @param id - The draft order's ID.
228 * @param query - Configure the fields to retrieve in the order.
229 * @param headers - Headers to pass in the request.
230 * @returns The order's details.
231 *
232 * @example
233 * sdk.admin.draftOrder.convertToOrder("order_123")
234 * .then(({ order }) => {
235 * console.log(order)
236 * })
237 */
238 async convertToOrder(
239 id: string,
240 query?: HttpTypes.AdminDraftOrderParams,
241 headers?: ClientHeaders
242 ) {
243 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
244 `/admin/draft-orders/${id}/convert-to-order`,
245 {
246 method: "POST",
247 query,
248 headers,
249 }
250 )
251 }
252
253 /**
254 * This method adds items to a draft order. It sends a request to the
255 * [Add Draft Order Items](https://docs.medusajs.com/api/admin/draft-orders/add-item) API route.
256 *
257 * @param id - The draft order's ID.
258 * @param body - The data to add the items to the draft order.
259 * @param headers - Headers to pass in the request.
260 * @returns The draft order preview's details.
261 *
262 * @example
263 * sdk.admin.draftOrder.addItems("order_123", {
264 * items: [
265 * {
266 * variant_id: "variant_123",
267 * quantity: 1,
268 * },
269 * ],
270 * })
271 * .then(({ draft_order_preview }) => {
272 * console.log(draft_order_preview)
273 * })
274 */
275 async addItems(
276 id: string,
277 body: HttpTypes.AdminAddDraftOrderItems,
278 headers?: ClientHeaders
279 ) {
280 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
281 `/admin/draft-orders/${id}/edit/items`,
282 {
283 method: "POST",
284 body,
285 headers,
286 }
287 )
288 }
289
290 /**
291 * This method updates an item that is part of an action in a draft order. It sends a request to the
292 * [Update Draft Order Item](https://docs.medusajs.com/api/admin/draft-orders/update-new-item) API route.
293 *
294 * @param id - The draft order's ID.
295 * @param actionId - The action ID.
296 * @param body - The data to update the item.
297 * @param headers - Headers to pass in the request.
298 * @returns The draft order preview's details.
299 *
300 * @example
301 * sdk.admin.draftOrder.updateActionItem("order_123", "action_123", {
302 * quantity: 2,
303 * })
304 * .then(({ draft_order_preview }) => {
305 * console.log(draft_order_preview)
306 * })
307 */
308 async updateActionItem(
309 id: string,
310 actionId: string,
311 body: HttpTypes.AdminUpdateDraftOrderItem,
312 headers?: ClientHeaders
313 ) {
314 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
315 `/admin/draft-orders/${id}/edit/items/${actionId}`,
316 {
317 method: "POST",
318 body,
319 headers,
320 }
321 )
322 }
323
324 /**
325 * This method removes an item that is part of an action in a draft order. It sends a request to the
326 * [Remove Draft Order Item](https://docs.medusajs.com/api/admin/draft-orders/remove-item) API route.
327 *
328 * @param id - The draft order's ID.
329 * @param actionId - The action ID.
330 * @param headers - Headers to pass in the request.
331 * @returns The draft order preview's details.
332 *
333 * @example
334 * sdk.admin.draftOrder.removeActionItem("order_123", "action_123")
335 * .then(({ draft_order_preview }) => {
336 * console.log(draft_order_preview)
337 * })
338 */
339 async removeActionItem(
340 id: string,
341 actionId: string,
342 headers?: ClientHeaders
343 ) {
344 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
345 `/admin/draft-orders/${id}/edit/items/${actionId}`,
346 {
347 method: "DELETE",
348 headers,
349 }
350 )
351 }
352
353 /**
354 * This method updates an item in a draft order. It sends a request to the
355 * [Update Draft Order Item](https://docs.medusajs.com/api/admin/draft-orders/update-item) API route.
356 *
357 * @param id - The draft order's ID.
358 * @param itemId - The item ID.
359 * @param body - The data to update the item.
360 * @param headers - Headers to pass in the request.
361 * @returns The draft order preview's details.
362 *
363 * @example
364 * sdk.admin.draftOrder.updateItem("order_123", "item_123", {
365 * quantity: 2,
366 * })
367 * .then(({ draft_order_preview }) => {
368 * console.log(draft_order_preview)
369 * })
370 */
371 async updateItem(
372 id: string,
373 itemId: string,
374 body: HttpTypes.AdminUpdateDraftOrderItem,
375 headers?: ClientHeaders
376 ) {
377 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
378 `/admin/draft-orders/${id}/edit/items/item/${itemId}`,
379 {
380 method: "POST",
381 body,
382 headers,
383 }
384 )
385 }
386
387 /**
388 * This method adds promotions to a draft order. It sends a request to the
389 * [Add Draft Order Promotions](https://docs.medusajs.com/api/admin/draft-orders/add-promotions) API route.
390 *
391 * @param id - The draft order's ID.
392 * @param body - The data to add the promotions to the draft order.
393 * @param headers - Headers to pass in the request.
394 * @returns The draft order preview's details.
395 *
396 * @example
397 * sdk.admin.draftOrder.addPromotions("order_123", {
398 * promo_codes: ["PROMO_CODE_1", "PROMO_CODE_2"],
399 * })
400 * .then(({ draft_order_preview }) => {
401 * console.log(draft_order_preview)
402 * })
403 */
404 async addPromotions(
405 id: string,
406 body: HttpTypes.AdminAddDraftOrderPromotions,
407 headers?: ClientHeaders
408 ) {
409 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
410 `/admin/draft-orders/${id}/edit/promotions`,
411 {
412 method: "POST",
413 body,
414 headers,
415 }
416 )
417 }
418
419 /**
420 * This method removes promotions from a draft order. It sends a request to the
421 * [Remove Draft Order Promotions](https://docs.medusajs.com/api/admin/draft-orders/remove-promotions) API route.
422 *
423 * @param id - The draft order's ID.
424 * @param body - The data to remove the promotions from the draft order.
425 * @param headers - Headers to pass in the request.
426 * @returns The draft order preview's details.
427 *
428 * @example
429 * sdk.admin.draftOrder.removePromotions("order_123", {
430 * promo_codes: ["PROMO_CODE_1", "PROMO_CODE_2"],
431 * })
432 */
433 async removePromotions(
434 id: string,
435 body: HttpTypes.AdminRemoveDraftOrderPromotions,
436 headers?: ClientHeaders
437 ) {
438 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
439 `/admin/draft-orders/${id}/edit/promotions`,
440 {
441 method: "DELETE",
442 body,
443 headers,
444 }
445 )
446 }
447
448 /**
449 * This method adds a shipping method to a draft order. It sends a request to the
450 * [Add Draft Order Shipping Method](https://docs.medusajs.com/api/admin/draft-orders/add-shipping-method) API route.
451 *
452 * @param id - The draft order's ID.
453 * @param body - The data to add the shipping method to the draft order.
454 * @param headers - Headers to pass in the request.
455 * @returns The draft order preview's details.
456 *
457 * @example
458 * sdk.admin.draftOrder.addShippingMethod("order_123", {
459 * shipping_option_id: "shipping_option_123",
460 * })
461 * .then(({ draft_order_preview }) => {
462 * console.log(draft_order_preview)
463 * })
464 */
465 async addShippingMethod(
466 id: string,
467 body: HttpTypes.AdminAddDraftOrderShippingMethod,
468 headers?: ClientHeaders
469 ) {
470 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
471 `/admin/draft-orders/${id}/edit/shipping-methods`,
472 {
473 method: "POST",
474 body,
475 headers,
476 }
477 )
478 }
479
480 /**
481 * This method updates a shipping method in a draft order. It sends a request to the
482 * [Update Draft Order Shipping Method](https://docs.medusajs.com/api/admin/draft-orders/update-new-shipping-method) API route.
483 *
484 * @param id - The draft order's ID.
485 * @param actionId - The action ID.
486 * @param body - The data to update the shipping method.
487 * @param headers - Headers to pass in the request.
488 * @returns The draft order preview's details.
489 *
490 * @example
491 * sdk.admin.draftOrder.updateShippingMethod("order_123", "action_123", {
492 * shipping_option_id: "shipping_option_123",
493 * })
494 * .then(({ draft_order_preview }) => {
495 * console.log(draft_order_preview)
496 * })
497 */
498 async updateActionShippingMethod(
499 id: string,
500 actionId: string,
501 body: HttpTypes.AdminUpdateDraftOrderActionShippingMethod,
502 headers?: ClientHeaders
503 ) {
504 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
505 `/admin/draft-orders/${id}/edit/shipping-methods/${actionId}`,
506 {
507 method: "POST",
508 body,
509 headers,
510 }
511 )
512 }
513
514 /**
515 * This method removes a shipping method from a draft order. It sends a request to the
516 * [Remove Draft Order Shipping Method](https://docs.medusajs.com/api/admin/draft-orders/remove-new-shipping-method) API route.
517 *
518 * @param id - The draft order's ID.
519 * @param actionId - The action ID.
520 * @param headers - Headers to pass in the request.
521 * @returns The draft order preview's details.
522 *
523 * @example
524 * sdk.admin.draftOrder.removeShippingMethod("order_123", "action_123")
525 * .then(({ draft_order_preview }) => {
526 * console.log(draft_order_preview)
527 * })
528 */
529 async removeActionShippingMethod(
530 id: string,
531 actionId: string,
532 headers?: ClientHeaders
533 ) {
534 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
535 `/admin/draft-orders/${id}/edit/shipping-methods/${actionId}`,
536 {
537 method: "DELETE",
538 headers,
539 }
540 )
541 }
542
543 /**
544 * This method removes a shipping method from an edited draft order. It sends a request to the
545 * [Remove Draft Order Shipping Method](https://docs.medusajs.com/api/admin/draft-orders/remove-shipping-method) API route.
546 *
547 * @param id - The draft order's ID.
548 * @param shippingMethodId - The shipping method's ID.
549 * @param headers - Headers to pass in the request.
550 * @returns The draft order preview's details.
551 *
552 * @example
553 * sdk.admin.draftOrder.removeShippingMethod(
554 * "order_123",
555 * "shipping_method_123"
556 * )
557 * .then(({ draft_order_preview }) => {
558 * console.log(draft_order_preview)
559 * })
560 */
561 async removeShippingMethod(
562 id: string,
563 shippingMethodId: string,
564 headers?: ClientHeaders
565 ) {
566 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
567 `/admin/draft-orders/${id}/edit/shipping-methods/method/${shippingMethodId}`,
568 {
569 method: "DELETE",
570 headers,
571 }
572 )
573 }
574
575 /**
576 * This method updates a shipping method in a draft order. It sends a request to the
577 * [Update Draft Order Shipping Method](https://docs.medusajs.com/api/admin/draft-orders/update-shipping-method) API route.
578 *
579 * @param id - The draft order's ID.
580 * @param methodId - The shipping method's ID.
581 * @param body - The data to update the shipping method.
582 * @param headers - Headers to pass in the request.
583 * @returns The draft order preview's details.
584 *
585 * @example
586 * sdk.admin.draftOrder.updateShippingMethod("order_123", "sm_123", {
587 * shipping_option_id: "so_123",
588 * })
589 * .then(({ draft_order_preview }) => {
590 * console.log(draft_order_preview)
591 * })
592 */
593 async updateShippingMethod(
594 id: string,
595 methodId: string,
596 body: HttpTypes.AdminUpdateDraftOrderShippingMethod,
597 headers?: ClientHeaders
598 ) {
599 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
600 `/admin/draft-orders/${id}/edit/shipping-methods/method/${methodId}`,
601 {
602 method: "POST",
603 body,
604 headers,
605 }
606 )
607 }
608 /**
609 * This method begins an edit to a draft order. It sends a request to the
610 * [Begin Draft Order Edit](https://docs.medusajs.com/api/admin/draft-orders/create-edit) API route.
611 *
612 * @param id - The draft order's ID.
613 * @param headers - Headers to pass in the request.
614 *
615 * @example
616 * sdk.admin.draftOrder.beginEdit("order_123")
617 * .then(({ draft_order_preview }) => {
618 * console.log(draft_order_preview)
619 * })
620 */
621 async beginEdit(id: string, headers?: ClientHeaders) {
622 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
623 `/admin/draft-orders/${id}/edit`,
624 {
625 method: "POST",
626 headers,
627 }
628 )
629 }
630
631 /**
632 * This method cancels an edit to a draft order. It sends a request to the
633 * [Cancel Draft Order Edit](https://docs.medusajs.com/api/admin/draft-orders/cancel-edit) API route.
634 *
635 * @param id - The draft order's ID.
636 * @param headers - Headers to pass in the request.
637 * @returns The cancelation's details.
638 *
639 * @example
640 * sdk.admin.draftOrder.cancelEdit("order_123")
641 * .then(({ id, object, deleted }) => {
642 * console.log(id, object, deleted)
643 * })
644 */
645 async cancelEdit(id: string, headers?: ClientHeaders) {
646 return await this.client.fetch<
647 HttpTypes.DeleteResponse<"draft-order-edit">
648 >(`/admin/draft-orders/${id}/edit`, {
649 method: "DELETE",
650 headers,
651 })
652 }
653
654 /**
655 * This method requests an edit to a draft order. It sends a request to the
656 * [Request Draft Order Edit](https://docs.medusajs.com/api/admin/draft-orders/request-edit) API route.
657 *
658 * @param id - The draft order's ID.
659 * @param headers - Headers to pass in the request.
660 * @returns The draft order preview's details.
661 *
662 * @example
663 * sdk.admin.draftOrder.requestEdit("order_123")
664 * .then(({ draft_order_preview }) => {
665 * console.log(draft_order_preview)
666 * })
667 */
668 async requestEdit(id: string, headers?: ClientHeaders) {
669 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
670 `/admin/draft-orders/${id}/edit/request`,
671 {
672 method: "POST",
673 headers,
674 }
675 )
676 }
677
678 /**
679 * This method confirms an edit to a draft order. It sends a request to the
680 * [Confirm Draft Order Edit](https://docs.medusajs.com/api/admin/draft-orders/confirm-edit) API route.
681 *
682 * @param id - The draft order's ID.
683 * @param headers - Headers to pass in the request.
684 * @returns The draft order preview's details.
685 *
686 * @example
687 * sdk.admin.draftOrder.confirmEdit("order_123")
688 * .then(({ draft_order_preview }) => {
689 * console.log(draft_order_preview)
690 * })
691 */
692 async confirmEdit(id: string, headers?: ClientHeaders) {
693 return await this.client.fetch<HttpTypes.AdminDraftOrderPreviewResponse>(
694 `/admin/draft-orders/${id}/edit/confirm`,
695 {
696 method: "POST",
697 headers,
698 }
699 )
700 }
701 }