Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dtsgeneratorws

tsapko36282MIT2.5.4TypeScript support: included

TypeScript d.ts file generator for JSON Schema file

TypeScript, JSON Schema, OpenAPI

readme

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 dtsgeneratorws

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

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.5.1 (2020-04-27)

  • fixed:
    • Fix: the bug of name conversion by #402. Thank you @unclechu :+1:
    • Fix: Error when spaces included in a 'name' property under paths/parameters by #407. Thank you @scvnathan :+1:

v2.5.0 (2020-02-28)

  • features:
    • Add to support the text media type on Open API Schema by #396. Thank you @silesky :+1:

v2.4.1 (2020-02-05)

  • fixed
    • Fix: the bug of #386. thank you for reporting issue @Oloompa :+1:

v2.4.0 (2020-02-04)

  • fixed
    • Fix: typescript import statement #381. thank you @lupus92 :+1:
    • Fix: use export when given empty namespace #386. Thank you @zregvart :+1:

older versions history

ChangeLogs

License

dtsgenerator is licensed under the MIT license.

Copyright © 2016-2020, Hiroki Horiuchi

changelog

ChangeLog

Old Versions

v2.3.2 (2019-11-21)

  • fixed
    • Fix: the bug of the specific identifier #375. Thank you for reporting the bug @adriengibrat :+1:

v2.3.1 (2019-09-02)

  • features:
    • Support the dynamic JSON media type #363. Thank you @buelsenfrucht :+1:
    • Add to support the number type enum values #365. Thank you @vincentlin02 :+1:

v2.3.0 (2019-08-23)

  • features:
    • Improve the tuple type output by TypeScript v3 syntax, and Support maxItems property #339. Thank you @keean :+1:

v2.2.0 (2019-08-06)

  • fixed
    • Fix: "_" character at the end of generated type #358. Thank you @KostblLb :+1:

v2.1.0 (2019-07-18)

  • features:
    • Add to support objected example #356. Thank you for reporting the issue #348 by @fantapop :+1:
    • 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 :+1:

v2.0.7 (2019-05-07)

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

v2.0.6 (2019-02-14)

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

v2.0.5 (2019-01-28)

v2.0.4 (2019-01-07)

  • fixed
    • Fix missing schema due to yaml references #318. Thank you @sakari :+1:

v2.0.1 (2018-12-21)

  • fixed
    • A property name cannot contain a "/" #315. Thank you @Reggino :+1:
    • update .npmignore

v2.0.0 (2018-10-22)

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

v1.2.0 (2018-09-12)

  • features
    • Add --namespace <namespace> option to control the emitted namespace. Thank you @DavidBiesack :+1:

v1.1.0 (2018-06-01)

v1.0.0 (2018-03-22)

  • features
    • Support JSON Schema Draft-07 and OpenAPI v3
    • Add the library interface for customize type name
    • Remove some unusual command line options
    • And rebuild the architecture
  • others
    • Remove the node v4 support

v0.9.9 (2018-01-12)

  • Bug fix about previous version code by #267. Thank you @bricka :+1:

v0.9.8 (2018-01-09)

  • Bug fix about a block comment code in example node by #221. Thank you @GongT :+1:

v0.9.7 (2017-12-12)

  • Support the integer enum type by #263. Thank you @heapx :+1:

v0.9.6 (2017-09-28)

  • Bug fix about unresoled $refs by #253. Thank you @skuligowski :+1:

v0.9.5 (2017-07-24)

  • Bug fix about tuple type support by #244. Thank you @laurelnaiad :+1:

v0.9.4 (2017-07-22)

  • Support tuple type of #239, #241. Thank you @laurelnaiad :+1:

v0.9.2 (2017-06-19)

  • Fix the bug about allOf property of #226. Thank you @dawidgarus @philliphoff :+1:

v0.9.1 (2017-01-27)

  • Improve message in error log. Thank you @gasi :+1:

v0.9.0 (2016-12-15)

  • Add yaml format support! Thank you @jdthorpe :+1:

v0.8.2 (2016-11-7)

  • Remove gulp scripts

v0.8.1 (2016-10-24)

v0.8.0 (2016-10-16)

  • Add to support null type for TypeScript 2.0
  • Add some input parameters

v0.7.2 (2016-9-19)

  • Update dependencies library

v0.7.1 (2016-7-5)

  • Fix crash bug #67

v0.7.0 (2016-7-3)

  • Update with some braking change

v0.6.1 (2016-4-28)

  • Minor update from v0.6.0

v0.6.0 (2016-4-14)

  • First stable version of dtsgenerator