1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class ShippingOption {
6 /**
7 * @ignore
8 */
9 private client: Client
10 /**
11 * @ignore
12 */
13 constructor(client: Client) {
14 this.client = client
15 }
16 /**
17 * This method creates a shipping option. It sends a request to the
18 * [Create Shipping Option](https://docs.medusajs.com/api/admin/shipping-options/create-shipping-option)
19 * API route.
20 *
21 * @param body - The details of the shipping option to create.
22 * @param query - Configure the fields and relations to retrieve in the shipping option.
23 * @param headers - Headers to pass in the request.
24 * @returns The shipping option's details.
25 *
26 * @example
27 * sdk.admin.shippingOption.create({
28 * name: "Standard Shipping",
29 * profile_id: "shp_123",
30 * })
31 * .then(({ shipping_option }) => {
32 * console.log(shipping_option)
33 * })
34 */
35 async create(
36 body: HttpTypes.AdminCreateShippingOption,
37 query?: HttpTypes.SelectParams,
38 headers?: ClientHeaders
39 ) {
40 return await this.client.fetch<HttpTypes.AdminShippingOptionResponse>(
41 `/admin/shipping-options`,
42 {
43 method: "POST",
44 headers,
45 body,
46 query,
47 }
48 )
49 }
50
51 /**
52 * This method retrieves a shipping option. It sends a request to the
53 * [Get Shipping Option](https://docs.medusajs.com/api/admin/shipping-options/get-a-shipping-option)
54 * API route.
55 *
56 * @param id - The ID of the shipping option to retrieve.
57 * @param query - Configure the fields and relations to retrieve in the shipping option.
58 * @param headers - Headers to pass in the request.
59 * @returns The shipping option's details.
60 *
61 * @example
62 * To retrieve a shipping option by its ID:
63 *
64 * ```ts
65 * sdk.admin.shippingOption.retrieve("so_123")
66 * .then(({ shipping_option }) => {
67 * console.log(shipping_option)
68 * })
69 * ```
70 *
71 * To specify the fields and relations to retrieve:
72 *
73 * ```ts
74 * sdk.admin.shippingOption.retrieve("so_123", {
75 * fields: "id,*service_zone"
76 * })
77 * .then(({ shipping_option }) => {
78 * console.log(shipping_option)
79 * })
80 * ```
81 *
82 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
83 */
84 async retrieve(
85 id: string,
86 query?: HttpTypes.SelectParams,
87 headers?: ClientHeaders
88 ) {
89 return await this.client.fetch<HttpTypes.AdminShippingOptionResponse>(
90 `/admin/shipping-options/${id}`,
91 {
92 method: "GET",
93 headers,
94 query,
95 }
96 )
97 }
98
99 /**
100 * This method updates a shipping option. It sends a request to the
101 * [Update Shipping Option](https://docs.medusajs.com/api/admin/shipping-options/update-a-shipping-option)
102 * API route.
103 *
104 * @param id - The ID of the shipping option to update.
105 * @param body - The details of the shipping option to update.
106 * @param query - Configure the fields and relations to retrieve in the shipping option.
107 * @param headers - Headers to pass in the request.
108 * @returns The shipping option's details.
109 *
110 * @example
111 * sdk.admin.shippingOption.update("so_123", {
112 * name: "Standard Shipping",
113 * })
114 * .then(({ shipping_option }) => {
115 * console.log(shipping_option)
116 * })
117 */
118 async update(
119 id: string,
120 body: HttpTypes.AdminUpdateShippingOption,
121 query?: HttpTypes.SelectParams,
122 headers?: ClientHeaders
123 ) {
124 return await this.client.fetch<HttpTypes.AdminShippingOptionResponse>(
125 `/admin/shipping-options/${id}`,
126 {
127 method: "POST",
128 headers,
129 body,
130 query,
131 }
132 )
133 }
134
135 /**
136 * This method deletes a shipping option. It sends a request to the
137 * [Delete Shipping Option](https://docs.medusajs.com/api/admin/shipping-options/delete-a-shipping-option)
138 * API route.
139 *
140 * @param id - The ID of the shipping option to delete.
141 * @param headers - Headers to pass in the request.
142 * @returns The deletion's details.
143 *
144 * @example
145 * sdk.admin.shippingOption.delete("so_123")
146 * .then(({ deleted }) => {
147 * console.log(deleted)
148 * })
149 */
150 async delete(id: string, headers?: ClientHeaders) {
151 return await this.client.fetch<HttpTypes.AdminShippingOptionDeleteResponse>(
152 `/admin/shipping-options/${id}`,
153 {
154 method: "DELETE",
155 headers,
156 }
157 )
158 }
159
160 /**
161 * This method retrieves a list of shipping options. It sends a request to the
162 * [List Shipping Options](https://docs.medusajs.com/api/admin/shipping-options/list-shipping-options)
163 * API route.
164 *
165 * @param query - Filters and pagination configurations.
166 * @param headers - Headers to pass in the request.
167 * @returns The list of shipping options.
168 *
169 * @example
170 * To retrieve the list of shipping options:
171 *
172 * ```ts
173 * sdk.admin.shippingOption.list()
174 * .then(({ shipping_options, count, limit, offset }) => {
175 * console.log(shipping_options)
176 * })
177 * ```
178 *
179 * To configure the pagination, pass the `limit` and `offset` query parameters.
180 *
181 * For example, to retrieve only 10 items and skip 10 items:
182 *
183 * ```ts
184 * sdk.admin.shippingOption.list({
185 * limit: 10,
186 * offset: 10
187 * })
188 * .then(({ shipping_options, count, limit, offset }) => {
189 * console.log(shipping_options)
190 * })
191 * ```
192 *
193 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
194 * in each shipping option:
195 *
196 * ```ts
197 * sdk.admin.shippingOption.list({
198 * fields: "id,*service_zone"
199 * })
200 * .then(({ shipping_options, count, limit, offset }) => {
201 * console.log(shipping_options)
202 * })
203 * ```
204 *
205 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
206 */
207 async list(
208 query?: HttpTypes.AdminShippingOptionListParams,
209 headers?: ClientHeaders
210 ) {
211 return await this.client.fetch<HttpTypes.AdminShippingOptionListResponse>(
212 `/admin/shipping-options`,
213 {
214 method: "GET",
215 headers,
216 query,
217 }
218 )
219 }
220
221 /**
222 * This method manages the rules of a shipping option to create, update, or remove them. It sends a request to the
223 * [Manage Rules of a Shipping Option](https://docs.medusajs.com/api/admin/shipping-options/manage-rules)
224 * API route.
225 *
226 * @param id - The ID of the shipping option to manage the rules for.
227 * @param body - The details of the shipping option rules to manage.
228 * @param headers - Headers to pass in the request.
229 * @returns The shipping option's details.
230 *
231 * @example
232 * sdk.admin.shippingOption.updateRules("so_123", {
233 * create: [{ attribute: "enabled_in_store", operator: "eq", value: "true" }],
234 * })
235 * .then(({ shipping_option }) => {
236 * console.log(shipping_option)
237 * })
238 */
239 async updateRules(
240 id: string,
241 body: HttpTypes.AdminUpdateShippingOptionRules,
242 headers?: ClientHeaders
243 ) {
244 return await this.client.fetch<HttpTypes.AdminUpdateShippingOptionRulesResponse>(
245 `/admin/shipping-options/${id}/rules/batch`,
246 {
247 method: "POST",
248 headers,
249 body,
250 }
251 )
252 }
253 }