|
| 1 | +import { HttpTypes } from "@medusajs/types" |
| 2 | + |
| 3 | +import { Client } from "../client" |
| 4 | +import { ClientHeaders } from "../types" |
| 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_getdraftordersid) |
| 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("draft_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("draft_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_getdraftorders) 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_postdraftorders) 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 | + * |
| 132 | + * @example |
| 133 | + * To create a draft order: |
| 134 | + * |
| 135 | + * ```ts |
| 136 | + * sdk.admin.draftOrder.create({ |
| 137 | + * email: "test@test.com", |
| 138 | + * items: [ |
| 139 | + * { |
| 140 | + * variant_id: "variant_123", |
| 141 | + * quantity: 1, |
| 142 | + * }, |
| 143 | + * ], |
| 144 | + * region_id: "region_123", |
| 145 | + * sales_channel_id: "sales_channel_123", |
| 146 | + * }) |
| 147 | + * .then(({ draft_order }) => { |
| 148 | + * console.log(draft_order) |
| 149 | + * }) |
| 150 | + * ``` |
| 151 | + */ |
| 152 | + async create( |
| 153 | + body: HttpTypes.AdminCreateDraftOrder, |
| 154 | + query?: HttpTypes.AdminDraftOrderParams, |
| 155 | + headers?: ClientHeaders |
| 156 | + ) { |
| 157 | + return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>( |
| 158 | + `/admin/draft-orders`, |
| 159 | + { |
| 160 | + method: "POST", |
| 161 | + body, |
| 162 | + query, |
| 163 | + headers, |
| 164 | + } |
| 165 | + ) |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * This method updates a draft order. It sends a request to the |
| 170 | + * [Update Draft Order](https://docs.medusajs.com/api/admin#draft-orders_postdraftordersid) API route. |
| 171 | + * |
| 172 | + * @param id - The draft order's ID. |
| 173 | + * @param body - The data to update the draft order. |
| 174 | + * @param query - Configure the fields to retrieve in the draft order. |
| 175 | + * @param headers - Headers to pass in the request. |
| 176 | + * |
| 177 | + * @example |
| 178 | + * To update a draft order: |
| 179 | + * |
| 180 | + * ```ts |
| 181 | + * sdk.admin.draftOrder.update("draft_order_123", { |
| 182 | + * email: "test@test.com", |
| 183 | + * }) |
| 184 | + * .then(({ draft_order }) => { |
| 185 | + * console.log(draft_order) |
| 186 | + * }) |
| 187 | + * ``` |
| 188 | + */ |
| 189 | + async update( |
| 190 | + id: string, |
| 191 | + body: HttpTypes.AdminUpdateDraftOrder, |
| 192 | + query?: HttpTypes.AdminDraftOrderParams, |
| 193 | + headers?: ClientHeaders |
| 194 | + ) { |
| 195 | + return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>( |
| 196 | + `/admin/draft-orders/${id}`, |
| 197 | + { |
| 198 | + method: "POST", |
| 199 | + body, |
| 200 | + query, |
| 201 | + headers, |
| 202 | + } |
| 203 | + ) |
| 204 | + } |
| 205 | +} |
0 commit comments