acacode / swagger-typescript-api

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

How to include/exclude endpoints? #836

Open n1kk opened 4 months ago

n1kk commented 4 months 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 3 months 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 });
    }
}
mvshvets commented 2 months 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.

Thank you, friend. This is not what I was looking for, but it became a solution.