acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.22k stars 353 forks source link

How to include/exclude endpoints? #836

Open n1kk opened 1 month ago

n1kk commented 1 month ago

Hi, I'm consuming a gigantic API, with hundreds of endpoints, but I only really need one or two. Even generating modular output and splitting endpoint into files by first tag generates quite huge classes.

Is there a way to filter what endpoints end up being generated into the http client class?

grushikhin commented 4 weeks ago

Hi! I had a similar issue, and I resolved it by using '@apidevtools/swagger-parser'. I split the OpenAPI file into smaller pieces before passing them to the generator.

code example:

export async function splitOpenAPIFile(inputFilePath: string, outputDir: string): Promise<void> {
    const api = await SwaggerParser.parse(inputFilePath) as OpenAPIV3.Document;

    const resources: { [key: string]: OpenAPIV3.Document } = {};

    for (const [path, methods] of Object.entries(api.paths)) {
        const resourceName = path.split('/')[1];

        if (!resources[resourceName]) {
            resources[resourceName] = {
                ...api,
                paths: {}
            };
        }

        resources[resourceName].paths[path] = methods;
    }

    await ensureDir(outputDir);

    for (const [resourceName, resourceApi] of Object.entries(resources)) {
        const resourceFilePath = path.join(outputDir, `${resourceName}.json`);
        await writeJson(resourceFilePath, resourceApi, { spaces: 2 });
    }
}