enkot / nuxt-open-fetch

Generate zero-overhead, typed OpenAPI clients for Nuxt.
https://nuxt-open-fetch.vercel.app
MIT License
134 stars 12 forks source link

Configure OpenAPI endpoint in runtime #13

Closed IlyaSemenov closed 10 months ago

IlyaSemenov commented 11 months ago

Currently, the documentation doesn't mention how one can configure the actual OpenAPI endpoint in runtime, such as when using dev endpoint for dev purposes and production endpoint in production. According to the documentation, one is supposed to use build-time nuxt.config.ts which is of course not a reasonable approach.

Reverse engineering the sources I ended up with:

export default defineNuxtConfig({
  openFetch: {
    clients: {
      acme: {}, // Must define empty object
    },
  },
  runtimeConfig: {
    public: {
      openFetch: {
        clients: {
          acme: {
            fetchOptions: {
              baseURL: "https://api-test.acme.com", // Use test endpoint by default
            },
          },
        },
      },
    },
  },
})

which is then possible to override with:

NUXT_PUBLIC_OPEN_FETCH_CLIENTS_ACME_FETCH_OPTIONS_BASE_URL=https://api.acme.com

So, 1) I believe this should be documented. 2) Any way to add some kind of shortcut for this purpose? This is probably the longest env variable I used in the last 10 years. :)

enkot commented 10 months ago

Simplified in https://github.com/enkot/nuxt-open-fetch/releases/tag/v0.3.0 You can now configure client using shorter env:

NUXT_OPEN_FETCH_ACME_BASE_URL=https://api.acme.com

Updated docs: https://nuxt-open-fetch.vercel.app/setup/configuration