1 import { HttpTypes, SelectParams } from "@medusajs/types"
2
3 import { Client } from "../client.js"
4 import { ClientHeaders } from "../types.js"
5
6 /**
7 * This class is used to send requests to the view configuration admin API routes.
8 * It can be accessed via `sdk.admin.views`.
9 *
10 * View configurations store the column visibility, ordering, filters, and sorting
11 * preferences for an entity's data table in the admin dashboard.
12 */
13 export class Views {
14 constructor(private client: Client) {}
15
16 /**
17 * This method retrieves the list of entities that support view configurations.
18 *
19 * @param {ClientHeaders} headers - Headers to pass in the request.
20 * @returns {Promise<HttpTypes.AdminEntityListResponse>} The list of supported entities.
21 *
22 * @example
23 * sdk.admin.views.listEntities()
24 * .then(({ entities }) => {
25 * console.log(entities)
26 * })
27 *
28 * @tags views
29 * @since 2.18.0
30 * @featureFlag view_configurations
31 */
32 async listEntities(
33 headers?: ClientHeaders
34 ): Promise<HttpTypes.AdminEntityListResponse> {
35 return await this.client.fetch("/admin/views/entities", {
36 method: "GET",
37 headers,
38 })
39 }
40
41 /**
42 * This method retrieves the columns available for an entity's data table.
43 *
44 * @param {string} entity - The entity to retrieve the columns for.
45 * @param {SelectParams} query - Configure the fields to retrieve in the columns.
46 * @param {ClientHeaders} headers - Headers to pass in the request.
47 * @returns {Promise<HttpTypes.AdminViewsEntityColumnsResponse>} The entity's available columns.
48 *
49 * @example
50 * const { columns } = await sdk.admin.views.columns("orders")
51 *
52 * @tags views
53 * @since 2.10.3
54 * @featureFlag view_configurations
55 */
56 async columns(
57 entity: string,
58 query?: SelectParams,
59 headers?: ClientHeaders
60 ): Promise<HttpTypes.AdminViewsEntityColumnsResponse> {
61 return await this.client.fetch(`/admin/views/${entity}/columns`, {
62 method: "GET",
63 headers,
64 query,
65 })
66 }
67
68 /**
69 * This method retrieves a paginated list of view configurations for an entity.
70 *
71 * @param {string} entity - The entity to retrieve the view configurations for.
72 * @param {HttpTypes.AdminGetViewConfigurationsParams} query - Filters and pagination configurations.
73 * @param {ClientHeaders} headers - Headers to pass in the request.
74 * @returns {Promise<HttpTypes.AdminViewConfigurationListResponse>} The paginated list of view configurations.
75 *
76 * @example
77 * To retrieve the list of view configurations:
78 *
79 * ```ts
80 * sdk.admin.views.listConfigurations("orders")
81 * .then(({ view_configurations, count, limit, offset }) => {
82 * console.log(view_configurations)
83 * })
84 * ```
85 *
86 * To configure the pagination, pass the `limit` and `offset` query parameters.
87 *
88 * For example, to retrieve only 10 items and skip 10 items:
89 *
90 * ```ts
91 * sdk.admin.views.listConfigurations("orders", {
92 * limit: 10,
93 * offset: 10
94 * })
95 * .then(({ view_configurations, count, limit, offset }) => {
96 * console.log(view_configurations)
97 * })
98 * ```
99 *
100 * @tags views
101 * @since 2.10.3
102 * @featureFlag view_configurations
103 */
104 async listConfigurations(
105 entity: string,
106 query?: HttpTypes.AdminGetViewConfigurationsParams,
107 headers?: ClientHeaders
108 ): Promise<HttpTypes.AdminViewConfigurationListResponse> {
109 return await this.client.fetch(`/admin/views/${entity}/configurations`, {
110 method: "GET",
111 headers,
112 query,
113 })
114 }
115
116 /**
117 * This method creates a view configuration for an entity.
118 *
119 * @param {string} entity - The entity to create the view configuration for.
120 * @param {HttpTypes.AdminCreateViewConfiguration} body - The view configuration's details.
121 * @param {ClientHeaders} headers - Headers to pass in the request.
122 * @returns {Promise<HttpTypes.AdminViewConfigurationResponse>} The created view configuration.
123 *
124 * @example
125 * const { view_configuration } = await sdk.admin.views.createConfiguration(
126 * "orders",
127 * {
128 * name: "My Orders View",
129 * configuration: {
130 * visible_columns: ["display_id", "status"],
131 * column_order: ["display_id", "status"],
132 * },
133 * }
134 * )
135 *
136 * @tags views
137 * @since 2.10.3
138 * @featureFlag view_configurations
139 */
140 async createConfiguration(
141 entity: string,
142 body: HttpTypes.AdminCreateViewConfiguration,
143 headers?: ClientHeaders
144 ): Promise<HttpTypes.AdminViewConfigurationResponse> {
145 return await this.client.fetch(`/admin/views/${entity}/configurations`, {
146 method: "POST",
147 headers,
148 body,
149 })
150 }
151
152 /**
153 * This method retrieves a view configuration by its ID.
154 *
155 * @param {string} entity - The entity the view configuration belongs to.
156 * @param {string} id - The ID of the view configuration to retrieve.
157 * @param {SelectParams} query - Configure the fields to retrieve in the view configuration.
158 * @param {ClientHeaders} headers - Headers to pass in the request.
159 * @returns {Promise<HttpTypes.AdminViewConfigurationResponse>} The view configuration's details.
160 *
161 * @example
162 * To retrieve a view configuration by its ID:
163 *
164 * ```ts
165 * sdk.admin.views.retrieveConfiguration("orders", "viewconfig_123")
166 * .then(({ view_configuration }) => {
167 * console.log(view_configuration)
168 * })
169 * ```
170 *
171 * To specify the fields and relations to retrieve:
172 *
173 * ```ts
174 * sdk.admin.views.retrieveConfiguration("orders", "viewconfig_123", {
175 * fields: "id,name,configuration"
176 * })
177 * .then(({ view_configuration }) => {
178 * console.log(view_configuration)
179 * })
180 * ```
181 *
182 * @tags views
183 * @since 2.10.3
184 * @featureFlag view_configurations
185 */
186 async retrieveConfiguration(
187 entity: string,
188 id: string,
189 query?: SelectParams,
190 headers?: ClientHeaders
191 ): Promise<HttpTypes.AdminViewConfigurationResponse> {
192 return await this.client.fetch(
193 `/admin/views/${entity}/configurations/${id}`,
194 {
195 method: "GET",
196 headers,
197 query,
198 }
199 )
200 }
201
202 /**
203 * This method updates a view configuration's details.
204 *
205 * @param {string} entity - The entity the view configuration belongs to.
206 * @param {string} id - The ID of the view configuration to update.
207 * @param {HttpTypes.AdminUpdateViewConfiguration} body - The details to update in the view configuration.
208 * @param {ClientHeaders} headers - Headers to pass in the request.
209 * @returns {Promise<HttpTypes.AdminViewConfigurationResponse>} The updated view configuration.
210 *
211 * @example
212 * const { view_configuration } = await sdk.admin.views.updateConfiguration(
213 * "orders",
214 * "viewconfig_123",
215 * {
216 * name: "Updated View",
217 * }
218 * )
219 *
220 * @tags views
221 * @since 2.10.3
222 * @featureFlag view_configurations
223 */
224 async updateConfiguration(
225 entity: string,
226 id: string,
227 body: HttpTypes.AdminUpdateViewConfiguration,
228 headers?: ClientHeaders
229 ): Promise<HttpTypes.AdminViewConfigurationResponse> {
230 return await this.client.fetch(
231 `/admin/views/${entity}/configurations/${id}`,
232 {
233 method: "POST",
234 headers,
235 body,
236 }
237 )
238 }
239
240 /**
241 * This method deletes a view configuration.
242 *
243 * @param {string} entity - The entity the view configuration belongs to.
244 * @param {string} id - The ID of the view configuration to delete.
245 * @param {ClientHeaders} headers - Headers to pass in the request.
246 * @returns {Promise<HttpTypes.AdminViewConfigurationDeleteResponse>} The deletion's details.
247 *
248 * @example
249 * const { id, object, deleted } = await sdk.admin.views.deleteConfiguration(
250 * "orders",
251 * "viewconfig_123"
252 * )
253 *
254 * @tags views
255 * @since 2.10.3
256 * @featureFlag view_configurations
257 */
258 async deleteConfiguration(
259 entity: string,
260 id: string,
261 headers?: ClientHeaders
262 ): Promise<HttpTypes.AdminViewConfigurationDeleteResponse> {
263 return await this.client.fetch(
264 `/admin/views/${entity}/configurations/${id}`,
265 {
266 method: "DELETE",
267 headers,
268 }
269 )
270 }
271
272 /**
273 * This method retrieves the active view configuration for an entity and the
274 * authenticated user.
275 *
276 * @param {string} entity - The entity to retrieve the active view configuration for.
277 * @param {ClientHeaders} headers - Headers to pass in the request.
278 * @returns {Promise<HttpTypes.AdminViewConfigurationResponse & { active_view_configuration_id?: string | null }>} The active view configuration, along with its ID.
279 *
280 * @example
281 * sdk.admin.views.retrieveActiveConfiguration("orders")
282 * .then(({ view_configuration, active_view_configuration_id }) => {
283 * console.log(view_configuration)
284 * })
285 *
286 * @tags views
287 * @since 2.10.3
288 * @featureFlag view_configurations
289 */
290 async retrieveActiveConfiguration(
291 entity: string,
292 headers?: ClientHeaders
293 ): Promise<
294 HttpTypes.AdminViewConfigurationResponse & {
295 active_view_configuration_id?: string | null
296 }
297 > {
298 return await this.client.fetch(
299 `/admin/views/${entity}/configurations/active`,
300 {
301 method: "GET",
302 headers,
303 }
304 )
305 }
306
307 /**
308 * This method sets the active view configuration for an entity and the
309 * authenticated user. Pass `null` as the `view_configuration_id` to clear the
310 * active view.
311 *
312 * @param {string} entity - The entity to set the active view configuration for.
313 * @param {object} body - The active view configuration's details.
314 * @param {string | null} body.view_configuration_id - The ID of the view configuration to set as active, or `null` to clear it.
315 * @param {ClientHeaders} headers - Headers to pass in the request.
316 * @returns {Promise<{ success: boolean }>} Whether the active view configuration was set successfully.
317 *
318 * @example
319 * await sdk.admin.views.setActiveConfiguration("orders", {
320 * view_configuration_id: "viewconfig_123",
321 * })
322 *
323 * @tags views
324 * @since 2.10.3
325 * @featureFlag view_configurations
326 */
327 async setActiveConfiguration(
328 entity: string,
329 body: { view_configuration_id: string | null },
330 headers?: ClientHeaders
331 ): Promise<{ success: boolean }> {
332 return await this.client.fetch(
333 `/admin/views/${entity}/configurations/active`,
334 {
335 method: "POST",
336 headers,
337 body,
338 }
339 )
340 }
341 }