@omnibase/core-js 0.7.1 → 0.7.3

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.
@@ -20,12 +20,263 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/permissions/index.ts
21
21
  var permissions_exports = {};
22
22
  __export(permissions_exports, {
23
- PermissionsClient: () => PermissionsClient
23
+ PermissionsClient: () => PermissionsClient,
24
+ RolesHandler: () => RolesHandler
24
25
  });
25
26
  module.exports = __toCommonJS(permissions_exports);
26
27
 
27
28
  // src/permissions/handler.ts
28
29
  var import_client = require("@ory/client");
30
+
31
+ // src/permissions/roles.ts
32
+ var RolesHandler = class {
33
+ constructor(client) {
34
+ this.client = client;
35
+ }
36
+ /**
37
+ * Get available namespace definitions for UI
38
+ *
39
+ * Returns all namespaces and their available relations/permissions.
40
+ * Useful for building role configuration UIs.
41
+ *
42
+ * @returns List of namespace definitions
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const definitions = await omnibase.permissions.roles.getDefinitions();
47
+ *
48
+ * // Output: [{ namespace: 'Tenant', relations: ['invite_user', 'delete_tenant', ...] }]
49
+ * definitions.forEach(def => {
50
+ * console.log(`${def.namespace} supports: ${def.relations.join(', ')}`);
51
+ * });
52
+ * ```
53
+ */
54
+ async getDefinitions() {
55
+ const response = await this.client.fetch(
56
+ "/api/v1/permissions/definitions",
57
+ {
58
+ method: "GET"
59
+ }
60
+ );
61
+ const data = await response.json();
62
+ if (!response.ok || data.error) {
63
+ throw new Error(data.error || "Failed to fetch definitions");
64
+ }
65
+ return data.data.definitions;
66
+ }
67
+ /**
68
+ * List all roles for the current tenant
69
+ *
70
+ * Returns both system roles (defined in roles.config.json) and
71
+ * custom roles created via the API. System roles have `tenant_id = null`.
72
+ *
73
+ * @returns List of roles
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const roles = await omnibase.permissions.roles.list();
78
+ *
79
+ * const systemRoles = roles.filter(r => r.tenant_id === null);
80
+ * const customRoles = roles.filter(r => r.tenant_id !== null);
81
+ *
82
+ * console.log(`System roles: ${systemRoles.map(r => r.role_name).join(', ')}`);
83
+ * console.log(`Custom roles: ${customRoles.map(r => r.role_name).join(', ')}`);
84
+ * ```
85
+ */
86
+ async list() {
87
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
88
+ method: "GET"
89
+ });
90
+ const data = await response.json();
91
+ if (!response.ok || data.error) {
92
+ throw new Error(data.error || "Failed to list roles");
93
+ }
94
+ return data.data.roles;
95
+ }
96
+ /**
97
+ * Create a new custom role
98
+ *
99
+ * Creates a tenant-specific role with the specified permissions.
100
+ * Permissions use the format `namespace#relation` or `namespace:id#relation`.
101
+ *
102
+ * @param request - Role creation request
103
+ * @returns Created role
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const role = await omnibase.permissions.roles.create({
108
+ * role_name: 'billing_manager',
109
+ * permissions: [
110
+ * 'tenant#manage_billing',
111
+ * 'tenant#view_invoices',
112
+ * 'tenant#update_payment_methods'
113
+ * ]
114
+ * });
115
+ *
116
+ * console.log(`Created role: ${role.id}`);
117
+ * ```
118
+ *
119
+ * @example
120
+ * Resource-specific permissions:
121
+ * ```typescript
122
+ * const devRole = await omnibase.permissions.roles.create({
123
+ * role_name: 'project_developer',
124
+ * permissions: [
125
+ * 'project:proj_abc123#deploy',
126
+ * 'project:proj_abc123#view_logs',
127
+ * 'tenant#invite_user'
128
+ * ]
129
+ * });
130
+ * ```
131
+ */
132
+ async create(request) {
133
+ const response = await this.client.fetch("/api/v1/permissions/roles", {
134
+ method: "POST",
135
+ headers: { "Content-Type": "application/json" },
136
+ body: JSON.stringify(request)
137
+ });
138
+ const data = await response.json();
139
+ if (!response.ok || data.error) {
140
+ throw new Error(data.error || "Failed to create role");
141
+ }
142
+ return data.data;
143
+ }
144
+ /**
145
+ * Update an existing role's permissions
146
+ *
147
+ * Updates the permissions for a role and automatically updates all
148
+ * Keto relationships for users assigned to this role. Old permissions
149
+ * are removed and new ones are created.
150
+ *
151
+ * @param roleId - ID of role to update
152
+ * @param request - Update request with new permissions
153
+ * @returns Updated role
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const updatedRole = await omnibase.permissions.roles.update('role_123', {
158
+ * permissions: [
159
+ * 'tenant#manage_billing',
160
+ * 'tenant#view_invoices',
161
+ * 'tenant#manage_users' // Added new permission
162
+ * ]
163
+ * });
164
+ *
165
+ * console.log(`Updated role with ${updatedRole.permissions.length} permissions`);
166
+ * ```
167
+ */
168
+ async update(roleId, request) {
169
+ const response = await this.client.fetch(
170
+ `/api/v1/permissions/roles/${roleId}`,
171
+ {
172
+ method: "PUT",
173
+ headers: { "Content-Type": "application/json" },
174
+ body: JSON.stringify(request)
175
+ }
176
+ );
177
+ const data = await response.json();
178
+ if (!response.ok || data.error) {
179
+ throw new Error(data.error || "Failed to update role");
180
+ }
181
+ return data.data;
182
+ }
183
+ /**
184
+ * Delete a role
185
+ *
186
+ * Deletes the role and automatically removes all Keto relationships
187
+ * for users assigned to this role. Cannot delete system roles.
188
+ *
189
+ * @param roleId - ID of role to delete
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * await omnibase.permissions.roles.delete('role_123');
194
+ * console.log('Role deleted successfully');
195
+ * ```
196
+ */
197
+ async delete(roleId) {
198
+ const response = await this.client.fetch(
199
+ `/api/v1/permissions/roles/${roleId}`,
200
+ {
201
+ method: "DELETE"
202
+ }
203
+ );
204
+ const data = await response.json();
205
+ if (!response.ok || data.error) {
206
+ throw new Error(data.error || "Failed to delete role");
207
+ }
208
+ }
209
+ /**
210
+ * Assign a role to a user
211
+ *
212
+ * Assigns a role to a user and automatically creates all necessary
213
+ * Keto relationship tuples based on the role's permissions. The user
214
+ * immediately gains all permissions defined in the role.
215
+ *
216
+ * Supports assignment by either role ID or role name for flexibility.
217
+ *
218
+ * @param userId - ID of user to assign role to
219
+ * @param request - Assignment request with either role_id or role_name
220
+ *
221
+ * @example
222
+ * Assign by role ID:
223
+ * ```typescript
224
+ * await omnibase.permissions.roles.assign('user_123', {
225
+ * role_id: 'role_456'
226
+ * });
227
+ * ```
228
+ *
229
+ * @example
230
+ * Assign by role name (system or custom role):
231
+ * ```typescript
232
+ * // Assign system role
233
+ * await omnibase.permissions.roles.assign('user_123', {
234
+ * role_name: 'owner'
235
+ * });
236
+ *
237
+ * // Assign custom role
238
+ * await omnibase.permissions.roles.assign('user_456', {
239
+ * role_name: 'billing_manager'
240
+ * });
241
+ * ```
242
+ *
243
+ * @example
244
+ * Verify permissions after assignment:
245
+ * ```typescript
246
+ * await omnibase.permissions.roles.assign('user_123', {
247
+ * role_name: 'admin'
248
+ * });
249
+ *
250
+ * // User now has all permissions from the admin role
251
+ * const canManage = await omnibase.permissions.permissions.checkPermission(
252
+ * undefined,
253
+ * {
254
+ * namespace: 'Tenant',
255
+ * object: 'tenant_789',
256
+ * relation: 'manage_billing',
257
+ * subjectId: 'user_123'
258
+ * }
259
+ * );
260
+ * // canManage.data.allowed === true
261
+ * ```
262
+ */
263
+ async assign(userId, request) {
264
+ const response = await this.client.fetch(
265
+ `/api/v1/permissions/users/${userId}/roles`,
266
+ {
267
+ method: "POST",
268
+ headers: { "Content-Type": "application/json" },
269
+ body: JSON.stringify(request)
270
+ }
271
+ );
272
+ const data = await response.json();
273
+ if (!response.ok || data.error) {
274
+ throw new Error(data.error || "Failed to assign role");
275
+ }
276
+ }
277
+ };
278
+
279
+ // src/permissions/handler.ts
29
280
  var PermissionsClient = class {
30
281
  /**
31
282
  * Ory Keto RelationshipApi for managing subject-object relationships
@@ -90,6 +341,31 @@ var PermissionsClient = class {
90
341
  * @group Permissions
91
342
  */
92
343
  permissions;
344
+ /**
345
+ * Handler for managing roles and role-based permissions
346
+ *
347
+ * Provides methods for creating custom roles, assigning permissions,
348
+ * and managing role assignments. Works alongside the Keto-based
349
+ * permissions system to provide dynamic RBAC capabilities.
350
+ *
351
+ * @example
352
+ * ```typescript
353
+ * // Create a custom role
354
+ * const role = await omnibase.permissions.roles.create({
355
+ * role_name: 'billing_manager',
356
+ * permissions: ['tenant#manage_billing', 'tenant#view_invoices']
357
+ * });
358
+ *
359
+ * // Assign role to user
360
+ * await omnibase.permissions.roles.assign('user_123', {
361
+ * role_id: role.id
362
+ * });
363
+ * ```
364
+ *
365
+ * @since 0.7.0
366
+ * @group Roles
367
+ */
368
+ roles;
93
369
  /**
94
370
  * Creates a new PermissionsClient instance
95
371
  *
@@ -98,24 +374,19 @@ var PermissionsClient = class {
98
374
  * for optimal performance and security separation.
99
375
  *
100
376
  * @param apiBaseUrl - The base URL for your Omnibase API instance
377
+ * @param client - The main OmnibaseClient instance (for roles handler)
101
378
  *
102
379
  * @throws {Error} When the base URL is invalid or cannot be reached
103
380
  *
104
381
  * @example
105
382
  * ```typescript
106
- * const client = new PermissionsClient('https://api.example.com');
107
- * ```
108
- *
109
- * @example
110
- * Local development:
111
- * ```typescript
112
- * const client = new PermissionsClient('http://localhost:8080');
383
+ * const client = new PermissionsClient('https://api.example.com', omnibaseClient);
113
384
  * ```
114
385
  *
115
386
  * @since 1.0.0
116
387
  * @group Client
117
388
  */
118
- constructor(apiBaseUrl) {
389
+ constructor(apiBaseUrl, client) {
119
390
  this.relationships = new import_client.RelationshipApi(
120
391
  void 0,
121
392
  `${apiBaseUrl}/api/v1/permissions/write`
@@ -124,9 +395,11 @@ var PermissionsClient = class {
124
395
  void 0,
125
396
  `${apiBaseUrl}/api/v1/permissions/read`
126
397
  );
398
+ this.roles = new RolesHandler(client);
127
399
  }
128
400
  };
129
401
  // Annotate the CommonJS export names for ESM import in node:
130
402
  0 && (module.exports = {
131
- PermissionsClient
403
+ PermissionsClient,
404
+ RolesHandler
132
405
  });
@@ -1,195 +1,2 @@
1
- import { RelationshipApi, PermissionApi } from '@ory/client';
2
-
3
- /**
4
- * Client for managing permissions and relationships using Ory Keto
5
- *
6
- * This client provides access to Ory Keto's permission system, allowing you to
7
- * create, manage, and check relationships between subjects and objects. It handles
8
- * both read operations (permission checks) and write operations (relationship management).
9
- *
10
- * The client automatically configures separate endpoints for read and write operations
11
- * to optimize performance and security by following Ory Keto's recommended architecture.
12
- *
13
- * @example
14
- * Basic permission checking:
15
- * ```typescript
16
- * import { PermissionsClient } from '@omnibase/core-js/permissions';
17
- *
18
- * const permissionsClient = new PermissionsClient('https://api.example.com');
19
- *
20
- * // Check if a user can view a tenant
21
- * const canView = await permissionsClient.permissions.checkPermission(
22
- * undefined,
23
- * {
24
- * namespace: 'Tenant',
25
- * object: 'tenant_123',
26
- * relation: 'view',
27
- * subjectId: 'user_456'
28
- * }
29
- * );
30
- *
31
- * if (canView.data.allowed) {
32
- * console.log('User can view the tenant');
33
- * }
34
- * ```
35
- *
36
- * @example
37
- * Creating tenant relationships:
38
- * ```typescript
39
- * // Create a relationship making a user an owner of a tenant
40
- * await permissionsClient.relationships.createRelationship(
41
- * undefined,
42
- * {
43
- * namespace: 'Tenant',
44
- * object: 'tenant_123',
45
- * relation: 'owners',
46
- * subjectId: 'user_456'
47
- * }
48
- * );
49
- *
50
- * // Now the user has owner permissions on the tenant
51
- * console.log('User is now an owner of the tenant');
52
- * ```
53
- *
54
- * @example
55
- * Complex tenant permission management:
56
- * ```typescript
57
- * const tenantId = 'tenant_123';
58
- * const userId = 'user_456';
59
- *
60
- * // Grant admin permissions to a user
61
- * await permissionsClient.relationships.createRelationship(
62
- * undefined,
63
- * {
64
- * namespace: 'Tenant',
65
- * object: tenantId,
66
- * relation: 'admins',
67
- * subjectId: userId
68
- * }
69
- * );
70
- *
71
- * // Check if user can manage members (admins and owners can)
72
- * const canManageMembers = await permissionsClient.permissions.checkPermission(
73
- * undefined,
74
- * {
75
- * namespace: 'Tenant',
76
- * object: tenantId,
77
- * relation: 'manage_members',
78
- * subjectId: userId
79
- * }
80
- * );
81
- *
82
- * if (canManageMembers.data.allowed) {
83
- * // User can invite/remove members
84
- * console.log('User can manage tenant members');
85
- * }
86
- *
87
- * // Later, remove admin permissions
88
- * await permissionsClient.relationships.deleteRelationships(
89
- * undefined,
90
- * {
91
- * namespace: 'Tenant',
92
- * object: tenantId,
93
- * relation: 'admins',
94
- * subjectId: userId
95
- * }
96
- * );
97
- * ```
98
- *
99
- * @since 1.0.0
100
- * @public
101
- * @group Client
102
- */
103
- declare class PermissionsClient {
104
- /**
105
- * Ory Keto RelationshipApi for managing subject-object relationships
106
- *
107
- * Provides methods for creating, updating, and deleting relationships between
108
- * subjects (users, groups) and objects (tenants, resources). This API handles
109
- * write operations and is used to establish permission structures.
110
- *
111
- * Key methods:
112
- * - `createRelationship()` - Creates a new relationship tuple
113
- * - `deleteRelationships()` - Removes existing relationship tuples
114
- * - `getRelationships()` - Queries existing relationships
115
- * - `patchRelationships()` - Updates multiple relationships atomically
116
- *
117
- * @example
118
- * ```typescript
119
- * // Create a relationship
120
- * await client.relationships.createRelationship(
121
- * undefined,
122
- * {
123
- * namespace: 'Tenant',
124
- * object: 'tenant_123',
125
- * relation: 'members',
126
- * subjectId: 'user_456'
127
- * }
128
- * );
129
- * ```
130
- *
131
- * @since 1.0.0
132
- * @group Relationships
133
- */
134
- relationships: RelationshipApi;
135
- /**
136
- * Ory Keto PermissionApi for checking permissions
137
- *
138
- * Provides methods for querying whether a subject has a specific permission
139
- * on an object. This API handles read operations and is optimized for fast
140
- * permission checks in your application logic.
141
- *
142
- * Key methods:
143
- * - `checkPermission()` - Checks if a subject has permission on an object
144
- * - `checkPermissionOrError()` - Same as above but throws error if denied
145
- * - `expandPermissions()` - Expands relationships to show all granted permissions
146
- *
147
- * @example
148
- * ```typescript
149
- * // Check permission
150
- * const result = await client.permissions.checkPermission(
151
- * undefined,
152
- * {
153
- * namespace: 'Tenant',
154
- * object: 'tenant_123',
155
- * relation: 'view',
156
- * subjectId: 'user_456'
157
- * }
158
- * );
159
- *
160
- * console.log('Has permission:', result.data.allowed);
161
- * ```
162
- *
163
- * @since 1.0.0
164
- * @group Permissions
165
- */
166
- permissions: PermissionApi;
167
- /**
168
- * Creates a new PermissionsClient instance
169
- *
170
- * Initializes the client with separate endpoints for read and write operations.
171
- * The client automatically appends the appropriate Keto API paths to the base URL
172
- * for optimal performance and security separation.
173
- *
174
- * @param apiBaseUrl - The base URL for your Omnibase API instance
175
- *
176
- * @throws {Error} When the base URL is invalid or cannot be reached
177
- *
178
- * @example
179
- * ```typescript
180
- * const client = new PermissionsClient('https://api.example.com');
181
- * ```
182
- *
183
- * @example
184
- * Local development:
185
- * ```typescript
186
- * const client = new PermissionsClient('http://localhost:8080');
187
- * ```
188
- *
189
- * @since 1.0.0
190
- * @group Client
191
- */
192
- constructor(apiBaseUrl: string);
193
- }
194
-
195
- export { PermissionsClient };
1
+ export { A as AssignRoleRequest, C as CreateRoleRequest, N as NamespaceDefinition, P as PermissionsClient, a as Role, R as RolesHandler, U as UpdateRoleRequest } from '../payments/index.cjs';
2
+ import '@ory/client';