@tenxyte/core 0.1.5 → 0.9.0
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 +184 -0
- package/dist/index.cjs +951 -496
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1709 -1265
- package/dist/index.d.ts +1709 -1265
- package/dist/index.js +919 -464
- package/dist/index.js.map +1 -1
- package/package.json +70 -66
- package/src/client.ts +50 -21
- package/src/http/client.ts +162 -162
- package/src/http/index.ts +1 -1
- package/src/http/interceptors.ts +117 -117
- package/src/index.ts +7 -7
- package/src/modules/ai.ts +178 -0
- package/src/modules/auth.ts +116 -95
- package/src/modules/b2b.ts +177 -0
- package/src/modules/rbac.ts +207 -160
- package/src/modules/security.ts +313 -122
- package/src/modules/user.ts +95 -80
- package/src/storage/cookie.ts +39 -39
- package/src/storage/index.ts +29 -29
- package/src/storage/localStorage.ts +75 -75
- package/src/storage/memory.ts +30 -30
- package/src/types/index.ts +152 -150
- package/src/utils/base64url.ts +25 -0
- package/src/utils/device_info.ts +94 -94
- package/src/utils/events.ts +71 -71
- package/src/utils/jwt.ts +51 -51
- package/tests/http.test.ts +144 -144
- package/tests/modules/auth.test.ts +93 -93
- package/tests/modules/rbac.test.ts +95 -95
- package/tests/modules/security.test.ts +85 -75
- package/tests/modules/user.test.ts +76 -76
- package/tests/storage.test.ts +96 -96
- package/tests/utils.test.ts +71 -71
- package/tsup.config.ts +10 -10
- package/vitest.config.ts +7 -7
package/README.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# @tenxyte/core
|
|
2
|
+
|
|
3
|
+
The official core JavaScript/TypeScript SDK for the Tenxyte API.
|
|
4
|
+
This SDK is the foundation for interacting securely with Tenxyte's robust authentication, multi-tenant organization management, and Advanced AI Security (AIRS) capabilities.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
You can install the package using `npm`, `yarn`, or `pnpm`:
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @tenxyte/core
|
|
12
|
+
# or
|
|
13
|
+
yarn add @tenxyte/core
|
|
14
|
+
# or
|
|
15
|
+
pnpm add @tenxyte/core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Initialization
|
|
19
|
+
|
|
20
|
+
The single entry point for all operations is the `TenxyteClient`. You must initialize it with your API's base URL and (if you're using App-Centric auth) the `appKey` in headers.
|
|
21
|
+
|
|
22
|
+
> **Important**: Never expose an `appSecret` in frontend environments like React or Vue client bundles. Use it exclusively in server-side processes.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { TenxyteClient } from '@tenxyte/core';
|
|
26
|
+
|
|
27
|
+
const tx = new TenxyteClient({
|
|
28
|
+
baseUrl: 'https://api.my-backend.com',
|
|
29
|
+
headers: {
|
|
30
|
+
'X-Access-Key': 'your-public-app-key' // Optional, based on your backend configs.
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The SDK is composed of separate functional modules: `auth`, `security`, `rbac`, `user`, `b2b`, and `ai`.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Authentication Flows
|
|
40
|
+
|
|
41
|
+
### Standard Email / Password
|
|
42
|
+
```typescript
|
|
43
|
+
try {
|
|
44
|
+
const { user, tokens } = await tx.auth.loginWithEmail('user@example.com', 'secure_password!');
|
|
45
|
+
console.log(`Welcome back, ${user.first_name}!`);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error.code === '2FA_REQUIRED') {
|
|
48
|
+
// Collect TOTP code from user...
|
|
49
|
+
await tx.auth.loginWithEmail('user@example.com', 'secure_password!', { totpCode: '123456' });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Social Login (OAuth2)
|
|
55
|
+
```typescript
|
|
56
|
+
// Direct token exchange
|
|
57
|
+
const response = await tx.auth.loginWithSocial('google', {
|
|
58
|
+
id_token: 'google_id_token_jwt'
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Or using OAuth code exchange
|
|
62
|
+
const callback = await tx.auth.handleSocialCallback('github', 'auth_code', 'https://myapp.com/callback');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Passwordless (Magic Link)
|
|
66
|
+
```typescript
|
|
67
|
+
// 1. Request the link
|
|
68
|
+
await tx.auth.requestMagicLink('user@example.com', 'https://myapp.com/verify-magic');
|
|
69
|
+
|
|
70
|
+
// 2. On the callback page, verify the token returned in the URL
|
|
71
|
+
const { user, tokens } = await tx.auth.verifyMagicLink(urlParams.get('token'));
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Authorization & RBAC
|
|
77
|
+
|
|
78
|
+
The SDK automatically intercepts your requests to attach `Authorization: Bearer <token>` when available.
|
|
79
|
+
By utilizing the embedded `EventEmitter`, you can listen to rotation and expiration changes.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
tx.http.addResponseInterceptor(async (response) => {
|
|
83
|
+
// You can intercept logic, or use tx.on(...) to be built later over the SDK Event layer.
|
|
84
|
+
return response;
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Verifying Roles and Permissions
|
|
89
|
+
```typescript
|
|
90
|
+
// Fetch user roles & direct permissions across their active scope
|
|
91
|
+
const myRoles = await tx.user.getMyRoles();
|
|
92
|
+
|
|
93
|
+
// List backend global roles
|
|
94
|
+
const roles = await tx.rbac.listRoles();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Advanced Security
|
|
100
|
+
|
|
101
|
+
### WebAuthn / Passkeys
|
|
102
|
+
The `security` module natively wraps browser credentials APIs to seamlessly interact with Tenxyte's FIDO2 bindings.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Register a new device/Passkey for the authenticated user
|
|
106
|
+
await tx.security.registerWebAuthn('My MacBook Chrome');
|
|
107
|
+
|
|
108
|
+
// Authenticate securely (Without needing a password)
|
|
109
|
+
const session = await tx.security.authenticateWebAuthn('user@example.com');
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 2FA (TOTP) Enrollment
|
|
113
|
+
```typescript
|
|
114
|
+
const { secret, qr_code_url, backup_codes } = await tx.security.setup2FA();
|
|
115
|
+
// Show the QR code to the user, then confirm their first valid code
|
|
116
|
+
await tx.security.confirm2FA(userProvidedCode);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## B2B Organizations (Multi-Tenancy)
|
|
122
|
+
|
|
123
|
+
Tenxyte natively supports complex multi-tenant B2B topologies. Using `switchOrganization` instructs the SDK to pass the context `X-Org-Slug` downstream transparently.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// Activate context
|
|
127
|
+
tx.b2b.switchOrganization('acme-corp');
|
|
128
|
+
|
|
129
|
+
// All subsequent calls inject `X-Org-Slug: acme-corp`.
|
|
130
|
+
const members = await tx.b2b.listMembers('acme-corp');
|
|
131
|
+
|
|
132
|
+
// Invite a collaborator into this organization
|
|
133
|
+
await tx.b2b.inviteMember('acme-corp', { email: 'dev@example.com', role_code: 'admin' });
|
|
134
|
+
|
|
135
|
+
// Clear context
|
|
136
|
+
tx.b2b.clearOrganization();
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## AIRS (AI Responsibility & Security)
|
|
142
|
+
|
|
143
|
+
If your architecture includes orchestrating authenticated LLM agents that take action via Tenxyte endpoints, you must use **AgentTokens**.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// 1. Authenticated User delegates secure permissions to an Agent
|
|
147
|
+
const agentTokenData = await tx.ai.createAgentToken({
|
|
148
|
+
agent_id: 'Invoice-Parser-Bot',
|
|
149
|
+
permissions: ['invoices.read', 'invoices.create'],
|
|
150
|
+
budget_limit_usd: 5.00, // strict budget enforcing
|
|
151
|
+
circuit_breaker: { max_requests: 100, window_seconds: 60 }
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// 2. Instruct the SDK to flip into Agent Mode
|
|
155
|
+
tx.ai.setAgentToken(agentTokenData.token);
|
|
156
|
+
|
|
157
|
+
// The SDK will now authorize using `AgentBearer <token>`.
|
|
158
|
+
// 3. Keep the agent alive
|
|
159
|
+
await tx.ai.sendHeartbeat(agentTokenData.id);
|
|
160
|
+
|
|
161
|
+
// 4. Report LLM consumption cost transparently back to backend
|
|
162
|
+
await tx.ai.reportUsage(agentTokenData.id, {
|
|
163
|
+
cost_usd: 0.015,
|
|
164
|
+
prompt_tokens: 1540,
|
|
165
|
+
completion_tokens: 420
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// Disable agent mode and return to standard User flow
|
|
169
|
+
tx.ai.clearAgentToken();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Human In The Loop (HITL) & Auditing
|
|
173
|
+
```typescript
|
|
174
|
+
// Linking operations to prompt identifiers for debugging
|
|
175
|
+
tx.ai.setTraceId('trace-1234abcd-prompt');
|
|
176
|
+
// Request will now include X-Prompt-Trace-ID
|
|
177
|
+
|
|
178
|
+
// Any requests generating a `HTTP 202 Accepted` indicate HITL.
|
|
179
|
+
const pendingActions = await tx.ai.listPendingActions();
|
|
180
|
+
await tx.ai.confirmPendingAction(pendingActions[0].confirmation_token);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
MIT
|