npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@datocms/cma-schema-types-generator

v5.5.4

Published

Generate TypeScript type definitions from a DatoCMS project schema

Downloads

18,030

Readme

@datocms/cma-schema-types-generator

Generate TypeScript type definitions from a DatoCMS project schema.

Produces ItemTypeDefinition-based types (one per model/block) plus AnyBlock / AnyModel / AnyBlockOrModel union types, ready to be used with @datocms/cma-client for type-safe content management and migration scripts.

Installation

npm install @datocms/cma-schema-types-generator

@datocms/cma-client is a peer dependency.

Usage

import { buildClient } from '@datocms/cma-client';
import { generateSchemaTypes } from '@datocms/cma-schema-types-generator';

const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN! });
const code = await generateSchemaTypes(client);

code is a string containing prettier-formatted TypeScript, ready to be written to disk:

import { writeFile } from 'node:fs/promises';

await writeFile('src/generated/schema.ts', code);

Example output

import type { ItemTypeDefinition } from '@datocms/cma-client';

type EnvironmentSettings = {
  locales: 'en' | 'it';
};

export type BlogPost = ItemTypeDefinition<
  EnvironmentSettings,
  '12345',
  {
    title: { type: 'string'; localized: true };
    content: { type: 'rich_text'; blocks: Hero };
  }
>;
export const BlogPost = {
  ID: '12345',
  REF: { type: 'item_type', id: '12345' },
} as const;

export type Hero = ItemTypeDefinition<EnvironmentSettings, '67890', { /* … */ }>;
export const Hero = {
  ID: '67890',
  REF: { type: 'item_type', id: '67890' },
} as const;

export type AnyBlock = Hero;
export type AnyModel = BlogPost;
export type AnyBlockOrModel = AnyBlock | AnyModel;

Type-and-value declaration merging

Each item type is emitted twice: once as a TypeScript type (the ItemTypeDefinition<…> alias) and once as an export const carrying its runtime metadata. TypeScript merges the two — Schema.BlogPost resolves both as a type and as a value, depending on the position it's used in.

The runtime const exposes two fields:

  • ID — the bare item type id (e.g. '12345'), typed as a string literal so it can be used for narrowing.
  • REF — the JSON:API resource identifier object { type: 'item_type', id: '<sameId>' }, ready to be passed wherever the API expects an item type reference.

This lets call sites stay free of hardcoded string ids:

import { buildClient } from '@datocms/cma-client';
import * as Schema from './generated/schema';

const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN! });

await client.items.create<Schema.LandingPage>({
  item_type: Schema.LandingPage.REF,
  title: 'Hello world',
});

if (block.__itemTypeId === Schema.HeroBlock.ID) {
  // narrowed to Schema.HeroBlock
}

Options

Both entry points accept an optional SchemaTypesGeneratorOptions object:

  • itemTypesFilter — comma-separated list of model api keys to include. Block dependencies referenced by the kept models (through rich_text, structured_text, single_block) are pulled in automatically. Unknown api keys are ignored.

Migration variant

generateSchemaTypesForMigration produces the same declarations without the import type { ItemTypeDefinition } line — meant to be inlined directly inside a migration script that already has access to the type.

import { generateSchemaTypesForMigration } from '@datocms/cma-schema-types-generator';

const inline = await generateSchemaTypesForMigration(client, {
  itemTypesFilter: 'blog_post,author',
});