Allow customizing fetch

This issue has been tracked since 2023-01-09.

Is your feature request related to a problem? Please describe.
I'd like to make it easier to integrate connect-web into sveltekit but the way sveltekit provides a custom fetch is not compatible with connect-web.

Describe the solution you'd like
I think it would be sufficient to add an optional param when creating a client to take a custom fetch and this fetch will be used instead of the native fetch API. Or possibly on each request, which is probably more ergonomic.

Describe alternatives you've considered
There may be a way to extract the headers used and just build a custom interceptor but I couldn't find anything in the docs. Perhaps just accessing cookies would be enough but also not very ergonomic and it still wouldn't solve the hydration problem for SSR.

timostamm wrote this answer on 2023-02-22

From #424 (comment):

It looks like an option to provide fetch in the transport is the best way forward.

The pattern could be to define a function for creating clients with the desired transport at a single place in the app:

export function createClient<T extends ServiceType>(service: T, fetch?: FetchFn): PromiseClient<T> {
  const t = createConnectTransport({baseUrl: "/", fetch});
  return createPromiseClient(service, t);
}

And than use it in the SvelteKit load functions:

export async function load({ fetch, params }) {
  const c = createClient(SomeService, fetch);
  return await c.someMethod({id: params.id});
}

Some initial testing by @paul-sachs looked promising, but there are issues with binary data and we did not look into server-streaming yet. Supporting SSR via a custom fetch sounds great, but we need more investigation and also integration tests to make sure it works properly.

More Details About Repo
Owner Name bufbuild
Repo Name connect-es
Full Name bufbuild/connect-es
Language TypeScript
Created Date 2022-02-16
Updated Date 2023-03-24
Star Count 852
Watcher Count 20
Fork Count 33
Issue Count 17

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date