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

Generated endpoint headers are not passed to fetch #1779

Closed
clemeth opened this issue Dec 30, 2024 · 1 comment · Fixed by #1780
Closed

Generated endpoint headers are not passed to fetch #1779

clemeth opened this issue Dec 30, 2024 · 1 comment · Fixed by #1780
Assignees
Labels
fetch Fetch client related issue
Milestone

Comments

@clemeth
Copy link
Contributor

clemeth commented Dec 30, 2024

What are the steps to reproduce this issue?

  1. Set headers: true and client: "fetch" in orval.config.ts.
  2. Run orval.
  3. Call a generated function with endpoint headers.

What happens?

The headers are not passed to fetch.

This is an example of generated code:

export const getUser = async (
  headers: GetUserHeaders,
  options?: RequestInit
): Promise<User> => {
  const res = await fetch(getGetUserUrl(), {
    ...options,
    method: "GET",
  });
  const data = await res.json();

  return data as User;
};

What were you expecting to happen?

The headers should be passed to fetch:

export const getUser = async (
  headers: GetUserHeaders,
  options?: RequestInit
): Promise<User> => {
  const res = await fetch(getGetUserUrl(), {
    ...options,
+   headers: { ...headers, ...options?.headers },
    method: "GET",
  });
  const data = await res.json();

  return data as User;
};

With axios it works as expected:

export const getUser = <TData = AxiosResponse<User>>(
  headers: GetUserHeaders,
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.get(`http://127.0.0.1:3999/user`, {
    ...options,
    headers: { ...headers, ...options?.headers },
  });
};

What versions are you using?

orval: 7.3.0
react: 19.0.0
@melloware
Copy link
Collaborator

PR is welcome!

@melloware melloware added the fetch Fetch client related issue label Dec 30, 2024
@melloware melloware added this to the 7.3.1 milestone Dec 31, 2024
soartec-lab pushed a commit that referenced this issue Jan 5, 2025
#1780)

* fix(fetch): pass headers to `fetch` when `headers` option is activated (#1779)

* test(fetch): add test for `headers` config parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fetch Fetch client related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants