Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .changeset/cms-definition-entry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
'@tanstack/cms-core': minor
'@tanstack/cms-ui-react': patch
---

Add the light CMS definition entry, `@tanstack/cms-core/definition`, now the
recommended shape for new apps. `defineCmsDefinition({ collections, admin,
plugins })` holds what routing and build tooling need and imports no React,
server code, or Start, so `vite.config.ts` can import it. Its accessors are
derived from it: `adminBasePath()`, `collections()`, `pluginRoutes()`, and
`routeKeys()`. The entry also exports the pure authoring helpers, the same
functions the root exports: `defineCollection`, `defineBlock`,
`defineFieldType`, `definePlugin`, `seoField`, `assetField`,
`redirectsCollection`, `isSafeRedirectTarget`, and `isBuiltInFieldType`. It
exports the types collections use too, so a `cms.definition.ts` and its
collection modules never need the package root.

`defineCms(definition, runtime)` (and `createCms(definition, runtime)`) extends
a definition with the runtime config: `auth`, `storage`, `ai`, `ops`,
`eventHandlers`, `seed`, and the rest. The runtime config can't repeat
`collections`, `admin`, or `plugins`. It resolves the same config, with the same
plugin merging and name checks, as `defineCms(config)`, which keeps working.

```ts
// src/cms.definition.ts
import { defineCmsDefinition } from '@tanstack/cms-core/definition'

export const definition = defineCmsDefinition({
collections: [post, author],
admin: { basePath: '/admin' },
})

// src/cms.config.ts
export const cms = defineCms(definition, { blockTypes: [], auth, storage })
```

`Register` accepts the definition (`cms: typeof definition`) as well as the full
config, and `RegisteredCollectionName`, `RegisteredDoc`, and
`RegisteredAdminBasePath` infer from it.

The CMS route keys now live in core as `CMS_ROUTE_KEYS` (grouped by `public`,
`admin`, and `api`). `CmsRouteKey` from `@tanstack/cms-ui-react/virtual-routes`
is the same type, re-exported.
109 changes: 104 additions & 5 deletions docs/api/cms-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
title: "@tanstack/cms-core"
id: api-cms-core
order: 1
description: "API reference for @tanstack/cms-core: defineCms and the config types, theme and SEO helpers, the schema and field system, the operation registry, the auth contract, roles and permissions, structured errors, plus the /storage and /media entry points."
description: "API reference for @tanstack/cms-core: defineCms, the light /definition entry, and the config types, theme and SEO helpers, the schema and field system, the operation registry, the auth contract, roles and permissions, structured errors, plus the /storage and /media entry points."
keywords:
- tanstack cms
- api reference
- "@tanstack/cms-core"
- defineCms
- defineCmsDefinition
- CmsConfig
- schemaFor
- cmsHead
Expand All @@ -16,11 +17,12 @@ keywords:
- createLocalMedia
---

`@tanstack/cms-core` is the framework-agnostic engine. It has four entry points:
`@tanstack/cms-core` is the framework-agnostic engine. It has five entry points:

