Skip to content

Commit ceb15f6

Browse files
committed
chore: export generateClientFiles fn (same used as in the CLI)
1 parent c6e805c commit ceb15f6

File tree

4 files changed

+56
-40
lines changed

4 files changed

+56
-40
lines changed

.changeset/smart-corners-make.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
Export generateClientFiles fn (same used as in the CLI)

packages/typed-openapi/src/cli.ts

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
import SwaggerParser from "@apidevtools/swagger-parser";
21
import { cac } from "cac";
3-
import type { OpenAPIObject } from "openapi3-ts/oas31";
4-
import { basename, join } from "pathe";
5-
import { type } from "arktype";
62

7-
import { writeFile } from "fs/promises";
8-
import { allowedRuntimes, generateFile } from "./generator.ts";
9-
import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
10-
import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
113
import { readFileSync } from "fs";
12-
import { prettify } from "./format.ts";
13-
import { dirname } from "path";
4+
import { generateClientFiles } from "./generate-client-files.ts";
5+
import { allowedRuntimes } from "./generator.ts";
146

157
const { name, version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
16-
const cwd = process.cwd();
178
const cli = cac(name);
18-
const now = new Date();
19-
20-
const optionsSchema = type({ "output?": "string", runtime: allowedRuntimes, tanstack: "boolean | string" });
219

2210
cli
2311
.command("<input>", "Generate")
@@ -29,32 +17,7 @@ cli
2917
)
3018
.option("--tanstack [name]", "Generate tanstack client, defaults to false, can optionally specify a name for the generated file")
3119
.action(async (input, _options) => {
32-
const options = optionsSchema.assert(_options);
33-
const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject;
34-
35-
const ctx = mapOpenApiEndpoints(openApiDoc);
36-
console.log(`Found ${ctx.endpointList.length} endpoints`);
37-
38-
const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
39-
const outputPath = join(
40-
cwd,
41-
options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`,
42-
);
43-
44-
console.log("Generating client...", outputPath);
45-
await writeFile(outputPath, content);
46-
47-
if (options.tanstack) {
48-
const tanstackContent = await generateTanstackQueryFile({
49-
...ctx,
50-
relativeApiClientPath: './' + basename(outputPath),
51-
});
52-
const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
53-
console.log("Generating tanstack client...", tanstackOutputPath);
54-
await writeFile(tanstackOutputPath, tanstackContent);
55-
}
56-
57-
console.log(`Done in ${new Date().getTime() - now.getTime()}ms !`);
20+
return generateClientFiles(input, _options);
5821
});
5922

6023
cli.help();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import SwaggerParser from "@apidevtools/swagger-parser";
2+
import type { OpenAPIObject } from "openapi3-ts/oas31";
3+
import { basename, join, dirname } from "pathe";
4+
import { type } from "arktype";
5+
import { writeFile } from "fs/promises";
6+
import { allowedRuntimes, generateFile } from "./generator.ts";
7+
import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
8+
import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
9+
import { prettify } from "./format.ts";
10+
11+
const cwd = process.cwd();
12+
const now = new Date();
13+
14+
export const optionsSchema = type({
15+
"output?": "string",
16+
runtime: allowedRuntimes,
17+
tanstack: "boolean | string"
18+
});
19+
20+
export async function generateClientFiles(input: string, options: typeof optionsSchema.infer) {
21+
const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject;
22+
23+
const ctx = mapOpenApiEndpoints(openApiDoc);
24+
console.log(`Found ${ctx.endpointList.length} endpoints`);
25+
26+
const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
27+
const outputPath = join(
28+
cwd,
29+
options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`,
30+
);
31+
32+
console.log("Generating client...", outputPath);
33+
await writeFile(outputPath, content);
34+
35+
if (options.tanstack) {
36+
const tanstackContent = await generateTanstackQueryFile({
37+
...ctx,
38+
relativeApiClientPath: './' + basename(outputPath),
39+
});
40+
const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
41+
console.log("Generating tanstack client...", tanstackOutputPath);
42+
await writeFile(tanstackOutputPath, tanstackContent);
43+
}
44+
45+
console.log(`Done in ${new Date().getTime() - now.getTime()}ms !`);
46+
}

packages/typed-openapi/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export * from "./box-factory.ts";
2+
export { prettify } from "./format.ts";
23
export { generateFile, type OutputRuntime } from "./generator.ts";
4+
export { generateClientFiles } from "./generate-client-files.ts"
35
export * from "./tanstack-query.generator.ts"
46
export * from "./map-openapi-endpoints.ts";
57
export * from "./openapi-schema-to-ts.ts";

0 commit comments

Comments
 (0)