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

Wrong return type when use a custom FetchFn #1105

Open
fschwalm opened this issue Jan 29, 2024 · 1 comment
Open

Wrong return type when use a custom FetchFn #1105

fschwalm opened this issue Jan 29, 2024 · 1 comment

Comments

@fschwalm
Copy link

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!

@ardatan
Copy link
Owner

ardatan commented Feb 2, 2024

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.

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

No branches or pull requests

2 participants