Giter VIP home page Giter VIP logo

content-type-to-typescript's Introduction

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 --access-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 --access-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.

content-type-to-typescript's People

Contributors

jarkkosyrjala avatar rerakers avatar sbezludny avatar

Watchers

 avatar  avatar

Forkers

halkeye

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.