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

fix(fetch): pass headers to fetch when headers option is activated #1780

Merged
merged 2 commits into from
Jan 5, 2025

Conversation

clemeth
Copy link
Contributor

@clemeth clemeth commented Dec 30, 2024

Status

READY

Description

Passes headers to fetch when headers option is activated.

Fixes #1779.

I have not tested the patch since the build system doesn't support the system I am on.

melloware
melloware previously approved these changes Dec 31, 2024
Copy link
Collaborator

@melloware melloware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but i will let @soartec-lab review

@soartec-lab soartec-lab self-assigned this Dec 31, 2024
@soartec-lab
Copy link
Member

Sure, please give me a few days to review it 👍

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clemeth
Thanks!
Since we are managing test cases, could you please add a test case that uses headers: true?

https://github.com/orval-labs/orval/blob/master/tests/configs/fetch.config.ts

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@soartec-lab soartec-lab merged commit 9784454 into orval-labs:master Jan 5, 2025
@camillecroci
Copy link

Hi @clemeth
This PR says "pass headers to fetch when headers option is activated "
but when i set the property headers:false to my orval config, I still get headers in the generated response. Am I misunderstanding that pr?
(A bit more of the config just in case:

output: {
	mode: 'split',
	target: './generated/client.ts',
	client: 'fetch',
	override: {
		mutator: './lib/custom-fetch.ts',
		name: 'customFetch',
	},
	mock: true,
	headers: false,
},

@clemeth
Copy link
Contributor Author

clemeth commented Jan 16, 2025

Hello, @camillecroci. This PR actually introduced a bug which was fixed by #1770 before the next release, so please refer to that for current code. I can still try to clarify some things.

The headers option enables generated functions to take the endpoint headers directly as an argument, like this:

  const result = await getFoo({ "User-ID": userId });

With the option disabled, headers have to be passed through the options argument:

  const result = await getFoo({ headers: { "User-ID": userId } });

It's unclear to me what you're looking for, but this doesn't seem like it. Can you share some more details?

@camillecroci
Copy link

camillecroci commented Jan 17, 2025

aaah, yes it is very different indeed !
In my case, I am trying to understand what changes in 7.3.0 or 7.4.x have started to add headers to the reponse of my generated function. This is an example :
Generated function:

export const postSavedArticles = async (
	options?: RequestInit
): Promise<postSavedArticlesResponse> => {
	return postSavedArticlesMutator<postSavedArticlesResponse>(
		getPostSavedArticlesUrl(),
		{
			...options,
			method: 'POST',
			headers: { 'Content-Type': 'application/json', ...options?.headers },
		}
	)
}

The return type:

export type postSavedArticlesResponse = {
	data: PostSavedArticles | ErrorResponse
	status: number
	headers: Headers
}

It used to have only data and status and my headers can be quite big so I would prefer not to have to return them
But yeah, it looks like this is not it

@clemeth
Copy link
Contributor Author

clemeth commented Jan 17, 2025

mutator: './lib/custom-fetch.ts',
name: 'customFetch',

Could it be because of your custom fetch function?

@camillecroci
Copy link

the customFetch function has not changed since those headers appeared. I think I need to dig into other changes from other versions

@clemeth
Copy link
Contributor Author

clemeth commented Jan 17, 2025

Oh, of course, I was too quick there. That behavior was changed in #1699, I believe. There is, however, the includeHttpResponseReturnType option, which omits both status and headers from the return types. Would that fit your use case? Otherwise, it could perhaps be split into two options, one to omit status and one to omit headers, in case you need one but not the other.

We are getting off topic here, so perhaps you could open a feature request if that is something you need.

@camillecroci
Copy link

@clemeth thank you very much for finding that out. I will open a feature request then !

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

Successfully merging this pull request may close these issues.

Generated endpoint headers are not passed to fetch
4 participants