1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 const taxRegionUrl = "/admin/tax-regions"
6
7 /**
8 * @privateRemarks
9 *
10 * TODO: Add support for updating a tax region
11 */
12 export class TaxRegion {
13 /**
14 * @ignore
15 */
16 private client: Client
17 /**
18 * @ignore
19 */
20 constructor(client: Client) {
21 this.client = client
22 }
23
24 /**
25 * This method creates a tax region. It sends a request to the
26 * [Create Tax Region](https://docs.medusajs.com/api/admin/tax-regions/create-tax-region)
27 * API route.
28 *
29 * @param body - The details of the tax region to create.
30 * @param query - Configure the fields and relations to retrieve in the tax region.
31 * @param headers - Headers to pass in the request.
32 * @returns The tax region's details.
33 *
34 * @example
35 * sdk.admin.taxRegion.create({
36 * country_code: "us",
37 * province_code: "ca",
38 * default_tax_rate: {
39 * code: "VAT",
40 * name: "VAT",
41 * rate: 20, // 20%
42 * is_combinable: true,
43 * },
44 * })
45 * .then(({ tax_region }) => {
46 * console.log(tax_region)
47 * })
48 */
49 async create(
50 body: HttpTypes.AdminCreateTaxRegion,
51 query?: HttpTypes.SelectParams,
52 headers?: ClientHeaders
53 ) {
54 return await this.client.fetch<HttpTypes.AdminTaxRegionResponse>(
55 taxRegionUrl,
56 {
57 method: "POST",
58 headers,
59 body,
60 query,
61 }
62 )
63 }
64
65 /**
66 * This method updates a tax region. It sends a request to the
67 * [Update Tax Region](https://docs.medusajs.com/api/admin/tax-regions/update-a-tax-region)
68 * API route.
69 *
70 * @since 2.8.0
71 *
72 * @param id - The ID of the tax region to update.
73 * @param body - The details of the tax region to update.
74 * @param query - Configure the fields and relations to retrieve in the tax region.
75 * @param headers - Headers to pass in the request.
76 * @returns The tax region's details.
77 *
78 * @example
79 * sdk.admin.taxRegion.update("txreg_123", {
80 * province_code: "ca",
81 * })
82 * .then(({ tax_region }) => {
83 * console.log(tax_region)
84 * })
85 */
86 async update(
87 id: string,
88 body: HttpTypes.AdminUpdateTaxRegion,
89 query?: HttpTypes.SelectParams,
90 headers?: ClientHeaders
91 ) {
92 return await this.client.fetch<HttpTypes.AdminTaxRegionResponse>(
93 `${taxRegionUrl}/${id}`,
94 {
95 method: "POST",
96 headers,
97 body,
98 query,
99 }
100 )
101 }
102
103 /**
104 * This method deletes a tax region. It sends a request to the
105 * [Delete Tax Region](https://docs.medusajs.com/api/admin/tax-regions/delete-a-tax-region)
106 * API route.
107 *
108 * @param id - The ID of the tax region to delete.
109 * @param headers - Headers to pass in the request.
110 * @returns The deletion's details.
111 *
112 * @example
113 * sdk.admin.taxRegion.delete("txreg_123")
114 * .then(({ deleted }) => {
115 * console.log(deleted)
116 * })
117 */
118 async delete(id: string, headers?: ClientHeaders) {
119 return await this.client.fetch<HttpTypes.AdminTaxRegionDeleteResponse>(
120 `${taxRegionUrl}/${id}`,
121 {
122 method: "DELETE",
123 headers,
124 }
125 )
126 }
127
128 /**
129 * This method retrieves a tax region. It sends a request to the
130 * [Get Tax Region](https://docs.medusajs.com/api/admin/tax-regions/get-a-tax-region)
131 * API route.
132 *
133 * @param id - The ID of the tax region to retrieve.
134 * @param query - Configure the fields and relations to retrieve in the tax region.
135 * @param headers - Headers to pass in the request.
136 * @returns The tax region's details.
137 *
138 * @example
139 * To retrieve a tax region by its ID:
140 *
141 * ```ts
142 * sdk.admin.taxRegion.retrieve("txreg_123")
143 * .then(({ tax_region }) => {
144 * console.log(tax_region)
145 * })
146 * ```
147 *
148 * To specify the fields and relations to retrieve:
149 *
150 * ```ts
151 * sdk.admin.taxRegion.retrieve("txreg_123", {
152 * fields: "id,*tax_rates"
153 * })
154 * .then(({ tax_region }) => {
155 * console.log(tax_region)
156 * })
157 * ```
158 *
159 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
160 */
161 async retrieve(
162 id: string,
163 query?: HttpTypes.SelectParams,
164 headers?: ClientHeaders
165 ) {
166 return await this.client.fetch<HttpTypes.AdminTaxRegionResponse>(
167 `${taxRegionUrl}/${id}`,
168 {
169 method: "GET",
170 headers,
171 query,
172 }
173 )
174 }
175
176 /**
177 * This method retrieves a list of tax regions. It sends a request to the
178 * [List Tax Regions](https://docs.medusajs.com/api/admin/tax-regions/list-tax-regions)
179 * API route.
180 *
181 * @param query - Filters and pagination configurations.
182 * @param headers - Headers to pass in the request.
183 * @returns The list of tax regions.
184 *
185 * @example
186 * To retrieve the list of tax regions:
187 *
188 * ```ts
189 * sdk.admin.taxRegion.list()
190 * .then(({ tax_regions, count, limit, offset }) => {
191 * console.log(tax_regions)
192 * })
193 * ```
194 *
195 * To configure the pagination, pass the `limit` and `offset` query parameters.
196 *
197 * For example, to retrieve only 10 items and skip 10 items:
198 *
199 * ```ts
200 * sdk.admin.taxRegion.list({
201 * limit: 10,
202 * offset: 10
203 * })
204 * .then(({ tax_regions, count, limit, offset }) => {
205 * console.log(tax_regions)
206 * })
207 * ```
208 *
209 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
210 * in each tax region:
211 *
212 * ```ts
213 * sdk.admin.taxRegion.list({
214 * fields: "id,*tax_rates"
215 * })
216 * .then(({ tax_regions, count, limit, offset }) => {
217 * console.log(tax_regions)
218 * })
219 * ```
220 *
221 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
222 */
223 async list(
224 query?: HttpTypes.AdminTaxRegionListParams,
225 headers?: ClientHeaders
226 ) {
227 return await this.client.fetch<HttpTypes.AdminTaxRegionListResponse>(
228 taxRegionUrl,
229 {
230 method: "GET",
231 headers,
232 query,
233 }
234 )
235 }
236 }