api-platform / create-client

Generate React or Vue.js-based Progressive Web Apps from an Hydra-enabled API. Also support React Native.
https://api-platform.com/docs/client-generator/
MIT License
374 stars 132 forks source link

Dynamic entrypoint #248

Open mbrodala opened 3 years ago

mbrodala commented 3 years ago

Description
There should be an option to have a dynamic entrypoint when generating client code.

Currently the API endpoint passed to the generator is also used as entrypoint. This is not optimal:

  1. A development setup may be accessed differently via CLI and via web (e.g. using internal service host names and public URLs in a Docker Compose setup)
  2. Different environments need different host names, hardcoding one will break requests.

Example
Running the generator with http://apache/api/ will generate all code and lead to exactly this being set in config/entrypoint.js. Now accessing http://app.local/<resource> will fail since apache is a host name internally resolved in a Docker Compose setup while the setup is accessed via app.local from the host machine.

Currently we work around this by adjusting entrypoint.js like this after generating the code:

export const ENTRYPOINT = `${location.protocol}//${location.host}/api/`;

Maybe this or similar could be integrated for dynamic entrypoints.

alOneh commented 3 years ago

Hi @mbrodala, thanks for reporting this issue. As the entrypoint.js file is generated once, I think we should let people implements their own logic in this file to retrieve the right value for this variable. Personally, in my project, I prefer using a dot env file containing the right value for the different environments I use.

When you generate your code for the first time you can use the env API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT to set the correct value of your entrypoint if it helps.

https://github.com/api-platform/client-generator/blob/b1b89e8acec69b2483ad366161564ebaaa2b61ac/src/index.js#L46-L55

mbrodala commented 3 years ago

Yeah, I noticed the support for environment variables. But this wouldn't really help in our case since the API is accessed differently via CLI and web as mentioned.

Also my impression so far is that I should be able to drop the generated code at any time and re-generate it to get the latest updates/fixes from the templates of this package. This currently requires me to override the entrypoint.js after each re-generation.

Using .env is a possible solution but needs to be taken into account when deploying to an environment.