| Entry point | Environment | Contents |
| --- | --- | --- |
| `@tanstack/cms-core` | client and server | Config, schema and fields, theme, SEO head helpers, roles and permissions, errors (this page). |
| `@tanstack/cms-core/definition` | anywhere, including `vite.config.ts` | `defineCmsDefinition()`, the light CMS definition, and the CMS route keys ([below](#the-cms-definition-definecmsdefinition)). |
| `@tanstack/cms-core/server` | server only | Store, workflow, media, SEO files, webhooks, transfer, auth guards. See [`@tanstack/cms-core/server`](./cms-core-server). |
| `@tanstack/cms-core/storage` | config-safe | `StorageAdapter` contract, `createSqliteStorage()`, and the SQL adapter kit ([below](#tanstackcms-corestorage)). |
| `@tanstack/cms-core/media` | config-safe | `MediaAdapter` contract and `createLocalMedia()` ([below](#tanstackcms-coremedia)). |
Expand Down Expand Up @@ -86,6 +88,103 @@ export const cms = defineCms({

`cms.config.ts` is bundled for the browser too. Read secrets lazily inside functions (AI adapter factories, webhook `url`/`secret` functions), never at module top level.

### The CMS definition: `defineCmsDefinition()`

**The recommended shape for new apps.** Split the CMS into a light definition and the runtime config that extends it:

- `src/cms.definition.ts` holds what routing and build tooling need: `collections`, `admin`, and `plugins`. It and its collection modules import only `@tanstack/cms-core/definition`, so `vite.config.ts` can import it without loading auth, storage, AI, ops, or event handlers.
- `src/cms.config.ts` calls `defineCms(definition, runtime)` with everything else. Nothing is repeated.

```ts
// src/collections.ts
import { defineCollection, seoField } from '@tanstack/cms-core/definition'

export const post = defineCollection({
name: 'post',
label: 'Posts',
titleField: 'title',
fields: [{ name: 'title', label: 'Title', type: 'text' }, seoField()],
})
```

```ts
// src/cms.definition.ts
import { defineCmsDefinition } from '@tanstack/cms-core/definition'
import { post, author } from './collections'

export const definition = defineCmsDefinition({
collections: [post, author],
admin: { basePath: '/admin' },
})
```

```ts
// src/cms.config.ts
import { defineCms } from '@tanstack/cms-core'
import { definition } from './cms.definition'

export const cms = defineCms(definition, {
blockTypes: [],
auth,
storage,
ai,
ops,
eventHandlers,
seed,
})
```

```ts
function defineCmsDefinition<const TConfig extends CmsDefinitionConfig>(
config: TConfig,
): CmsDefinition<TConfig>

function defineCms<const TDefinition, const TRuntime extends CmsRuntimeConfig>(
definition: CmsDefinition<TDefinition>,
runtime: TRuntime,
): WithPlugins<DefinedCmsConfig<TDefinition, TRuntime>>
```

`defineCmsDefinition` throws on the same collection and plugin name clashes as `defineCms`. The runtime config is every `CmsConfig` option except `collections`, `admin`, and `plugins`; repeating one of those is a type error and throws. `defineCms(definition, runtime)` resolves exactly the config `defineCms({ ...definitionConfig, ...runtime })` would, and `createCms(definition, runtime)` builds an instance the same way. `defineCms(config)` with one config keeps working, and is fine for an app whose config is already light.

The definition's accessors are derived from it, never written by hand, and return the same frozen values on every call:

| Accessor | Returns |
| --- | --- |
| `adminBasePath()` | The normalized `admin.basePath`: `''` or `/segment` without a trailing slash. |
| `collections()` | `{ name }` for each app and plugin collection, in order. Public URL prefixes come later. |
| `pluginRoutes()` | `{ routes, adminRoutes }`, each entry `{ plugin, route }`: the plugins' route nodes, tagged with the plugin name. |
| `routeKeys()` | Every CMS route key (`index`, `content`, `edit`, `review`, `apiV1`, …). The list is `CMS_ROUTE_KEYS`, grouped by surface (`public`, `admin`, `api`); `CmsRouteKey` is its union. |

`definition.config` is the config as written. `isCmsDefinition(value)` tells a definition apart from a config.

`@tanstack/cms-core/definition` also exports the pure authoring helpers, so collection modules never need the package root: `defineCollection`, `defineBlock`, `defineFieldType`, `definePlugin`, `seoField`, `assetField`, `redirectsCollection`, `isSafeRedirectTarget`, and `isBuiltInFieldType`. These are the same functions the root exports. The entry also exports the types collections use, including `Collection`, `Field`, `BlockType`, `InferDoc`, `CollectionAccess`, `FieldAccess`, and `CmsPlugin`. `defineInterfaceCollection` stays root-only, because its validators need the interface schemas.

```ts validate=type
import { defineCms } from '@tanstack/cms-core'
import { defineCmsDefinition } from '@tanstack/cms-core/definition'

const definition = defineCmsDefinition({
collections: [
{
name: 'post',
label: 'Posts',
titleField: 'title',
fields: [{ name: 'title', label: 'Title', type: 'text', required: true }],
},
],
admin: { basePath: '/admin' },
})

definition.adminBasePath() // '/admin'
definition.collections() // [{ name: 'post' }]

export const cms = defineCms(definition, { blockTypes: [], dataDir: '.data' })
cms.collections.post.label // 'Posts'
```

`pnpm test:definitions` (`scripts/verify-definitions.ts`) loads every `cms.definition.ts` under `app/`, `starters/`, and `examples/` in plain Node and reports any import of `react`, `react-dom`, a `.tsx` file, `@tanstack/react-start`, `@tanstack/react-router`, the `@tanstack/cms-ui-react` root, `@tanstack/cms-core/server`, or a `*.server.*` file. It runs report-only for now; `--strict` makes it fail.

### `getCmsConfig()`

```ts
Expand Down Expand Up @@ -372,20 +471,20 @@ interface SeedStore {
### `Register` and registered types

```ts
interface Register {} // augment with { cms: typeof cms }
interface Register {} // augment with { cms: typeof definition } or { cms: typeof cms }

type RegisteredCollections // your collections keyed by name, or Record<string, Collection>
type RegisteredCollectionName // their names (string when nothing is registered)
type RegisteredDoc<C extends string> // InferDoc of collection C, or Doc
type RegisteredAdminBasePath // admin.basePath literal, or ''
```

Augment `Register` once to type `RegisteredDoc`, `adminPath()`, and the `@tanstack/cms-client` collection helpers:
Augment `Register` once to type `RegisteredDoc`, `adminPath()`, and the `@tanstack/cms-client` collection helpers. `cms` may be the definition (collections, plugin collections, and `admin.basePath` are typed from it), the `defineCms` result, or the config passed to it. Register the `defineCms` result instead when you need app ops typed (`RegisteredOps`), since ops live in the runtime config:

```ts
declare module '@tanstack/cms-core' {
interface Register {
cms: typeof cms
cms: typeof definition
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion docs/api/cms-ui-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ type RelationSearch = (
```ts
function cmsVirtualRoutes(options?: CmsVirtualRoutesOptions): VirtualRootRoute

// Re-exported from @tanstack/cms-core/definition (CMS_ROUTE_KEYS)
type CmsRouteKey =
| 'index' | 'page' | 'post' | 'sitemap' | 'robots' | 'notFound'
| 'index' | 'page' | 'post' | 'preview' | 'sitemap' | 'robots' | 'notFound'
| 'content' | 'edit' | 'agent' | 'review' | 'settings'
| 'apiWebMcp' | 'apiV1' | 'apiMcp' | 'apiMedia' | 'apiMediaFile'
```
Expand Down
8 changes: 5 additions & 3 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"ignore": [
"kiira.config.ts",
".claude/skills/**",
"scripts/codemods/__fixtures__/**"
"scripts/codemods/__fixtures__/**",
"scripts/__fixtures__/**"
],
"ignoreExportsUsedInFile": true,
"workspaces": {
Expand All @@ -27,8 +28,9 @@
},
"packages/core": {
"entry": [
"src/{index,server,storage,media}.ts",
"src/{storage,media}/conformance.ts"
"src/{index,definition,server,storage,media}.ts",
"src/{storage,media}/conformance.ts",
"tests/register-definition/*.ts"
]
},
"packages/ui-react": {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"clean": "pnpm --filter \"./packages/**\" --filter \"./onboarding/packages/**\" run clean",
"clean:all": "git clean -fdx --exclude=\"!.env\"",
"test": "pnpm run test:ci",
"test:pr": "VITEST_MAX_WORKERS=1 nx affected --targets=test:sherif,test:knip,test:docs,test:starters,test:kiira,test:oxlint,test:lib,test:starter,test:types,test:scripts,test:build,build --exclude=*-before && pnpm test:dts && pnpm test:dialects",
"test:ci": "VITEST_MAX_WORKERS=1 nx run-many --targets=test:sherif,test:knip,test:docs,test:starters,test:kiira,test:oxlint,test:lib,test:starter,test:types,test:scripts,test:build,build --exclude=*-before && pnpm test:dts && pnpm test:dialects",
"test:pr": "VITEST_MAX_WORKERS=1 nx affected --targets=test:sherif,test:knip,test:docs,test:starters,test:kiira,test:oxlint,test:lib,test:starter,test:types,test:scripts,test:build,build --exclude=*-before && pnpm test:dts && pnpm test:dialects && pnpm test:definitions",
"test:ci": "VITEST_MAX_WORKERS=1 nx run-many --targets=test:sherif,test:knip,test:docs,test:starters,test:kiira,test:oxlint,test:lib,test:starter,test:types,test:scripts,test:build,build --exclude=*-before && pnpm test:dts && pnpm test:dialects && pnpm test:definitions",
"test:oxlint": "nx affected --target=test:oxlint --exclude=examples/**",
"test:sherif": "sherif --ignore-dependency typescript --ignore-package payload-before --ignore-package astro-starlight-before",
"test:lib": "VITEST_MAX_WORKERS=1 nx affected --targets=test:lib --exclude=examples/**",
Expand All @@ -30,7 +30,8 @@
"test:docs": "tsx scripts/verify-links.ts",
"test:starters": "tsx scripts/verify-starters.ts",
"test:dialects": "tsx scripts/dialect-inventory.ts --check",
"test:scripts": "vitest run scripts/codemods scripts/dialect-inventory.test.ts scripts/seo-snapshots",
"test:definitions": "tsx scripts/verify-definitions.ts",
"test:scripts": "vitest run scripts/codemods scripts/dialect-inventory.test.ts scripts/seo-snapshots scripts/verify-definitions.test.ts",
"test:dts": "node scripts/scan-dangling-dts.mjs",
"test:kiira": "kiira check",
"build": "nx affected --skip-nx-cache --targets=build --exclude=examples/**",
Expand All @@ -51,6 +52,7 @@
},
"nx": {
"includedScripts": [
"test:definitions",
"test:dialects",
"test:docs",
"test:dts",
Expand Down
9 changes: 7 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tanstack/cms-core",
"version": "0.1.0",
"description": "Framework-agnostic CMS engine for TanStack — schema-as-code, pluggable storage and media, drafts and review workflow, roles and access control, agent operations.",
"description": "Framework-agnostic CMS engine for TanStack \u2014 schema-as-code, pluggable storage and media, drafts and review workflow, roles and access control, agent operations.",
"author": "Tanner Linsley",
"license": "MIT",
"homepage": "https://tanstack.com/cms",
Expand All @@ -21,6 +21,7 @@
"sideEffects": false,
"exports": {
".": "./src/index.ts",
"./definition": "./src/definition.ts",
"./server": "./src/server.ts",
"./flags": "./src/flags.ts",
"./storage": "./src/storage.ts",
Expand All @@ -43,6 +44,10 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./definition": {
"types": "./dist/definition.d.ts",
"import": "./dist/definition.js"
},
"./server": {
"types": "./dist/server.d.ts",
"import": "./dist/server.js"
Expand Down Expand Up @@ -84,7 +89,7 @@
"test:coverage:watch": "vitest --coverage --watch",
"test:lib": "vitest run",
"test:lib:dev": "pnpm test:lib --watch",
"test:types": "tsc --noEmit",
"test:types": "tsc --noEmit && tsc --noEmit -p tests/register-definition",
"build": "tsdown",
"test:build": "publint --strict --pack pnpm"
},
Expand Down
25 changes: 2 additions & 23 deletions packages/core/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import type { AssetMediaKind, Collection, Field } from './schema'
import type { CmsAssetsConfig } from './config'

export { assetField } from './lib/authoring'

export {
DEFAULT_ASSET_COLLECTION,
assetCollectionName,
Expand Down Expand Up @@ -130,29 +132,6 @@ export interface AssetFieldOptions {
index?: boolean
}

/**
* A strongly-typed `asset` field: a specialization of `relation` that targets
* the asset collection and stores the asset id (or ids for `many`). `kinds`
* narrows the picker to specific asset classes (e.g. `['image']`). The target
* collection is resolved from `defineCms({ assets: { collection } })`.
*
* ```ts
* fields: [assetField({ name: 'hero', label: 'Hero', kinds: ['image'] })]
* ```
*/
export function assetField(options: AssetFieldOptions): Field {
return {
name: options.name,
label: options.label,
type: 'asset',
...(options.many ? { many: true } : {}),
...(options.required !== undefined ? { required: options.required } : {}),
...(options.kinds ? { kinds: [...options.kinds] } : {}),
...(options.description ? { description: options.description } : {}),
...(options.index ? { index: options.index } : {}),
}
}

/**
* Whether a field references the asset collection: an `asset` field, or a
* plain `relation` already pointed at the asset collection. An `image` field
Expand Down
Loading
Loading