hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.33k stars 104 forks source link

Generating code based on multiple OpenAPI schemas #1056

Open frankPairs opened 1 month ago

frankPairs commented 1 month ago

Description

Hey everyone!

I am facing a challenge using the library. On the project I am working on, we have several microservices, each of them with its own OpenAPI schema, which means that I have to deal with different .yaml files.

I am not sure if this is possible, but is there any way to declare several inputs instead of just one? kind of:

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

export default defineConfig({
  client: '@hey-api/client-axios',
  input: {
     "service1": '@generated/openapi-schemas/service1/service1.yaml',
     "service2": '@generated/openapi-schemas/service2/service2.yaml',
     ...
  },
  output: {
    format: 'prettier',
    lint: 'eslint',
    path: '@generated/api',
  },
})

I was checking the ClientConfig documentation, and it looks like the input property can be an object, but I am doing something wrong because it's not working as I expected.

I would appreciate any guidance.

Thanks a lot in advance!

mrlubos commented 1 month ago

Hey @frankPairs, it can be an object, but it does not support what you're describing. This should be easy to add though. What's up with the "service1" naming? What's the expected output?

frankPairs commented 1 month ago

My first intuition is that the files generated from @generated/openapi-schemas/service1/service1.yaml would be within the output folder using the same name. Just following the example describe above, it would look like:

Screenshot 2024-09-18 at 13 26 17
robert-lore commented 1 month ago

I'd like to do something similar, but was thinking something like this would work, and found out it doesn't:

export default [
  defineConfig({
    client: '@hey-api/client-fetch',
    input: 'https://api1/openapi.json',
    output: 'src/clients/api1',
  }),
  defineConfig({
    client: '@hey-api/client-fetch',
    input: 'https://api2/openapi.json',
    output: 'src/clients/api2',
  }),
];
floleroy59000 commented 1 month ago

Like said by @frankPairs, i will be very interested by this functionnality because on my project i have several swagger, and i would wish they are classified by input name in output directory. But actually, I have this issue if i launch the command

npm run openapi

Unexpected error occurred. Unsupported OpenAPI specification: { while in the documentation, the input type is a string or a Record<string, unknown>;

On project openapi-typescript-codegen https://github.com/ferdikoomen/openapi-typescript-codegen/tree/main?tab=readme-ov-file I was not bothered because I was running X commands (in the package.json) for X swagger to get the data generated. openapi -i http://localhost:8080/api/xxxx/swagger.json -o src/api/xxxx --client axios --indent 2