Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support baseUrl is function instead string in ConnectTransportOptions #678

Closed
vctqs1 opened this issue Jun 16, 2023 · 2 comments
Closed
Labels
enhancement New feature or request

Comments

@vctqs1
Copy link

vctqs1 commented Jun 16, 2023

Is your feature request related to a problem? Please describe.
As currently, my application have dynamic baseUrl based on few conditionals. Could we have support to pass baseUrl as function instead plain text
(I'm willing to contribute to connect-es source if this is possible to do that without any negative side effect)

Describe the solution you'd like
Let look example below

const transport = createGrpcWebTransport({
  baseUrl: () => appConfiguration.getEndpoint()
  useBinaryFormat: true,
  credentials: "include",
  interceptors: [],
});

and able to override baseUrl in each request call


const res = await client.say({
  sentence: "I feel happy.",
}, {
   headers: overrideHeaders
   baseUrl: () => "overrideURL"
});

Please specify whether the request is for Connect for Web or Connect for Node.js.
Web

Describe alternatives you've considered
My workaround was to just force re-create transport and whole services in each request call. because it is hard to know when the baseUrl will be change

Additional context
N/A

@vctqs1 vctqs1 added the enhancement New feature or request label Jun 16, 2023
@vctqs1 vctqs1 changed the title Support baseUrl is function instead string Support baseUrl is function instead string in ConnectTransportOptions Jun 16, 2023
@timostamm
Copy link
Member

Hey Thu Vo 👋, creating a Transport on the fly is completely fine. In our documentation for managing clients and transports, we create a small helper function to get a client. You can adapt it to create a new Transport if the baseUrl changes, for example:

import type { ServiceType } from "@bufbuild/protobuf";
import { type PromiseClient, createPromiseClient } from "@bufbuild/connect";
import { createGrpcWebTransport } from "@bufbuild/connect-web";
import { useMemo } from "react";

export function useClient<T extends ServiceType>(service: T): PromiseClient<T> {
  const baseUrl = appConfiguration.getEndpoint();
  return useMemo(() => {
    const transport = createGrpcWebTransport({
      baseUrl: baseUrl,
      useBinaryFormat: true,
      credentials: "include",
      interceptors: [],
    });
    return createPromiseClient(service, transport);
  }, [service, baseUrl]);
}

Another option is to use an interceptor to modify the request URL just when a request is made.

Changing the baseUrl option to also accept a function is a great idea, but we prefer to keep the options simple.

@timostamm timostamm closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2023
@vctqs1
Copy link
Author

vctqs1 commented Jun 18, 2023

@timostamm, thank you. If creating a new client every time the URL changes is acceptable, then I believe we should stick with the current approach.

Please let me know when it is available for change, or if I have the opportunity, I would also like to contribute. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants