@halkeye/content-type-to-typescript
Compile Contentful Models to TypeScript type definitions
Last updated 7 months ago by halkeye .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @halkeye/content-type-to-typescript 
SYNC missed versions from official npm registry.

Content-type-to-typescript

This is a tool to convert Contentful Models (Content Types) to the TS Definitions. Provides a way to automate TS Definitions generation. Could be used as a library or cli tool.

Try-it-out

Installation

$ npm install content-type-to-typescript --save

Usage

  1. As CLI
$ ./node_modules/.bin/content-type-to-typescript --management-token <token> --space <space> --output <filepath>

This command will generate TS Definition file. Could also be used as a npm script.

package.json:

"scripts": {
  "sync-contentful-types": "content-type-to-typescript --management-token <token> --space <space> --output <filepath>"
}

Usage:

npm run sync-contentful-types
  1. As a library using JSON preview from Web App

Copy JSON Preview from Contentful Web App

import { compileFromContentTypes } from 'content-type-to-typescript';

const category = {
    name: 'Category',
    description: null,
    fields: [
      {
        id: 'title',
        name: 'Title',
        type: 'Text',
        required: true,
        omitted: false,
      },
    ],
  }

const typings = await compileFromContentTypes([category]);

console.log(typings);
compileFromContentTypes(
  contentTypes: Array<Partial<ContentType>>,
  options?: Partial<Options>
): Promise<string>

ContentType

The structure of the Content Type is described here Contentful data model.

Options

Property Type Required? Description
bannerComment String A comment at the top of the response

Example

Input:

{
  "name": "Category",
  "fields": [
    {
      "id": "title",
      "name": "Title",
      "type": "Text",
      "required": true,
      "omitted": false
    },
    {
      "id": "icon",
      "name": "Icon",
      "type": "Link",
      "required": false,
      "omitted": false,
      "linkType": "Asset"
    },
    {
      "id": "categoryDescription",
      "name": "Description",
      "type": "Text",
      "required": false,
      "omitted": false
    },
    {
      "id": "simpleTextField",
      "name": "Simple text field",
      "type": "Symbol",
      "required": false,
      "validations": [],
      "disabled": false,
      "omitted": false
    }
  ]
}

Output:

export interface Category {
  title: string;
  icon?: AssetLink;
  categoryDescription?: string;
  simpleTextField?: string;
}

export interface AssetLink {
  type: string;
  linkType: string;
  id: string;
}

Built with

License

MIT License - fork, modify and use however you want.

Current Tags

  • 1.1.1                                ...           latest (7 months ago)

3 Versions

  • 1.1.1                                ...           7 months ago
  • 1.1.0                                ...           7 months ago
  • 1.0.0                                ...           7 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 3
Last Month 3
Dependencies (6)
Dev Dependencies (29)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |