ardatan/feTS

Wrong return type when use a custom FetchFn

fschwalm opened this issue · 1 comments

Hi, firstly congratulations for this amazing library!

I'm using a custom fetchFn as described below.

const client = createClient<typeof oas>({
  fetchFn: fetchWrapper
})
async function fetchWrapper<T>(url: string, options?: RequestInit): Promise<T> {
  try {
    const response = await fetch(url, options);

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data: T = await response.json();
    return data;
  } catch (error) {
    console.error('Fetch error:', error);
    throw error;
  }
}

However i still getting TypedResponse<T> and i that case the expect result would be directly T.

Is there anyway to achieve it?

Thank you!

fetchFn is not intended to use in that way. That option is to replace the default fetch function. Since fetch itself doesn't have typed responses etc, we cannot provide typed response there. Even if we try to implement it;

  • We cannot magically provide the typed response from a url that is string.
  • Typed responses etc won't be accepted by TypeScript since the default fetch signature doesn't have typed stuff.