OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.5k forks source link

[REQ] [typescript-fetch] Allow to generate runtime.ts and DefaultApi separately. Useful for projects with multiple clients. #4034

Open kierson opened 5 years ago

kierson commented 5 years ago

Allow to generate runtime.ts only for projects with multiple clients.

My frontend application is consuming many microservices. Each of them has its own swagger definition file. For each of them I generate API client using typescript-fetch generator.

Now I have two problems:

  1. Runtime for each of the clients is exactly the same code, however I have it bundled into my frontend application multiple times. This makes the whole bundle much bigger than it should be.

I've read in other issue about rewriting all typescript generators into single one that you want to put more and more abstraction into runtime.ts. I think my issue will be even bigger then. I think runtime should be as thin as possible.

  1. If I want to have my own extensions like Configurations that are the same for each API I cannot safely import something from runtime of one of the clients and pass it to the other client. Code of these runtimes is the same however for typescript these are completely different things.

For example I would like to have a single Configuration for each of API clients.

Such code won't compile:

// MyConfigurationForAllClients.ts
import { Configuration } from 'generated/client-one/src';

export class MyConfigurationForAllClients extends Configuration {
  // extend configuration somehow
}

// client-two:
import { DefaultApi } from 'generated/client-one/src';
import { MyConfigurationForAllClients } from 'common/api/MyConfigurationForAllClients'

const apiClient = new DefaultApi(new MyConfigurationForAllClients());
^^^ // Here typescript will break saying that `Configuration` from `client-one` is not matching the type of ``Configuration` from `client-two`.

export apiClient;

Describe the solution you'd like

Ideally I would to have possibility to:

  1. Generate runtime and client code separately.
  2. Pass the runtime in constructor when creating DefaultApi instance

This would require that generated code depends more on interfaces rather than particular classes. I think this has other advantage like I can pass my own runtime as long as it implements proper interface if I want to customize it somehow.

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

macjohnny commented 5 years ago

I would suggest to write a small script in your build/generation process that replaces the content of all but one runtime.ts with something like

export * from 'path/to/my/master/runtime';