openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

BaseURL not being set when using client.setConfig

Open byeze opened this issue 1 year ago • 9 comments

Description

Hello! Im using the generated client on my nextjs frontend, at the layout component (a component that is used by every page) we set the client config, but when trying to query data from my API, the base url is not being set, instead the query is made to localhost:3000

Reproducible example or configuration

Client frontend configuration:

 useLayoutEffect(() => {
    const token = Cookies.get("auth_token");
    if (token) {
      login(token);
    }

    if (!configSetRef.current && typeof token !== "undefined") {
      console.log("setting config");

      client.setConfig({
        baseURL: "https://example.com",
        headers: {
          Authorization: `Bearer ${token}`,
        },
      });
   }
}, [token]);

(the frontend log shows the setting config log and I logged the getConfig response, and the baseURL is set)

SDK Generation config:

  // generate sdk
  await createClient({
    client: {
      bundle: true,
      name: "@hey-api/client-axios",
    },
    input: "docs/openapi.json",
    output: "docs/sdk",
    services: {
      asClass: true,
      name: "{{name}}",
    },
  });

OpenAPI specification (optional)

No response

System information (optional)

No response

byeze avatar Sep 09 '24 17:09 byeze

@byeze in your client.setConfig example, where does client come from?

Are you able to fork a StackBlitz example and recreate the issue?

mrlubos avatar Sep 09 '24 18:09 mrlubos

same

fecapark avatar Sep 30 '24 05:09 fecapark

same issue, I continue to get this error:

code: 'ERR_INVALID_URL',
    input: '/v1/integrations/abc/'
  }

when I just use the client in my server action requests, like this won't work to set the base URL even though I've done it in layout. To make it work, I would have to do it manually:

await createTasks({
                client,
                body: {
                    title,
                    tasks,
                },
            });

But weirdly, everything works when running dev mode, its just when you build and run or in production it breaks.

elijahmuraoka avatar Oct 01 '24 09:10 elijahmuraoka

Hey all, if someone can create a reproducible example, that would be the easiest way to debug what's going on

mrlubos avatar Oct 01 '24 11:10 mrlubos

@mrlubos is the client instance supposed to work only on the client and not with server actions e.g. setting up the config async using "use server" and then importing it into a server action and using it?

I'm thinking that is probably the most straightforward reason why it could be an issue for me.

Sorry I couldn't create a reproducible example.

elijahmuraoka avatar Oct 01 '24 19:10 elijahmuraoka

The base url property is named baseUrl with a lowercase 'rl' I believe so the docs maybe incorrect

mbenniston avatar Oct 05 '24 16:10 mbenniston

@mbenniston that depends on the client used. Axios uses baseURL, Fetch API uses baseUrl

mrlubos avatar Oct 05 '24 16:10 mrlubos

Ah nvm then docs look good

mbenniston avatar Oct 05 '24 16:10 mbenniston

Hey all, I've got a solution for Next.js https://hey-api-docs-git-feat-client-api-hey-api.vercel.app/openapi-ts/clients/fetch#runtime-api

It's effectively the same as being able to define your own client instance inside sdk.gen.ts. I've tested it with Next.js and can confirm it solves the issues mentioned above.

mrlubos avatar Jan 27 '25 19:01 mrlubos