Replies: 1 comment
|
Short version: The constraint you've hit is actually Next.js's, not hey-api's. Anything the browser needs must be inlined into the client bundle, and There are two clean ways to get true runtime Option A — Inject
|
|
Short version: The constraint you've hit is actually Next.js's, not hey-api's. Anything the browser needs must be inlined into the client bundle, and There are two clean ways to get true runtime Option A — Inject
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
As stated in docs:
https://heyapi.dev/openapi-ts/clients/next-js#runtime-api
for
@hey-api/client-nextrecommended way to configure client is to set path to external fileruntimeConfigPath: './src/hey-api.ts', this is my file:https://github.com/nemanjam/full-stack-fastapi-template-nextjs/blob/main/frontend/apps/web/src/lib/hey-api.ts
Here I have custom fetch function
serverFetchto forward cookies. That is mostly irrelevant for this topic, but it's one example how to have async code in configuration, which is relevant for this thread. I see thatexport const createClientConfig: CreateClientConfig = (config) => ({...})cannot be async.Main idea:
I want to have reusable Docker image with runtime environment variables. Having dynamic
baseUrl: NEXT_PUBLIC_API_URLapplied on both server and client@hey-api/client-nextis part of that requirement.For runtime environment variables i would have to read them in
import 'server-only'file, that will fork for server client but is unavailable for client that runs in browser.For browser i would need to inject
window.__ENVin some script tag in server component and use that to set baseUrl for client. Or make API endpoint and fetch env, but it would make it async.createClientConfig()isnt async to set async baseUrl, butfetchoverride is async function, I could set baseUrl via the first argument infetch(input, { ...init, headers }).These are all options and possible directions, more or less hacky, but is there already standard way to accomplish main goal? To set baseUrl at runtime for both server and client in a
frontend/apps/web/src/lib/hey-api.tsfile. I would want to avoid passing custom client instance for each specific call, or useclient.setConfig()sinceruntimeConfigPathis already stated as proffered method, and I already use it for cookies.All reactions