elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

Provide fetch implementation to wretch #158

Closed liegeandlief closed 1 year ago

liegeandlief commented 1 year ago

Is it possible to pass the fetch implementation to wretch? If not I think this could be really helpful. It could be part of the ‘options’ object maybe.

An example of where this would be useful is in SvelteKit load functions where fetch is passed into the function as an argument.

elbywan commented 1 year ago

Hey @liegeandlief,

Is it possible to pass the fetch implementation to wretch?

Absolutely, you can use the polyfills methods to pass a custom fetch implementation in a global or scoped way.

An example of where this would be useful is in SvelteKit load functions where fetch is passed into the function as an argument.

The non-global way might be what you are looking for:

export async function load({ fetch, params }) {
  const item = await wretch(`/api/items/${params.id}`).polyfills({ fetch }).get().json();
 
  return { item };
}
liegeandlief commented 1 year ago

That's brilliant, thank you!