typed-openapi
Version:
44 lines (40 loc) • 1.51 kB
JavaScript
import {
allowedRuntimes,
generateFile,
mapOpenApiEndpoints
} from "./chunk-WT2PCM73.js";
// src/cli.ts
import SwaggerParser from "@apidevtools/swagger-parser";
import { cac } from "cac";
import { join } from "pathe";
import { type } from "arktype";
import { writeFile } from "fs/promises";
// package.json
var name = "typed-openapi";
var version = "0.10.1";
// src/cli.ts
var cwd = process.cwd();
var cli = cac(name);
var now = /* @__PURE__ */ new Date();
var optionsSchema = type({ "output?": "string", runtime: allowedRuntimes });
cli.command("<input>", "Generate").option("-o, --output <path>", "Output path for the api client ts file (defaults to `<input>.<runtime>.ts`)").option(
"-r, --runtime <name>",
`Runtime to use for validation; defaults to \`none\`; available: ${allowedRuntimes.definition}`,
{ default: "none" }
).action(async (input, _options) => {
const options = optionsSchema.assert(_options);
const openApiDoc = await SwaggerParser.bundle(input);
const ctx = mapOpenApiEndpoints(openApiDoc);
console.log(`Found ${ctx.endpointList.length} endpoints`);
const content = generateFile({ ...ctx, runtime: options.runtime });
const output = join(
cwd,
options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`
);
console.log("Generating...", output);
await writeFile(output, content);
console.log(`Done in ${(/* @__PURE__ */ new Date()).getTime() - now.getTime()}ms !`);
});
cli.help();
cli.version(version);
cli.parse();