Skip to content

Commit 4a7d634

Browse files
committed
feat: add options from openapi-typescript-codegen
1 parent cb4c150 commit 4a7d634

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

src/cli.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,36 @@
22
import { generate } from "./generate";
33
import { Command } from "commander";
44
import packageJson from "../package.json";
5+
import { Options } from "openapi-typescript-codegen";
56

67
export type CLIOptions = {
7-
outputDir: string;
8-
path: string;
9-
};
8+
output?: string;
9+
client?: Options["httpClient"];
10+
} & Pick<Options, 'exportSchemas' | 'postfix' | 'request' | 'indent' | 'input'>;
1011

1112
const program = new Command();
1213

1314
program
14-
.name('openapi-rq')
15+
.name("openapi-rq")
1516
.version(packageJson.version)
1617
.description("Generate React Query code based on OpenAPI")
17-
.requiredOption("-p, --path <path>", "Path to OpenAPI file")
18+
.requiredOption(
19+
"-i, --input <value>",
20+
"OpenAPI specification, can be a path, url or string content (required)"
21+
)
22+
.option("-o, --output <value>", "Output directory", "openapi")
1823
.option(
19-
"-o, --output-dir [directory]",
20-
"Directory to output the generated package",
21-
"openapi"
24+
"-c, --client <value>",
25+
"HTTP client to generate [fetch, xhr, node, axios, angular]",
26+
"fetch"
2227
)
28+
.option("--exportSchemas <value>", "Write schemas to disk", false)
29+
.option("--indent <value>", "Indentation options [4, 2, tabs]", "4")
30+
.option("--postfix <value>", "Service name postfix", "Service")
31+
.option("--request <value>", "Path to custom request file")
2332
.parse();
2433

2534
const options = program.opts<CLIOptions>();
2635

27-
console.log(`Generating React Query code using OpenApi file ${options.path}`);
36+
console.log(`Generating React Query code using OpenApi file ${options.output}`);
2837
generate(options);

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const defaultOutputPath = "openapi";
2+
export const queriesOutputPath = "queries";
3+
export const requestsOutputPath = "requests";

src/generate.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { print } from "./print";
33
import { CLIOptions } from "./cli";
44
import path from "path";
55
import { createSource } from "./createSource";
6+
import { defaultOutputPath, requestsOutputPath } from "./constants";
67

78
export async function generate(options: CLIOptions) {
8-
const openApiOutputPath = path.join(options.outputDir, "requests");
9+
const openApiOutputPath = path.join(
10+
options.output ?? defaultOutputPath,
11+
requestsOutputPath
12+
);
13+
914
await generateTSClients({
10-
input: options.path,
15+
...options,
16+
httpClient: options.client,
1117
output: openApiOutputPath,
1218
});
1319
const source = createSource(openApiOutputPath);

src/print.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import fs from "fs";
22
import path from "path";
33
import { CLIOptions } from "./cli";
4+
import { defaultOutputPath, queriesOutputPath } from "./constants";
45

56
function printGeneratedTS(result: string, options: CLIOptions) {
6-
const dir = path.join(options.outputDir, "queries")
7-
if (!fs.existsSync(dir)){
7+
const dir = path.join(options.output ?? defaultOutputPath, queriesOutputPath);
8+
if (!fs.existsSync(dir)) {
89
fs.mkdirSync(dir, { recursive: true });
9-
}
10-
fs.writeFileSync(path.join(dir, 'index.ts'), result);
10+
}
11+
fs.writeFileSync(path.join(dir, "index.ts"), result);
1112
}
1213

1314
export function print(result: string, options: CLIOptions) {
14-
if (!fs.existsSync(options.outputDir)) {
15-
fs.mkdirSync(options.outputDir);
15+
const outputPath = options.output ?? defaultOutputPath;
16+
if (!fs.existsSync(outputPath)) {
17+
fs.mkdirSync(outputPath);
1618
}
1719

1820
printGeneratedTS(result, options);

0 commit comments

Comments
 (0)