Generated types
Download TypeScript types for your project's content model, see what the file contains and use it with the JavaScript client.
Mojimoto can write the TypeScript types for your content model for you. One request returns a .ts file with a type for every content type, every blocks field and every block, so your editor completes field names and a mistyped one fails the build.
The file is built from the content model as it is at the moment you ask. Download it again whenever the model changes.
Download the file
There are two ways to get it, and both return the same file.
With the CLI
npx @mojimoto/cli types \
--endpoint https://mojimoto.com/api/v1 \
--project acme \
--token mjmt_your_read_token✓ Wrote mojimoto.generated.ts for project "acme" (83 lines).Add -o types/mojimoto.ts to write it somewhere else. Every flag is on the CLI page.
With the types endpoint
/api/v1/{project}/typescurl -H "Authorization: Bearer mjmt_your_read_token" \
https://mojimoto.com/api/v1/acme/types -o mojimoto.generated.tsAny token that can read will do, including the project's public read token. The response is the file itself, sent as application/typescript with the filename mojimoto.generated.ts. Without a token the answer is 401.
The API keys page of your project shows this curl command with your project id and public read token already filled in, under Generate typed SDK.
What the file contains
This is the whole file for the sample site a new project can start with (see the Quickstart): four content types, one of them with a blocks field.
// Generated by Mojimoto - do not edit by hand.
// Project: acme
export interface MojimotoDocument<T extends string, D> {
id: number
uid: string | null
type: T
lang: string
status: "draft" | "published"
published_at: string | null
updated_at: string
data: D
}
// A resolved reference is the linked entry; unresolved, just its id + type.
export type MojimotoLink = MojimotoAnyDocument | { id: number; type: string; uid: string | null }
// A media field: the linked asset (url, alt, dimensions) plus how it
// should sit in its frame. id/alt/dimensions are null for legacy
// url-only values that predate asset linking.
export type MojimotoMedia = {
id: number | null
url: string
alt: string | null
width: number | null
height: number | null
content_type: string | null
fit: "cover" | "contain"
}
// One block in a blocks field. Same shape as a document, so slice
// zones render blocks and referenced sections alike.
export type MojimotoBlock<T extends string = string, D = Record<string, unknown>> = {
id: string
type: T
data: D
}
export type AuthorData = {
name: string
slug: string
role: string
}
export type PageData = {
title: string
slug: string
sections: PageSectionsBlock[]
}
export type PageSectionsHeroBlockData = {
heading: string
text: string
}
export type PageSectionsTextBlockData = {
body: unknown
}
export type PageSectionsCallToActionBlockData = {
label: string
url: string
}
export type PageSectionsBlock = MojimotoBlock<"hero", PageSectionsHeroBlockData> | MojimotoBlock<"text", PageSectionsTextBlockData> | MojimotoBlock<"call_to_action", PageSectionsCallToActionBlockData>
export type PostData = {
title: string
slug: string
published_on: string
excerpt: string
body: unknown
author: MojimotoLink | null
}
export type SiteSettingsData = {
site_name: string
tagline: string
footer_note: string
}
export type MojimotoAnyDocument = MojimotoDocument<"author", AuthorData> | MojimotoDocument<"page", PageData> | MojimotoDocument<"post", PostData> | MojimotoDocument<"site_settings", SiteSettingsData>;The shared types
The first four types are the same in every project:
| Type | What it describes |
|---|---|
MojimotoDocument<T, D> |
One delivered entry: its id, uid, type, lang, status, dates and data. T is the content type's api id and D the type of its fields. |
MojimotoLink |
The value of a reference field: the linked entry, or the stub { id, type, uid } it collapses to when it is beyond the requested depth or has nothing published. |
MojimotoMedia |
The value of a media field: the asset's url, alt, size and file type, plus the fit chosen for it. |
MojimotoBlock<T, D> |
One block of a blocks field: an id, the block type and its fields. |
The types for your model
After those come the types made from your content model:
- One
Datatype per content type, in alphabetical order of api id. The name is the api id in PascalCase plusData, sosite_settingsbecomesSiteSettingsData. It lists every field of the type across all its tabs. - For each blocks field, a union named after the content type and the field, plus
Block: thesectionsfield onpagebecomesPageSectionsBlock. Each block type gets its own data type, such asPageSectionsHeroBlockData. MojimotoAnyDocument, the union of every content type's document. A project with no content types getsnever.
How each field type is typed
| Field type | TypeScript type |
|---|---|
symbol, text, slug, select, date |
string |
number |
number |
boolean |
boolean |
rich_text |
unknown |
media |
MojimotoMedia | null |
reference |
MojimotoLink | null |
references |
MojimotoLink[] |
location |
{ lat: number; lon: number } | null |
json |
Record<string, unknown> |
blocks |
An array of that field's block union, such as PageSectionsBlock[] |
Three things the types do not tell you:
- Whether a field is filled. A
symbolfield is typedstringwhether or not it is required, and an entry that never had a value for it has no such key indata. Check before you use a field an editor may have left empty. - The options of a select field. It is typed
string, not as a union of its options. - Which types a reference allows.
MojimotoLinkcovers every content type in the project, so narrow it bytypebefore reading itsdata.
Use the types with the client
Every method of @mojimoto/client takes the document type as a type argument. Pair MojimotoDocument with a Data type from the file:
import { createClient, type MojimotoDocument } from '@mojimoto/client'
import type { PostData } from './mojimoto.generated'
type Post = MojimotoDocument<'post', PostData>
const cms = createClient({
endpoint: 'https://mojimoto.com/api/v1',
project: 'acme',
token: process.env.MOJIMOTO_TOKEN!,
})
const posts = await cms.all<Post>({ type: 'post' })
posts[0].data.title // string
posts[0].data.titel // error: Property 'titel' does not exist on type 'PostData'Rich text
A rich_text field is typed unknown. Cast it to MojiRichText, which the client exports:
import type { MojiRichText } from '@mojimoto/client'
const body = posts[0].data.body as MojiRichTextRendering it is covered on the Rich text page.
References
A reference is the linked entry or a stub, so check which one you have:
const author = posts[0].data.author
if (author && 'data' in author && author.type === 'author') {
author.data.name // string
}Blocks
Each block has a type, and checking it narrows data to that block's fields:
import type { PageSectionsBlock } from './mojimoto.generated'
function headingOf(section: PageSectionsBlock): string | null {
if (section.type === 'hero') {
return section.data.heading
}
return null
}More on the client itself is on the JavaScript client page.
Regenerate when the model changes
The file only changes when the content model changes: a type or a field added, removed, renamed or given a different field type. When that happens, run the command again and commit the result, or have your build fetch it so the two cannot drift.
npm install --save-dev @mojimoto/cli{
"scripts": {
"cms:types": "mojimoto types -o types/mojimoto.ts",
"prebuild": "mojimoto types -o types/mojimoto.ts"
}
}The scripts read MOJIMOTO_ENDPOINT, MOJIMOTO_PROJECT and MOJIMOTO_TOKEN from the environment, so set those three where the build runs.
Other ways to read the content model
GET /api/v1/{project}/content-typesreturns the model as JSON, with every field's settings. It is part of the Content Delivery API.- The MCP server serves this same TypeScript file to coding agents as the
mojimoto://content-modelresource. npx @mojimoto/cli initsaves it into a Claude Code skill. See the CLI page.
Something wrong or missing on this page? Tell us and we will fix it.