Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
84 changes: 84 additions & 0 deletions .changeset/headless-backend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
'@swirls/spools': minor
'@swirls/create-spools': minor
---

Spools is headless. It is the backend for custom business software, behind an HTTP API, and draws nothing: the
front end is yours, on any framework.

**Removed.** The interface a Spool came with is gone, with everything that existed only to
describe it:

- The pages, views and components (`@swirls/spools/pages/*`, `ui/*`, `theme.css`,
`auth/*/ui`), the framework's own server functions, and the starter's `src/routes`,
`src/spool.ui.ts`, `vite.config.ts` and build.
- Layouts (`ui` in `spool.config.json`, `spools ui`), in-app guides (`guides`,
`@swirls/spools/guides`, the `guide_progress` table) and alerts.
- A module's browser half, routes, guides and sections.
- From the config: an object's `icon` and `sections`; a view's `type`, `columns`, `groupBy`
and stored calculations (a view is now a named filter and sort, and `records.list` takes
`pageSize`, `fields` and `calculations`); a role's `record`, `hide` and `home.create`;
`home.create`, `home.counts` and `home.links`; `assistant.greeting` and
`assistant.suggestions`.
- `bun create @swirls/spools --headless`: there is one template, and it is headless.
- Starting without a vault's keys. A production server does not start without the `CS_*`
variables (`vault: cipherstash()`) or `VAULT_KEY` (`vault: aes()`), and on your own
machine a stand-in seals until they are set.

**What a Spool is now.**

- **Operations.** Everything a Spool does for whoever asks is defined once, as an operation
(`@swirls/spools/operations`).
- **The server.** `headlessServer()` from `@swirls/spools/headless` answers the API and
every endpoint a Spool has (sign-in, files, the assistant, webhooks, cron), with no web
framework, and takes `routes` for endpoints of your own. A new Spool's `src/server.ts` is
that, and `bun run dev` serves it with no build.
- **The HTTP API.** `httpApi()` from `@swirls/spools/http` answers every operation at
`POST /api/v1/<name>`, on any server, and describes itself at `GET /api/v1` and as OpenAPI
3.1 at `/api/v1/openapi.json`. A caller is known by the session cookie on the same site, a
sign-in token from a front end you name, or an API key. The access list, record scopes,
role access and record rules apply to every one of them.
- **API keys.** `spools keys create --name ... --as <email>` makes a key for a script or a
service, once the server says `keys: true`. It acts as that teammate, can be read-only,
and ends after 90 days unless you say otherwise. What is kept is sealed by the vault.
- **A typed client.** `createClient<SpoolObjects>()` from `@swirls/spools/client` knows every
operation and, through the `src/spool.types.ts` that `spools gen` writes, every field of
your objects.
- **Query options.** `createQueries` from `@swirls/spools/query` gives every operation as
query and mutation options for TanStack Query.
- **React hooks.** `useListState`, `useRecordForm` and `useRelationSearch` from
`@swirls/spools/react` hold the state of a list, a record's form and a relation picker,
and draw nothing. The rules they are made of are in `@swirls/spools/state`, for any
framework.
- **In a TanStack Start app.** `serveApi` from `@swirls/spools/api/http` mounts the API as
two route files, `startClient` from `@swirls/spools/start/client` asks it in-process on the
server and over HTTP in the browser, and `staffHandler` from `@swirls/spools/handlers` checks
who is asking in your own server functions. `examples/tanstack-start` is a small front
end that uses all of it.
- **Clerk without TanStack Start.** `clerk()` from `@swirls/spools/auth/clerk/backend` reads
the session from the request with Clerk's backend SDK.
- **Better Auth in an app.** `betterAuth({ bearer: true })` lets a phone or desktop app
carry its session as a bearer token.
- **Where your front end is.** `frontEnd` in `defineSpool` says where its sign-in,
integrations and unsubscribe pages are, for the few answers that send a browser to one.
Without an unsubscribe page the server answers the link in an email itself.
- **Routes that check who is calling.** `headlessServer` refuses to start with a route
whose handler was not made with `apiRoute`, `cronRoute` or `publicRoute`.
- **More in the answers.** `marketing.get` answers the tag report with install code and each
ad's click rate and costs, `files.startUpload` takes a `contentType`, and
`@swirls/spools/state` has the search matching and the marketing checks.

