dtsgenerator
TypeScript d.ts file generator from JSON Schema file or OpenAPI(Swagger) spec file.Table of Contents
Install
npm install -g dtsgenerator
Usage
CLI
$ dtsgen --help
Usage: dtsgenerator [options] <file ... | file patterns using node-glob>
Options:
-V, --version output the version number
-c, --config <file> set configuration file path.
--url <url> input json schema from the url. (default: [])
--stdin read stdin with other files or urls.
-o, --out <file> output filename.
-t, --target <version> Specify ECMAScript target version: 'ES3', 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018',
'ES2019', 'ES2020', or 'ESNEXT' (default).
--info for developer mode. output loaded config and plugin details only.
--output-ast output TypeScript AST instead of d.ts file.
-h, --help display help for command
Examples:
$ dtsgen --help
$ dtsgen --out types.d.ts schema/**/*.schema.json
$ cat schema1.json | dtsgen -c dtsgen.json
$ dtsgen -o swaggerSchema.d.ts --url https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v2.0/schema.json
$ dtsgen -o petstore.d.ts --url https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/yaml/petstore.yaml
$ dtsgen -c dtsgen-test.json --info
NodeJS API
const { default: dtsgenerator, parseSchema } = require('dtsgenerator');
dtsgenerator({
contents: [parseSchema({/* JsonSchema object */})],
config: {/* Config object */},
}).then(content => {
/* Do someting with parsed content */
}).catch(err => {
/* Handle errors */
});
Use HTTP/HTTPS Proxy
If you need a proxy to fetch the schema, please set the following environment variables.export http_proxy=http://proxy.example.com:8080/
export https_proxy=http://proxy.example.com:8080/
# If there are exceptionally hosts that do not go through a proxy
export no_proxy=google.com, yahoo.com
Migration from v2
The dtsgenerator v3 has made the following braking changes from v2.- Support Plug-in feature. See the Plug-in section for more information.
- Change the command line options.
- Remove the `--namespace` option. Use the `@dtsgenerator/replace-namespace` plug-in instead.
- Add the `--config` option. Mainly for setting up the Plug-in.
- And add more options.
- TypeScript AST is now used internally to generate type definitions.
Plug-in
How to find plug-in
- Search by npm: https://www.npmjs.com/search?q=dtsgenerator%20plugin
- Find by the @dtsgenerator repositories: https://github.com/dtsgenerator
- `@dtsgenerator/replace-namespace` : This plug-in is instead the `--namespace` option on old version.
- `@dtsgenerator/decorate-typename` : This plug-in can decorate the output type name.
- `@dtsgenerator/single-quote` : This plug-in replace the quote mark to single.
How to create plug-in
- Scaffold by the command:
- `npm init @dtsgenerator **plugin-name**`
- Edit
**plugin-name**/index.ts
- Do test:
- `npm test`
- Build it:
- `npm run build`
- Publish to npm:
- `npm publish`
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
- OpenAPI
ChangeLog
v3.12.1 (2021-05-19)
- fixed:
v3.12.0 (2021-05-13)
- features:
file
by #496. Thank you @Christian24 :+1:
- Improve the conversion results of parameters property on OpenAPI by #501.v3.11.0 (2021-05-11)
- features:
- fixed:
readOnly
property, because of wrong interpretation by #498. Thank you @hallsbyra :+1:v3.10.0 (2021-04-26)
- features:
unknown
keyword by #488. Thank you @medfreeman :+1:
- Support the content
property in Parameters
by #472. Thank you for report @npdev453 :+1:v3.9.2 (2021-04-19)
- fixed:
oneOf
& anyOf
keywords by #486. Thank you @medfreeman :+1:v3.9.1 (2021-04-16)
- fixed:
generate
function to use recommended immutable approach for typescript transforms by #483. Thank you again @medfreeman :+1:v3.9.0 (2021-04-13)
- features:
v3.8.0 (2021-04-08)
- features:
v3.7.1 (2021-02-18)
- fixed:
mergeSchema
by #474. Thank you @ricokahler :+1:v3.7.0 (2021-01-05)
- features:
void
type support by #468. Thank you for your propose @henhal by #445 :+1:v3.6.0 (2020-12-25)
- features:
v3.5.0 (2020-12-21)
- features:
ts
object for to use the same version TypeScript in all plugins by #465.v3.4.1 (2020-12-16)
- fixed:
v3.4.0 (2020-12-15)
- features:
v3.3.1 (2020-10-05)
- fixed:
v3.3.0 (2020-07-29)
- features:
patternProperties
by #436. Thank you @nfroidure :+1:- fixed:
prettier
config on .eslintrc.json
.v3.2.0 (2020-07-20)
- features:
v3.1.1 (2020-06-23)
- fixed:
v3.1.0 (2020-06-22)
- features:
v3.0.3 (2020-06-15)
- fixed:
v3.0.2 (2020-06-11)
- fixed:
v3.0.1 (2020-06-09)
- features:
older versions history
ChangeLogsLicense
dtsgenerator
is licensed under the MIT license.Copyright © 2016-2020, Hiroki Horiuchi