{"id":14991010,"url":"https://github.com/intercom/contentful-typescript-codegen","last_synced_at":"2025-05-16T02:09:11.513Z","repository":{"id":37927694,"uuid":"174304742","full_name":"intercom/contentful-typescript-codegen","owner":"intercom","description":"Generate TypeScript interfaces from a Contentful environment","archived":false,"fork":false,"pushed_at":"2024-09-26T22:17:07.000Z","size":1163,"stargazers_count":281,"open_issues_count":45,"forks_count":54,"subscribers_count":81,"default_branch":"master","last_synced_at":"2024-10-29T17:31:39.886Z","etag":null,"topics":["codegen","contentful","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/intercom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-07T08:37:58.000Z","updated_at":"2024-09-29T10:42:25.000Z","dependencies_parsed_at":"2024-01-10T05:49:15.351Z","dependency_job_id":"95267a13-b251-43fb-9fb1-dcff3cae074e","html_url":"https://github.com/intercom/contentful-typescript-codegen","commit_stats":{"total_commits":65,"total_committers":16,"mean_commits":4.0625,"dds":0.6,"last_synced_commit":"e212b2bd2560e66c2d8948f5eee1df632d3527de"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fcontentful-typescript-codegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fcontentful-typescript-codegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fcontentful-typescript-codegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intercom%2Fcontentful-typescript-codegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/intercom","download_url":"https://codeload.github.com/intercom/contentful-typescript-codegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247492513,"owners_count":20947544,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","contentful","typescript"],"created_at":"2024-09-24T14:21:17.440Z","updated_at":"2025-04-06T14:09:51.506Z","avatar_url":"https://github.com/intercom.png","language":"TypeScript","readme":"# contentful-typescript-codegen\n\nGenerate typings from your Contentful environment.\n\n- Content Types become interfaces.\n- Locales (and your default locale) become string types.\n- Assets and Rich Text link to Contentful's types.\n\nAt Intercom, we use this in our [website] to increase developer confidence and productivity,\nensure that breaking changes to our Content Types don't cause an outage, and because it's neat.\n\n[website]: https://www.intercom.com\n\n## Usage\n\n```sh\nyarn add --dev contentful-typescript-codegen\n```\n\nThen, add the following to your `package.json`:\n\n```jsonc\n{\n  // ...\n  \"scripts\": {\n    \"contentful-typescript-codegen\": \"contentful-typescript-codegen --output @types/generated/contentful.d.ts\"\n  }\n}\n```\n\nFeel free to change the output path to whatever you like.\n\nNext, the codegen will expect you to have created a file called either `getContentfulEnvironment.js` or `getContentfulEnvironment.ts`\nin the root of your project directory, which should export a promise that resolves with your Contentful environment.\n\nThe reason for this is that you can do whatever you like to set up your Contentful Management\nClient. Here's an example of a JavaScript config:\n\n```js\nconst contentfulManagement = require(\"contentful-management\")\n\nmodule.exports = function() {\n  const contentfulClient = contentfulManagement.createClient({\n    accessToken: process.env.CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,\n  })\n\n  return contentfulClient\n    .getSpace(process.env.CONTENTFUL_SPACE_ID)\n    .then(space =\u003e space.getEnvironment(process.env.CONTENTFUL_ENVIRONMENT))\n}\n```\n\nAnd the same example in TypeScript:\n\n```ts\nimport { strict as assert } from \"assert\"\nimport contentfulManagement from \"contentful-management\"\nimport { EnvironmentGetter } from \"contentful-typescript-codegen\"\n\nconst { CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN, CONTENTFUL_SPACE_ID, CONTENTFUL_ENVIRONMENT } = process.env\n\nassert(CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN)\nassert(CONTENTFUL_SPACE_ID)\nassert(CONTENTFUL_ENVIRONMENT)\n\nconst getContentfulEnvironment: EnvironmentGetter = () =\u003e {\n  const contentfulClient = contentfulManagement.createClient({\n    accessToken: CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,\n  })\n\n  return contentfulClient\n    .getSpace(CONTENTFUL_SPACE_ID)\n    .then(space =\u003e space.getEnvironment(CONTENTFUL_ENVIRONMENT))\n}\n\nmodule.exports = getContentfulEnvironment\n```\n\n\u003e **Note**\n\u003e\n\u003e `ts-node` must be installed to use a TypeScript config\n\n### Command line options\n\n```\nUsage\n  $ contentful-typescript-codegen --output \u003cfile\u003e \u003coptions\u003e\n\nOptions\n  --output,      -o  Where to write to\n  --poll,        -p  Continuously refresh types\n  --interval N,  -i  The interval in seconds at which to poll (defaults to 15)\n```\n\n## Example output\n\nHere's an idea of what the output will look like for a Content Type:\n\n```ts\ninterface IBlogPostFields {\n  /** Title */\n  title: string\n\n  /** Body */\n  body: Document\n\n  /** Author link */\n  author: IAuthor\n\n  /** Image */\n  image: Asset\n\n  /** Published? */\n  published: boolean | null\n\n  /** Tags */\n  tags: string[]\n\n  /** Blog CTA variant */\n  ctaVariant: \"new-cta\" | \"old-cta\"\n}\n\n/**\n * A blog post.\n */\nexport interface IBlogPost extends Entry\u003cIBlogPostFields\u003e {}\n```\n\nYou can see that a few things are handled for you:\n\n- Documentation comments are automatically generated from Contentful descriptions.\n- Links, like `author`, are resolved to other TypeScript interfaces.\n- Assets are handled properly.\n- Validations on symbols and text fields are expanded to unions.\n- Non-required attributes automatically have `| null` appended to their type.\n- The output is formatted using **your** Prettier config.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintercom%2Fcontentful-typescript-codegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintercom%2Fcontentful-typescript-codegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintercom%2Fcontentful-typescript-codegen/lists"}