0.1.3 • Published 8 years ago

json-schema-typescript v0.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

JSON schema to TypeScript Interfaces

Simple node module to generate Typescript interfaces from JSON Schema files. This package generates Typescript interfaces (.d.ts files) for a set of related JSON schema documents.

NOTE interface files are not generated for schemas which are not of type "object". At present all referenced schemas are only looked up against the glob pattern specified.

Installation

npm install json-schema-typescript

Usage

const jsonToTypescript = require('json-schema-typescript');

jsonToTypescript('path/to/schema/**/*.json', './outputDirectory')
    .then(files => console.log(files.length, 'TypeScript interfaces generated'))
    .catch(ex => console.error(ex));

jsonToTypescript(globPattern, [outputDirectory]) Argument one is a glob pattern to match against to load schemas. Argument two is an optional output directory. Defaults to current directory if not specified.

Returns a promise chain which completes with an array of file paths for the interfaces generated. You should implement a catch function to handle any errors.

Examples

For examples please see tests directory in the source code however an example output is shown below.

import {Name} from './Name';
import {Phone} from './Phone';
import {Address} from './Address';

export interface Customer {
  address?: Address;
  events?: Array<any|string|number>;
  id: string;
  interests?: Array<string>;
  name: Name;
  optIn?: {
    email?: boolean;
    phone?: boolean;
  };
  phone?: Phone;
  salutation?: string;
}

(The above example is generated from this schema)

Issues/Bugs

Please raise issues on github.

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago