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

NUXT_OPEN_FETCH_XXX_BASE_URL is not taken into account #24

Closed asapha closed 6 months ago

asapha commented 7 months ago

Hi, according to the docs, we can use NUXT_OPEN_FETCH_PETS_BASE_URL=https://petstore3.swagger.io/api/v3/ to set the base url. But, doing so doesn't seem to work.

With

// nuxt.config.ts

[...]
  openFetch: {
    clients: {
      example: {},
    },
  },

runtimeConfig: {
    openFetch: {
      example: {
       // url for local dev
        baseURL: "https://local.example.com",
      },
    },
  },
[...]
// app.vue
<script lang="ts" setup>
const config = useRuntimeConfig();
console.log(config);
console.log("public", JSON.stringify(config.public?.openFetch?.example));
console.log("non-public", JSON.stringify(config.openFetch?.example));

const { error } = await useExampleFetch("/foo");
console.log("example error", error.value);
</script>
pnpm build
NUXT_OPEN_FETCH_EXAMPLE_BASE_URL=https://override.example.com node .output/server/index.mjs

leads to

[...]
  public: {
     openFetch: { example: [Object] },
  },
  openFetch: { example: { baseURL: 'https://override.example.com' } },
}
public {"baseURL":"https://local.example.com"}
non-public {"baseURL":"https://override.example.com"}

// ==> override.example.com should have been used
example error FetchError: [GET] "https://local.example.com/foo": <no response> fetch failed

Setting NUXT_PUBLIC_OPEN_FETCH_EXAMPLE_BASE_URL instead leads to the correct result.

NUXT_PUBLIC_OPEN_FETCH_EXAMPLE_BASE_URL=https://override.example.com node .output/server/index.mjs
public {"baseURL":"https://override.example.com"}
non-public {"baseURL":"https://local.example.com"}
// ==> Correctly uses override.example.com
example error FetchError: [GET] "https://override.example.com/foo": <no response> fetch failed

Note : In my situation, since both the client and the server need access to the URL, it seems like it would be more appropriate to specify this URL in the public property of runtimeConfig. (nuxt docs)

runtimeConfig: {
    public: {
      openFetch: {
        example: {
          baseURL: "https://local.example.com",
      },
    },
    },
  },

I've just started to use Nuxt, please let me know if I missed something.

Tested with

"nuxt": "^3.10.2",
"nuxt-open-fetch": "^0.4.5",
enkot commented 7 months ago

@asapha Good catch! I will fix the docs soon.