@sylphx/sdk 0.23.0 → 0.26.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 +47 -561
- package/dist/index.d.ts +311 -1
- package/dist/index.mjs +721 -16
- package/dist/index.mjs.map +1 -1
- package/dist/nextjs/index.mjs +24 -24
- package/dist/nextjs/index.mjs.map +1 -1
- package/dist/react/index.mjs +1 -1
- package/dist/react/index.mjs.map +1 -1
- package/dist/server/index.mjs +24 -24
- package/dist/server/index.mjs.map +1 -1
- package/package.json +126 -123
package/README.md
CHANGED
|
@@ -1,592 +1,78 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@sylphx/sdk` — TypeScript Platform SDK
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
[](https://www.typescriptlang.org/)
|
|
6
|
-
[](https://sylphx.com/docs)
|
|
3
|
+
First-class **external** TypeScript client for Sylphx Platform.
|
|
7
4
|
|
|
8
|
-
|
|
5
|
+
| Language | App (runtime) | Management (operators) |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| **TypeScript** | `@sylphx/sdk` → `Client` | `@sylphx/sdk` → `ManagementClient` |
|
|
8
|
+
| **Rust** | `sylphx-sdk` | `sylphx-sdk-management` |
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
**Wire authority:** Protobuf under `api/proto/sylphx/platform/v1/` (sole).
|
|
11
|
+
Not Effect Schema / `@sylphx/contract`.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
13
|
+
## Install
|
|
15
14
|
|
|
16
15
|
```bash
|
|
17
16
|
npm install @sylphx/sdk
|
|
18
|
-
# or
|
|
19
|
-
pnpm add @sylphx/sdk
|
|
20
|
-
bun add @sylphx/sdk
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
## Quick Start (Next.js)
|
|
26
|
-
|
|
27
|
-
### 1. Environment Variables
|
|
28
|
-
|
|
29
|
-
Your server and browser connection URLs from your
|
|
30
|
-
[Platform Console](https://sylphx.com/console). Sylphx deploys inject these
|
|
31
|
-
automatically; local development can pull the same values into `.env.local`.
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
# .env.local
|
|
35
|
-
SYLPHX_SECRET_URL=sylphx://sk_dev_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@bold-river-a1b2c3.api.sylphx.com
|
|
36
|
-
SYLPHX_URL=sylphx://pk_dev_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@bold-river-a1b2c3.api.sylphx.com
|
|
37
|
-
NEXT_PUBLIC_SYLPHX_URL=sylphx://pk_dev_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@bold-river-a1b2c3.api.sylphx.com
|
|
17
|
+
# or: bun add @sylphx/sdk
|
|
38
18
|
```
|
|
39
19
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
`SYLPHX_SECRET_URL` is used only on the server for confidential token exchange
|
|
43
|
-
and HttpOnly cookie handling. `SYLPHX_URL` / `NEXT_PUBLIC_SYLPHX_URL` carry the
|
|
44
|
-
publishable credential used by browser-safe auth routes such as
|
|
45
|
-
`/auth/login`, `/auth/oauth-providers`, passkeys, and OAuth authorize.
|
|
46
|
-
|
|
47
|
-
> **Key formats**
|
|
48
|
-
> - `sylphx://sk_*@<tenant-slug>.api.sylphx.com` — Server connection URL (server only, never expose)
|
|
49
|
-
> - `sylphx://pk_*@<tenant-slug>.api.sylphx.com` — Browser connection URL (publishable)
|
|
50
|
-
>
|
|
51
|
-
> Get both from **Console → Your App → API Keys**. The hosted BaaS API is always
|
|
52
|
-
> addressed as `<tenant-slug>.api.sylphx.com`.
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
### 2. Middleware
|
|
57
|
-
|
|
58
|
-
Handles auth routes (`/auth/callback`, `/auth/signout`) and route protection automatically.
|
|
20
|
+
## App client (runtime / BaaS)
|
|
59
21
|
|
|
60
22
|
```ts
|
|
61
|
-
|
|
62
|
-
import { createSylphxMiddleware } from '@sylphx/sdk/nextjs'
|
|
63
|
-
|
|
64
|
-
export default createSylphxMiddleware({
|
|
65
|
-
publicRoutes: ['/', '/about', '/pricing', '/login'],
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
export const config = {
|
|
69
|
-
matcher: ['/((?!_next|.*\\..*).*)', '/'],
|
|
70
|
-
}
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
No manual `/api/auth/*` routes needed — the middleware handles everything.
|
|
74
|
-
|
|
75
|
-
Organization context is part of the same contract. Calling the mounted
|
|
76
|
-
`POST /auth/switch-org` route stores the active org in SDK-owned HttpOnly
|
|
77
|
-
cookies; subsequent refresh-token rotations restore the org-scoped access token
|
|
78
|
-
automatically. If a user belongs to exactly one organization, the middleware
|
|
79
|
-
auto-scopes the refreshed session without app-specific glue code.
|
|
80
|
-
|
|
81
|
-
---
|
|
82
|
-
|
|
83
|
-
### 3. Root Layout
|
|
84
|
-
|
|
85
|
-
Fetch config server-side once, pass to the provider:
|
|
86
|
-
|
|
87
|
-
```tsx
|
|
88
|
-
// app/layout.tsx
|
|
89
|
-
import { getAppConfig } from '@sylphx/sdk/server'
|
|
90
|
-
import { SylphxProvider } from '@sylphx/sdk/react'
|
|
91
|
-
import { createServerClient } from '@sylphx/sdk'
|
|
92
|
-
|
|
93
|
-
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
94
|
-
const sylphx = createServerClient(process.env.SYLPHX_SECRET_URL!)
|
|
95
|
-
const apiUrl = sylphx.baseUrl.replace(/\/v[0-9]+$/, '')
|
|
96
|
-
|
|
97
|
-
const config = await getAppConfig({
|
|
98
|
-
secretKey: sylphx.secretKey!,
|
|
99
|
-
appId: process.env.NEXT_PUBLIC_SYLPHX_APP_ID!,
|
|
100
|
-
platformUrl: apiUrl,
|
|
101
|
-
})
|
|
23
|
+
import { Client } from '@sylphx/sdk'
|
|
102
24
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
{children}
|
|
112
|
-
</SylphxProvider>
|
|
113
|
-
</body>
|
|
114
|
-
</html>
|
|
115
|
-
)
|
|
116
|
-
}
|
|
25
|
+
const client = Client.create(process.env.SYLPHX_SECRET_KEY!, 'my-tenant')
|
|
26
|
+
await client.kvSet('user:1', { name: 'Ada' }, 60)
|
|
27
|
+
const value = await client.kvGet('user:1')
|
|
28
|
+
await client.kvDelete('user:1')
|
|
29
|
+
// Typed beyond foundation (Protobuf KvIncr):
|
|
30
|
+
await client.kvIncr('visits', 1)
|
|
31
|
+
await client.kvExists('visits')
|
|
32
|
+
await client.kvExpire('visits', 3600)
|
|
117
33
|
```
|
|
118
34
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
### 4. Protect Pages (Server Components)
|
|
122
|
-
|
|
123
|
-
```tsx
|
|
124
|
-
// app/dashboard/page.tsx
|
|
125
|
-
import { currentUser } from '@sylphx/sdk/nextjs'
|
|
126
|
-
import { redirect } from 'next/navigation'
|
|
127
|
-
|
|
128
|
-
export default async function Dashboard() {
|
|
129
|
-
const user = await currentUser()
|
|
130
|
-
if (!user) redirect('/login')
|
|
131
|
-
|
|
132
|
-
return <h1>Hello, {user.name}</h1>
|
|
133
|
-
}
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
---
|
|
137
|
-
|
|
138
|
-
### 5. Auth UI (Client Components)
|
|
139
|
-
|
|
140
|
-
```tsx
|
|
141
|
-
'use client'
|
|
142
|
-
import { useUser, SignedIn, SignedOut, UserButton } from '@sylphx/sdk/react'
|
|
143
|
-
|
|
144
|
-
export default function Header() {
|
|
145
|
-
const { user } = useUser()
|
|
146
|
-
|
|
147
|
-
return (
|
|
148
|
-
<header>
|
|
149
|
-
<SignedOut>
|
|
150
|
-
<a href="/login">Sign in</a>
|
|
151
|
-
</SignedOut>
|
|
152
|
-
<SignedIn>
|
|
153
|
-
<span>Hello, {user?.name}</span>
|
|
154
|
-
<UserButton afterSignOutUrl="/" />
|
|
155
|
-
</SignedIn>
|
|
156
|
-
</header>
|
|
157
|
-
)
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
---
|
|
162
|
-
|
|
163
|
-
## Server-Side
|
|
164
|
-
|
|
165
|
-
### Get Current User
|
|
166
|
-
|
|
167
|
-
```tsx
|
|
168
|
-
import { auth, currentUser, currentUserId } from '@sylphx/sdk/nextjs'
|
|
169
|
-
|
|
170
|
-
// Full auth state
|
|
171
|
-
const { userId, user, sessionToken } = await auth()
|
|
172
|
-
|
|
173
|
-
// Just the user object (null if not signed in)
|
|
174
|
-
const user = await currentUser()
|
|
175
|
-
|
|
176
|
-
// Just the user ID
|
|
177
|
-
const userId = await currentUserId()
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### Server API Client
|
|
35
|
+
## Management client
|
|
181
36
|
|
|
182
37
|
```ts
|
|
183
|
-
import {
|
|
184
|
-
|
|
185
|
-
const sylphx = createServerClient(process.env.SYLPHX_SECRET_URL!)
|
|
38
|
+
import { ManagementClient } from '@sylphx/sdk'
|
|
186
39
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
40
|
+
const mgmt = ManagementClient.create(process.env.SYLPHX_TOKEN!)
|
|
41
|
+
await mgmt.health()
|
|
42
|
+
const me = await mgmt.whoami()
|
|
43
|
+
const projects = await mgmt.listProjects()
|
|
44
|
+
const project = await mgmt.getProject('prj_…')
|
|
45
|
+
// Typed beyond foundation (Protobuf ListEnvironments):
|
|
46
|
+
const envs = await mgmt.listEnvironments('prj_…')
|
|
47
|
+
const env = await mgmt.getEnvironment('prj_…', 'env_…')
|
|
48
|
+
const deps = await mgmt.listDeployments('prj_…')
|
|
49
|
+
// Escape hatch for any Management REST route:
|
|
50
|
+
await mgmt.api('GET', '/projects')
|
|
192
51
|
```
|
|
193
52
|
|
|
194
|
-
##
|
|
195
|
-
|
|
196
|
-
`@sylphx/sdk` also exports browser-safe CI runner profile helpers for tools
|
|
197
|
-
that generate GitHub Actions workflows:
|
|
53
|
+
## Contract identity
|
|
198
54
|
|
|
199
55
|
```ts
|
|
200
|
-
import {
|
|
201
|
-
|
|
202
|
-
const profile = getCiRunnerProfileById('macos-standard')
|
|
203
|
-
const runsOn = formatCiRunnerWorkflowRunsOn(profile)
|
|
204
|
-
// [self-hosted, sylphx, macos, standard]
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
These helpers expose the public workflow label contract only. Applications do
|
|
208
|
-
not need to know the backing scheduler, cluster, runner group, capacity formula,
|
|
209
|
-
or placement details.
|
|
210
|
-
|
|
211
|
-
## Sandbox Capability Catalog
|
|
212
|
-
|
|
213
|
-
Sandbox clients expose the authenticated capability report served by the
|
|
214
|
-
exec-server:
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
import { SandboxClient } from '@sylphx/sdk'
|
|
218
|
-
|
|
219
|
-
const sandbox = await SandboxClient.create(sylphx)
|
|
220
|
-
const report = await sandbox.capabilities.get()
|
|
221
|
-
|
|
222
|
-
const storage = report.capabilities.find((capability) => capability.id === 'workspace-storage')
|
|
223
|
-
if (storage?.workspaceStorage?.pressure === 'critical') {
|
|
224
|
-
// Clean caches/artifacts, fork from a clean workspace, or offload heavy work.
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const mobile = report.capabilities.find((capability) => capability.id === 'mobile-device-test')
|
|
228
|
-
if (mobile?.status === 'not-local') {
|
|
229
|
-
// Build artifacts in the workspace or CI, then run device tests through the
|
|
230
|
-
// managed mobile device runtime when available.
|
|
231
|
-
}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
The report is tenant-neutral and product-level. It tells agents what is local to
|
|
235
|
-
the sandbox (`exec`, files, browser, desktop) versus another Platform execution
|
|
236
|
-
plane (`ci-runner`, `managed-device-runtime`) without exposing Kubernetes
|
|
237
|
-
placement or storage internals.
|
|
238
|
-
|
|
239
|
-
Runtime workspace subtree cleanup uses the fail-closed subpath reclaim route:
|
|
240
|
-
|
|
241
|
-
```ts
|
|
242
|
-
import { reclaimWorkspaceSubpath } from '@sylphx/sdk'
|
|
243
|
-
|
|
244
|
-
await reclaimWorkspaceSubpath(sylphx, 'wksp_...', {
|
|
245
|
-
subPath: 'workspaces/session-a',
|
|
246
|
-
reason: 'retention-expired',
|
|
247
|
-
})
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
Use `reclaimWorkspace` for whole-workspace archive/delete semantics only.
|
|
251
|
-
`reclaimWorkspaceSubpath` is a separate route so older Platform versions fail
|
|
252
|
-
with 404 instead of treating an unknown `subPath` field as whole-workspace
|
|
253
|
-
reclaim.
|
|
254
|
-
|
|
255
|
-
### Prefetch App Config
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
import {
|
|
259
|
-
getAppConfig, // All config in one call (recommended)
|
|
260
|
-
getPlans, // Billing plans
|
|
261
|
-
getFeatureFlags, // Feature flag definitions
|
|
262
|
-
getConsentTypes, // GDPR consent config
|
|
263
|
-
} from '@sylphx/sdk/server'
|
|
264
|
-
import { createServerClient } from '@sylphx/sdk'
|
|
265
|
-
|
|
266
|
-
const sylphx = createServerClient(process.env.SYLPHX_SECRET_URL!)
|
|
267
|
-
|
|
268
|
-
const config = await getAppConfig({
|
|
269
|
-
secretKey: sylphx.secretKey!,
|
|
270
|
-
appId: process.env.NEXT_PUBLIC_SYLPHX_APP_ID!,
|
|
271
|
-
platformUrl: sylphx.baseUrl.replace(/\/v[0-9]+$/, ''),
|
|
272
|
-
})
|
|
273
|
-
// config.plans, config.featureFlags, config.oauthProviders, config.consentTypes
|
|
56
|
+
import { WIRE_CONTRACT_PACKAGE, WIRE_CONTRACT_REVISION } from '@sylphx/sdk'
|
|
57
|
+
// sylphx.platform.v1 @ 0.3.0 — same as Rust sylphx-wire
|
|
274
58
|
```
|
|
275
59
|
|
|
276
|
-
|
|
60
|
+
## CLI
|
|
277
61
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
export async function POST(request: Request) {
|
|
282
|
-
const body = await request.text()
|
|
283
|
-
|
|
284
|
-
const result = await verifyWebhook({
|
|
285
|
-
payload: body,
|
|
286
|
-
signatureHeader: request.headers.get('x-webhook-signature'),
|
|
287
|
-
secret: process.env.SYLPHX_WEBHOOK_SECRET!,
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
if (!result.valid) {
|
|
291
|
-
return new Response('Unauthorized', { status: 401 })
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const { event, data } = result.payload!
|
|
295
|
-
// handle event...
|
|
296
|
-
return Response.json({ received: true })
|
|
297
|
-
}
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
Or use the handler factory:
|
|
301
|
-
|
|
302
|
-
```ts
|
|
303
|
-
import { createWebhookHandler } from '@sylphx/sdk/server'
|
|
304
|
-
|
|
305
|
-
export const POST = createWebhookHandler({
|
|
306
|
-
secret: process.env.SYLPHX_WEBHOOK_SECRET!,
|
|
307
|
-
handlers: {
|
|
308
|
-
'user.created': async (data) => { /* ... */ },
|
|
309
|
-
'subscription.updated': async (data) => { /* ... */ },
|
|
310
|
-
},
|
|
311
|
-
})
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
### JWT Verification
|
|
315
|
-
|
|
316
|
-
```ts
|
|
317
|
-
import { createServerClient } from '@sylphx/sdk'
|
|
318
|
-
import { verifyAccessToken } from '@sylphx/sdk/server'
|
|
319
|
-
|
|
320
|
-
const sylphx = createServerClient(process.env.SYLPHX_SECRET_URL!)
|
|
321
|
-
|
|
322
|
-
const payload = await verifyAccessToken(token, {
|
|
323
|
-
platformUrl: sylphx.baseUrl.replace(/\/v[0-9]+$/, ''),
|
|
324
|
-
audience: process.env.NEXT_PUBLIC_SYLPHX_APP_ID!,
|
|
325
|
-
})
|
|
326
|
-
// payload.sub, payload.email, payload.role, payload.app_id
|
|
327
|
-
// Org-scoped tokens also preserve payload.org_id, payload.org_slug, payload.org_role
|
|
328
|
-
// DPoP-bound tokens preserve payload.cnf?.jkt for proof validation.
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
Always pass `audience` on a resource server. The server verifier rejects a
|
|
332
|
-
validly-signed token when its `aud` claim is for another app or API. Omitting
|
|
333
|
-
`audience` is kept only for backward compatibility and skips audience
|
|
334
|
-
validation. If you authorize organization resources, compare `payload.org_id`
|
|
335
|
-
with the addressed resource's org and check `payload.org_role` before granting
|
|
336
|
-
access.
|
|
337
|
-
|
|
338
|
-
---
|
|
339
|
-
|
|
340
|
-
### Service Accounts (machine-to-machine)
|
|
341
|
-
|
|
342
|
-
Authenticate **as a service account** with the OAuth 2.0 client-credentials
|
|
343
|
-
grant (RFC 6749 §4.4, ADR-2062). Returns a short-lived org-scoped access token
|
|
344
|
-
carrying `org_id` / `org_slug` / `org_role` claims — verify it on the resource
|
|
345
|
-
server with `verifyAccessToken` (above).
|
|
346
|
-
|
|
347
|
-
```ts
|
|
348
|
-
import { createServerClient, signInAsService, withToken } from '@sylphx/sdk'
|
|
62
|
+
Operator CLI remains **Rust** binary `sylphx` (`sylphx-cli` on crates.io).
|
|
63
|
+
TS `@sylphx/cli` is not the product path.
|
|
349
64
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
const { token, expiresIn } = await signInAsService(sylphx, {
|
|
353
|
-
clientId: process.env.SYLPHX_SERVICE_CLIENT_ID!, // `sa_…`, stable across rotations
|
|
354
|
-
clientSecret: process.env.SYLPHX_SERVICE_CLIENT_SECRET!,
|
|
355
|
-
// scope: 'read:foo write:bar', // optional scope downgrade
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
const authed = withToken(sylphx, token)
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
No refresh token is issued — re-mint before `expiresIn` (seconds) elapses.
|
|
362
|
-
Secrets support rotation with a grace window, so a rotated secret keeps
|
|
363
|
-
authenticating running consumers until its deadline. `org_role` is one of
|
|
364
|
-
`admin` / `developer` / `viewer` (never `owner` / `super_admin` — those stay
|
|
365
|
-
human-only).
|
|
366
|
-
|
|
367
|
-
---
|
|
368
|
-
|
|
369
|
-
## React Hooks
|
|
370
|
-
|
|
371
|
-
### Auth
|
|
372
|
-
|
|
373
|
-
```tsx
|
|
374
|
-
import { useUser, useAuth } from '@sylphx/sdk/react'
|
|
375
|
-
|
|
376
|
-
const { user, isLoading, isSignedIn } = useUser()
|
|
377
|
-
const { signIn, signUp, signOut, forgotPassword } = useAuth()
|
|
378
|
-
|
|
379
|
-
await signIn({ email: 'user@example.com', password: '...' })
|
|
380
|
-
await signOut()
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
### Billing
|
|
384
|
-
|
|
385
|
-
```tsx
|
|
386
|
-
import { useBilling } from '@sylphx/sdk/react'
|
|
387
|
-
|
|
388
|
-
const { subscription, isPremium, plans, createCheckout, openPortal } = useBilling()
|
|
389
|
-
|
|
390
|
-
// Check access
|
|
391
|
-
if (!isPremium) return <UpgradePrompt />
|
|
392
|
-
|
|
393
|
-
// Start checkout
|
|
394
|
-
const url = await createCheckout('pro', 'monthly')
|
|
395
|
-
window.location.href = url
|
|
396
|
-
|
|
397
|
-
// Manage subscription
|
|
398
|
-
await openPortal()
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### Analytics
|
|
402
|
-
|
|
403
|
-
```tsx
|
|
404
|
-
import { useAnalytics } from '@sylphx/sdk/react'
|
|
405
|
-
|
|
406
|
-
const { track, identify } = useAnalytics()
|
|
407
|
-
|
|
408
|
-
track('button_clicked', { button: 'upgrade' })
|
|
409
|
-
identify('user-id', { name: 'John', email: 'john@example.com' })
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
### Feature Flags
|
|
413
|
-
|
|
414
|
-
```tsx
|
|
415
|
-
import { useFeatureFlag } from '@sylphx/sdk/react'
|
|
416
|
-
|
|
417
|
-
const { isEnabled } = useFeatureFlag('new-dashboard')
|
|
418
|
-
if (isEnabled) return <NewDashboard />
|
|
419
|
-
```
|
|
420
|
-
|
|
421
|
-
### AI
|
|
422
|
-
|
|
423
|
-
```tsx
|
|
424
|
-
import { useChat, useCompletion } from '@sylphx/sdk/react'
|
|
425
|
-
|
|
426
|
-
const { messages, send, isLoading } = useChat({
|
|
427
|
-
model: 'anthropic/claude-3.5-sonnet',
|
|
428
|
-
})
|
|
429
|
-
|
|
430
|
-
await send('What is the meaning of life?')
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
### Storage
|
|
434
|
-
|
|
435
|
-
```tsx
|
|
436
|
-
import { useStorage } from '@sylphx/sdk/react'
|
|
437
|
-
|
|
438
|
-
const { upload, uploadAvatar, isUploading, progress } = useStorage()
|
|
439
|
-
|
|
440
|
-
const url = await upload(file, { folder: 'documents' })
|
|
441
|
-
const avatarUrl = await uploadAvatar(imageFile)
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
### More Hooks
|
|
445
|
-
|
|
446
|
-
```tsx
|
|
447
|
-
import { useConsent } from '@sylphx/sdk/react' // GDPR consent
|
|
448
|
-
import { useFeatureFlags } from '@sylphx/sdk/react' // All flags at once
|
|
449
|
-
import { useNotifications } from '@sylphx/sdk/react' // In-app notifications
|
|
450
|
-
import { useReferral } from '@sylphx/sdk/react' // Referral program
|
|
451
|
-
import { useOrganization } from '@sylphx/sdk/react' // Multi-tenant orgs
|
|
452
|
-
import { useTasks } from '@sylphx/sdk/react' // Background tasks
|
|
453
|
-
import { useErrorTracking } from '@sylphx/sdk/react' // Error capture
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
---
|
|
457
|
-
|
|
458
|
-
## UI Components
|
|
459
|
-
|
|
460
|
-
### Auth
|
|
461
|
-
|
|
462
|
-
```tsx
|
|
463
|
-
import { SignIn, SignUp, UserButton, SignedIn, SignedOut } from '@sylphx/sdk/react'
|
|
464
|
-
|
|
465
|
-
<SignedOut><SignIn mode="embedded" afterSignInUrl="/dashboard" /></SignedOut>
|
|
466
|
-
<SignedIn><UserButton afterSignOutUrl="/" /></SignedIn>
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
### Billing
|
|
470
|
-
|
|
471
|
-
```tsx
|
|
472
|
-
import { PricingTable, CheckoutButton } from '@sylphx/sdk/react'
|
|
473
|
-
|
|
474
|
-
<PricingTable plans={plans} />
|
|
475
|
-
<CheckoutButton planSlug="pro" interval="monthly">Upgrade</CheckoutButton>
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
### Route Protection
|
|
479
|
-
|
|
480
|
-
```tsx
|
|
481
|
-
import { Protect } from '@sylphx/sdk/react'
|
|
482
|
-
|
|
483
|
-
<Protect role="admin">
|
|
484
|
-
<AdminPanel />
|
|
485
|
-
</Protect>
|
|
486
|
-
```
|
|
487
|
-
|
|
488
|
-
---
|
|
489
|
-
|
|
490
|
-
## Pure Functions (Server or Client)
|
|
491
|
-
|
|
492
|
-
For non-React environments or maximum control:
|
|
493
|
-
|
|
494
|
-
```ts
|
|
495
|
-
import { createClient, signIn, track, getPlans } from '@sylphx/sdk'
|
|
496
|
-
|
|
497
|
-
const config = createClient(process.env.NEXT_PUBLIC_SYLPHX_URL!)
|
|
498
|
-
|
|
499
|
-
// Auth
|
|
500
|
-
const tokens = await signIn(config, { email, password })
|
|
501
|
-
const authedConfig = withToken(config, tokens.accessToken)
|
|
502
|
-
|
|
503
|
-
// Analytics
|
|
504
|
-
await track(config, { event: 'purchase', properties: { amount: 99 } })
|
|
505
|
-
|
|
506
|
-
// Billing
|
|
507
|
-
const plans = await getPlans(config)
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
---
|
|
511
|
-
|
|
512
|
-
## Entry Points
|
|
513
|
-
|
|
514
|
-
| Import path | Use for |
|
|
515
|
-
|---|---|
|
|
516
|
-
| `@sylphx/sdk` | Pure functions (server or client, no React) |
|
|
517
|
-
| `@sylphx/sdk/react` | React hooks, components, `SylphxProvider` |
|
|
518
|
-
| `@sylphx/sdk/server` | JWT verification, webhook verification, server client |
|
|
519
|
-
| `@sylphx/sdk/nextjs` | `createSylphxMiddleware`, `auth()`, `currentUser()` |
|
|
520
|
-
| `@sylphx/sdk/web-analytics` | Standalone web-analytics tracker (rrweb + web-vitals) |
|
|
521
|
-
| `@sylphx/sdk/health` | Multi-signal health score for the `sylphx-health-agent` sidecar (ADR-111 Phase B) |
|
|
522
|
-
|
|
523
|
-
### `@sylphx/sdk/health` — Phase B health score (ADR-111)
|
|
524
|
-
|
|
525
|
-
Apps register signals (event-loop lag, queue depth, error rate, memory pressure)
|
|
526
|
-
and the SDK folds them into a continuous score in `[0, 1]`. The
|
|
527
|
-
`sylphx-health-agent` sidecar polls the score and decides liveness /
|
|
528
|
-
readiness / drain via the three-tier gate. See
|
|
529
|
-
[`src/health/README.md`](./src/health/README.md) for the full guide.
|
|
530
|
-
|
|
531
|
-
```ts
|
|
532
|
-
import { sylphxHealth, eventLoopLagSignal, queueDepthSignal } from '@sylphx/sdk/health'
|
|
533
|
-
|
|
534
|
-
const health = sylphxHealth({
|
|
535
|
-
signals: [
|
|
536
|
-
eventLoopLagSignal({ degradedMs: 5000, deadMs: 30000 }),
|
|
537
|
-
queueDepthSignal({ getter: () => queue.size, fullThreshold: 1000 }),
|
|
538
|
-
],
|
|
539
|
-
})
|
|
540
|
-
|
|
541
|
-
app.get('/healthz', health.handler())
|
|
542
|
-
// Or — Unix-socket transport for the sidecar:
|
|
543
|
-
health.serveUnixSocket() // → /var/run/sylphx/health.sock
|
|
544
|
-
```
|
|
545
|
-
|
|
546
|
-
---
|
|
547
|
-
|
|
548
|
-
## TypeScript
|
|
549
|
-
|
|
550
|
-
All types are fully inferred. Import them directly:
|
|
551
|
-
|
|
552
|
-
```ts
|
|
553
|
-
import type { User, Plan, Subscription } from '@sylphx/sdk'
|
|
554
|
-
import type { AppConfig } from '@sylphx/sdk/server'
|
|
555
|
-
import type { AuthResult } from '@sylphx/sdk/nextjs'
|
|
556
|
-
```
|
|
557
|
-
|
|
558
|
-
---
|
|
559
|
-
|
|
560
|
-
## Self-Hosting / Custom Deployment
|
|
561
|
-
|
|
562
|
-
If you're running your own Sylphx Platform deployment, configure the base URL via the CLI:
|
|
65
|
+
## Install (commercial)
|
|
563
66
|
|
|
564
67
|
```bash
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
Or in the SDK via an explicit custom-domain connection URL:
|
|
68
|
+
# TypeScript library
|
|
69
|
+
npm install @sylphx/sdk
|
|
569
70
|
|
|
570
|
-
|
|
571
|
-
|
|
71
|
+
# Rust libraries
|
|
72
|
+
# Cargo.toml: sylphx-sdk = "0.1", sylphx-sdk-management = "0.1"
|
|
572
73
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
)
|
|
74
|
+
# Operator CLI (Rust only — not npm @sylphx/cli)
|
|
75
|
+
cargo install sylphx-cli
|
|
576
76
|
```
|
|
577
77
|
|
|
578
|
-
|
|
579
|
-
> host for self-hosted deployments or a documented legacy migration.
|
|
580
|
-
|
|
581
|
-
---
|
|
582
|
-
|
|
583
|
-
## Architecture — contract-first
|
|
584
|
-
|
|
585
|
-
This SDK is part of the [two-plane architecture](../../docs/adr/ADR-083-two-plane-sdk-architecture.md) (ADR-083) with a single source of truth (ADR-084):
|
|
586
|
-
|
|
587
|
-
- **`@sylphx/contract`** (Effect Schema) drives the types you see here. The types are not hand-written and not separately maintained — add an endpoint to the contract and both this SDK and the sibling [`@sylphx/management`](../management) inherit it. Hand-written shims are a bug.
|
|
588
|
-
- **Promise surface, Effect-free.** Internally `@sylphx/contract` is Effect Schema, but the published `.d.ts` is stripped of Effect imports (strip-at-publish CI guard). You get clean `Promise<User>` / `Promise<Plan[]>` signatures with zero Effect dependency in your bundle.
|
|
589
|
-
- **Standard Schema compliant.** Every input schema in the contract exposes the `~standard` interface — compatible with Zod, Valibot, ArkType, TypeBox, Effect Schema, and any future Standard-Schema-compliant validator.
|
|
590
|
-
- **Agent surface via the CLI.** The `sylphx` CLI is the first-class agent entry point (Claude Code, Cursor, ChatGPT Apps). If you want a custom MCP, compose it on top of `@sylphx/management` yourself — Sylphx does not ship a standalone stdio MCP binary.
|
|
591
|
-
|
|
592
|
-
See [ADR-084](../../docs/adr/ADR-084-effect-schema-contract.md) for the full design rationale.
|
|
78
|
+
**Not product:** `npm install @sylphx/cli` (retired TypeScript CLI).
|