create-signet-app 0.4.8 → 0.6.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 +133 -33
- package/{template-full → base/api}/api/app.ts +105 -13
- package/base/api/api/db/open.ts +121 -0
- package/{template-full → base/api}/api/db/schema.ts +34 -7
- package/{template-full → base/api}/api/env.ts +16 -0
- package/base/api/api/server.ts +163 -0
- package/{template-full → base/api}/api/services.ts +103 -10
- package/base/api/drizzle/0002_ai_paywalls_workflows.sql +77 -0
- package/base/api/drizzle/meta/0002_snapshot.json +1759 -0
- package/{template-full → base/api}/drizzle/meta/_journal.json +7 -0
- package/base/api/test/api.test.ts +51 -0
- package/base/api/test/helpers.ts +51 -0
- package/base/both/scripts/dev.ts +44 -0
- package/base/shared/README.md +385 -0
- package/{template-full → base/shared}/_github/workflows/ci.yml +1 -1
- package/{template-full → base/web}/src/lib/api.ts +28 -1
- package/{template-full → base/web}/src/main.tsx +4 -1
- package/{template-full → base/web}/src/routeTree.gen.ts +155 -90
- package/{template-full → base/web}/src/router.test.ts +13 -9
- package/base/web/src/routes/_app/index.tsx +55 -0
- package/{template-full → base/web}/src/routes/_app.tsx +40 -16
- package/{template-full → base/web}/vite.config.ts +3 -1
- package/package.json +9 -5
- package/parts/account/api/test/account.test.ts +30 -0
- package/parts/account/part.ts +6 -0
- package/parts/admin/api/test/admin.test.ts +35 -0
- package/parts/admin/part.ts +6 -0
- package/{template-full → parts/admin/web}/src/routes/_app/admin.tsx +2 -1
- package/parts/ai/api/api/routes/ai.ts +28 -0
- package/parts/ai/api/test/ai.test.ts +52 -0
- package/parts/ai/part.ts +16 -0
- package/{template-full → parts/auth/api}/api/routes/account.ts +32 -3
- package/{template-full → parts/auth/api}/api/routes/auth.ts +5 -3
- package/parts/auth/api/test/auth.test.ts +28 -0
- package/{template-full → parts/auth/both}/e2e/smoke.spec.ts +21 -7
- package/parts/auth/part.ts +22 -0
- package/parts/growth/part.ts +7 -0
- package/{template-full/_mcp → parts/mcp/api}/test/mcp.test.ts +25 -16
- package/parts/mcp/part.ts +8 -0
- package/parts/notifications/api/test/notifications.test.ts +18 -0
- package/parts/notifications/part.ts +6 -0
- package/{template-full → parts/notifications/web}/src/routes/_app/notifications.tsx +1 -1
- package/parts/orgs/api/test/orgs.test.ts +56 -0
- package/parts/orgs/part.ts +7 -0
- package/{template-full → parts/payments/api}/api/routes/billing.ts +3 -2
- package/parts/payments/api/test/payments.test.ts +31 -0
- package/parts/payments/part.ts +12 -0
- package/{template-full → parts/payments/web}/src/routes/_app/billing.tsx +6 -3
- package/parts/paywalls/api/api/routes/paywall.ts +126 -0
- package/parts/paywalls/api/test/paywalls.test.ts +71 -0
- package/parts/paywalls/part.ts +8 -0
- package/parts/privacy/api/test/privacy.test.ts +20 -0
- package/parts/privacy/part.ts +7 -0
- package/parts/workflows/api/api/workflows.ts +33 -0
- package/parts/workflows/api/test/workflows.test.ts +44 -0
- package/parts/workflows/part.ts +7 -0
- package/parts/workflows/web/src/routes/_app/approvals.tsx +34 -0
- package/src/compose.ts +426 -0
- package/src/desktop.ts +10 -10
- package/src/index.ts +407 -174
- package/template-api/_targets/deno/Dockerfile +12 -0
- package/template-api/_targets/deno/_dockerignore +10 -0
- package/template-api/_targets/deno/src/server.ts +43 -0
- package/template-full/README.md +0 -166
- package/template-full/api/db/open.ts +0 -33
- package/template-full/api/server.ts +0 -50
- package/template-full/scripts/dev.ts +0 -17
- package/template-full/src/routes/_app/index.tsx +0 -33
- package/template-full/test/api.test.ts +0 -238
- package/template-full/tsconfig.json +0 -9
- /package/{template-full → base/api}/api/db/index.ts +0 -0
- /package/{template-full → base/api}/api/db/kv.ts +0 -0
- /package/{template-full → base/api}/drizzle/0000_init.sql +0 -0
- /package/{template-full → base/api}/drizzle/0001_orgs.sql +0 -0
- /package/{template-full → base/api}/drizzle/meta/0000_snapshot.json +0 -0
- /package/{template-full → base/api}/drizzle/meta/0001_snapshot.json +0 -0
- /package/{template-full → base/api}/drizzle.config.ts +0 -0
- /package/{template-full → base/api}/tsconfig.api.json +0 -0
- /package/{template-full → base/shared}/_gitignore +0 -0
- /package/{template-full → base/shared}/biome.json +0 -0
- /package/{template-full → base/web}/index.html +0 -0
- /package/{template-full → base/web}/src/app.css +0 -0
- /package/{template-full → base/web}/src/components/toaster.tsx +0 -0
- /package/{template-full → base/web}/src/router.ts +0 -0
- /package/{template-full → base/web}/src/routes/__root.tsx +0 -0
- /package/{template-full → base/web}/tsconfig.app.json +0 -0
- /package/{template-full → base/web}/tsconfig.node.json +0 -0
- /package/{template-full → base/web}/tsconfig.test.json +0 -0
- /package/{template-full → parts/account/web}/src/routes/_app/account.tsx +0 -0
- /package/{template-full → parts/auth/both}/playwright.config.ts +0 -0
- /package/{template-full → parts/auth/web}/src/routes/login.tsx +0 -0
- /package/{template-full → parts/notifications/api}/api/routes/notifications.ts +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/index.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/members.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/new.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/invite.$token.tsx +0 -0
- /package/{template-full → parts/payments/api}/api/plans.ts +0 -0
- /package/{template-full → parts/privacy/api}/api/routes/privacy.ts +0 -0
- /package/{template-full → parts/privacy/web}/src/routes/_app/privacy.tsx +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { expect, spyOn, test } from "bun:test"
|
|
2
|
+
import { app, createApp, openDb, sqlKv } from "./helpers.ts"
|
|
3
|
+
|
|
4
|
+
test("observability is off unless configured: no /health, no log lines", async () => {
|
|
5
|
+
const lines: string[] = []
|
|
6
|
+
const spy = spyOn(console, "log").mockImplementation((l: string) => void lines.push(l))
|
|
7
|
+
try {
|
|
8
|
+
expect((await app.request("/health/live")).status).toBe(404)
|
|
9
|
+
expect((await app.request("/api/health")).status).toBe(200)
|
|
10
|
+
} finally {
|
|
11
|
+
spy.mockRestore()
|
|
12
|
+
}
|
|
13
|
+
expect(lines).toEqual([])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test("observability on: request logs, probes left out, /health/ready checks the database", async () => {
|
|
17
|
+
const lines: string[] = []
|
|
18
|
+
const spy = spyOn(console, "log").mockImplementation((l: string) => void lines.push(l))
|
|
19
|
+
try {
|
|
20
|
+
const { db, query } = openDb(":memory:")
|
|
21
|
+
const on = createApp({
|
|
22
|
+
db,
|
|
23
|
+
query,
|
|
24
|
+
kv: sqlKv(db.orm),
|
|
25
|
+
observability: { serviceName: "demo", mixin: () => ({ region: "test" }) },
|
|
26
|
+
})
|
|
27
|
+
const live = await on.request("/health/live")
|
|
28
|
+
expect(live.status).toBe(200)
|
|
29
|
+
expect(await live.json()).toEqual({ ok: true })
|
|
30
|
+
const ready = await on.request("/health/ready")
|
|
31
|
+
expect(ready.status).toBe(200)
|
|
32
|
+
expect(await ready.json()).toEqual({ ok: true, checks: { db: true } })
|
|
33
|
+
expect(lines).toEqual([]) // probes are not logged
|
|
34
|
+
const headers = { cookie: "session=s" }
|
|
35
|
+
const res = await on.request("/api/health?token=abc", { headers })
|
|
36
|
+
expect([res.status, await res.text()]).toEqual([200, '{"ok":true}'])
|
|
37
|
+
} finally {
|
|
38
|
+
spy.mockRestore()
|
|
39
|
+
}
|
|
40
|
+
const logged = lines.map((l) => JSON.parse(l) as Record<string, unknown>)
|
|
41
|
+
expect(logged.map((l) => l.msg)).toEqual(["request.started", "request.completed"])
|
|
42
|
+
expect(logged[1]).toMatchObject({
|
|
43
|
+
service: "demo",
|
|
44
|
+
region: "test",
|
|
45
|
+
method: "GET",
|
|
46
|
+
route: "/api/health",
|
|
47
|
+
status: 200,
|
|
48
|
+
})
|
|
49
|
+
expect(typeof logged[1]?.reqId).toBe("string")
|
|
50
|
+
expect(lines.join("\n")).not.toMatch(/token=abc|session=s/)
|
|
51
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { expect } from "bun:test" // @auth
|
|
2
|
+
|
|
3
|
+
// Shared by the API tests: one app over an in-memory database. bun test runs
|
|
4
|
+
// every file in one process, and the env is validated once, on first read, so
|
|
5
|
+
// it is set here, before the app is imported. CI has no env file; these are
|
|
6
|
+
// throwaway test values.
|
|
7
|
+
export const ADMIN = "+919000000001" // @auth
|
|
8
|
+
Object.assign(process.env, {
|
|
9
|
+
APP_ENV: "development",
|
|
10
|
+
// auth:begin
|
|
11
|
+
SESSION_SECRET: "test-session-secret-0123456789abcdef",
|
|
12
|
+
OTP_SECRET: "test-otp-secret-0123456789abcdef0123",
|
|
13
|
+
ADMIN_PHONES: ADMIN,
|
|
14
|
+
// auth:end
|
|
15
|
+
BILLING: "fake", // @payments
|
|
16
|
+
})
|
|
17
|
+
export const { createApp } = await import("../api/app.ts")
|
|
18
|
+
export const { openDb } = await import("../api/db/open.ts")
|
|
19
|
+
export const { sqlKv } = await import("../api/db/kv.ts")
|
|
20
|
+
|
|
21
|
+
// auth:begin
|
|
22
|
+
/** The last code sent to each phone. */
|
|
23
|
+
export const codes = new Map<string, string>()
|
|
24
|
+
// auth:end
|
|
25
|
+
export const { db, query } = openDb(":memory:")
|
|
26
|
+
export const app = createApp({
|
|
27
|
+
db,
|
|
28
|
+
query,
|
|
29
|
+
kv: sqlKv(db.orm),
|
|
30
|
+
sms: { send: async (to, message) => void codes.set(to, message.match(/\d{6}/)?.[0] ?? "") }, // @auth
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export const post = (path: string, body: unknown, cookie = "", ip = "") =>
|
|
34
|
+
app.request(path, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { "content-type": "application/json", cookie, "x-forwarded-for": ip },
|
|
37
|
+
body: JSON.stringify(body),
|
|
38
|
+
})
|
|
39
|
+
// auth:begin
|
|
40
|
+
|
|
41
|
+
let signIns = 0
|
|
42
|
+
/** Sign in with a one-time code; returns the session cookie. Each sign-in
|
|
43
|
+
* comes from its own address: codes are limited to 10 an hour per address. */
|
|
44
|
+
export async function signIn(phone: string): Promise<string> {
|
|
45
|
+
const ip = `10.0.0.${++signIns}`
|
|
46
|
+
expect((await post("/api/auth/otp", { phone }, "", ip)).status).toBe(200)
|
|
47
|
+
const res = await post("/api/auth/verify", { phone, code: codes.get(phone) })
|
|
48
|
+
expect(res.status).toBe(200)
|
|
49
|
+
return (res.headers.get("set-cookie") ?? "").split(";")[0] ?? ""
|
|
50
|
+
}
|
|
51
|
+
// auth:end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/// <reference types="bun" /> // @bun
|
|
2
|
+
// `dev`: the API (restarting on change) and Vite, which proxies /api to it.
|
|
3
|
+
// A script rather than `a & b` so it runs the same on Windows; when either
|
|
4
|
+
// stops, so does the other.
|
|
5
|
+
// bun:begin
|
|
6
|
+
const bun = process.execPath
|
|
7
|
+
const procs = [
|
|
8
|
+
Bun.spawn([bun, "--watch", "api/server.ts"], { stdio: ["inherit", "inherit", "inherit"] }),
|
|
9
|
+
Bun.spawn([bun, "x", "vite"], { stdio: ["inherit", "inherit", "inherit"] }),
|
|
10
|
+
]
|
|
11
|
+
const stop = () => {
|
|
12
|
+
for (const p of procs) p.kill()
|
|
13
|
+
}
|
|
14
|
+
process.on("SIGINT", stop)
|
|
15
|
+
process.on("SIGTERM", stop)
|
|
16
|
+
const code = await Promise.race(procs.map((p) => p.exited))
|
|
17
|
+
stop()
|
|
18
|
+
process.exit(code)
|
|
19
|
+
// bun:end
|
|
20
|
+
// !bun:begin
|
|
21
|
+
import { spawn } from "node:child_process"
|
|
22
|
+
import { join } from "node:path"
|
|
23
|
+
|
|
24
|
+
const self = process.execPath // node or deno, whichever runs this
|
|
25
|
+
const api = ["--env-file-if-exists=.env", "--watch", "api/server.ts"] // @node
|
|
26
|
+
const api = ["run", "-A", "--env-file=.env", "--watch", "api/server.ts"] // @deno
|
|
27
|
+
const vite = join("node_modules", "vite", "bin", "vite.js") // @node
|
|
28
|
+
const vite = ["run", "-A", join("node_modules", "vite", "bin", "vite.js")] // @deno
|
|
29
|
+
const procs = [
|
|
30
|
+
spawn(self, api, { stdio: "inherit" }),
|
|
31
|
+
spawn(self, [vite], { stdio: "inherit" }), // @node
|
|
32
|
+
spawn(self, vite, { stdio: "inherit" }), // @deno
|
|
33
|
+
]
|
|
34
|
+
const stop = () => {
|
|
35
|
+
for (const p of procs) p.kill()
|
|
36
|
+
}
|
|
37
|
+
process.on("SIGINT", stop)
|
|
38
|
+
process.on("SIGTERM", stop)
|
|
39
|
+
const code = await Promise.race(
|
|
40
|
+
procs.map((p) => new Promise<number>((done) => p.on("exit", (c) => done(c ?? 0)))),
|
|
41
|
+
)
|
|
42
|
+
stop()
|
|
43
|
+
process.exit(code)
|
|
44
|
+
// !bun:end
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
# __APP_NAME__
|
|
2
|
+
|
|
3
|
+
<!-- web:begin -->
|
|
4
|
+
A web app and its API in one project, on Signet. <!-- @api -->
|
|
5
|
+
A web app on Signet. Its API lives elsewhere: point `VITE_API_URL` at it. <!-- @!api -->
|
|
6
|
+
<!-- web:end -->
|
|
7
|
+
An API on Signet: Hono on Bun, with SQLite. <!-- @!web -->
|
|
8
|
+
<!-- auth:begin -->
|
|
9
|
+
It has:
|
|
10
|
+
|
|
11
|
+
- phone sign-in
|
|
12
|
+
- organisations with members, roles and invite links <!-- @orgs -->
|
|
13
|
+
- an account page with devices and API keys <!-- @account -->
|
|
14
|
+
- an in-app inbox <!-- @notifications -->
|
|
15
|
+
- privacy (DPDP consent and data requests) <!-- @privacy -->
|
|
16
|
+
- plans and billing <!-- @payments -->
|
|
17
|
+
- remote paywalls <!-- @paywalls -->
|
|
18
|
+
- AI spend caps and a sample AI route <!-- @ai -->
|
|
19
|
+
- durable workflows with approval steps <!-- @workflows -->
|
|
20
|
+
- an admin panel over the whole database <!-- @admin -->
|
|
21
|
+
- MCP for agents at `/mcp` <!-- @mcp -->
|
|
22
|
+
<!-- auth:end -->
|
|
23
|
+
<!-- !auth:begin -->
|
|
24
|
+
- MCP for agents at `/mcp` <!-- @mcp -->
|
|
25
|
+
<!-- !auth:end -->
|
|
26
|
+
<!-- api:begin -->
|
|
27
|
+
|
|
28
|
+
The API runs on [Bun](https://bun.sh), whichever package manager installed it.
|
|
29
|
+
<!-- api:end -->
|
|
30
|
+
|
|
31
|
+
## First day
|
|
32
|
+
|
|
33
|
+
### Run it (5 minutes)
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
__PM_INSTALL__
|
|
37
|
+
__PM_RUN__ dev
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
<!-- web:begin -->
|
|
41
|
+
<!-- api:begin -->
|
|
42
|
+
`dev` starts the API on port 3000 and Vite next to it; open the URL Vite
|
|
43
|
+
prints.
|
|
44
|
+
<!-- api:end -->
|
|
45
|
+
<!-- !api:begin -->
|
|
46
|
+
`dev` starts Vite; open the URL it prints. Put your API's origin in
|
|
47
|
+
`VITE_API_URL` (in `.env.local`); unset, the app calls its own origin.
|
|
48
|
+
|
|
49
|
+
The client in `src/lib/api.ts` is untyped until you give it your API's type.
|
|
50
|
+
To type it, replace its `api` line with:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import type { AppType } from "<your api>/api/app.ts" // an app made with --api exports it
|
|
54
|
+
export const api = createRpcClient(hc<AppType>, { baseUrl }).api
|
|
55
|
+
```
|
|
56
|
+
<!-- !api:end -->
|
|
57
|
+
<!-- web:end -->
|
|
58
|
+
`dev` starts the API on port 3000 and restarts it on change. <!-- @!web -->
|
|
59
|
+
<!-- auth:begin -->
|
|
60
|
+
Sign in with any phone number. In development no SMS is sent: the
|
|
61
|
+
code is printed in the terminal running the API.
|
|
62
|
+
<!-- auth:end -->
|
|
63
|
+
<!-- api:begin -->
|
|
64
|
+
|
|
65
|
+
The env file at the project root was written for you with fresh <!-- @auth -->
|
|
66
|
+
`SESSION_SECRET` and `OTP_SECRET` values. It is gitignored. Every variable is <!-- @auth -->
|
|
67
|
+
The env file at the project root was written for you. It is gitignored. Every variable is <!-- @!auth -->
|
|
68
|
+
declared, with its default, in `api/env.ts`.
|
|
69
|
+
<!-- api:end -->
|
|
70
|
+
<!-- orgs:begin -->
|
|
71
|
+
|
|
72
|
+
### Organisations (5 minutes)
|
|
73
|
+
|
|
74
|
+
Signing up made you a **Personal** org, and you own it.
|
|
75
|
+
<!-- web:begin -->
|
|
76
|
+
The switcher in the
|
|
77
|
+
header lists your orgs; **New organisation** makes another and moves you into
|
|
78
|
+
it. **Org** has its settings (name, theme, brand colour) and
|
|
79
|
+
**Members and invites**: create an invite link, open it in a private window,
|
|
80
|
+
sign in as another number and accept. That person sees the orgs they belong
|
|
81
|
+
to and nothing else.
|
|
82
|
+
<!-- web:end -->
|
|
83
|
+
<!-- !web:begin -->
|
|
84
|
+
`/api/orgs` makes more, switches between them, and manages members and
|
|
85
|
+
invite links. A person sees the orgs they belong to and nothing else.
|
|
86
|
+
<!-- !web:end -->
|
|
87
|
+
|
|
88
|
+
- Roles are `owner` (everything), `admin` (settings, members, invites) and
|
|
89
|
+
`member` (sees the member list), from signet-db's `DEFAULT_ORG_ROLES`. No
|
|
90
|
+
one can hand out a role with more permissions than their own, and an org
|
|
91
|
+
always keeps an owner.
|
|
92
|
+
- Which org a request is for: in development, the `signet_org` cookie the
|
|
93
|
+
switcher sets, or an `x-org` header. In production, also the org's custom
|
|
94
|
+
domain, or `<slug>.<ORG_ROOT_DOMAIN>` when you set `ORG_ROOT_DOMAIN`.
|
|
95
|
+
- Every org write (create, settings, roles, members, invites) is in the
|
|
96
|
+
audit log.
|
|
97
|
+
<!-- orgs:end -->
|
|
98
|
+
<!-- admin:begin -->
|
|
99
|
+
|
|
100
|
+
### Make yourself platform admin (2 minutes)
|
|
101
|
+
|
|
102
|
+
Put your number in `ADMIN_PHONES` (E.164, comma-separated, e.g.
|
|
103
|
+
`+919876543210`) and sign in again. **Admin** appears in the header: the
|
|
104
|
+
operator's view of every table across every org, searchable and editable, with
|
|
105
|
+
each write in the audit log. Platform roles live in `users.role`: `admin` does
|
|
106
|
+
everything, `staff` all but delete, `readonly` looks. They are separate from
|
|
107
|
+
org roles: an org owner is not a platform admin.
|
|
108
|
+
<!-- admin:end -->
|
|
109
|
+
<!-- payments:begin -->
|
|
110
|
+
|
|
111
|
+
### Try billing (2 minutes)
|
|
112
|
+
|
|
113
|
+
<!-- web:begin -->
|
|
114
|
+
Open **Billing**, pick Pro and continue. `BILLING=fake` switches the plan at once, no
|
|
115
|
+
money moves, and the inbox gets a "Plan changed" notice. The API refuses to <!-- @notifications -->
|
|
116
|
+
money moves. The API refuses to <!-- @!notifications -->
|
|
117
|
+
start with `BILLING=fake` in staging or production.
|
|
118
|
+
<!-- web:end -->
|
|
119
|
+
<!-- !web:begin -->
|
|
120
|
+
`POST /api/billing/checkout` with `{ "plan": "pro" }`: `BILLING=fake` switches the
|
|
121
|
+
plan at once and no money moves. The API refuses to start with `BILLING=fake`
|
|
122
|
+
in staging or production.
|
|
123
|
+
<!-- !web:end -->
|
|
124
|
+
|
|
125
|
+
<!-- api:begin -->
|
|
126
|
+
Plans are in `api/plans.ts`: `definePlans` says what each plan allows,
|
|
127
|
+
`PLAN_DISPLAY` the name, description and price the page shows. Check a plan anywhere in the API:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
await s.entitlements.can(userId, "export")
|
|
131
|
+
await s.entitlements.consume(userId, "projects", 1) // { ok, remaining }
|
|
132
|
+
```
|
|
133
|
+
<!-- api:end -->
|
|
134
|
+
<!-- !api:begin -->
|
|
135
|
+
Plans are in `src/lib/plans.ts`, a copy of your API's: `definePlans` says what
|
|
136
|
+
each plan allows, `PLAN_DISPLAY` the name, description and price the page shows.
|
|
137
|
+
Keep it in step with the API.
|
|
138
|
+
<!-- !api:end -->
|
|
139
|
+
<!-- payments:end -->
|
|
140
|
+
<!-- paywalls:begin -->
|
|
141
|
+
|
|
142
|
+
### Paywalls (2 minutes)
|
|
143
|
+
|
|
144
|
+
A placement ("onboarding", "upgrade") serves an offering: products mapped to
|
|
145
|
+
plans, and what the paywall says. `GET /api/paywall/<placement>?country=IN&platform=web`
|
|
146
|
+
returns the one for the caller: the first audience override that matches,
|
|
147
|
+
else the placement's own, and 404 until the placement exists. Each publish is
|
|
148
|
+
a new version and a published one never changes, so what a paywall sells
|
|
149
|
+
changes without a release.
|
|
150
|
+
<!-- admin:begin -->
|
|
151
|
+
|
|
152
|
+
Admins and staff publish with `POST /api/admin/paywalls/offerings/<key>`
|
|
153
|
+
(`{ products, paywall }`) and point a placement at it with
|
|
154
|
+
`PUT /api/admin/paywalls/placements/<placement>` (`{ offering, overrides? }`).
|
|
155
|
+
The admin panel lists both tables read only.
|
|
156
|
+
<!-- admin:end -->
|
|
157
|
+
<!-- !admin:begin -->
|
|
158
|
+
|
|
159
|
+
Publish from code, e.g. in a seed script:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const actor = { type: "system" as const, id: "seed" }
|
|
163
|
+
const products = [{ id: "plan_pro_monthly", store: "web", plan: "pro" }]
|
|
164
|
+
await s.offerings.publish("standard", { products, paywall: { title: "Go further" } }, { actor })
|
|
165
|
+
await s.offerings.setPlacement("upgrade", { offering: "standard" }, { actor })
|
|
166
|
+
```
|
|
167
|
+
<!-- !admin:end -->
|
|
168
|
+
<!-- paywalls:end -->
|
|
169
|
+
<!-- ai:begin -->
|
|
170
|
+
|
|
171
|
+
### AI (2 minutes)
|
|
172
|
+
|
|
173
|
+
`POST /api/ai/summary` with `{ "text": "..." }` answers from a canned
|
|
174
|
+
provider, so it runs with no key. Each call costs $0.001, charged to the
|
|
175
|
+
caller's daily cap (`AI_CAP_USER_DAY`) and the app's monthly one
|
|
176
|
+
(`AI_CAP_GLOBAL_MONTH`), in cost units: 1,000,000 is one US dollar. Over a
|
|
177
|
+
cap, every `/api/ai` route answers 429 with `Retry-After` before it runs.
|
|
178
|
+
Spend is counted in `usage_counters`; a cap stored in `ai_spend_caps` wins
|
|
179
|
+
over the env ones.
|
|
180
|
+
Admins read spend and set caps per user or for the app under <!-- @admin -->
|
|
181
|
+
`/api/admin/ai-spend` (`GET /usage`, `GET /caps`, `PUT /caps`). <!-- @admin -->
|
|
182
|
+
Set one from code with `s.aiSpend.setCap({ scope, subject, period, units })`. <!-- @!admin -->
|
|
183
|
+
|
|
184
|
+
For a real model, replace the fake provider in `api/services.ts` with yours
|
|
185
|
+
(a signet-ai `Provider`: name, model, `pricing`, and `generate`); its pricing
|
|
186
|
+
is what the caps count. Copy `api/routes/ai.ts` for your own AI routes.
|
|
187
|
+
<!-- ai:end -->
|
|
188
|
+
<!-- workflows:begin -->
|
|
189
|
+
|
|
190
|
+
### Workflows (5 minutes)
|
|
191
|
+
|
|
192
|
+
<!-- api:begin -->
|
|
193
|
+
`api/workflows.ts` has one workflow, a generic approval request: someone
|
|
194
|
+
asks, one of the listed approvers approves or rejects, and the run ends with
|
|
195
|
+
the decision. `api/server.ts` runs the runner: every second it takes the runs
|
|
196
|
+
that are due, including any a restart cut short.
|
|
197
|
+
<!-- web:begin -->
|
|
198
|
+
**Approvals** lists the approval steps waiting on you.
|
|
199
|
+
<!-- web:end -->
|
|
200
|
+
|
|
201
|
+
Start one with `POST /api/workflows/runs` and
|
|
202
|
+
`{ "workflow": "approval-request", "input": { "summary": "...", "approvers": ["<user id>"] } }`.
|
|
203
|
+
Approvers find it at `GET /api/workflows/approvals` and decide with
|
|
204
|
+
`POST /api/workflows/runs/<id>/steps/approval/approve` (or `reject`); admins
|
|
205
|
+
see every run at `GET /api/workflows/runs`.
|
|
206
|
+
|
|
207
|
+
Add your own with `defineWorkflow` in `api/workflows.ts` and list it in
|
|
208
|
+
`WORKFLOWS`. Put each side effect in `step.run`, so a replay after a restart
|
|
209
|
+
does not repeat it. Who may start which workflow over HTTP is `canStart` in
|
|
210
|
+
`api/app.ts`.
|
|
211
|
+
<!-- api:end -->
|
|
212
|
+
<!-- !api:begin -->
|
|
213
|
+
**Approvals** lists the approval steps waiting on you, from your API's
|
|
214
|
+
`/api/workflows` (an app made with `--api --with workflows` serves it), each
|
|
215
|
+
with Approve and Reject.
|
|
216
|
+
<!-- !api:end -->
|
|
217
|
+
<!-- workflows:end -->
|
|
218
|
+
|
|
219
|
+
### Build your first feature (the rest of the day)
|
|
220
|
+
|
|
221
|
+
<!-- api:begin -->
|
|
222
|
+
1. **Table.** Add it to `api/db/schema.ts` (see `kv` for the shape, and <!-- @!auth -->
|
|
223
|
+
`@dapplelight/signet-db/columns/sqlite` for the column helpers). <!-- @!auth -->
|
|
224
|
+
1. **Table.** Add it to `api/db/schema.ts` (see `users` for the column <!-- @auth -->
|
|
225
|
+
helpers). <!-- @auth -->
|
|
226
|
+
If it belongs to an org, give it <!-- @orgs -->
|
|
227
|
+
`orgId: text("org_id").notNull()`. <!-- @orgs -->
|
|
228
|
+
Then run `__PM_RUN__ db:generate`. The migration lands in `drizzle/`;
|
|
229
|
+
the API applies it on its next start. `__PM_RUN__ db:check` flags migrations
|
|
230
|
+
that drop or rename data.
|
|
231
|
+
2. **Route.** Add a file in `api/routes/` and mount it in `api/app.ts`.
|
|
232
|
+
<!-- auth:begin -->
|
|
233
|
+
List its path in `SIGNED_IN` if it needs a user;
|
|
234
|
+
`c.get("user")` is then the signed-in user.
|
|
235
|
+
<!-- auth:end -->
|
|
236
|
+
<!-- orgs:begin -->
|
|
237
|
+
For org data, guard the route
|
|
238
|
+
with `orgGuards({ orgs: s.orgs, userId }).requirePermission("projects:write")`
|
|
239
|
+
and read and write through `scoped(s.db, { orgId: currentTenant(c).id })`.
|
|
240
|
+
Another org's rows are then unreachable, and a table without `orgId`
|
|
241
|
+
throws instead of leaking.
|
|
242
|
+
<!-- orgs:end -->
|
|
243
|
+
Validate input with `zValidator`: the route is
|
|
244
|
+
then also typed for the client and, with MCP on, a tool.
|
|
245
|
+
<!-- api:end -->
|
|
246
|
+
<!-- web:begin -->
|
|
247
|
+
3. **Page.** Add `src/routes/_app/<name>.tsx`. Call the route through `api` <!-- @api -->
|
|
248
|
+
from `src/lib/api.ts`: the path, input and response are typed from the <!-- @api -->
|
|
249
|
+
server, so a renamed field fails typecheck instead of production. <!-- @api -->
|
|
250
|
+
1. **Page.** Add `src/routes/_app/<name>.tsx` and call your API through `api` <!-- @!api -->
|
|
251
|
+
from `src/lib/api.ts`. Give it your API's `AppType` (see the file) and the <!-- @!api -->
|
|
252
|
+
calls are typed from the server. <!-- @!api -->
|
|
253
|
+
<!-- web:end -->
|
|
254
|
+
<!-- notifications:begin -->
|
|
255
|
+
<!-- api:begin -->
|
|
256
|
+
4. **Notify.** Add a template to `templates` in `api/services.ts` and call
|
|
257
|
+
`s.notifier.notify(userId, "your.event", data)`. In-app works now; add
|
|
258
|
+
`email`, `sms` or `whatsapp` variants and senders from
|
|
259
|
+
`@dapplelight/signet-server/notify/channels` when you have provider keys.
|
|
260
|
+
<!-- api:end -->
|
|
261
|
+
<!-- notifications:end -->
|
|
262
|
+
<!-- admin:begin -->
|
|
263
|
+
|
|
264
|
+
The admin panel picks up new tables by itself.
|
|
265
|
+
<!-- admin:end -->
|
|
266
|
+
|
|
267
|
+
### Check it
|
|
268
|
+
|
|
269
|
+
```sh
|
|
270
|
+
__PM_RUN__ lint && __PM_RUN__ typecheck && __PM_RUN__ test && __PM_RUN__ build <!-- @web -->
|
|
271
|
+
__PM_RUN__ lint && __PM_RUN__ typecheck && __PM_RUN__ test <!-- @!web -->
|
|
272
|
+
<!-- web:begin -->
|
|
273
|
+
<!-- api:begin -->
|
|
274
|
+
<!-- auth:begin -->
|
|
275
|
+
__PM_X__ playwright install chromium # once
|
|
276
|
+
__PM_RUN__ test:e2e # sign in, then each page
|
|
277
|
+
<!-- auth:end -->
|
|
278
|
+
<!-- api:end -->
|
|
279
|
+
<!-- web:end -->
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
CI (`.github/workflows/ci.yml`) runs the first line on every push. Commit
|
|
283
|
+
your lockfile.
|
|
284
|
+
|
|
285
|
+
## Layout
|
|
286
|
+
|
|
287
|
+
```
|
|
288
|
+
<!-- api:begin -->
|
|
289
|
+
api/
|
|
290
|
+
app.ts routes, the signed-in gate, MCP; exports AppType for the client
|
|
291
|
+
plans.ts plans and what the billing page shows (shared with the browser) <!-- @payments -->
|
|
292
|
+
workflows.ts the workflows the runner runs <!-- @workflows -->
|
|
293
|
+
services.ts what the routes use, built once per process
|
|
294
|
+
routes/ one file per area <!-- @auth -->
|
|
295
|
+
db/schema.ts every table; drizzle/ holds the migrations
|
|
296
|
+
db/kv.ts sessions, codes, API keys, inbox and rate limits live in the kv table
|
|
297
|
+
server.ts Bun entry: the API under /api, the built app for every other path <!-- @web -->
|
|
298
|
+
server.ts Bun entry: the API under /api <!-- @!web -->
|
|
299
|
+
<!-- api:end -->
|
|
300
|
+
<!-- web:begin -->
|
|
301
|
+
src/
|
|
302
|
+
routes/login.tsx sign-in <!-- @auth -->
|
|
303
|
+
routes/_app/ the signed-in pages, home first <!-- @auth -->
|
|
304
|
+
routes/_app/ the pages, home first <!-- @!auth -->
|
|
305
|
+
routes/invite.$token.tsx the invite link <!-- @orgs -->
|
|
306
|
+
lib/api.ts the API client
|
|
307
|
+
<!-- !api:begin -->
|
|
308
|
+
lib/plans.ts plans, a copy of your API's <!-- @payments -->
|
|
309
|
+
<!-- !api:end -->
|
|
310
|
+
<!-- web:end -->
|
|
311
|
+
<!-- api:begin -->
|
|
312
|
+
test/ API tests (in-memory database)
|
|
313
|
+
e2e/ the browser smoke test <!-- @auth -->
|
|
314
|
+
<!-- api:end -->
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Deploying
|
|
318
|
+
|
|
319
|
+
<!-- api:begin -->
|
|
320
|
+
`__PM_RUN__ build`, then `__PM_RUN__ start` serves the API and the built app from one <!-- @web -->
|
|
321
|
+
Bun process. <!-- @web -->
|
|
322
|
+
`__PM_RUN__ start` serves the API from one Bun process. <!-- @!web -->
|
|
323
|
+
Mount a volume for `DATABASE_PATH` (default `data/app.db`) and set
|
|
324
|
+
`APP_ENV=production` on the host,
|
|
325
|
+
and `SESSION_SECRET`, `OTP_SECRET` and `ADMIN_PHONES`, <!-- @auth -->
|
|
326
|
+
and `ORG_ROOT_DOMAIN` if orgs get subdomains. For an org's custom <!-- @orgs -->
|
|
327
|
+
domain, point its DNS at the host and serve TLS for it there. <!-- @orgs -->
|
|
328
|
+
SQLite wants one writer: run one instance.
|
|
329
|
+
<!-- auth:begin -->
|
|
330
|
+
|
|
331
|
+
Before production:
|
|
332
|
+
|
|
333
|
+
- **SMS.** `api/services.ts` uses `devSmsSender`, which refuses to run in
|
|
334
|
+
staging or production. Swap in a real sender, e.g. `smsSenderFor(msg91Sms(...))`
|
|
335
|
+
from `@dapplelight/signet-server/notify/channels` (Indian numbers need DLT
|
|
336
|
+
template ids).
|
|
337
|
+
<!-- payments:begin -->
|
|
338
|
+
- **Taking real payments.** Set `BILLING=razorpay` and the `RAZORPAY_*` keys.
|
|
339
|
+
Gateway webhooks arrive at `/api/webhooks/razorpay`; signet-payments verifies
|
|
340
|
+
them and each subscription event moves the user's plan through
|
|
341
|
+
`entitlements.applyBillingEvent`. Map your Razorpay plan ids to plans in
|
|
342
|
+
`billing.plans` (`api/services.ts`). Creating the subscription at checkout is
|
|
343
|
+
the one step left to you: see the `TODO(billing)` in `api/routes/billing.ts`
|
|
344
|
+
(pass the user id in the subscription's notes and read it back with
|
|
345
|
+
`billing.subjectOf`).
|
|
346
|
+
<!-- payments:end -->
|
|
347
|
+
<!-- privacy:begin -->
|
|
348
|
+
- **Privacy requests.** Users open export and erasure requests from Privacy.
|
|
349
|
+
Fulfil them with `privacy.fulfil(id, { actor })` (export returns the data to
|
|
350
|
+
send; erasure deletes the user's rows listed in `userTables`).
|
|
351
|
+
<!-- privacy:end -->
|
|
352
|
+
<!-- ai:begin -->
|
|
353
|
+
- **AI.** `api/services.ts` answers from a canned provider. Put a real one
|
|
354
|
+
there, and set `AI_CAP_USER_DAY` and `AI_CAP_GLOBAL_MONTH` to what you will
|
|
355
|
+
pay.
|
|
356
|
+
<!-- ai:end -->
|
|
357
|
+
<!-- auth:end -->
|
|
358
|
+
<!-- api:end -->
|
|
359
|
+
<!-- !api:begin -->
|
|
360
|
+
`__PM_RUN__ build` writes the app to `dist/`. Serve it from any static host
|
|
361
|
+
that falls back to `index.html` for unknown paths, with `VITE_API_URL` set at
|
|
362
|
+
build time. The API must allow this origin (CORS, credentials) and set its
|
|
363
|
+
session cookie for it.
|
|
364
|
+
<!-- !api:end -->
|
|
365
|
+
<!-- api:begin -->
|
|
366
|
+
|
|
367
|
+
## Observability
|
|
368
|
+
|
|
369
|
+
Off by default. Set `OBSERVABILITY=on` and the API writes a JSON log line pair
|
|
370
|
+
per request (`request.started`, then `request.completed` or `request.failed`)
|
|
371
|
+
with the request id and trace id, opens an OpenTelemetry span per request, and
|
|
372
|
+
answers `/health/live` and `/health/ready` (the database). Probes are left out
|
|
373
|
+
of spans and logs. Spans are no-ops until you register an OpenTelemetry SDK or
|
|
374
|
+
turn on Cloudflare tracing; Signet adds no exporter. To put the trace id on
|
|
375
|
+
every log line, pass `mixin` in `api/server.ts`. Prompts, bodies, cookies and
|
|
376
|
+
headers are never recorded.
|
|
377
|
+
<!-- api:end -->
|
|
378
|
+
<!-- web:begin -->
|
|
379
|
+
|
|
380
|
+
## Themes
|
|
381
|
+
|
|
382
|
+
`src/app.css` imports the __THEME__ theme from `@dapplelight/signet-tokens`.
|
|
383
|
+
Swap the import (`editorial`, `cinematic`, `finance`) and every component
|
|
384
|
+
reshapes. No component changes.
|
|
385
|
+
<!-- web:end -->
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { createRpcClient } from "@dapplelight/signet-lib/api"
|
|
2
|
+
// api:begin
|
|
2
3
|
import { hc } from "hono/client"
|
|
3
4
|
// Types only: no server code reaches the browser bundle.
|
|
4
5
|
import type { AppType } from "../../api/app.ts"
|
|
6
|
+
// api:end
|
|
7
|
+
import { hc } from "hono/client" // @!api
|
|
5
8
|
|
|
9
|
+
// api:begin
|
|
6
10
|
/**
|
|
7
11
|
* The typed client for the API in api/. Paths, inputs and responses come from
|
|
8
12
|
* the server's routes, so a renamed route or field fails typecheck here.
|
|
@@ -12,12 +16,34 @@ import type { AppType } from "../../api/app.ts"
|
|
|
12
16
|
* const { user } = await (await api.account.me.$get()).json()
|
|
13
17
|
*/
|
|
14
18
|
export const api = createRpcClient(hc<AppType>, { baseUrl: "/" }).api
|
|
19
|
+
// api:end
|
|
20
|
+
// !api:begin
|
|
21
|
+
/**
|
|
22
|
+
* The client for your API, which lives outside this project. Set VITE_API_URL
|
|
23
|
+
* (e.g. in .env.local) to its origin; unset, it calls this origin. A 401
|
|
24
|
+
* throws AuthError, any other failure an ApiError with the envelope's message.
|
|
25
|
+
*
|
|
26
|
+
* Calls are untyped until you give the client your API's type (an app
|
|
27
|
+
* scaffolded with --api exports it); then a renamed route or field fails
|
|
28
|
+
* typecheck here:
|
|
29
|
+
*
|
|
30
|
+
* import type { AppType } from "<your api>/api/app.ts"
|
|
31
|
+
* export const api = createRpcClient(hc<AppType>, { baseUrl }).api
|
|
32
|
+
*/
|
|
33
|
+
const baseUrl = import.meta.env.VITE_API_URL ?? "/"
|
|
34
|
+
// biome-ignore lint/suspicious/noExplicitAny: untyped until it has your API's AppType (above)
|
|
35
|
+
export const api: any = (createRpcClient(hc, { baseUrl }) as any).api
|
|
36
|
+
// !api:end
|
|
37
|
+
// auth:begin
|
|
15
38
|
|
|
16
|
-
/** The signed-in user and their plan; shared by the shell and the pages. */
|
|
39
|
+
/** The signed-in user and their plan; shared by the shell and the pages. */ // @payments
|
|
40
|
+
/** The signed-in user; shared by the shell and the pages. */ // @!payments
|
|
17
41
|
export const meQuery = {
|
|
18
42
|
queryKey: ["me"],
|
|
19
43
|
queryFn: async () => (await api.account.me.$get()).json(),
|
|
20
44
|
}
|
|
45
|
+
// auth:end
|
|
46
|
+
// orgs:begin
|
|
21
47
|
|
|
22
48
|
/** The user's orgs with their role, for the switcher. */
|
|
23
49
|
export const orgsQuery = {
|
|
@@ -36,5 +62,6 @@ export async function switchOrg(orgId: string, to = "/") {
|
|
|
36
62
|
await api.orgs.switch.$post({ json: { orgId } })
|
|
37
63
|
window.location.assign(to)
|
|
38
64
|
}
|
|
65
|
+
// orgs:end
|
|
39
66
|
|
|
40
67
|
export { ApiError, AuthError } from "@dapplelight/signet-lib/api"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { onAuthErrorRedirect, queryDefaults } from "@dapplelight/signet-lib/api/query-client"
|
|
1
|
+
import { onAuthErrorRedirect, queryDefaults } from "@dapplelight/signet-lib/api/query-client" // @auth
|
|
2
|
+
import { queryDefaults } from "@dapplelight/signet-lib/api/query-client" // @!auth
|
|
2
3
|
import { ThemeProvider } from "@dapplelight/signet-ui/components/theme-provider"
|
|
3
4
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
|
4
5
|
import { RouterProvider } from "@tanstack/react-router"
|
|
@@ -10,6 +11,7 @@ import "./app.css"
|
|
|
10
11
|
|
|
11
12
|
const queryClient = new QueryClient({ defaultOptions: queryDefaults() })
|
|
12
13
|
const router = createAppRouter()
|
|
14
|
+
// auth:begin
|
|
13
15
|
|
|
14
16
|
// One redirect on session expiry, however many requests fail at once. A full
|
|
15
17
|
// page load, not router.navigate: it drops every cached query of the old
|
|
@@ -20,6 +22,7 @@ const router = createAppRouter()
|
|
|
20
22
|
onAuthErrorRedirect(queryClient, () => {
|
|
21
23
|
window.location.assign(router.buildLocation({ to: "/login" }).href)
|
|
22
24
|
})
|
|
25
|
+
// auth:end
|
|
23
26
|
|
|
24
27
|
const rootElement = document.getElementById("root")
|
|
25
28
|
if (!rootElement) throw new Error("Root element #root not found in index.html")
|