hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
646 stars 46 forks source link

Multiple configurations/specifying a configuration file #564

Open slahn opened 1 month ago

slahn commented 1 month ago

Description

Our projects needs clients/models for multiple different OpenAPI specifications.

Is it possible to specify multiple configurations in the config file, or to instruct the CLI to load a specific config file?

If this is already possible, some mention in the documentation would be helpful.

mrlubos commented 1 month ago

Hey, this isn't currently possible, see https://github.com/hey-api/openapi-ts/discussions/444. Are you able to share how you work around this today?

slahn commented 1 month ago

Currently working around this by not using a config file, and having multiple invocations with command line options in package.json.

mrlubos commented 1 month ago

I looked into this and the current blocker is that c12 does not support it. I'll see if I get a reply in the issue and if not, I will fork that package and add support. https://github.com/unjs/c12/issues/92

ecline123 commented 6 days ago

This is the workaround I'm using. Define multiple config values and switch on an env var.

import { defineConfig } from '@hey-api/openapi-ts';

const APIv1 = defineConfig({
  input: 'https://myapi.com/v1/openapi.json',
  output: 'api/v1',
  client: '@hey-api/client-fetch',
});

const APIv2 = defineConfig({
  input: 'https://myapi.com/v2/openapi.json',
  output: 'api/v2',
  client: '@hey-api/client-fetch',
});

let configExport;
switch (process.env.API_SPEC) {
    case "APIv1": {
        configExport = APIv1;
        break;
    }
    case "APIv2": {
        configExport = APIv2;
        break;
    }
    default:
        configExport = APIv1;
}

export default configExport;

Then commands can be run simply with:

API_SPEC=APIv1 npx @hey-api/openapi-ts