@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 +330 -0
- package/dist/auth/index.d.cts +402 -44
- package/dist/auth/index.d.ts +402 -44
- package/dist/database/index.d.cts +96 -47
- package/dist/database/index.d.ts +96 -47
- package/dist/index.d.ts +3 -10
- package/dist/tenants/index.cjs +3 -0
- package/dist/tenants/index.d.cts +583 -23
- package/dist/tenants/index.d.ts +583 -23
- package/dist/tenants/index.js +3 -0
- package/package.json +8 -3
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,103 +1,461 @@
|
|
|
1
|
-
import { LoginFlow as LoginFlow$1, RecoveryFlow as RecoveryFlow$1, VerificationFlow as VerificationFlow$1, RegistrationFlow as RegistrationFlow$1, SettingsFlow as SettingsFlow$1, SessionAuthenticationMethod, AuthenticatorAssuranceLevel, SessionDevice, Identity } from '@ory/client-fetch';
|
|
1
|
+
import { LoginFlow as LoginFlow$1, RecoveryFlow as RecoveryFlow$1, VerificationFlow as VerificationFlow$1, RegistrationFlow as RegistrationFlow$1, SettingsFlow as SettingsFlow$1, LogoutFlow as LogoutFlow$1, SessionAuthenticationMethod, AuthenticatorAssuranceLevel, SessionDevice, Identity } from '@ory/client-fetch';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Union type representing all possible authentication flow types
|
|
5
|
+
*
|
|
6
|
+
* This type encompasses all self-service authentication flows supported by Ory Kratos.
|
|
7
|
+
* Each flow type has its own specific structure and validation rules, but they all
|
|
8
|
+
* follow the same general pattern of initialization, form rendering, and submission.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Type guard for flow identification:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* function isLoginFlow(flow: FlowType): flow is LoginFlow {
|
|
14
|
+
* return flow.type === 'login';
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* function handleFlow(flow: FlowType) {
|
|
18
|
+
* if (isLoginFlow(flow)) {
|
|
19
|
+
* // Handle login-specific logic
|
|
20
|
+
* console.log('Processing login flow:', flow.id);
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* Generic flow processing:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* function processFlowUI(flow: FlowType) {
|
|
29
|
+
* return flow.ui.nodes.map(node => ({
|
|
30
|
+
* name: node.attributes.name,
|
|
31
|
+
* type: node.type,
|
|
32
|
+
* required: node.attributes.required || false
|
|
33
|
+
* }));
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @since 0.1.0
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
3
40
|
type FlowType = LoginFlow | RecoveryFlow | VerificationFlow | RegistrationFlow | SettingsFlow;
|
|
4
41
|
/**
|
|
5
|
-
*
|
|
42
|
+
* Logout flow for terminating user sessions
|
|
43
|
+
*
|
|
44
|
+
* The logout flow ensures that user sessions are properly terminated on the
|
|
45
|
+
* authentication server. This flow handles both cookie-based and token-based
|
|
46
|
+
* session termination, providing secure logout functionality across different
|
|
47
|
+
* authentication methods.
|
|
6
48
|
*
|
|
7
|
-
*
|
|
49
|
+
* @example
|
|
50
|
+
* Initiating logout flow:
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // Browser-based logout
|
|
53
|
+
* window.location.href = logoutFlow.logout_url;
|
|
54
|
+
*
|
|
55
|
+
* // Or for SPA/API-based logout
|
|
56
|
+
* await fetch(logoutFlow.logout_url, {
|
|
57
|
+
* method: 'GET',
|
|
58
|
+
* credentials: 'include'
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @since 0.1.0
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
type LogoutFlow = LogoutFlow$1;
|
|
66
|
+
/**
|
|
67
|
+
* Login flow for user authentication
|
|
68
|
+
*
|
|
69
|
+
* Represents a complete login flow initiated through Ory Kratos. This flow handles
|
|
70
|
+
* various authentication methods including password-based login, social providers,
|
|
71
|
+
* and multi-factor authentication. The flow contains all necessary UI elements
|
|
72
|
+
* and validation rules for rendering secure login forms.
|
|
73
|
+
*
|
|
74
|
+
* Key properties:
|
|
75
|
+
* - `ui`: Contains form nodes with input fields, buttons, and validation messages
|
|
76
|
+
* - `oauth2_login_challenge`: OAuth2 challenge for federated login scenarios
|
|
77
|
+
* - `refresh`: Indicates if this is a forced re-authentication
|
|
78
|
+
* - `request_url`: Original URL that initiated the flow
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* Processing login form:
|
|
82
|
+
* ```typescript
|
|
83
|
+
* function renderLoginForm(flow: LoginFlow) {
|
|
84
|
+
* const emailField = flow.ui.nodes.find(
|
|
85
|
+
* node => node.attributes.name === 'identifier'
|
|
86
|
+
* );
|
|
87
|
+
* const passwordField = flow.ui.nodes.find(
|
|
88
|
+
* node => node.attributes.name === 'password'
|
|
89
|
+
* );
|
|
90
|
+
*
|
|
91
|
+
* // Render form with these fields
|
|
92
|
+
* return {
|
|
93
|
+
* action: flow.ui.action,
|
|
94
|
+
* method: flow.ui.method,
|
|
95
|
+
* fields: [emailField, passwordField]
|
|
96
|
+
* };
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* Handling social login:
|
|
102
|
+
* ```typescript
|
|
103
|
+
* function getSocialProviders(flow: LoginFlow) {
|
|
104
|
+
* return flow.ui.nodes
|
|
105
|
+
* .filter(node => node.group === 'oidc')
|
|
106
|
+
* .map(node => ({
|
|
107
|
+
* provider: node.attributes.value,
|
|
108
|
+
* name: node.meta?.label?.text || 'Unknown Provider'
|
|
109
|
+
* }));
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @since 0.1.0
|
|
114
|
+
* @public
|
|
8
115
|
*/
|
|
9
116
|
type LoginFlow = LoginFlow$1;
|
|
10
117
|
/**
|
|
11
|
-
*
|
|
118
|
+
* Account recovery flow for password reset and account recovery
|
|
119
|
+
*
|
|
120
|
+
* This flow enables users to recover their accounts through various recovery
|
|
121
|
+
* methods such as email-based password reset or account unlock procedures.
|
|
122
|
+
* The recovery process is typically initiated when a user cannot access their
|
|
123
|
+
* account due to forgotten credentials or account lockout.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* Initiating account recovery:
|
|
127
|
+
* ```typescript
|
|
128
|
+
* function handleRecovery(flow: RecoveryFlow) {
|
|
129
|
+
* const emailField = flow.ui.nodes.find(
|
|
130
|
+
* node => node.attributes.name === 'email'
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* if (emailField) {
|
|
134
|
+
* // Render email input for recovery
|
|
135
|
+
* return {
|
|
136
|
+
* action: flow.ui.action,
|
|
137
|
+
* method: flow.ui.method,
|
|
138
|
+
* placeholder: 'Enter your email address'
|
|
139
|
+
* };
|
|
140
|
+
* }
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @since 0.1.0
|
|
145
|
+
* @public
|
|
12
146
|
*/
|
|
13
147
|
type RecoveryFlow = RecoveryFlow$1;
|
|
14
148
|
/**
|
|
15
|
-
*
|
|
149
|
+
* Verification flow for validating communication channels
|
|
150
|
+
*
|
|
151
|
+
* Used to verify out-of-band communication channels such as email addresses
|
|
152
|
+
* or phone numbers. This flow ensures that users have access to the contact
|
|
153
|
+
* methods they've provided, which is essential for account security and
|
|
154
|
+
* communication delivery.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* Email verification handling:
|
|
158
|
+
* ```typescript
|
|
159
|
+
* function handleEmailVerification(flow: VerificationFlow) {
|
|
160
|
+
* if (flow.state === 'sent_email') {
|
|
161
|
+
* return {
|
|
162
|
+
* message: 'Verification email sent. Please check your inbox.',
|
|
163
|
+
* canResend: true
|
|
164
|
+
* };
|
|
165
|
+
* }
|
|
166
|
+
*
|
|
167
|
+
* if (flow.state === 'passed_challenge') {
|
|
168
|
+
* return {
|
|
169
|
+
* message: 'Email verified successfully!',
|
|
170
|
+
* redirect: '/dashboard'
|
|
171
|
+
* };
|
|
172
|
+
* }
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @since 0.1.0
|
|
177
|
+
* @public
|
|
16
178
|
*/
|
|
17
179
|
type VerificationFlow = VerificationFlow$1;
|
|
18
180
|
/**
|
|
19
|
-
*
|
|
181
|
+
* Registration flow for new user account creation
|
|
182
|
+
*
|
|
183
|
+
* Handles the complete user registration process including identity validation,
|
|
184
|
+
* password requirements, and any custom registration fields defined in the
|
|
185
|
+
* identity schema. The registration flow can include email verification,
|
|
186
|
+
* terms acceptance, and integration with external identity providers.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* Processing registration form:
|
|
190
|
+
* ```typescript
|
|
191
|
+
* function buildRegistrationForm(flow: RegistrationFlow) {
|
|
192
|
+
* const passwordNode = flow.ui.nodes.find(
|
|
193
|
+
* node => node.attributes.name === 'password'
|
|
194
|
+
* );
|
|
195
|
+
*
|
|
196
|
+
* const requirements = passwordNode?.messages?.map(msg => msg.text) || [];
|
|
197
|
+
*
|
|
198
|
+
* return {
|
|
199
|
+
* action: flow.ui.action,
|
|
200
|
+
* method: flow.ui.method,
|
|
201
|
+
* passwordRequirements: requirements,
|
|
202
|
+
* hasTerms: flow.ui.nodes.some(node => node.attributes.name === 'terms')
|
|
203
|
+
* };
|
|
204
|
+
* }
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* Handling registration validation:
|
|
209
|
+
* ```typescript
|
|
210
|
+
* function getRegistrationErrors(flow: RegistrationFlow) {
|
|
211
|
+
* return flow.ui.nodes.reduce((errors, node) => {
|
|
212
|
+
* if (node.messages && node.messages.length > 0) {
|
|
213
|
+
* errors[node.attributes.name] = node.messages[0].text;
|
|
214
|
+
* }
|
|
215
|
+
* return errors;
|
|
216
|
+
* }, {} as Record<string, string>);
|
|
217
|
+
* }
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @since 0.1.0
|
|
221
|
+
* @public
|
|
20
222
|
*/
|
|
21
223
|
type RegistrationFlow = RegistrationFlow$1;
|
|
22
224
|
/**
|
|
23
|
-
*
|
|
225
|
+
* Settings flow for user profile and account management
|
|
226
|
+
*
|
|
227
|
+
* This flow enables users to update their account settings in a self-service manner.
|
|
228
|
+
* It supports updating profile information, changing passwords, managing two-factor
|
|
229
|
+
* authentication, updating email addresses, and other account-related settings.
|
|
230
|
+
* The flow enforces proper validation and security measures for sensitive changes.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* Profile update handling:
|
|
234
|
+
* ```typescript
|
|
235
|
+
* function handleProfileSettings(flow: SettingsFlow) {
|
|
236
|
+
* const profileFields = flow.ui.nodes
|
|
237
|
+
* .filter(node => node.group === 'profile')
|
|
238
|
+
* .map(node => ({
|
|
239
|
+
* name: node.attributes.name,
|
|
240
|
+
* value: node.attributes.value,
|
|
241
|
+
* type: node.attributes.type,
|
|
242
|
+
* required: node.attributes.required
|
|
243
|
+
* }));
|
|
244
|
+
*
|
|
245
|
+
* return {
|
|
246
|
+
* action: flow.ui.action,
|
|
247
|
+
* method: flow.ui.method,
|
|
248
|
+
* fields: profileFields
|
|
249
|
+
* };
|
|
250
|
+
* }
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* Password change flow:
|
|
255
|
+
* ```typescript
|
|
256
|
+
* function handlePasswordChange(flow: SettingsFlow) {
|
|
257
|
+
* const passwordNodes = flow.ui.nodes.filter(
|
|
258
|
+
* node => node.group === 'password'
|
|
259
|
+
* );
|
|
260
|
+
*
|
|
261
|
+
* return passwordNodes.map(node => ({
|
|
262
|
+
* name: node.attributes.name,
|
|
263
|
+
* label: node.meta?.label?.text || node.attributes.name,
|
|
264
|
+
* required: node.attributes.required || false
|
|
265
|
+
* }));
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @since 0.1.0
|
|
270
|
+
* @public
|
|
24
271
|
*/
|
|
25
272
|
type SettingsFlow = SettingsFlow$1;
|
|
26
273
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
274
|
+
* User session object representing an authenticated user's session state
|
|
275
|
+
*
|
|
276
|
+
* A session contains all information about a user's authentication state,
|
|
277
|
+
* including when they authenticated, what methods were used, device information,
|
|
278
|
+
* and the associated identity. Sessions have expiration times and can be
|
|
279
|
+
* invalidated when users log out or when security policies require it.
|
|
280
|
+
*
|
|
281
|
+
* Sessions are central to maintaining authentication state across requests
|
|
282
|
+
* and provide the foundation for authorization decisions throughout your
|
|
283
|
+
* application.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* Checking session validity:
|
|
287
|
+
* ```typescript
|
|
288
|
+
* function isSessionValid(session: Session): boolean {
|
|
289
|
+
* if (!session.active) {
|
|
290
|
+
* return false;
|
|
291
|
+
* }
|
|
292
|
+
*
|
|
293
|
+
* if (session.expires_at && new Date(session.expires_at) < new Date()) {
|
|
294
|
+
* return false;
|
|
295
|
+
* }
|
|
296
|
+
*
|
|
297
|
+
* return true;
|
|
298
|
+
* }
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* Extracting user information:
|
|
303
|
+
* ```typescript
|
|
304
|
+
* function getUserInfo(session: Session) {
|
|
305
|
+
* return {
|
|
306
|
+
* id: session.identity?.id,
|
|
307
|
+
* email: session.identity?.traits?.email,
|
|
308
|
+
* name: session.identity?.traits?.name,
|
|
309
|
+
* isVerified: session.identity?.verifiable_addresses?.some(
|
|
310
|
+
* addr => addr.verified
|
|
311
|
+
* ) || false
|
|
312
|
+
* };
|
|
313
|
+
* }
|
|
314
|
+
* ```
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* Session security analysis:
|
|
318
|
+
* ```typescript
|
|
319
|
+
* function analyzeSessionSecurity(session: Session) {
|
|
320
|
+
* const authMethods = session.authentication_methods || [];
|
|
321
|
+
* const hasMFA = authMethods.length > 1;
|
|
322
|
+
* const recentAuth = session.authenticated_at &&
|
|
323
|
+
* new Date(session.authenticated_at) > new Date(Date.now() - 3600000); // 1 hour
|
|
324
|
+
*
|
|
325
|
+
* return {
|
|
326
|
+
* hasMFA,
|
|
327
|
+
* recentAuth,
|
|
328
|
+
* assuranceLevel: session.authenticator_assurance_level,
|
|
329
|
+
* deviceCount: session.devices?.length || 0
|
|
330
|
+
* };
|
|
331
|
+
* }
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* @since 0.1.0
|
|
335
|
+
* @public
|
|
30
336
|
*/
|
|
31
337
|
type Session = {
|
|
32
338
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
339
|
+
* Session active state
|
|
340
|
+
*
|
|
341
|
+
* Indicates whether the session is currently active and valid for
|
|
342
|
+
* authentication purposes. When false, the session should be treated
|
|
343
|
+
* as invalid regardless of expiration time.
|
|
344
|
+
*
|
|
345
|
+
* @defaultValue true
|
|
36
346
|
*/
|
|
37
347
|
active?: boolean;
|
|
38
348
|
/**
|
|
39
|
-
*
|
|
349
|
+
* Session authentication timestamp
|
|
350
|
+
*
|
|
351
|
+
* The exact time when this session was authenticated. For multi-factor
|
|
352
|
+
* authentication scenarios, this represents when the last factor was
|
|
353
|
+
* successfully verified (e.g., TOTP code completion, biometric verification).
|
|
40
354
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @type {Date}
|
|
44
|
-
* @memberof Session
|
|
355
|
+
* This timestamp is crucial for implementing security policies that require
|
|
356
|
+
* recent authentication for sensitive operations.
|
|
45
357
|
*/
|
|
46
358
|
authenticated_at?: Date;
|
|
47
359
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
360
|
+
* Authentication methods used for this session
|
|
361
|
+
*
|
|
362
|
+
* A comprehensive list of all authentication methods used to establish
|
|
363
|
+
* this session. This includes passwords, social logins, multi-factor
|
|
364
|
+
* authentication tokens, and any other verification methods.
|
|
365
|
+
*
|
|
366
|
+
* Useful for implementing step-up authentication or authorization decisions
|
|
367
|
+
* based on authentication strength.
|
|
51
368
|
*/
|
|
52
369
|
authentication_methods?: Array<SessionAuthenticationMethod>;
|
|
53
370
|
/**
|
|
371
|
+
* Authenticator assurance level
|
|
54
372
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
373
|
+
* Indicates the confidence level in the user's identity based on the
|
|
374
|
+
* authentication methods used. Higher assurance levels typically require
|
|
375
|
+
* multiple factors or stronger authentication methods.
|
|
376
|
+
*
|
|
377
|
+
* Common levels:
|
|
378
|
+
* - `aal1`: Single-factor authentication (password, social login)
|
|
379
|
+
* - `aal2`: Multi-factor authentication (password + TOTP, biometrics)
|
|
57
380
|
*/
|
|
58
381
|
authenticator_assurance_level?: AuthenticatorAssuranceLevel;
|
|
59
382
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
383
|
+
* Session device history
|
|
384
|
+
*
|
|
385
|
+
* Complete history of all devices and endpoints where this session
|
|
386
|
+
* has been used. This information is valuable for security monitoring,
|
|
387
|
+
* anomaly detection, and providing users with visibility into their
|
|
388
|
+
* account access patterns.
|
|
389
|
+
*
|
|
390
|
+
* Each device entry includes IP address, user agent, location data,
|
|
391
|
+
* and timestamp information for comprehensive session tracking.
|
|
63
392
|
*/
|
|
64
393
|
devices?: Array<SessionDevice>;
|
|
65
394
|
/**
|
|
66
|
-
*
|
|
395
|
+
* Session expiration timestamp
|
|
396
|
+
*
|
|
397
|
+
* The exact time when this session expires and becomes invalid.
|
|
398
|
+
* After this time, the session should not be accepted for authentication
|
|
399
|
+
* and the user should be required to log in again.
|
|
67
400
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* @memberof Session
|
|
401
|
+
* Session expiration can be extended through refresh tokens or
|
|
402
|
+
* re-authentication flows depending on your application's security policy.
|
|
71
403
|
*/
|
|
72
404
|
expires_at?: Date;
|
|
73
405
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
406
|
+
* Unique session identifier
|
|
407
|
+
*
|
|
408
|
+
* A unique identifier for this session that can be used for session
|
|
409
|
+
* management operations such as invalidation, refresh, or audit logging.
|
|
410
|
+
* This ID is typically used in session storage and tracking systems.
|
|
77
411
|
*/
|
|
78
412
|
id: string;
|
|
79
413
|
/**
|
|
414
|
+
* Associated user identity
|
|
415
|
+
*
|
|
416
|
+
* The complete identity object of the authenticated user, containing
|
|
417
|
+
* all user profile information, traits, verifiable addresses, and
|
|
418
|
+
* recovery addresses. This is the primary source of user information
|
|
419
|
+
* for the session.
|
|
80
420
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
421
|
+
* The identity structure is defined by your identity schema and may
|
|
422
|
+
* include custom fields specific to your application's user model.
|
|
83
423
|
*/
|
|
84
424
|
identity?: Identity;
|
|
85
425
|
/**
|
|
86
|
-
*
|
|
426
|
+
* Session issuance timestamp
|
|
87
427
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
428
|
+
* The time when this session was initially created and issued.
|
|
429
|
+
* This is typically equal to or very close to `authenticated_at`,
|
|
430
|
+
* but may differ in cases where session creation and authentication
|
|
431
|
+
* are separate operations.
|
|
432
|
+
*
|
|
433
|
+
* Useful for implementing session age limits and security policies
|
|
434
|
+
* that consider the total session lifetime.
|
|
91
435
|
*/
|
|
92
436
|
issued_at?: Date;
|
|
93
437
|
/**
|
|
94
|
-
* Tokenized
|
|
438
|
+
* Tokenized session representation
|
|
439
|
+
*
|
|
440
|
+
* A tokenized version of the session, typically in JWT format, that
|
|
441
|
+
* can be used for stateless authentication scenarios. This field is
|
|
442
|
+
* only populated when specifically requested through the `tokenize_as`
|
|
443
|
+
* query parameter during session retrieval.
|
|
444
|
+
*
|
|
445
|
+
* The tokenized session contains essential session information in a
|
|
446
|
+
* cryptographically signed format, allowing for distributed authentication
|
|
447
|
+
* without requiring centralized session storage lookups.
|
|
95
448
|
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
449
|
+
* @example
|
|
450
|
+
* Using tokenized session:
|
|
451
|
+
* ```typescript
|
|
452
|
+
* if (session.tokenized) {
|
|
453
|
+
* // Use JWT token for API authentication
|
|
454
|
+
* localStorage.setItem('auth_token', session.tokenized);
|
|
455
|
+
* }
|
|
456
|
+
* ```
|
|
99
457
|
*/
|
|
100
458
|
tokenized?: string;
|
|
101
459
|
};
|
|
102
460
|
|
|
103
|
-
export type { FlowType, LoginFlow, RecoveryFlow, RegistrationFlow, Session, SettingsFlow, VerificationFlow };
|
|
461
|
+
export type { FlowType, LoginFlow, LogoutFlow, RecoveryFlow, RegistrationFlow, Session, SettingsFlow, VerificationFlow };
|
|
@@ -1,56 +1,105 @@
|
|
|
1
|
-
import * as _supabase_postgrest_js from '@supabase/postgrest-js';
|
|
2
1
|
import { PostgrestClient } from '@supabase/postgrest-js';
|
|
3
2
|
|
|
4
|
-
declare type GenericRelationship = {
|
|
5
|
-
foreignKeyName: string;
|
|
6
|
-
columns: string[];
|
|
7
|
-
isOneToOne?: boolean;
|
|
8
|
-
referencedRelation: string;
|
|
9
|
-
referencedColumns: string[];
|
|
10
|
-
};
|
|
11
|
-
declare type GenericTable = {
|
|
12
|
-
Row: Record<string, unknown>;
|
|
13
|
-
Insert: Record<string, unknown>;
|
|
14
|
-
Update: Record<string, unknown>;
|
|
15
|
-
Relationships: GenericRelationship[];
|
|
16
|
-
};
|
|
17
|
-
declare type GenericUpdatableView = {
|
|
18
|
-
Row: Record<string, unknown>;
|
|
19
|
-
Insert: Record<string, unknown>;
|
|
20
|
-
Update: Record<string, unknown>;
|
|
21
|
-
Relationships: GenericRelationship[];
|
|
22
|
-
};
|
|
23
|
-
declare type GenericNonUpdatableView = {
|
|
24
|
-
Row: Record<string, unknown>;
|
|
25
|
-
Relationships: GenericRelationship[];
|
|
26
|
-
};
|
|
27
|
-
declare type GenericView = GenericUpdatableView | GenericNonUpdatableView;
|
|
28
|
-
declare type GenericFunction = {
|
|
29
|
-
Args: Record<string, unknown>;
|
|
30
|
-
Returns: unknown;
|
|
31
|
-
};
|
|
32
|
-
declare type GenericSchema = {
|
|
33
|
-
Tables: Record<string, GenericTable>;
|
|
34
|
-
Views: Record<string, GenericView>;
|
|
35
|
-
Functions: Record<string, GenericFunction>;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
3
|
/**
|
|
39
|
-
* PostgREST client
|
|
4
|
+
* Creates a PostgREST client for database operations
|
|
5
|
+
*
|
|
6
|
+
* This function creates a configured PostgREST client that handles authentication
|
|
7
|
+
* through JWT tokens. It supports both cookie-based authentication (for server-side
|
|
8
|
+
* usage) and fallback to anonymous keys. The client provides a type-safe interface
|
|
9
|
+
* for database operations when used with generated TypeScript types.
|
|
10
|
+
*
|
|
11
|
+
* The client automatically handles authentication headers and provides access to
|
|
12
|
+
* all PostgREST functionality including queries, mutations, and real-time subscriptions.
|
|
13
|
+
*
|
|
14
|
+
* @template T - Database schema types for type-safe operations. Should be generated
|
|
15
|
+
* from your database schema using a tool like Supabase CLI or Omnibase CLI
|
|
16
|
+
*
|
|
17
|
+
* @param url - The PostgREST API endpoint URL (e.g., 'https://api.example.com/rest/v1')
|
|
18
|
+
* @param anonKey - Anonymous/public API key used as fallback when no JWT token is available
|
|
19
|
+
* @param getCookie - Function to retrieve cookies by name, typically used to get JWT tokens
|
|
20
|
+
*
|
|
21
|
+
* @returns A configured PostgREST client instance with authentication headers set
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* Browser usage with cookie authentication:
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { createClient } from '@omnibase/core-js/database';
|
|
27
|
+
*
|
|
28
|
+
* interface DatabaseTypes {
|
|
29
|
+
* users: {
|
|
30
|
+
* Row: { id: string; email: string; name: string };
|
|
31
|
+
* Insert: { email: string; name: string };
|
|
32
|
+
* Update: { email?: string; name?: string };
|
|
33
|
+
* };
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* const client = createClient<DatabaseTypes>(
|
|
37
|
+
* 'https://api.example.com/rest/v1',
|
|
38
|
+
* 'your-anon-key',
|
|
39
|
+
* (name) => document.cookie
|
|
40
|
+
* .split('; ')
|
|
41
|
+
* .find(row => row.startsWith(`${name}=`))
|
|
42
|
+
* ?.split('=')[1] || ''
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* // Type-safe query
|
|
46
|
+
* const { data, error } = await client
|
|
47
|
+
* .from('users')
|
|
48
|
+
* .select('id, email, name')
|
|
49
|
+
* .eq('email', 'user@example.com');
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* Node.js usage with custom JWT retrieval:
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { createClient } from '@omnibase/core-js/database';
|
|
56
|
+
*
|
|
57
|
+
* const client = createClient(
|
|
58
|
+
* process.env.POSTGREST_URL!,
|
|
59
|
+
* process.env.POSTGREST_ANON_KEY!,
|
|
60
|
+
* (name) => {
|
|
61
|
+
* // Custom logic to retrieve JWT from request headers, session, etc.
|
|
62
|
+
* return getJwtFromSession(name) || '';
|
|
63
|
+
* }
|
|
64
|
+
* );
|
|
65
|
+
*
|
|
66
|
+
* // Insert new record
|
|
67
|
+
* const { data, error } = await client
|
|
68
|
+
* .from('users')
|
|
69
|
+
* .insert({ email: 'new@example.com', name: 'New User' })
|
|
70
|
+
* .select();
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* Advanced usage with error handling:
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const client = createClient<DatabaseTypes>(
|
|
77
|
+
* 'https://api.example.com/rest/v1',
|
|
78
|
+
* 'your-anon-key',
|
|
79
|
+
* (name) => getCookieValue(name)
|
|
80
|
+
* );
|
|
81
|
+
*
|
|
82
|
+
* try {
|
|
83
|
+
* const { data, error } = await client
|
|
84
|
+
* .from('users')
|
|
85
|
+
* .select('*')
|
|
86
|
+
* .order('created_at', { ascending: false })
|
|
87
|
+
* .limit(10);
|
|
40
88
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
89
|
+
* if (error) {
|
|
90
|
+
* console.error('Database error:', error.message);
|
|
91
|
+
* return;
|
|
92
|
+
* }
|
|
43
93
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
94
|
+
* console.log('Users:', data);
|
|
95
|
+
* } catch (err) {
|
|
96
|
+
* console.error('Network error:', err);
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
46
99
|
*
|
|
47
|
-
* @
|
|
48
|
-
* @
|
|
49
|
-
* @param getCookie - Custom callback to get cookie by name
|
|
50
|
-
* @returns
|
|
100
|
+
* @since 1.0.0
|
|
101
|
+
* @public
|
|
51
102
|
*/
|
|
52
|
-
declare const createClient: <T = any>(url: string, anonKey: string, getCookie: (cookie: string) => string) => PostgrestClient<T
|
|
53
|
-
__InternalSupabase: infer I extends _supabase_postgrest_js.PostgrestClientOptions;
|
|
54
|
-
} ? I : {}, "public" extends Exclude<keyof T, "__InternalSupabase"> ? Exclude<keyof T, "__InternalSupabase"> & "public" : string & Exclude<keyof T, "__InternalSupabase">, Omit<T, "__InternalSupabase">["public" extends Exclude<keyof T, "__InternalSupabase"> ? Exclude<keyof T, "__InternalSupabase"> & "public" : string & Exclude<keyof T, "__InternalSupabase">] extends GenericSchema ? Omit<T, "__InternalSupabase">["public" extends Exclude<keyof T, "__InternalSupabase"> ? Exclude<keyof T, "__InternalSupabase"> & "public" : string & Exclude<keyof T, "__InternalSupabase">] : any>;
|
|
103
|
+
declare const createClient: <T = any>(url: string, anonKey: string, getCookie: (cookie: string) => string) => PostgrestClient<T>;
|
|
55
104
|
|
|
56
105
|
export { createClient };
|