@omnibase/core-js 0.1.4 → 0.1.6

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.
package/README.md ADDED
@@ -0,0 +1,330 @@
1
+ # @omnibase/core-js
2
+
3
+
4
+ ***
5
+
6
+
7
+ OmniBase Core SDK - Comprehensive authentication and multi-tenant platform integration
8
+
9
+ The OmniBase Core SDK provides everything you need to integrate with the OmniBase platform,
10
+ including comprehensive authentication flows via Ory Kratos, multi-tenant management, user
11
+ invitations, and database operations. Built for both browser and Node.js environments with
12
+ full TypeScript support.
13
+
14
+ [![npm version](https://badge.fury.io/js/%40omnibase%2Fcore-js.svg)](https://badge.fury.io/js/%40omnibase%2Fcore-js)
15
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
16
+ [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
17
+
18
+ ## Features
19
+
20
+ - 🔐 **Authentication Flows** - Complete Ory Kratos integration with login, registration, recovery, settings, and verification
21
+ - 🏢 **Multi-tenant Management** - Organization lifecycle management with billing integration
22
+ - 👥 **User Invitations** - Secure tenant user invitation and acceptance workflows
23
+ - 🔄 **Tenant Switching** - Seamless switching between multiple tenant contexts
24
+ - 🗄️ **Database Integration** - Supabase PostgREST client for advanced database operations
25
+ - 🛡️ **Type Safe** - Full TypeScript definitions with comprehensive type safety
26
+ - 🌐 **Universal** - Works in browser and Node.js environments
27
+ - 🔒 **Secure** - HTTP-only cookies and JWT token-based authentication
28
+
29
+ ## Quick Start
30
+
31
+ ```typescript
32
+ import {
33
+ createTenant,
34
+ switchActiveTenant,
35
+ createTenantUserInvite,
36
+ acceptTenantInvite
37
+ } from '@omnibase/core-js';
38
+
39
+ // Create a new tenant
40
+ const tenant = await createTenant({
41
+ name: 'My Company',
42
+ billing_email: 'billing@company.com',
43
+ user_id: 'user_123'
44
+ });
45
+
46
+ if (tenant.status === 200 && tenant.data) {
47
+ // Switch to the new tenant
48
+ await switchActiveTenant(tenant.data.tenant.id);
49
+
50
+ // Invite a user to the tenant
51
+ const invite = await createTenantUserInvite(tenant.data.tenant.id, {
52
+ email: 'colleague@company.com'
53
+ });
54
+
55
+ if (invite.status === 200 && invite.data) {
56
+ console.log(`Invitation sent with token: ${invite.data.token}`);
57
+ }
58
+ }
59
+ ```
60
+
61
+ ## Installation
62
+
63
+ ```bash
64
+ npm install @omnibase/core-js
65
+ # or
66
+ yarn add @omnibase/core-js
67
+ # or
68
+ pnpm add @omnibase/core-js
69
+ # or
70
+ bun add @omnibase/core-js
71
+ ```
72
+
73
+ ## Modules
74
+
75
+ - **Authentication** - Ory Kratos flow management for login, registration, recovery, settings, and verification flows
76
+ - **Tenants** - Multi-tenant organization management including creation, deletion, user invitations, and tenant switching
77
+ - **Database** - Supabase PostgREST client for direct database access and advanced queries
78
+
79
+ ## Authentication Integration
80
+
81
+ The SDK integrates seamlessly with Ory Kratos self-service authentication flows:
82
+
83
+ ```typescript
84
+ import type {
85
+ LoginFlow,
86
+ RegistrationFlow,
87
+ RecoveryFlow,
88
+ SettingsFlow,
89
+ VerificationFlow,
90
+ Session,
91
+ FlowType
92
+ } from '@omnibase/core-js';
93
+
94
+ // Type-safe flow handling
95
+ function handleAuthFlow(flow: FlowType) {
96
+ switch (flow.type) {
97
+ case 'login':
98
+ return processLoginFlow(flow as LoginFlow);
99
+ case 'registration':
100
+ return processRegistrationFlow(flow as RegistrationFlow);
101
+ case 'recovery':
102
+ return processRecoveryFlow(flow as RecoveryFlow);
103
+ default:
104
+ throw new Error(`Unsupported flow type: ${flow.type}`);
105
+ }
106
+ }
107
+
108
+ // Session validation
109
+ function isSessionValid(session: Session): boolean {
110
+ return session.active === true &&
111
+ session.expires_at ? new Date(session.expires_at) > new Date() : false;
112
+ }
113
+ ```
114
+
115
+ ## Multi-tenant Examples
116
+
117
+ ### Complete Tenant Workflow
118
+
119
+ ```typescript
120
+ import {
121
+ createTenant,
122
+ createTenantUserInvite,
123
+ acceptTenantInvite,
124
+ switchActiveTenant,
125
+ deleteTenant,
126
+ type CreateTenantRequest,
127
+ type Tenant
128
+ } from '@omnibase/core-js';
129
+
130
+ // 1. Create a new tenant with billing
131
+ const tenantData: CreateTenantRequest = {
132
+ name: 'Acme Corporation',
133
+ billing_email: 'billing@acmecorp.com',
134
+ user_id: 'current-user-id'
135
+ };
136
+
137
+ const newTenant = await createTenant(tenantData);
138
+ if (newTenant.status === 200 && newTenant.data) {
139
+ console.log(`Created tenant: ${newTenant.data.tenant.name}`);
140
+
141
+ // 2. Invite users to the tenant
142
+ const invitation = await createTenantUserInvite(newTenant.data.tenant.id, {
143
+ email: 'admin@acmecorp.com'
144
+ });
145
+
146
+ if (invitation.status === 200 && invitation.data) {
147
+ console.log(`Invitation sent with token: ${invitation.data.token}`);
148
+
149
+ // 3. Accept invitation (done by the invited user)
150
+ const acceptance = await acceptTenantInvite({
151
+ token: invitation.data.token
152
+ });
153
+
154
+ if (acceptance.status === 200 && acceptance.data) {
155
+ console.log(`User joined tenant: ${acceptance.data.tenant_id}`);
156
+
157
+ // 4. Switch active tenant context
158
+ await switchActiveTenant(newTenant.data.tenant.id);
159
+ console.log('Switched to new tenant context');
160
+ }
161
+ }
162
+ }
163
+ ```
164
+
165
+ ### Tenant Management
166
+
167
+ ```typescript
168
+ import { deleteTenant, type ApiResponse } from '@omnibase/core-js';
169
+
170
+ // Delete a tenant
171
+ const deleteResult = await deleteTenant('tenant-uuid');
172
+ if (deleteResult.status === 200) {
173
+ console.log('Tenant successfully deleted');
174
+ } else {
175
+ console.error('Failed to delete tenant:', deleteResult.error);
176
+ }
177
+ ```
178
+
179
+ ## Database Integration
180
+
181
+ ```typescript
182
+ import { createClient } from '@omnibase/core-js';
183
+
184
+ // Create database client for advanced operations
185
+ const db = createClient({
186
+ url: process.env.DATABASE_URL || 'your-database-url',
187
+ // additional PostgREST client options
188
+ });
189
+
190
+ // Use the client for direct database operations
191
+ const { data, error } = await db
192
+ .from('tenants')
193
+ .select('*')
194
+ .eq('active', true);
195
+
196
+ if (error) {
197
+ console.error('Database query failed:', error);
198
+ } else {
199
+ console.log('Active tenants:', data);
200
+ }
201
+ ```
202
+
203
+ ## Type Definitions
204
+
205
+ The SDK provides comprehensive TypeScript types for all operations:
206
+
207
+ ```typescript
208
+ import type {
209
+ // Authentication types
210
+ LoginFlow,
211
+ RegistrationFlow,
212
+ RecoveryFlow,
213
+ SettingsFlow,
214
+ VerificationFlow,
215
+ LogoutFlow,
216
+ Session,
217
+ FlowType,
218
+
219
+ // Tenant types
220
+ Tenant,
221
+ CreateTenantRequest,
222
+ CreateTenantResponse,
223
+ CreateTenantUserInviteRequest,
224
+ CreateTenantUserInviteResponse,
225
+ AcceptTenantInviteResponse,
226
+ SwitchActiveTenantResponse,
227
+ DeleteTenantResponse,
228
+ ApiResponse
229
+ } from '@omnibase/core-js';
230
+ ```
231
+
232
+ ## Error Handling
233
+
234
+ All SDK functions return consistent `ApiResponse` types with proper error handling:
235
+
236
+ ```typescript
237
+ try {
238
+ const result = await createTenant({
239
+ name: 'My Tenant',
240
+ billing_email: 'billing@example.com',
241
+ user_id: 'user-123'
242
+ });
243
+
244
+ if (result.status === 200 && result.data) {
245
+ console.log('Tenant created:', result.data.tenant);
246
+ } else {
247
+ console.error('Failed to create tenant:', result.error);
248
+ }
249
+ } catch (error) {
250
+ console.error('Network or unexpected error:', error);
251
+ }
252
+ ```
253
+
254
+ ## Environment Configuration
255
+
256
+ The SDK requires environment configuration for proper operation:
257
+
258
+ ```bash
259
+ # Required environment variables
260
+ OMNIBASE_API_URL=https://your-api-endpoint.com
261
+
262
+ # Optional database configuration
263
+ DATABASE_URL=postgresql://user:pass@host:5432/dbname
264
+ ```
265
+
266
+ ## Security Features
267
+
268
+ - **HTTP-only Cookies** - Secure session management resistant to XSS attacks
269
+ - **JWT Tokens** - Row-level security (RLS) policy support for database operations
270
+ - **CSRF Protection** - Built-in protection against cross-site request forgery
271
+ - **Flow-based Authentication** - Secure, stateful authentication flows via Ory Kratos
272
+ - **Token Expiration** - Automatic handling of invitation token expiration
273
+
274
+ ## Environment Support
275
+
276
+ - ✅ **Browser** - Works in all modern browsers with proper CORS handling
277
+ - ✅ **Node.js** - Version 16+ supported with full server-side functionality
278
+ - ✅ **React/Vue/Angular** - Framework agnostic with TypeScript support
279
+ - ✅ **TypeScript** - Full type definitions included with comprehensive coverage
280
+ - ✅ **ESM/CJS** - Supports both module formats for maximum compatibility
281
+ - ✅ **Edge Runtime** - Compatible with Vercel Edge, Cloudflare Workers, and similar platforms
282
+
283
+ ## Related Packages
284
+
285
+ - [`@omnibase/sdk-nextjs`](../sdk-nextjs) - Next.js optimized SDK with middleware and server components
286
+ - [`@omnibase/sdk-shadcn`](../sdk-shadcn) - Pre-built UI components with shadcn/ui integration
287
+
288
+ ## Architecture
289
+
290
+ The SDK is built around three core architectural concepts:
291
+
292
+ 1. **Authentication Flows** - Comprehensive Ory Kratos integration handling all user authentication states and transitions
293
+ 2. **Multi-tenant Context** - Complete organizational boundary management with billing integration and user access control
294
+ 3. **Database Integration** - Direct Supabase PostgREST access for advanced queries and data operations
295
+
296
+ Each module is designed to work independently or together, allowing you to use only the parts you need for your specific integration requirements. The SDK maintains consistency across all operations with standardized response types and error handling patterns.
297
+
298
+ ## Interfaces
299
+
300
+ - [TenantInvite](interfaces/TenantInvite.md)
301
+
302
+ ## Type Aliases
303
+
304
+ - [FlowType](type-aliases/FlowType.md)
305
+ - [LogoutFlow](type-aliases/LogoutFlow.md)
306
+ - [LoginFlow](type-aliases/LoginFlow.md)
307
+ - [RecoveryFlow](type-aliases/RecoveryFlow.md)
308
+ - [VerificationFlow](type-aliases/VerificationFlow.md)
309
+ - [RegistrationFlow](type-aliases/RegistrationFlow.md)
310
+ - [SettingsFlow](type-aliases/SettingsFlow.md)
311
+ - [Session](type-aliases/Session.md)
312
+ - [AcceptTenantInviteRequest](type-aliases/AcceptTenantInviteRequest.md)
313
+ - [AcceptTenantInviteResponse](type-aliases/AcceptTenantInviteResponse.md)
314
+ - [CreateTenantUserInviteResponse](type-aliases/CreateTenantUserInviteResponse.md)
315
+ - [CreateTenantUserInviteRequest](type-aliases/CreateTenantUserInviteRequest.md)
316
+ - [CreateTenantResponse](type-aliases/CreateTenantResponse.md)
317
+ - [Tenant](type-aliases/Tenant.md)
318
+ - [CreateTenantRequest](type-aliases/CreateTenantRequest.md)
319
+ - [DeleteTenantResponse](type-aliases/DeleteTenantResponse.md)
320
+ - [SwitchActiveTenantResponse](type-aliases/SwitchActiveTenantResponse.md)
321
+ - [ApiResponse](type-aliases/ApiResponse.md)
322
+
323
+ ## Functions
324
+
325
+ - [createClient](functions/createClient.md)
326
+ - [acceptTenantInvite](functions/acceptTenantInvite.md)
327
+ - [createTenantUserInvite](functions/createTenantUserInvite.md)
328
+ - [createTenant](functions/createTenant.md)
329
+ - [deleteTenant](functions/deleteTenant.md)
330
+ - [switchActiveTenant](functions/switchActiveTenant.md)