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 Policy API routes.
7 *
8 * @since 2.15.5
9 */
10 export class RbacPolicy {
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 policies. It sends a request to the
24 * List RBAC Policies 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 policies.
29 *
30 * @example
31 * To retrieve the list of policies:
32 *
33 * ```ts
34 * sdk.admin.rbacPolicy.list()
35 * .then(({ policies, count, limit, offset }) => {
36 * console.log(policies)
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.rbacPolicy.list({
46 * limit: 10,
47 * offset: 10
48 * })
49 * .then(({ policies, count, limit, offset }) => {
50 * console.log(policies)
51 * })
52 * ```
53 *
54 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
55 * in each policy:
56 *
57 * ```ts
58 * sdk.admin.rbacPolicy.list({
59 * fields: "id,key,resource,operation"
60 * })
61 * .then(({ policies, count, limit, offset }) => {
62 * console.log(policies)
63 * })
64 * ```
65 *
66 * @tags rbac
67 * @since 2.15.5
68 */
69 async list(
70 queryParams?: HttpTypes.AdminRbacPolicyListParams,
71 headers?: ClientHeaders
72 ) {
73 return await this.client.fetch<HttpTypes.AdminRbacPolicyListResponse>(
74 `/admin/rbac/policies`,
75 {
76 query: queryParams,
77 headers,
78 }
79 )
80 }
81
82 /**
83 * This method retrieves an RBAC policy's details. It sends a request to the
84 * Get RBAC Policy API route.
85 *
86 * @param id - The policy's ID.
87 * @param query - Configure the fields to retrieve in the policy.
88 * @param headers - Headers to pass in the request.
89 * @returns The policy's details.
90 *
91 * @example
92 * sdk.admin.rbacPolicy.retrieve("policy_123")
93 * .then(({ policy }) => {
94 * console.log(policy)
95 * })
96 *
97 * @tags rbac
98 * @since 2.15.5
99 */
100 async retrieve(
101 id: string,
102 query?: HttpTypes.AdminRbacPolicyParams,
103 headers?: ClientHeaders
104 ) {
105 return await this.client.fetch<HttpTypes.AdminRbacPolicyResponse>(
106 `/admin/rbac/policies/${id}`,
107 {
108 query,
109 headers,
110 }
111 )
112 }
113
114 /**
115 * This method creates an RBAC policy. It sends a request to the Create RBAC Policy
116 * API route.
117 *
118 * @param body - The policy's details.
119 * @param query - Configure the fields to retrieve in the created policy.
120 * @param headers - Headers to pass in the request
121 * @returns The created policy
122 *
123 * @example
124 * sdk.admin.rbacPolicy.create({
125 * key: "product.read",
126 * resource: "product",
127 * operation: "read"
128 * })
129 * .then(({ policy }) => {
130 * console.log(policy)
131 * })
132 *
133 * @tags rbac
134 * @since 2.15.5
135 */
136 async create(
137 body: HttpTypes.AdminCreateRbacPolicy,
138 query?: HttpTypes.AdminRbacPolicyParams,
139 headers?: ClientHeaders
140 ) {
141 return await this.client.fetch<HttpTypes.AdminRbacPolicyResponse>(
142 `/admin/rbac/policies`,
143 {
144 method: "POST",
145 headers,
146 body,
147 query,
148 }
149 )
150 }
151
152 /**
153 * This method updates an RBAC policy's details. It sends a request to the
154 * Update RBAC Policy API route.
155 *
156 * @param id - The policy's ID.
157 * @param body - The data to update in the policy.
158 * @param query - Configure the fields to retrieve in the policy.
159 * @param headers - Headers to pass in the request.
160 * @returns The policy's details.
161 *
162 * @example
163 * sdk.admin.rbacPolicy.update("policy_123", {
164 * name: "Read Products",
165 * description: "Allows reading product information"
166 * })
167 * .then(({ policy }) => {
168 * console.log(policy)
169 * })
170 *
171 * @tags rbac
172 * @since 2.15.5
173 */
174 async update(
175 id: string,
176 body: HttpTypes.AdminUpdateRbacPolicy,
177 query?: HttpTypes.AdminRbacPolicyParams,
178 headers?: ClientHeaders
179 ) {
180 return await this.client.fetch<HttpTypes.AdminRbacPolicyResponse>(
181 `/admin/rbac/policies/${id}`,
182 {
183 method: "POST",
184 headers,
185 body,
186 query,
187 }
188 )
189 }
190
191 /**
192 * This method deletes an RBAC policy by its ID. It sends a request to the
193 * Delete RBAC Policy API route.
194 *
195 * @param id - The policy's ID.
196 * @param headers - Headers to pass in the request.
197 * @returns The deletion's details.
198 *
199 * @example
200 * sdk.admin.rbacPolicy.delete("policy_123")
201 * .then(({ deleted }) => {
202 * console.log(deleted)
203 * })
204 *
205 * @tags rbac
206 * @since 2.15.5
207 */
208 async delete(id: string, headers?: ClientHeaders) {
209 return await this.client.fetch<HttpTypes.AdminRbacPolicyDeleteResponse>(
210 `/admin/rbac/policies/${id}`,
211 {
212 method: "DELETE",
213 headers,
214 }
215 )
216 }
217
218 /**
219 * Lists the roles that include the given policy.
220 */
221 async listRoles(
222 id: string,
223 queryParams?: HttpTypes.AdminRbacPolicyRoleListParams,
224 headers?: ClientHeaders
225 ) {
226 return await this.client.fetch<HttpTypes.AdminRbacPolicyRolesListResponse>(
227 `/admin/rbac/policies/${id}/roles`,
228 {
229 query: queryParams,
230 headers,
231 }
232 )
233 }
234
235 /**
236 * Lists the policies the authenticated actor is allowed to assign.
237 *
238 * @param queryParams - Filters and pagination configurations.
239 * @param headers - Headers to pass in the request.
240 * @returns The assignable policies.
241 */
242 async listAssignable(
243 queryParams?: HttpTypes.AdminRbacPolicyListParams,
244 headers?: ClientHeaders
245 ) {
246 return await this.client.fetch<HttpTypes.AdminRbacAssignablePoliciesListResponse>(
247 `/admin/rbac/policies/assignable`,
248 {
249 method: "GET",
250 query: queryParams,
251 headers,
252 }
253 )
254 }
255 }