Skip to content

Commit 6c37efa

Browse files
committed
feat: add CLI options
Adds CLI options for client inclusion and success codes Introduces flags to control API client generation and customize success status codes via the command line, enhancing flexibility for different use cases.
1 parent 79ffdb2 commit 6c37efa

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/typed-openapi/src/cli.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { cac } from "cac";
2-
32
import { readFileSync } from "fs";
43
import { generateClientFiles } from "./generate-client-files.ts";
54
import { allowedRuntimes } from "./generator.ts";
@@ -11,16 +10,21 @@ cli
1110
.command("<input>", "Generate")
1211
.option("-o, --output <path>", "Output path for the api client ts file (defaults to `<input>.<runtime>.ts`)")
1312
.option(
14-
"-r, --runtime <name>",
13+
"-r, --runtime <n>",
1514
`Runtime to use for validation; defaults to \`none\`; available: ${allowedRuntimes.toString()}`,
1615
{ default: "none" },
1716
)
1817
.option("--schemas-only", "Only generate schemas, skipping client generation (defaults to false)", { default: false })
18+
.option("--include-client", "Include API client types and implementation (defaults to true)", { default: true })
19+
.option(
20+
"--success-status-codes <codes>",
21+
"Comma-separated list of success status codes (defaults to 2xx and 3xx ranges)",
22+
)
1923
.option(
2024
"--tanstack [name]",
2125
"Generate tanstack client, defaults to false, can optionally specify a name for the generated file",
2226
)
23-
.action(async (input, _options) => {
27+
.action(async (input: string, _options: any) => {
2428
return generateClientFiles(input, _options);
2529
});
2630

packages/typed-openapi/src/generate-client-files.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export const optionsSchema = type({
2424
runtime: allowedRuntimes,
2525
tanstack: "boolean | string",
2626
schemasOnly: "boolean",
27+
"includeClient?": "boolean | 'true' | 'false'",
28+
"successStatusCodes?": "string",
2729
});
2830

2931
export async function generateClientFiles(input: string, options: typeof optionsSchema.infer) {
@@ -32,11 +34,22 @@ export async function generateClientFiles(input: string, options: typeof options
3234
const ctx = mapOpenApiEndpoints(openApiDoc);
3335
console.log(`Found ${ctx.endpointList.length} endpoints`);
3436

37+
// Parse success status codes if provided
38+
const successStatusCodes = options.successStatusCodes
39+
? (options.successStatusCodes.split(",").map((code) => parseInt(code.trim(), 10)) as readonly number[])
40+
: undefined;
41+
42+
// Convert string boolean to actual boolean
43+
const includeClient =
44+
options.includeClient === "false" ? false : options.includeClient === "true" ? true : options.includeClient;
45+
3546
const content = await prettify(
3647
generateFile({
3748
...ctx,
3849
runtime: options.runtime,
3950
schemasOnly: options.schemasOnly,
51+
...(includeClient !== undefined && { includeClient }),
52+
...(successStatusCodes !== undefined && { successStatusCodes }),
4053
}),
4154
);
4255
const outputPath = join(

0 commit comments

Comments
 (0)