MicroCMS TypeScript Type Generator

1.0.15 · active · verified Sun Apr 19

The `microcms-typescript` package is a command-line interface (CLI) tool designed to generate TypeScript type definitions directly from MicroCMS API schema JSON files. Currently stable at version 1.0.15, this utility provides a bridge between your MicroCMS content models and your TypeScript codebase, enabling strong type-safety for your MicroCMS data. Unlike the `microcms-js-sdk` or `microcms-ts-sdk` which are client libraries for interacting with the MicroCMS API, `microcms-typescript` focuses solely on code generation. It processes local JSON files representing MicroCMS API schemas and outputs corresponding TypeScript `type` declarations, including a comprehensive `EndPoints` type that encapsulates `gets`, `get`, `post`, `put`, and `patch` operations, ensuring type-safe access to your content structure. This distinction is crucial as it targets the build-time generation of types rather than runtime API interaction.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install the `microcms-typescript` CLI, create a sample MicroCMS schema JSON file, run the tool to generate `cms-types.ts`, and then import and use the generated `EndPoints` types in a TypeScript application to ensure type safety for your MicroCMS data.

mkdir microcms-schemas
cat > microcms-schemas/api-blogs-schema.json <<EOF
{
  "apiFields": [
    {
      "idValue": "blogTitleField",
      "fieldId": "title",
      "name": "Title",
      "kind": "text",
      "required": true,
      "isUnique": false
    },
    {
      "fieldId": "slug",
      "name": "Slug",
      "kind": "text",
      "required": true,
      "isUnique": true
    },
    {
      "fieldId": "body",
      "name": "Body",
      "kind": "richEditor",
      "required": true
    },
    {
      "fieldId": "category",
      "name": "Category",
      "kind": "relation",
      "required": false
    }
  ],
  "customFields": []
}
EOF

# Install the CLI tool
npm install microcms-typescript --save-dev

# Generate TypeScript types from your schema files
npx microcms-typescript microcms-schemas cms-types.ts

# Example TypeScript usage (e.g., in src/types.ts or similar)
// Make sure your tsconfig.json includes 'cms-types.ts'
/*
// tsconfig.json example:
// {
//   "compilerOptions": {
//     "target": "ES2020",
//     "module": "ESNext",
//     "moduleResolution": "Bundler",
//     "strict": true,
//     "esModuleInterop": true,
//     "skipLibCheck": true,
//     "forceConsistentCasingInFileNames": true,
//     "types": ["node"],
//     "outDir": "./dist",
//     "rootDir": ".",
//     "paths": { "@/*": ["./src/*"] }
//   },
//   "include": ["./src/**/*.ts", "./src/**/*.d.ts", "cms-types.ts"]
// }
*/

import type { EndPoints } from './cms-types';

// Type for a list of blog posts
type BlogListResponse = EndPoints['gets']['blogs'];

// Type for a single blog post's detail
type BlogDetail = EndPoints['get']['blogs'];

// Type for creating a new blog post (POST payload)
type BlogCreatePayload = EndPoints['post']['blogs'];

// Example usage with dummy data
const fetchedBlogs: BlogListResponse = {
  contents: [
    {
      id: 'blog123',
      title: 'My First Blog Post',
      slug: 'my-first-blog-post',
      body: '<p>Content here</p>',
      createdAt: '2023-01-01T00:00:00.000Z',
      updatedAt: '2023-01-01T00:00:00.000Z',
      publishedAt: '2023-01-01T00:00:00.000Z',
      revisedAt: '2023-01-01T00:00:00.000Z',
      category: 'cat456'
    }
  ],
  totalCount: 1,
  limit: 10,
  offset: 0
};

console.log(fetchedBlogs.contents[0].title); // Accessing type-safe data

view raw JSON →