@omnibase/core-js 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,378 @@
1
+ // src/permissions/handler.ts
2
+ import { RelationshipApi, PermissionApi } from "@ory/client";
3
+
4
+ // src/permissions/roles.ts
5
+ var RolesHandler = class {
6
+ constructor(client) {
7
+ this.client = client;
8
+ }
9
+ /**
10
+ * Get available namespace definitions for UI
11
+ *
12
+ * Returns all namespaces and their available relations/permissions.
13
+ * Useful for building role configuration UIs.
14
+ *
15
+ * @returns List of namespace definitions
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const definitions = await omnibase.permissions.roles.getDefinitions();
20
+ *
21
+ * // Output: [{ namespace: 'Tenant', relations: ['invite_user', 'delete_tenant', ...] }]
22
+ * definitions.forEach(def => {
23
+ * console.log(`${def.namespace} supports: ${def.relations.join(', ')}`);
24
+ * });
25
+ * ```
26
+ */
27
+ async getDefinitions() {
28
+ const response = await this.client.fetch(
29
+ "/api/v1/permissions/definitions",
30
+ {
31
+ method: "GET"
32
+ }
33
+ );
34
+ const data = await response.json();
35
+ if (!response.ok || data.error) {
36
+ throw new Error(data.error || "Failed to fetch definitions");
37
+ }
38
+ return data.data.definitions;
39
+ }
40
+ /**
41
+ * List all roles for the current tenant
42
+ *
43
+ * Returns both system roles (defined in roles.config.json) and
44
+ * custom roles created via the API. System roles have `tenant_id = null`.
45
+ *
46
+ * @returns List of roles
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const roles = await omnibase.permissions.roles.list();
51
+ *
52
+ * const systemRoles = roles.filter(r => r.tenant_id === null);
53
+ * const customRoles = roles.filter(r => r.tenant_id !== null);
54
+ *
55
+ * console.log(`System roles: ${systemRoles.map(r => r.role_name).join(', ')}`);
56
+ * console.log(`Custom roles: ${customRoles.map(r => r.role_name).join(', ')}`);
57
+ * ```
58
+ */
59
+ async list() {
60
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
61
+ method: "GET"
62
+ });
63
+ const data = await response.json();
64
+ if (!response.ok || data.error) {
65
+ throw new Error(data.error || "Failed to list roles");
66
+ }
67
+ return data.data.roles;
68
+ }
69
+ /**
70
+ * Create a new custom role
71
+ *
72
+ * Creates a tenant-specific role with the specified permissions.
73
+ * Permissions use the format `namespace#relation` or `namespace:id#relation`.
74
+ *
75
+ * @param request - Role creation request
76
+ * @returns Created role
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const role = await omnibase.permissions.roles.create({
81
+ * role_name: 'billing_manager',
82
+ * permissions: [
83
+ * 'tenant#manage_billing',
84
+ * 'tenant#view_invoices',
85
+ * 'tenant#update_payment_methods'
86
+ * ]
87
+ * });
88
+ *
89
+ * console.log(`Created role: ${role.id}`);
90
+ * ```
91
+ *
92
+ * @example
93
+ * Resource-specific permissions:
94
+ * ```typescript
95
+ * const devRole = await omnibase.permissions.roles.create({
96
+ * role_name: 'project_developer',
97
+ * permissions: [
98
+ * 'project:proj_abc123#deploy',
99
+ * 'project:proj_abc123#view_logs',
100
+ * 'tenant#invite_user'
101
+ * ]
102
+ * });
103
+ * ```
104
+ */
105
+ async create(request) {
106
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
107
+ method: "POST",
108
+ headers: { "Content-Type": "application/json" },
109
+ body: JSON.stringify(request)
110
+ });
111
+ const data = await response.json();
112
+ if (!response.ok || data.error) {
113
+ throw new Error(data.error || "Failed to create role");
114
+ }
115
+ return data.data;
116
+ }
117
+ /**
118
+ * Update an existing role's permissions
119
+ *
120
+ * Updates the permissions for a role and automatically updates all
121
+ * Keto relationships for users assigned to this role. Old permissions
122
+ * are removed and new ones are created.
123
+ *
124
+ * @param roleId - ID of role to update
125
+ * @param request - Update request with new permissions
126
+ * @returns Updated role
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const updatedRole = await omnibase.permissions.roles.update('role_123', {
131
+ * permissions: [
132
+ * 'tenant#manage_billing',
133
+ * 'tenant#view_invoices',
134
+ * 'tenant#manage_users' // Added new permission
135
+ * ]
136
+ * });
137
+ *
138
+ * console.log(`Updated role with ${updatedRole.permissions.length} permissions`);
139
+ * ```
140
+ */
141
+ async update(roleId, request) {
142
+ const response = await this.client.fetch(
143
+ `/api/v1/permissions/roles/${roleId}`,
144
+ {
145
+ method: "PUT",
146
+ headers: { "Content-Type": "application/json" },
147
+ body: JSON.stringify(request)
148
+ }
149
+ );
150
+ const data = await response.json();
151
+ if (!response.ok || data.error) {
152
+ throw new Error(data.error || "Failed to update role");
153
+ }
154
+ return data.data;
155
+ }
156
+ /**
157
+ * Delete a role
158
+ *
159
+ * Deletes the role and automatically removes all Keto relationships
160
+ * for users assigned to this role. Cannot delete system roles.
161
+ *
162
+ * @param roleId - ID of role to delete
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * await omnibase.permissions.roles.delete('role_123');
167
+ * console.log('Role deleted successfully');
168
+ * ```
169
+ */
170
+ async delete(roleId) {
171
+ const response = await this.client.fetch(
172
+ `/api/v1/permissions/roles/${roleId}`,
173
+ {
174
+ method: "DELETE"
175
+ }
176
+ );
177
+ const data = await response.json();
178
+ if (!response.ok || data.error) {
179
+ throw new Error(data.error || "Failed to delete role");
180
+ }
181
+ }
182
+ /**
183
+ * Assign a role to a user
184
+ *
185
+ * Assigns a role to a user and automatically creates all necessary
186
+ * Keto relationship tuples based on the role's permissions. The user
187
+ * immediately gains all permissions defined in the role.
188
+ *
189
+ * Supports assignment by either role ID or role name for flexibility.
190
+ *
191
+ * @param userId - ID of user to assign role to
192
+ * @param request - Assignment request with either role_id or role_name
193
+ *
194
+ * @example
195
+ * Assign by role ID:
196
+ * ```typescript
197
+ * await omnibase.permissions.roles.assign('user_123', {
198
+ * role_id: 'role_456'
199
+ * });
200
+ * ```
201
+ *
202
+ * @example
203
+ * Assign by role name (system or custom role):
204
+ * ```typescript
205
+ * // Assign system role
206
+ * await omnibase.permissions.roles.assign('user_123', {
207
+ * role_name: 'owner'
208
+ * });
209
+ *
210
+ * // Assign custom role
211
+ * await omnibase.permissions.roles.assign('user_456', {
212
+ * role_name: 'billing_manager'
213
+ * });
214
+ * ```
215
+ *
216
+ * @example
217
+ * Verify permissions after assignment:
218
+ * ```typescript
219
+ * await omnibase.permissions.roles.assign('user_123', {
220
+ * role_name: 'admin'
221
+ * });
222
+ *
223
+ * // User now has all permissions from the admin role
224
+ * const canManage = await omnibase.permissions.permissions.checkPermission(
225
+ * undefined,
226
+ * {
227
+ * namespace: 'Tenant',
228
+ * object: 'tenant_789',
229
+ * relation: 'manage_billing',
230
+ * subjectId: 'user_123'
231
+ * }
232
+ * );
233
+ * // canManage.data.allowed === true
234
+ * ```
235
+ */
236
+ async assign(userId, request) {
237
+ const response = await this.client.fetch(
238
+ `/api/v1/permissions/users/${userId}/roles`,
239
+ {
240
+ method: "POST",
241
+ headers: { "Content-Type": "application/json" },
242
+ body: JSON.stringify(request)
243
+ }
244
+ );
245
+ const data = await response.json();
246
+ if (!response.ok || data.error) {
247
+ throw new Error(data.error || "Failed to assign role");
248
+ }
249
+ }
250
+ };
251
+
252
+ // src/permissions/handler.ts
253
+ var PermissionsClient = class {
254
+ /**
255
+ * Ory Keto RelationshipApi for managing subject-object relationships
256
+ *
257
+ * Provides methods for creating, updating, and deleting relationships between
258
+ * subjects (users, groups) and objects (tenants, resources). This API handles
259
+ * write operations and is used to establish permission structures.
260
+ *
261
+ * Key methods:
262
+ * - `createRelationship()` - Creates a new relationship tuple
263
+ * - `deleteRelationships()` - Removes existing relationship tuples
264
+ * - `getRelationships()` - Queries existing relationships
265
+ * - `patchRelationships()` - Updates multiple relationships atomically
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * // Create a relationship
270
+ * await client.relationships.createRelationship(
271
+ * undefined,
272
+ * {
273
+ * namespace: 'Tenant',
274
+ * object: 'tenant_123',
275
+ * relation: 'members',
276
+ * subjectId: 'user_456'
277
+ * }
278
+ * );
279
+ * ```
280
+ *
281
+ * @since 1.0.0
282
+ * @group Relationships
283
+ */
284
+ relationships;
285
+ /**
286
+ * Ory Keto PermissionApi for checking permissions
287
+ *
288
+ * Provides methods for querying whether a subject has a specific permission
289
+ * on an object. This API handles read operations and is optimized for fast
290
+ * permission checks in your application logic.
291
+ *
292
+ * Key methods:
293
+ * - `checkPermission()` - Checks if a subject has permission on an object
294
+ * - `checkPermissionOrError()` - Same as above but throws error if denied
295
+ * - `expandPermissions()` - Expands relationships to show all granted permissions
296
+ *
297
+ * @example
298
+ * ```typescript
299
+ * // Check permission
300
+ * const result = await client.permissions.checkPermission(
301
+ * undefined,
302
+ * {
303
+ * namespace: 'Tenant',
304
+ * object: 'tenant_123',
305
+ * relation: 'view',
306
+ * subjectId: 'user_456'
307
+ * }
308
+ * );
309
+ *
310
+ * console.log('Has permission:', result.data.allowed);
311
+ * ```
312
+ *
313
+ * @since 1.0.0
314
+ * @group Permissions
315
+ */
316
+ permissions;
317
+ /**
318
+ * Handler for managing roles and role-based permissions
319
+ *
320
+ * Provides methods for creating custom roles, assigning permissions,
321
+ * and managing role assignments. Works alongside the Keto-based
322
+ * permissions system to provide dynamic RBAC capabilities.
323
+ *
324
+ * @example
325
+ * ```typescript
326
+ * // Create a custom role
327
+ * const role = await omnibase.permissions.roles.create({
328
+ * role_name: 'billing_manager',
329
+ * permissions: ['tenant#manage_billing', 'tenant#view_invoices']
330
+ * });
331
+ *
332
+ * // Assign role to user
333
+ * await omnibase.permissions.roles.assign('user_123', {
334
+ * role_id: role.id
335
+ * });
336
+ * ```
337
+ *
338
+ * @since 0.7.0
339
+ * @group Roles
340
+ */
341
+ roles;
342
+ /**
343
+ * Creates a new PermissionsClient instance
344
+ *
345
+ * Initializes the client with separate endpoints for read and write operations.
346
+ * The client automatically appends the appropriate Keto API paths to the base URL
347
+ * for optimal performance and security separation.
348
+ *
349
+ * @param apiBaseUrl - The base URL for your Omnibase API instance
350
+ * @param client - The main OmnibaseClient instance (for roles handler)
351
+ *
352
+ * @throws {Error} When the base URL is invalid or cannot be reached
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const client = new PermissionsClient('https://api.example.com', omnibaseClient);
357
+ * ```
358
+ *
359
+ * @since 1.0.0
360
+ * @group Client
361
+ */
362
+ constructor(apiBaseUrl, client) {
363
+ this.relationships = new RelationshipApi(
364
+ void 0,
365
+ `${apiBaseUrl}/api/v1/permissions/write`
366
+ );
367
+ this.permissions = new PermissionApi(
368
+ void 0,
369
+ `${apiBaseUrl}/api/v1/permissions/read`
370
+ );
371
+ this.roles = new RolesHandler(client);
372
+ }
373
+ };
374
+
375
+ export {
376
+ RolesHandler,
377
+ PermissionsClient
378
+ };
package/dist/index.cjs CHANGED
@@ -537,9 +537,12 @@ var RolesHandler = class {
537
537
  * ```
538
538
  */
539
539
  async getDefinitions() {
540
- const response = await this.client.fetch("/api/v1/roles/definitions", {
541
- method: "GET"
542
- });
540
+ const response = await this.client.fetch(
541
+ "/api/v1/permissions/definitions",
542
+ {
543
+ method: "GET"
544
+ }
545
+ );
543
546
  const data = await response.json();
544
547
  if (!response.ok || data.error) {
545
548
  throw new Error(data.error || "Failed to fetch definitions");
@@ -566,7 +569,7 @@ var RolesHandler = class {
566
569
  * ```
567
570
  */
568
571
  async list() {
569
- const response = await this.client.fetch("/api/v1/roles/roles", {
572
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
570
573
  method: "GET"
571
574
  });
572
575
  const data = await response.json();
@@ -612,7 +615,7 @@ var RolesHandler = class {
612
615
  * ```
613
616
  */
614
617
  async create(request) {
615
- const response = await this.client.fetch("/api/v1/roles/roles", {
618
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
616
619
  method: "POST",
617
620
  headers: { "Content-Type": "application/json" },
618
621
  body: JSON.stringify(request)
@@ -648,11 +651,14 @@ var RolesHandler = class {
648
651
  * ```
649
652
  */
650
653
  async update(roleId, request) {
651
- const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
652
- method: "PUT",
653
- headers: { "Content-Type": "application/json" },
654
- body: JSON.stringify(request)
655
- });
654
+ const response = await this.client.fetch(
655
+ `/api/v1/permissions/roles/${roleId}`,
656
+ {
657
+ method: "PUT",
658
+ headers: { "Content-Type": "application/json" },
659
+ body: JSON.stringify(request)
660
+ }
661
+ );
656
662
  const data = await response.json();
657
663
  if (!response.ok || data.error) {
658
664
  throw new Error(data.error || "Failed to update role");
@@ -674,9 +680,12 @@ var RolesHandler = class {
674
680
  * ```
675
681
  */
676
682
  async delete(roleId) {
677
- const response = await this.client.fetch(`/api/v1/roles/roles/${roleId}`, {
678
- method: "DELETE"
679
- });
683
+ const response = await this.client.fetch(
684
+ `/api/v1/permissions/roles/${roleId}`,
685
+ {
686
+ method: "DELETE"
687
+ }
688
+ );
680
689
  const data = await response.json();
681
690
  if (!response.ok || data.error) {
682
691
  throw new Error(data.error || "Failed to delete role");
@@ -689,18 +698,42 @@ var RolesHandler = class {
689
698
  * Keto relationship tuples based on the role's permissions. The user
690
699
  * immediately gains all permissions defined in the role.
691
700
  *
701
+ * Supports assignment by either role ID or role name for flexibility.
702
+ *
692
703
  * @param userId - ID of user to assign role to
693
- * @param request - Assignment request with role ID
704
+ * @param request - Assignment request with either role_id or role_name
694
705
  *
695
706
  * @example
707
+ * Assign by role ID:
696
708
  * ```typescript
697
- * // Assign billing manager role to user
698
709
  * await omnibase.permissions.roles.assign('user_123', {
699
710
  * role_id: 'role_456'
700
711
  * });
712
+ * ```
713
+ *
714
+ * @example
715
+ * Assign by role name (system or custom role):
716
+ * ```typescript
717
+ * // Assign system role
718
+ * await omnibase.permissions.roles.assign('user_123', {
719
+ * role_name: 'owner'
720
+ * });
701
721
  *
702
- * // User now has all permissions from the role
703
- * const canManageBilling = await omnibase.permissions.permissions.checkPermission(
722
+ * // Assign custom role
723
+ * await omnibase.permissions.roles.assign('user_456', {
724
+ * role_name: 'billing_manager'
725
+ * });
726
+ * ```
727
+ *
728
+ * @example
729
+ * Verify permissions after assignment:
730
+ * ```typescript
731
+ * await omnibase.permissions.roles.assign('user_123', {
732
+ * role_name: 'admin'
733
+ * });
734
+ *
735
+ * // User now has all permissions from the admin role
736
+ * const canManage = await omnibase.permissions.permissions.checkPermission(
704
737
  * undefined,
705
738
  * {
706
739
  * namespace: 'Tenant',
@@ -709,12 +742,12 @@ var RolesHandler = class {
709
742
  * subjectId: 'user_123'
710
743
  * }
711
744
  * );
712
- * // canManageBilling.data.allowed === true
745
+ * // canManage.data.allowed === true
713
746
  * ```
714
747
  */
715
748
  async assign(userId, request) {
716
749
  const response = await this.client.fetch(
717
- `/api/v1/roles/users/${userId}/roles`,
750
+ `/api/v1/permissions/users/${userId}/roles`,
718
751
  {
719
752
  method: "POST",
720
753
  headers: { "Content-Type": "application/json" },
@@ -1256,8 +1289,6 @@ var TenantManger = class {
1256
1289
  * - Billing subscriptions are cancelled (if applicable)
1257
1290
  * - Audit logs for deletion are maintained for compliance
1258
1291
  *
1259
- * @param tenantId - The unique identifier of the tenant to delete
1260
- *
1261
1292
  * @returns Promise resolving to a confirmation message
1262
1293
  *
1263
1294
  * @throws {Error} When the tenantId parameter is missing or empty
@@ -1269,8 +1300,6 @@ var TenantManger = class {
1269
1300
  *
1270
1301
  * @example
1271
1302
  * ```typescript
1272
- * const tenantToDelete = 'tenant_abc123';
1273
- *
1274
1303
  * // Always confirm before deleting
1275
1304
  * const userConfirmed = confirm(
1276
1305
  * 'Are you sure you want to delete this tenant? This action cannot be undone.'
@@ -1278,7 +1307,7 @@ var TenantManger = class {
1278
1307
  *
1279
1308
  * if (userConfirmed) {
1280
1309
  * try {
1281
- * const result = await tenantManager.deleteTenant(tenantToDelete);
1310
+ * const result = await tenantManager.deleteTenant();
1282
1311
  * console.log(result.data.message);
1283
1312
  *
1284
1313
  * // Redirect user away from deleted tenant
@@ -1293,21 +1322,15 @@ var TenantManger = class {
1293
1322
  * @public
1294
1323
  * @group Tenant Management
1295
1324
  */
1296
- async deleteTenant(tenantId) {
1297
- if (!tenantId) {
1298
- throw new Error("Tenant ID is required");
1299
- }
1325
+ async deleteTenant() {
1300
1326
  try {
1301
- const response = await this.omnibaseClient.fetch(
1302
- `/api/v1/tenants/${tenantId}`,
1303
- {
1304
- method: "DELETE",
1305
- headers: {
1306
- "Content-Type": "application/json"
1307
- },
1308
- credentials: "include"
1309
- }
1310
- );
1327
+ const response = await this.omnibaseClient.fetch(`/api/v1/tenants`, {
1328
+ method: "DELETE",
1329
+ headers: {
1330
+ "Content-Type": "application/json"
1331
+ },
1332
+ credentials: "include"
1333
+ });
1311
1334
  if (!response.ok) {
1312
1335
  const errorData = await response.text();
1313
1336
  throw new Error(
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PermissionsClient,
3
3
  RolesHandler
4
- } from "./chunk-JNM7XP7L.js";
4
+ } from "./chunk-V4FWENQQ.js";
5
5
  import {
6
6
  PaymentHandler
7
7
  } from "./chunk-QPW6G4PA.js";
@@ -10,7 +10,7 @@ import {
10
10
  } from "./chunk-I6DMWC32.js";
11
11
  import {
12
12
  TenantHandler
13
- } from "./chunk-6OGESVXW.js";
13
+ } from "./chunk-DQBFDKRC.js";
14
14
 
15
15
  // src/client.ts
16
16
  var OmnibaseClient = class {