{"library":"microcms-typescript","title":"MicroCMS TypeScript Type Generator","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install microcms-typescript"],"cli":{"name":"microcms-typescript","version":null}},"imports":["import type { EndPoints } from './cms-types';","let listData: EndPoints['gets']['blogs'];","let detailData: EndPoints['get']['blogs'];"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"mkdir microcms-schemas\ncat > microcms-schemas/api-blogs-schema.json <<EOF\n{\n  \"apiFields\": [\n    {\n      \"idValue\": \"blogTitleField\",\n      \"fieldId\": \"title\",\n      \"name\": \"Title\",\n      \"kind\": \"text\",\n      \"required\": true,\n      \"isUnique\": false\n    },\n    {\n      \"fieldId\": \"slug\",\n      \"name\": \"Slug\",\n      \"kind\": \"text\",\n      \"required\": true,\n      \"isUnique\": true\n    },\n    {\n      \"fieldId\": \"body\",\n      \"name\": \"Body\",\n      \"kind\": \"richEditor\",\n      \"required\": true\n    },\n    {\n      \"fieldId\": \"category\",\n      \"name\": \"Category\",\n      \"kind\": \"relation\",\n      \"required\": false\n    }\n  ],\n  \"customFields\": []\n}\nEOF\n\n# Install the CLI tool\nnpm install microcms-typescript --save-dev\n\n# Generate TypeScript types from your schema files\nnpx microcms-typescript microcms-schemas cms-types.ts\n\n# Example TypeScript usage (e.g., in src/types.ts or similar)\n// Make sure your tsconfig.json includes 'cms-types.ts'\n/*\n// tsconfig.json example:\n// {\n//   \"compilerOptions\": {\n//     \"target\": \"ES2020\",\n//     \"module\": \"ESNext\",\n//     \"moduleResolution\": \"Bundler\",\n//     \"strict\": true,\n//     \"esModuleInterop\": true,\n//     \"skipLibCheck\": true,\n//     \"forceConsistentCasingInFileNames\": true,\n//     \"types\": [\"node\"],\n//     \"outDir\": \"./dist\",\n//     \"rootDir\": \".\",\n//     \"paths\": { \"@/*\": [\"./src/*\"] }\n//   },\n//   \"include\": [\"./src/**/*.ts\", \"./src/**/*.d.ts\", \"cms-types.ts\"]\n// }\n*/\n\nimport type { EndPoints } from './cms-types';\n\n// Type for a list of blog posts\ntype BlogListResponse = EndPoints['gets']['blogs'];\n\n// Type for a single blog post's detail\ntype BlogDetail = EndPoints['get']['blogs'];\n\n// Type for creating a new blog post (POST payload)\ntype BlogCreatePayload = EndPoints['post']['blogs'];\n\n// Example usage with dummy data\nconst fetchedBlogs: BlogListResponse = {\n  contents: [\n    {\n      id: 'blog123',\n      title: 'My First Blog Post',\n      slug: 'my-first-blog-post',\n      body: '<p>Content here</p>',\n      createdAt: '2023-01-01T00:00:00.000Z',\n      updatedAt: '2023-01-01T00:00:00.000Z',\n      publishedAt: '2023-01-01T00:00:00.000Z',\n      revisedAt: '2023-01-01T00:00:00.000Z',\n      category: 'cat456'\n    }\n  ],\n  totalCount: 1,\n  limit: 10,\n  offset: 0\n};\n\nconsole.log(fetchedBlogs.contents[0].title); // Accessing type-safe data","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}