Giter Club home page Giter Club logo

netlify-ts's Introduction

Netlify Type Generator

Build Status Supported Node versions Code coverage Project license

Turn your Netlify CMS collections into TypeScript typings!


This package generates a TypeScript schema of your Netlify CMS content collections to be consumed by frontend apps for better type support.

Features

  • Primitive types, e.g. string, number and boolean
  • Extract object widgets into own type interfaces
  • Single and nested lists
  • Multi-select and single option values
  • Optional fields
  • Unknown widgets resolve to any type
  • Relation fields

Installation

The package can be installed globally or as a devDependency using NPM or Yarn.

NPM:

npm install -g netlify-ts

# or

npm install -D netlify-ts

Yarn:

yarn global add netlify-ts

# or

yarn add -D netlify-ts

Usage

Method 1: CLI

The main method of usage is through the command-line. Having installed the package either globally or in project's devDependencies, simply call netlify-ts with a parameter pointing to your Netlify CMS admin config.yml file.

netlify-ts public/admin/config.yml

This generates by default a netlify-types.ts file in the project root containing types for your netlify content types.

Custom output location

You can also specify a custom output location by providing a second optional parameter. Omitting the filename outputs a file in the given directory with the default filename (netlify-types.ts).

netlify-ts public/admin/config.yml src/my-types.ts

Method 2: Programmatically

In case the CLI doesn't suit your project workflow or you need to invoke the type generation inside your code, the project exposes both a createNetlifyTypes and createNetlifyTypesAsync function that returns the generated type file as a string.

Config file

const fs = require("fs");
const { createNetlifyTypes } = require("netlify-ts");

function main() {
  const types = createNetlifyTypes("public/admin/config.yml");
  fs.writeFileSync("my-types.ts", types);
}

main();

Config object

const fs = require("fs");
const { createNetlifyTypes } = require("netlify-ts");

const cmsConfig = require("./config");

function main() {
  const types = createNetlifyTypes(cmsConfig);
  fs.writeFileSync("my-types.ts", types);
}

main();

License

MIT

netlify-ts's People

Contributors

gewfy avatar jsaari97 avatar

Watchers

 avatar

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.