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