Contentful TypeScript Codegen

3.4.0 · active · verified Sun Apr 19

Contentful TypeScript Codegen is a utility that generates TypeScript type definitions directly from a Contentful environment's content models. It transforms Content Types into interfaces, locales into string union types, and automatically handles links to Contentful Assets and Rich Text fields. The current stable version is 3.4.0, though 3.4.1 includes security fixes, suggesting a maintenance release cadence with feature updates being less frequent. A key differentiator is its ability to automatically generate documentation comments from Contentful descriptions, resolve linked entries to their respective interfaces, and expand Contentful validations into TypeScript union types. It also integrates with Prettier to format generated output according to a project's configuration, ensuring consistency. This tool is primarily a CLI for build-time type generation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up `contentful-typescript-codegen` in a project, including the necessary `package.json` script, the `getContentfulEnvironment.ts` configuration file for fetching your Contentful schema, and how to execute the code generation. It highlights the use of `EnvironmentGetter` and the importance of environment variables.

{
  "name": "my-contentful-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "contentful-codegen": "contentful-typescript-codegen --output @types/generated/contentful.d.ts"
  },
  "devDependencies": {
    "contentful-typescript-codegen": "^3.4.0",
    "contentful-management": "^10.0.0",
    "ts-node": "^10.0.0",
    "typescript": "^5.0.0",
    "prettier": "^2.0.0"
  }
}

// File: getContentfulEnvironment.ts
import { strict as assert } from "assert";
import contentfulManagement from "contentful-management";
import { EnvironmentGetter } from "contentful-typescript-codegen";

const CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN = process.env.CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN ?? '';
const CONTENTFUL_SPACE_ID = process.env.CONTENTFUL_SPACE_ID ?? '';
const CONTENTFUL_ENVIRONMENT = process.env.CONTENTFUL_ENVIRONMENT ?? '';

assert(CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN, "CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN is required.");
assert(CONTENTFUL_SPACE_ID, "CONTENTFUL_SPACE_ID is required.");
assert(CONTENTFUL_ENVIRONMENT, "CONTENTFUL_ENVIRONMENT is required.");

const getContentfulEnvironment: EnvironmentGetter = () => {
  const contentfulClient = contentfulManagement.createClient({
    accessToken: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,
  });

  return contentfulClient
    .getSpace(CONTENTFUL_SPACE_ID)
    .then((space) => space.getEnvironment(CONTENTFUL_ENVIRONMENT));
};

export = getContentfulEnvironment;

// To run: 
// 1. Set environment variables: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN, CONTENTFUL_SPACE_ID, CONTENTFUL_ENVIRONMENT
// 2. yarn install
// 3. yarn contentful-codegen

view raw JSON →