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