@spec2ts/openapi
Version:
Utility to convert OpenAPI v3 specifications to Typescript using TypeScript native compiler
73 lines (71 loc) • 2.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.describe = exports.usage = void 0;
const core_1 = require("@spec2ts/core");
const openapi_parser_1 = require("../lib/openapi-parser");
exports.usage = "$0 <input..>";
exports.describe = "Generate TypeScript types from OpenAPI specification";
function builder(argv) {
return argv
.positional("input", {
array: true,
type: "string",
describe: "Path to OpenAPI Specification(s) to convert to TypeScript",
demandOption: true
})
.option("output", {
type: "string",
alias: "o",
describe: "Output directory for generated types"
})
.option("ext", {
type: "string",
alias: "e",
describe: "Output extension for generated types",
choices: [".d.ts", ".ts"]
})
.option("cwd", {
type: "string",
alias: "c",
describe: "Root directory for resolving $refs"
})
.option("avoidAny", {
type: "boolean",
describe: "Avoid the `any` type and use `unknown` instead"
})
.option("enableDate", {
choices: [true, "strict", "lax"],
describe: "Build `Date` for format `date` and `date-time`"
})
.option("lowerHeaders", {
type: "boolean",
describe: "Lowercase headers keys to match Node.js standard"
})
.option("banner", {
type: "string",
alias: "b",
describe: "Comment prepended to the top of each generated file"
});
}
exports.builder = builder;
async function handler(options) {
const files = await core_1.cli.findFiles(options.input);
for (const file of files) {
const ast = await (0, openapi_parser_1.parseOpenApiFile)(file, options);
const content = core_1.printer.printNodes(ast.all);
const output = core_1.cli.getOutputPath(file, options);
await core_1.cli.mkdirp(output);
await core_1.cli.writeFile(output, (options.banner || defaultBanner()) +
"\n\n" +
content);
}
}
exports.handler = handler;
function defaultBanner() {
return `/**
* DO NOT MODIFY
* Generated using @spec2ts/openapi.
* See https://www.npmjs.com/package/@spec2ts/openapi
*/
/* eslint-disable */`;
}