**Renamed.** The names that said `crm` say `spool`: `spool.config.json`, `src/spool.types.ts`,
`bun run gen`, `defineSpool({ config })`, `spoolConfig()`, `SpoolConfig`, `staffHandler`,
`StaffUser`, `SpoolRecord` and the rest, and `spool_event` and the `spool_*` workflows in
`.swirls` files. [Upgrading](https://spools.codes/docs/upgrading) has the whole list.

**Built-in modules.** Email sequences, mailbox and calendar sync, marketing and contact
tracking are modules now, from `@swirls/spools/modules`. A Spool has each only when it lists
it: add `modules: [sequences, mailbox, marketing, contactTracking]` to `defineSpool` to keep
all four. Without one, its tables are not made, its operations and routes are not there, and
its part of the config is refused. `session.get` answers the ones listed as `features`.

This version changes the tables: it adds `api_keys`, and removes `guide_progress` and the
`type` column of `saved_views`. After updating run `bun run gen`, then `bun run db:push`
or your migration. See [Upgrading](https://spools.codes/docs/upgrading).
36 changes: 3 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ jobs:
- name: Check generated files
working-directory: apps/starter
run: |
bun run crm:gen
git diff --exit-code -- prisma crm.config.schema.json \
|| (echo "::error::Run 'bun run crm:gen' in apps/starter and commit the result" && exit 1)
bun run gen
git diff --exit-code -- prisma spool.config.schema.json src/spool.types.ts \
|| (echo "::error::Run 'bun run gen' in apps/starter and commit the result" && exit 1)
# The scope tests run on a Spool of their own, whose Prisma schema is generated too.
- name: Check the scope tests' generated schema
working-directory: packages/spools
Expand All @@ -66,12 +66,6 @@ jobs:
run: bun run lint
- name: Test
run: bun run test
- name: Build the starter
working-directory: apps/starter
run: |
bun run build
git diff --exit-code -- src/routeTree.gen.ts \
|| (echo "::error::Run 'bun run build' in apps/starter and commit src/routeTree.gen.ts" && exit 1)

integration:
name: integration (${{ matrix.store }})
Expand Down Expand Up @@ -150,30 +144,6 @@ jobs:
working-directory: apps/starter
run: swirls doctor

e2e:
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 15
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: 1.4.4
- name: Install
run: bun install --frozen-lockfile
- name: Install Chromium
run: bunx playwright install --with-deps chromium
- name: Test in a browser
run: bun run test:e2e
- name: Keep the report
if: failure()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: playwright-report
path: |
apps/starter/e2e/playwright-report/
apps/starter/e2e/test-results/
retention-days: 7

deploy-docs:
if: >-
github.event_name == 'push' && github.ref == 'refs/heads/main' &&
Expand Down
4 changes: 3 additions & 1 deletion .oxfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"**/coverage/**",
"**/routeTree.gen.ts",
"**/prisma/schema.*",
"**/crm.config.schema.json",
"**/spool.config.schema.json",
"**/spool.types.ts",
"**/migrations/**",
"**/supabase/**",
"**/docs-manifest.json",
"packages/create-spools/template/**",
"packages/create-spools/template-headless/**",
"tools/oxlint/anti-slop/**",
"bun.lock",
".agent/**",
Expand Down
55 changes: 25 additions & 30 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"**/.flox/**",
"**/routeTree.gen.ts",
"**/prisma/schema.d.ts",
"**/spool.types.ts",
"**/migrations/**",
"packages/create-spools/template/**",
"packages/create-spools/overrides/**",
".agent/**",
".agents/**",
".claude/**",
Expand Down Expand Up @@ -184,7 +186,7 @@
}
},
{
"files": ["**/*.config.ts", "**/*.config.e2e.ts"],
"files": ["**/*.config.ts"],
"rules": {
"effecttsgo/node-builtin-import": "off",
"effecttsgo/process-env": "off"
Expand All @@ -206,7 +208,7 @@
},
{
"files": [
"packages/spools/src/crm/extensions.ts",
"packages/spools/src/core/extensions.ts",
"packages/spools/src/server/runtime/agents.ts",
"packages/spools/src/server/store/objects.ts"
],
Expand All @@ -215,7 +217,12 @@
}
},
{
"files": ["packages/spools/src/crm/filters.ts", "packages/spools/src/crm/options.ts"],
"files": [
"packages/spools/src/core/filters.ts",
"packages/spools/src/core/options.ts",
"packages/spools/src/core/list-state.ts",
"packages/spools/src/core/record-state.ts"
],
"rules": {
"anti-slop/no-unknown-parameters": "off",
"anti-slop/no-known-value-widening": "off"
Expand All @@ -232,7 +239,7 @@
}
},
{
"files": ["packages/spools/src/server/**", "apps/starter/src/routes/api/**"],
"files": ["packages/spools/src/server/**"],
"excludeFiles": [
"packages/spools/src/server/runtime/**",
"packages/spools/src/server/env.ts",
Expand Down Expand Up @@ -268,13 +275,11 @@
"no-restricted-imports": [
"error",
{
"paths": [
{
"name": "@tanstack/react-start/server",
"message": "The request belongs to the boundary (packages/spools/src/server/runtime/boundary.ts); a program reads it from CurrentRequest."
}
],
"patterns": [
{
"group": ["@tanstack/**", "react", "react/**", "react-dom", "react-dom/**"],
"message": "The server is free of any web framework, so a Spool runs with none (docs/architecture.md). TanStack Start is bound in packages/spools/src/start.ts and start-client.ts, and React in react.ts."
},
{
"group": ["**/app/extensions", "**/spool.server"],
"message": "The framework never imports the app. Read it from the Spool (packages/spools/src/server/spool.ts)."
Expand Down Expand Up @@ -311,6 +316,10 @@
"error",
{
"patterns": [
{
"group": ["@tanstack/**", "react", "react/**", "react-dom", "react-dom/**"],
"message": "The server is free of any web framework, so a Spool runs with none (docs/architecture.md). TanStack Start is bound in packages/spools/src/start.ts and start-client.ts, and React in react.ts."
},
{
"group": ["**/app/extensions", "**/spool.server"],
"message": "The framework never imports the app. Read it from the Spool (packages/spools/src/server/spool.ts)."
Expand Down Expand Up @@ -341,40 +350,26 @@
}
},
{
"files": ["packages/spools/src/server/*.functions.ts"],
"rules": {
"spools/server-function-files": "error"
}
},
{
"files": ["packages/spools/src/api/**", "apps/starter/src/routes/**"],
"files": ["packages/spools/src/api/**"],
"rules": {
"spools/route-adapters": "error"
}
},
{
"files": [
"packages/spools/src/crm/**",
"packages/spools/src/pages/**",
"packages/spools/src/views/**",
"apps/starter/src/routes/**",
"apps/starter/src/router.tsx",
"apps/starter/src/start.ts",
"apps/starter/src/spool.ui.ts"
],
"excludeFiles": ["apps/starter/src/routes/api/**", "packages/spools/src/crm/codegen.ts"],
"files": ["packages/spools/src/core/**"],
"excludeFiles": ["packages/spools/src/core/codegen.ts"],
"rules": {
"no-restricted-imports": [
"error",
{
"patterns": [
{
"group": ["effect", "effect/*"],
"message": "Effect is server-only: it must not reach the browser bundle. Call a server function instead; see docs/server-code.md."
"message": "This code is shared with the browser (a front end imports it through @swirls/spools/state), and Effect is server-only; see docs/server-code.md."
},
{
"group": ["@clerk/**", "better-auth", "better-auth/*"],
"message": "Pages reach the auth provider with useAuth() (packages/spools/src/crm/ui/auth.tsx), so any adapter works; see docs/adapters.md."
"message": "Only an auth adapter (packages/spools/src/adapters/auth) talks to an auth provider; see docs/adapters.md."
},
{
"group": [
Expand All @@ -385,7 +380,7 @@
"@swirls/spools/email/*",
"@swirls/spools/files/*"
],
"message": "Pages never import an adapter. The app chooses its adapters in src/spool.server.ts and src/spool.ui.ts; see docs/adapters.md."
"message": "Code shared with the browser never imports an adapter. The app chooses its adapters in src/spool.server.ts; see docs/adapters.md."
}
]
}
Expand Down
Loading
Loading