Open TianyuKang opened 9 months ago
@TianyuKang Hi! If you are satisfied with having several clients, I can share my solution. We have several schemes and we perform generation for each of them.
1) We create builder.ts
:
import * as fs from "fs";
import * as path from "path";
import * as process from "process";
import { load } from "js-yaml";
import { generateApi } from "swagger-typescript-api";
const SOURCE_TYPES: Readonly<string[]> = [".json", ".yaml"];
const SOURCE_DIR = "./lib/swagger/source"; // our swaggers
const OUTPUT_DIR = "./lib/swagger/dist"; // result of parse
const source = fs
.readdirSync(path.resolve(process.cwd(), SOURCE_DIR))
.filter((file) => SOURCE_TYPES.includes(path.extname(file)));
const getFilePath = (filename: string): string =>
path.resolve(process.cwd(), SOURCE_DIR, filename);
const getName = async (filename: string) => {
// some our logic to parse output name
};
const run = async (filename: string) => {
const parsedName = await getName(filename);
generateApi({
name: "index.ts",
input: getFilePath(filename),
output: path.resolve(process.cwd(), OUTPUT_DIR, parsedName),
generateRouteTypes: true,
extractEnums: true,
extractRequestBody: true,
extractResponseBody: true,
extractRequestParams: true,
extractNestedObjects: true, // custom option in our fork
cleanOutput: true,
httpClientType: "axios",
});
};
source.forEach(run);
2) We add script inside package.json "swagger:gen": "node --loader ts-node/esm ./lib/swagger/builder.ts"
3) Now we have one script to generate all clients
It could potentially be improved so that the library accepts several sources at the same time, but there are some nuances there. You can solve the problem yourself by writing an adapter that will combine multiple schema files. Essentially, needs to be combined paths
and components
in source files, but for me this is a dangerous idea
It would be nice if we can just call npx swagger-typescript-api -p ./input/*.yml -o ./output
.
Hi bro, can i generate Api.ts with multiple swagger.json?
Thank you for your consideration and I will be looking forward to your reply.