1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 /**
6 * This class is used to send requests to the RBAC Role API routes.
7 *
8 * @since 2.15.5
9 */
10 export class RbacRole {
11 /**
12 * @ignore
13 */
14 private client: Client
15 /**
16 * @ignore
17 */
18 constructor(client: Client) {
19 this.client = client
20 }
21
22 /**
23 * This method retrieves a paginated list of RBAC roles. It sends a request to the
24 * List RBAC Roles API route.
25 *
26 * @param queryParams - Filters and pagination configurations.
27 * @param headers - Headers to pass in the request.
28 * @returns The paginated list of RBAC roles.
29 *
30 * @example
31 * To retrieve the list of roles:
32 *
33 * ```ts
34 * sdk.admin.rbacRole.list()
35 * .then(({ roles, count, limit, offset }) => {
36 * console.log(roles)
37 * })
38 * ```
39 *
40 * To configure the pagination, pass the `limit` and `offset` query parameters.
41 *
42 * For example, to retrieve only 10 items and skip 10 items:
43 *
44 * ```ts
45 * sdk.admin.rbacRole.list({
46 * limit: 10,
47 * offset: 10
48 * })
49 * .then(({ roles, count, limit, offset }) => {
50 * console.log(roles)
51 * })
52 * ```
53 *
54 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
55 * in each role:
56 *
57 * ```ts
58 * sdk.admin.rbacRole.list({
59 * fields: "id,name,*policies"
60 * })
61 * .then(({ roles, count, limit, offset }) => {
62 * console.log(roles)
63 * })
64 * ```
65 *
66 * @tags rbac
67 * @since 2.15.5
68 */
69 async list(
70 queryParams?: HttpTypes.AdminRbacRoleListParams,
71 headers?: ClientHeaders
72 ) {
73 return await this.client.fetch<HttpTypes.AdminRbacRoleListResponse>(
74 `/admin/rbac/roles`,
75 {
76 query: queryParams,
77 headers,
78 }
79 )
80 }
81
82 /**
83 * This method retrieves an RBAC role's details. It sends a request to the
84 * Get RBAC Role API route.
85 *
86 * @param id - The role's ID.
87 * @param query - Configure the fields to retrieve in the role.
88 * @param headers - Headers to pass in the request.
89 * @returns The role's details.
90 *
91 * @example
92 * sdk.admin.rbacRole.retrieve("role_123")
93 * .then(({ role }) => {
94 * console.log(role)
95 * })
96 *
97 * @tags rbac
98 * @since 2.15.5
99 */
100 async retrieve(
101 id: string,
102 query?: HttpTypes.AdminRbacRoleParams,
103 headers?: ClientHeaders
104 ) {
105 return await this.client.fetch<HttpTypes.AdminRbacRoleResponse>(
106 `/admin/rbac/roles/${id}`,
107 {
108 query,
109 headers,
110 }
111 )
112 }
113
114 /**
115 * This method creates an RBAC role. It sends a request to the Create RBAC Role
116 * API route.
117 *
118 * @param body - The role's details.
119 * @param query - Configure the fields to retrieve in the created role.
120 * @param headers - Headers to pass in the request
121 * @returns The created role
122 *
123 * @example
124 * sdk.admin.rbacRole.create({
125 * name: "Editor",
126 * description: "Can edit content"
127 * })
128 * .then(({ role }) => {
129 * console.log(role)
130 * })
131 *
132 * @tags rbac
133 * @since 2.15.5
134 */
135 async create(
136 body: HttpTypes.AdminCreateRbacRole,
137 query?: HttpTypes.AdminRbacRoleParams,
138 headers?: ClientHeaders
139 ) {
140 return await this.client.fetch<HttpTypes.AdminRbacRoleResponse>(
141 `/admin/rbac/roles`,
142 {
143 method: "POST",
144 headers,
145 body,
146 query,
147 }
148 )
149 }
150
151 /**
152 * This method updates an RBAC role's details. It sends a request to the
153 * Update RBAC Role API route.
154 *
155 * @param id - The role's ID.
156 * @param body - The data to update in the role.
157 * @param query - Configure the fields to retrieve in the role.
158 * @param headers - Headers to pass in the request.
159 * @returns The role's details.
160 *
161 * @example
162 * sdk.admin.rbacRole.update("role_123", {
163 * name: "Senior Editor"
164 * })
165 * .then(({ role }) => {
166 * console.log(role)
167 * })
168 *
169 * @tags rbac
170 * @since 2.15.5
171 */
172 async update(
173 id: string,
174 body: HttpTypes.AdminUpdateRbacRole,
175 query?: HttpTypes.AdminRbacRoleParams,
176 headers?: ClientHeaders
177 ) {
178 return await this.client.fetch<HttpTypes.AdminRbacRoleResponse>(
179 `/admin/rbac/roles/${id}`,
180 {
181 method: "POST",
182 headers,
183 body,
184 query,
185 }
186 )
187 }
188
189 /**
190 * This method deletes an RBAC role by its ID. It sends a request to the
191 * Delete RBAC Role API route.
192 *
193 * @param id - The role's ID.
194 * @param headers - Headers to pass in the request.
195 * @returns The deletion's details.
196 *
197 * @example
198 * sdk.admin.rbacRole.delete("role_123")
199 * .then(({ deleted }) => {
200 * console.log(deleted)
201 * })
202 */
203 async delete(id: string, headers?: ClientHeaders) {
204 return await this.client.fetch<HttpTypes.AdminRbacRoleDeleteResponse>(
205 `/admin/rbac/roles/${id}`,
206 {
207 method: "DELETE",
208 headers,
209 }
210 )
211 }
212
213 /**
214 * This method retrieves a paginated list of policies associated with an RBAC role.
215 * It sends a request to the List Role Policies API route.
216 *
217 * @param roleId - The role's ID.
218 * @param queryParams - Filters and pagination configurations.
219 * @param headers - Headers to pass in the request.
220 * @returns The paginated list of policies.
221 *
222 * @example
223 * sdk.admin.rbacRole.listPolicies("role_123")
224 * .then(({ policies, count, limit, offset }) => {
225 * console.log(policies)
226 * })
227 */
228 async listPolicies(
229 roleId: string,
230 queryParams?: HttpTypes.AdminRbacPolicyListParams,
231 headers?: ClientHeaders
232 ) {
233 return await this.client.fetch<HttpTypes.AdminRbacPolicyListResponse>(
234 `/admin/rbac/roles/${roleId}/policies`,
235 {
236 query: queryParams,
237 headers,
238 }
239 )
240 }
241
242 /**
243 * This method retrieves a paginated list of users associated with an RBAC role.
244 * It sends a request to the List Role Users API route.
245 *
246 * @param roleId - The role's ID.
247 * @param queryParams - Filters and pagination configurations.
248 * @param headers - Headers to pass in the request.
249 * @returns The paginated list of users.
250 *
251 * @example
252 * sdk.admin.rbacRole.listUsers("role_123")
253 * .then(({ users, count, limit, offset }) => {
254 * console.log(users)
255 * })
256 */
257 async listUsers(
258 roleId: string,
259 queryParams?: HttpTypes.AdminRbacRoleUserListParams,
260 headers?: ClientHeaders
261 ) {
262 return await this.client.fetch<HttpTypes.AdminRbacRoleUserListResponse>(
263 `/admin/rbac/roles/${roleId}/users`,
264 {
265 query: queryParams,
266 headers,
267 }
268 )
269 }
270
271 /**
272 * This method adds users to an RBAC role. It sends a request to the
273 * Add Role Users API route.
274 *
275 * @param roleId - The role's ID.
276 * @param body - The users to add to the role.
277 * @param headers - Headers to pass in the request.
278 * @returns The role's users.
279 *
280 * @example
281 * sdk.admin.rbacRole.addUsers("role_123", {
282 * users: ["user_123"]
283 * })
284 * .then(({ users }) => {
285 * console.log(users)
286 * })
287 */
288 async addUsers(
289 roleId: string,
290 body: HttpTypes.AdminAssignRoleUsers,
291 headers?: ClientHeaders
292 ) {
293 return await this.client.fetch<HttpTypes.AdminRbacRoleUsersResponse>(
294 `/admin/rbac/roles/${roleId}/users`,
295 {
296 method: "POST",
297 headers,
298 body,
299 }
300 )
301 }
302
303 /**
304 * This method removes users from an RBAC role. It sends a request to the
305 * Remove Role Users API route.
306 *
307 * @param roleId - The role's ID.
308 * @param body - The users to remove from the role.
309 * @param headers - Headers to pass in the request.
310 * @returns The removal's details.
311 *
312 * @example
313 * sdk.admin.rbacRole.removeUsers("role_123", {
314 * users: ["user_123"]
315 * })
316 * .then(({ deleted }) => {
317 * console.log(deleted)
318 * })
319 */
320 async removeUsers(
321 roleId: string,
322 body: HttpTypes.AdminRemoveRoleUsers,
323 headers?: ClientHeaders
324 ) {
325 return await this.client.fetch<HttpTypes.AdminRbacRoleUsersDeleteResponse>(
326 `/admin/rbac/roles/${roleId}/users`,
327 {
328 method: "DELETE",
329 headers,
330 body,
331 }
332 )
333 }
334
335 /**
336 * This method adds policies to an RBAC role. It sends a request to the
337 * Add Role Policies API route.
338 *
339 * @param roleId - The role's ID.
340 * @param body - The policies to add to the role.
341 * @param headers - Headers to pass in the request.
342 * @returns The updated role with policies.
343 *
344 * @example
345 * sdk.admin.rbacRole.addPolicies("role_123", {
346 * policies: ["policy_123", "policy_456"]
347 * })
348 * .then(({ policies }) => {
349 * console.log(policies)
350 * })
351 */
352 async addPolicies(
353 roleId: string,
354 body: HttpTypes.AdminAddRolePolicies,
355 headers?: ClientHeaders
356 ) {
357 return await this.client.fetch<HttpTypes.AdminRbacPolicyListResponse>(
358 `/admin/rbac/roles/${roleId}/policies`,
359 {
360 method: "POST",
361 headers,
362 body,
363 }
364 )
365 }
366
367 /**
368 * This method removes a policy from an RBAC role. It sends a request to the
369 * Remove Role Policy API route.
370 *
371 * @param roleId - The role's ID.
372 * @param policyId - The policy's ID to remove.
373 * @param headers - Headers to pass in the request.
374 * @returns The deletion's details.
375 *
376 * @example
377 * sdk.admin.rbacRole.removePolicy("role_123", "policy_456")
378 * .then(({ deleted }) => {
379 * console.log(deleted)
380 * })
381 */
382 async removePolicy(
383 roleId: string,
384 policyId: string,
385 headers?: ClientHeaders
386 ) {
387 return await this.client.fetch<HttpTypes.AdminRbacPolicyDeleteResponse>(
388 `/admin/rbac/roles/${roleId}/policies/${policyId}`,
389 {
390 method: "DELETE",
391 headers,
392 }
393 )
394 }
395
396 /**
397 * Retrieves the authenticated actor's resolved permission set: a flat list
398 * of `resource:operation` strings with wildcards already expanded.
399 *
400 * @param headers - Headers to pass in the request.
401 * @returns The flat permission list.
402 *
403 * @example
404 * sdk.admin.rbacRole.mePermissions().then(({ permissions }) => {
405 * console.log(permissions) // ["product:read", "customer:read", ...]
406 * })
407 */
408 async mePermissions(headers?: ClientHeaders) {
409 return await this.client.fetch<HttpTypes.AdminRbacMePermissionsResponse>(
410 `/admin/rbac/me/permissions`,
411 {
412 method: "GET",
413 headers,
414 }
415 )
416 }
417
418 /**
419 * Lists the roles the authenticated actor is allowed to assign.
420 *
421 * @param queryParams - Filters and pagination configurations.
422 * @param headers - Headers to pass in the request.
423 * @returns The assignable roles.
424 */
425 async listAssignable(
426 queryParams?: HttpTypes.AdminRbacRoleListParams,
427 headers?: ClientHeaders
428 ) {
429 return await this.client.fetch<HttpTypes.AdminRbacAssignableRolesListResponse>(
430 `/admin/rbac/roles/assignable`,
431 {
432 method: "GET",
433 query: queryParams,
434 headers,
435 }
436 )
437 }
438 }