Coder Social home page Coder Social logo

dtsgenerator's Introduction

dtsgenerator

TypeScript d.ts file generator from JSON Schema file or OpenAPI(Swagger) spec file.

nodejs version npm version build status Coverage Status Greenkeeper badge npm download count dtsgenerator Dev Token MIT license

Table of Contents

Install

npm install -g dtsgenerator

Usage

$ dtsgen --help
Usage: script [options] <file ... | file patterns using node-glob>

Options:

  -V, --version                output the version number
  --url <url>                  input json schema from the url. (default: )
  --stdin                      read stdin with other files or urls.
  -o, --out <file>             output d.ts filename.
  -n, --namespace <namespace>  use root namespace instead of definitions or components.schema from OpenAPI, or -n "" to suppress namespaces.
  -h, --help                   output usage information

  Examples:

    $ dtsgen --help
    $ dtsgen --out types.d.ts schema/**/*.schema.json
    $ cat schema1.json | dtsgen
    $ dtsgen -o swaggerSchema.d.ts --url https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v2.0/schema.json
    $ dtsgen -o petstore.d.ts -n PetStore --url https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/yaml/petstore.yaml

Advance Usage

For customize the output type name.

Install for library

npm install -S dtsgenerator

Usage for library

For example, it want to add the I prefix to the interface name. This is not usual example...

https://github.com/horiuchi/dtsgenerator/blob/master/example/add-prefix/index.ts

import dtsGenerator, { DefaultTypeNameConvertor, SchemaId } from 'dtsgenerator';
import * as fs from 'fs';

const filePath = '../../test/snapshots/json-schema-draft-04/schema/schema.json';

function typeNameConvertor(id: SchemaId): string[] {
    const names = DefaultTypeNameConvertor(id);
    if (names.length > 0) {
        const lastIndex = names.length - 1;
        names[lastIndex] = 'I' + names[lastIndex];
    }
    return names;
}

async function main(): Promise<void> {
    const content = JSON.parse( fs.readFileSync(filePath, 'utf-8') );
    const result = await dtsGenerator({
        contents: [content],
        typeNameConvertor,
    });
    console.log(result);
}
main();

Development

Debug

Output debug message by debug library.

$ DEBUG=dtsgen dtsgen schema/news.json

Links about JSON Schema and Swagger

Supported spec and features

  • JSON Schema

    • Draft-04 and before
    • Draft-07 and before
  • OpenAPI

    • OpenAPI Specification version 2.0
    • OpenAPI Specification version 3.0
  • supported features in these spec

ChangeLog

v2.2.0 (2019-08-06)

  • fixed
    • Fix: "_" character at the end of generated type #358. Thank you @KostblLb ๐Ÿ‘

v2.1.0 (2019-07-18)

  • features:
    • Add to support objected example #356. Thank you for reporting the issue #348 by @fantapop ๐Ÿ‘
    • Add to support the application/x-www-form-urlencoded branch on OpenAPI. #357

v2.0.8 (2019-06-25)

  • fixed
    • Fix: OneOf + type "object" produces empty interface #351. Thank you @polomani ๐Ÿ‘

v2.0.7 (2019-05-07)

  • fixed
    • Fix: typename when the path is nested #334. Thank you @steelydylan ๐Ÿ‘
    • Fix: Support ref objects for OAS3 requestBodies + responses #343. Thank you @anttiviljami ๐Ÿ‘

v2.0.6 (2019-02-14)

  • fixed
    • Fix "Error: The $ref target is not exists" on valid OpenAPI 3 spec #322. Thank you @f1cognite ๐Ÿ‘

v2.0.5 (2019-01-28)

v2.0.4 (2019-01-07)

  • fixed
    • Fix missing schema due to yaml references #318. Thank you @sakari ๐Ÿ‘

v2.0.1 (2018-12-21)

  • fixed
    • A property name cannot contain a "/" #315. Thank you @Reggino ๐Ÿ‘
    • update .npmignore

v2.0.0 (2018-10-22)

  • features
    • Add to support emit the Request/Response object schema of OpenAPI by #304.

older versions history

ChangeLogs

License

dtsgenerator is licensed under the MIT license.

Copyright ยฉ 2016-2019, Hiroki Horiuchi

dtsgenerator's People

Contributors

anttiviljami avatar davidbiesack avatar dmiorandi avatar gasi avatar gongt avatar greenkeeper[bot] avatar greenkeeperio-bot avatar heapx avatar horiuchi avatar jdthorpe avatar laurelnaiad avatar philliphoff avatar polomani avatar qwerty2501 avatar sakari avatar skuligowski avatar steelydylan 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.