TypeScript d.ts file generator from JSON Schema file or OpenAPI(Swagger) spec file.
npm install -g dtsgenerator
$ 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
For customize the output type name.
npm install -S dtsgenerator
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();
Output debug message by debug library.
$ DEBUG=dtsgen dtsgen schema/news.json
-
JSON Schema
- Draft-04 and before
- Draft-07 and before
-
OpenAPI
- OpenAPI Specification version 2.0
- OpenAPI Specification version 3.0
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)
- features
- Add to support the
nullable
property on OpenAPI by #319. Thank you @sakari 👍 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types
- Add to support the
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.
v1.2.0 (2018-09-12)
- features
- Add
--namespace <namespace>
option to control the emitted namespace. Thank you @DavidBiesack 👍
- Add
v1.1.0 (2018-06-01)
- features
- Add the supported features document
- Support some properties
title
,const
,readOnly
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
dtsgenerator
is licensed under the MIT license.
Copyright © 2016-2018, Hiroki Horiuchi