# dtsgenerator

> TypeScript d.ts file generator for JSON Schema file

Latest version **3.19.2** (published 2024-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install dtsgenerator
pnpm add dtsgenerator
yarn add dtsgenerator
bun add dtsgenerator
```

Provides the command `dtsgen`.

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.19.2 |
| Published | 2024-06-11 |
| First published | 2014-10-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 18.0 |
| Dependencies | 9 |
| Unpacked size | 368.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 590 |
| Author | horiuchi |
| Maintainers | horiuchi |
| Keywords | TypeScript, JSON Schema, OpenAPI |

## Links

- npm: https://www.npmjs.com/package/dtsgenerator
- Repository: https://github.com/horiuchi/dtsgenerator
- Issues: https://github.com/horiuchi/dtsgenerator/issues
- npm.io page: https://npm.io/package/dtsgenerator

## Dependencies (9)

- [glob](https://npm.io/package/glob.md) ^10.4.1
- [debug](https://npm.io/package/debug.md) ^4.3.5
- [tslib](https://npm.io/package/tslib.md) ^2.6.3
- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.0
- [commander](https://npm.io/package/commander.md) ^12.1.0
- [typescript](https://npm.io/package/typescript.md) ^5.4.5
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^4.0.0
- [http-proxy-agent](https://npm.io/package/http-proxy-agent.md) ^7.0.2
- [https-proxy-agent](https://npm.io/package/https-proxy-agent.md) ^7.0.4

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.19.2 (latest) — 2024-06-11
- 3.19.1 — 2023-08-25
- 3.19.0 — 2023-08-25
- 3.18.0 — 2023-03-02
- 3.17.0 — 2023-02-21
- 3.16.2 — 2022-12-20
- 3.16.1 — 2022-09-08
- 3.16.0 — 2022-06-09
- 3.15.1 — 2022-03-10
- 3.15.0 — 2022-02-05
- 3.14.0 — 2022-01-27
- 3.13.2 — 2021-10-08
- 3.13.1 — 2021-09-29
- 3.13.0 — 2021-08-31
- 3.12.1 — 2021-05-19
- … 74 more at https://npm.io/package/dtsgenerator/versions

## README

# dtsgenerator

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

[![nodejs version](https://img.shields.io/node/v/dtsgenerator.svg)](#)
[![npm version](https://badge.fury.io/js/dtsgenerator.svg)](https://www.npmjs.com/package/dtsgenerator)
[![Build Status](https://github.com/horiuchi/dtsgenerator/actions/workflows/workflow-ci.yaml/badge.svg)](https://github.com/horiuchi/dtsgenerator/actions/workflows/workflow-ci.yaml)
[![Coverage Status](https://coveralls.io/repos/github/horiuchi/dtsgenerator/badge.svg?branch=master)](https://coveralls.io/github/horiuchi/dtsgenerator?branch=master)
[![npm download count](https://img.shields.io/npm/dt/dtsgenerator.svg)](https://www.npmjs.com/package/dtsgenerator)
[![Stake to support us](https://badge.devprotocol.xyz/0x68c824db5A1634940BB838468Ff2aee2bDa5794B/descriptive)](https://stakes.social/0x68c824db5A1634940BB838468Ff2aee2bDa5794B)
[![MIT license](https://img.shields.io/npm/l/dtsgenerator.svg)](#)

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Migration from v2](#migration-from-v2)
- [Plug-in](#plug-in)
- [Development](#development)
- [ChangeLog](#changelog)
- [License](#license)

## Install

    npm install -g dtsgenerator

- [Releases](https://github.com/horiuchi/dtsgenerator/releases)

## Usage

### CLI

```sh
$ 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

```

For the configuration file, please refer to the file in the [config_sample](https://github.com/horiuchi/dtsgenerator/tree/master/config_sample/) directory.

### NodeJS API

```js
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.

```bash
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 breaking 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

1. Scaffold by the command:
    - `npm init @dtsgenerator **plugin-name**`
1. Edit `**plugin-name**/index.ts`
1. Do test:
    - `npm test`
1. Build it:
    - `npm run build`
1. Publish to npm:
    - `npm publish`

## Development

### Debug

Output debug message by [debug](https://www.npmjs.com/package/debug) library.

    DEBUG=dtsgen dtsgen schema/news.json

### Links about JSON Schema and Swagger

- [The home of JSON Schema](http://json-schema.org/)
- [The OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification)

### 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](https://github.com/horiuchi/dtsgenerator/blob/master/SupportedFeatures.md)

## ChangeLog

### [v3.19.2](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.19.2) (2024-06-11)

- fixed:
  - Support a negative number by #566. Thank you @maximebiloe :+1:

### [v3.19.1](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.19.1) (2023-08-25)

- features:
  - Update plugins for new TypeScript AST.
  - Changed supported Node.js version to 16 or later. Also, we have confirmed that it works with Node.js v20.

### [v3.18.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.18.0) (2023-03-02)

- features:
  - Support 'application/json' with parameter media type for #551. Thank you @denizkenan :+1:

### [v3.17.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.17.0) (2023-02-21)

- features:
  - Return exit code 1 when error occurs by #549. Thank you @lhotamir :+1:

### [v3.16.2](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.16.2) (2022-12-20)

- fixed:
  - Fix to remove deprecated decorators parameters for #547. Thank you @mcollina :+1:

### [v3.16.0](https://github.com/horiuchi/dtsgenerator/releases/tag/v3.16.0) (2022-06-10)

- features:
  - Support 'image/*' media types for #539. Thank you @Geloosa :+1:

### older versions history

[ChangeLogs](https://github.com/horiuchi/dtsgenerator/blob/master/CHANGELOG.md)

## License

`dtsgenerator` is licensed under the MIT license.

Copyright &copy; 2016-2020, [Hiroki Horiuchi](mailto:horiuchi.g@gmail.com)

---
_Source: https://npm.io/package/dtsgenerator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
