1 import { HttpTypes, SelectParams } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class StockLocation {
6 /**
7 * @ignore
8 */
9 private client: Client
10 /**
11 * @ignore
12 */
13 constructor(client: Client) {
14 this.client = client
15 }
16
17 /**
18 * This method creates a new stock location. It sends a request to the
19 * [Create Stock Location](https://docs.medusajs.com/api/admin/stock-locations/create-stock-location)
20 * API route.
21 *
22 * @param body - The details of the stock location to create.
23 * @param query - Configure the fields and relations to retrieve in the stock location.
24 * @param headers - Headers to pass in the request.
25 * @returns The stock location's details.
26 *
27 * @example
28 * sdk.admin.stockLocation.create({
29 * name: "Main Warehouse",
30 * address_id: "addr_123",
31 * })
32 * .then(({ stock_location }) => {
33 * console.log(stock_location)
34 * })
35 */
36 async create(
37 body: HttpTypes.AdminCreateStockLocation,
38 query?: SelectParams,
39 headers?: ClientHeaders
40 ) {
41 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
42 `/admin/stock-locations`,
43 {
44 method: "POST",
45 headers,
46 body,
47 query,
48 }
49 )
50 }
51
52 /**
53 * This method updates a stock location. It sends a request to the
54 * [Update Stock Location](https://docs.medusajs.com/api/admin/stock-locations/update-a-stock-location)
55 * API route.
56 *
57 * @param id - The ID of the stock location to update.
58 * @param body - The details of the stock location to update.
59 * @param query - Configure the fields and relations to retrieve in the stock location.
60 * @param headers - Headers to pass in the request.
61 * @returns The stock location's details.
62 *
63 * @example
64 * sdk.admin.stockLocation.update("sloc_123", {
65 * name: "European Warehouse",
66 * })
67 * .then(({ stock_location }) => {
68 * console.log(stock_location)
69 * })
70 */
71 async update(
72 id: string,
73 body: HttpTypes.AdminUpdateStockLocation,
74 query?: SelectParams,
75 headers?: ClientHeaders
76 ) {
77 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
78 `/admin/stock-locations/${id}`,
79 {
80 method: "POST",
81 headers,
82 body,
83 query,
84 }
85 )
86 }
87
88 /**
89 * This method deletes a stock location. It sends a request to the
90 * [Delete Stock Location](https://docs.medusajs.com/api/admin/stock-locations/delete-a-stock-location)
91 * API route.
92 *
93 * @param id - The ID of the stock location to delete.
94 * @param headers - Headers to pass in the request.
95 * @returns The deletion's details.
96 *
97 * @example
98 * sdk.admin.stockLocation.delete("sloc_123")
99 * .then(({ deleted }) => {
100 * console.log(deleted)
101 * })
102 */
103 async delete(id: string, headers?: ClientHeaders) {
104 return await this.client.fetch<HttpTypes.AdminStockLocationDeleteResponse>(
105 `/admin/stock-locations/${id}`,
106 {
107 method: "DELETE",
108 headers,
109 }
110 )
111 }
112
113 /**
114 * This method retrieves a stock location. It sends a request to the
115 * [Get Stock Location](https://docs.medusajs.com/api/admin/stock-locations/get-a-stock-location)
116 * API route.
117 *
118 * @param id - The ID of the stock location to retrieve.
119 * @param query - Configure the fields and relations to retrieve in the stock location.
120 * @param headers - Headers to pass in the request.
121 * @returns The stock location's details.
122 *
123 * @example
124 * To retrieve a stock location by its ID:
125 *
126 * ```ts
127 * sdk.admin.stockLocation.retrieve("sloc_123")
128 * .then(({ stock_location }) => {
129 * console.log(stock_location)
130 * })
131 * ```
132 *
133 * To specify the fields and relations to retrieve:
134 *
135 * ```ts
136 * sdk.admin.stockLocation.retrieve("sloc_123", {
137 * fields: "id,*sales_channels"
138 * })
139 * .then(({ stock_location }) => {
140 * console.log(stock_location)
141 * })
142 * ```
143 *
144 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
145 */
146 async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
147 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
148 `/admin/stock-locations/${id}`,
149 {
150 method: "GET",
151 headers,
152 query,
153 }
154 )
155 }
156
157 /**
158 * This method retrieves a list of stock locations. It sends a request to the
159 * [List Stock Locations](https://docs.medusajs.com/api/admin/stock-locations/list-stock-locations)
160 * API route.
161 *
162 * @param query - Filters and pagination configurations.
163 * @param headers - Headers to pass in the request.
164 * @returns The list of stock locations.
165 *
166 * @example
167 * To retrieve the list of stock locations:
168 *
169 * ```ts
170 * sdk.admin.stockLocation.list()
171 * .then(({ stock_locations, count, limit, offset }) => {
172 * console.log(stock_locations)
173 * })
174 * ```
175 *
176 * To configure the pagination, pass the `limit` and `offset` query parameters.
177 *
178 * For example, to retrieve only 10 items and skip 10 items:
179 *
180 * ```ts
181 * sdk.admin.stockLocation.list({
182 * limit: 10,
183 * offset: 10
184 * })
185 * .then(({ stock_locations, count, limit, offset }) => {
186 * console.log(stock_locations)
187 * })
188 * ```
189 *
190 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
191 * in each stock location:
192 *
193 * ```ts
194 * sdk.admin.stockLocation.list({
195 * fields: "id,*sales_channels"
196 * })
197 * .then(({ stock_locations, count, limit, offset }) => {
198 * console.log(stock_locations)
199 * })
200 * ```
201 *
202 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/admin/select-fields-and-relations).
203 */
204 async list(
205 query?: HttpTypes.AdminStockLocationListParams,
206 headers?: ClientHeaders
207 ) {
208 return await this.client.fetch<HttpTypes.AdminStockLocationListResponse>(
209 `/admin/stock-locations`,
210 {
211 method: "GET",
212 headers,
213 query,
214 }
215 )
216 }
217
218 /**
219 * This method manages the sales channels of a stock location by adding or removing them. It sends a request to the
220 * [Manage Stock Location Sales Channels](https://docs.medusajs.com/api/admin/stock-locations/manage-sales-channels)
221 * API route.
222 *
223 * @param id - The ID of the stock location to update the sales channels for.
224 * @param body - The details of the sales channels to update.
225 * @param headers - Headers to pass in the request.
226 * @returns The stock location's details.
227 *
228 * @example
229 * sdk.admin.stockLocation.updateSalesChannels("sloc_123", {
230 * add: ["sc_123"],
231 * remove: ["sc_456"],
232 * })
233 * .then(({ stock_location }) => {
234 * console.log(stock_location)
235 * })
236 */
237 async updateSalesChannels(
238 id: string,
239 body: HttpTypes.AdminUpdateStockLocationSalesChannels,
240 headers?: ClientHeaders
241 ) {
242 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
243 `/admin/stock-locations/${id}/sales-channels`,
244 {
245 method: "POST",
246 headers,
247 body,
248 }
249 )
250 }
251
252 /**
253 * This method adds a new fulfillment set to a stock location. It sends a request to the
254 * [Add Fulfillment Set to Stock Location](https://docs.medusajs.com/api/admin/stock-locations/add-fulfillment-set)
255 * API route.
256 *
257 * @param id - The ID of the stock location to add the fulfillment set to.
258 * @param body - The details of the fulfillment set to add.
259 * @param headers - Headers to pass in the request.
260 * @returns The stock location's details.
261 *
262 * @example
263 * sdk.admin.stockLocation.createFulfillmentSet("sloc_123", {
264 * name: "Shipping",
265 * type: "shipping",
266 * })
267 * .then(({ stock_location }) => {
268 * console.log(stock_location)
269 * })
270 */
271 async createFulfillmentSet(
272 id: string,
273 body: HttpTypes.AdminCreateStockLocationFulfillmentSet,
274 headers?: ClientHeaders
275 ) {
276 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
277 `/admin/stock-locations/${id}/fulfillment-sets`,
278 {
279 method: "POST",
280 headers,
281 body,
282 }
283 )
284 }
285
286 /**
287 * This method manages the fulfillment providers of a stock location by adding or removing them. It sends a request to the
288 * [Manage Fulfillment Providers of Stock Location](https://docs.medusajs.com/api/admin/stock-locations/manage-fulfillment-providers)
289 * API route.
290 *
291 * @param id - The ID of the stock location to manage the fulfillment providers for.
292 * @param body - The details of the fulfillment providers to manage.
293 * @param headers - Headers to pass in the request.
294 * @returns The stock location's details.
295 *
296 * @example
297 * sdk.admin.stockLocation.updateFulfillmentProviders("sloc_123", {
298 * add: ["fp_manual_manual"],
299 * remove: ["fp_shipstation_shipstation"],
300 * })
301 * .then(({ stock_location }) => {
302 * console.log(stock_location)
303 * })
304 */
305 async updateFulfillmentProviders(
306 id: string,
307 body: HttpTypes.AdminBatchLink,
308 headers?: ClientHeaders
309 ) {
310 return await this.client.fetch<HttpTypes.AdminStockLocationResponse>(
311 `/admin/stock-locations/${id}/fulfillment-providers`,
312 {
313 method: "POST",
314 headers,
315 body,
316 }
317 )
318 }
319 }