@anttiviljami/dtsgenerator

中文文档
3.20.0发布于 2024/06/12MITTypeScriptCommonJS近 30 天下载 22.9万

TypeScript d.ts file generator for JSON Schema file

原始使用文档
文档目录22 节
  1. Table of Contents
  2. Install
  3. Usage
  4. CLI
  5. NodeJS API
  6. Use HTTP/HTTPS Proxy
  7. Migration from v2
  8. Plug-in
  9. How to find plug-in
  10. How to create plug-in
  11. Development
  12. Debug
  13. Links about JSON Schema and Swagger
  14. Supported spec and features
  15. ChangeLog
  16. v3.19.1 (2023-08-25)
  17. v3.18.0 (2023-03-02)
  18. v3.17.0 (2023-02-21)
  19. v3.16.2 (2022-12-20)
  20. v3.16.0 (2022-06-10)
  21. older versions history
  22. License

dtsgenerator

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

nodejs version npm version Build Status Coverage Status npm download count Stake to support us MIT license

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

For the configuration file, please refer to the file in the config_sample directory.

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 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

How to create plug-in

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

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

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 (2023-03-02)

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

v3.17.0 (2023-02-21)

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

v3.16.2 (2022-12-20)

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

v3.16.0 (2022-06-10)

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

older versions history

ChangeLogs

License

dtsgenerator is licensed under the MIT license.

Copyright © 2016-2020, Hiroki Horiuchi

版本与发布记录

@anttiviljami/dtsgenerator 共发布 11 个版本,首次发布于 2019/05/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.20.0latest2024/06/122 年前
3.19.32024/01/272 年前
3.19.22024/01/272 年前
3.19.12023/11/092 年前
3.12.22021/11/074 年前
3.12.12021/11/074 年前

关于 @anttiviljami/dtsgenerator 的常见问题

@anttiviljami/dtsgenerator 是什么?

TypeScript d.ts file generator for JSON Schema file

如何安装 @anttiviljami/dtsgenerator?

在项目目录运行 npm install @anttiviljami/dtsgenerator,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 dtsgen,可以用 npx dtsgen 直接运行。

@anttiviljami/dtsgenerator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anttiviljami/dtsgenerator 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anttiviljami/dtsgenerator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@anttiviljami/dtsgenerator 可以商用吗?

@anttiviljami/dtsgenerator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anttiviljami/dtsgenerator 的下载量有多少?

根据 npm 官方下载统计,@anttiviljami/dtsgenerator 在 2026/10/06 – 2026/11/04 这 30 天内被下载 228,501 次,日均约 7,617 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anttiviljami/dtsgenerator 还在维护吗?

最新版本 3.20.0 发布于 2024/06/12(2 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/05/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。