Shaddix / react-query-swagger

Generates react-query hooks based on Swagger API definitions
MIT License
122 stars 4 forks source link

set*QueryName*RequestConfig not working as intended #36

Closed joehannouch closed 10 months ago

joehannouch commented 10 months ago

Maybe I didn't understand how this really works, but I have a useContentControllerUpdateContentMutationWithParameters that updates some content and I want to call it twice, each time with a different header (Accept-language).

using setContentControllerUpdateContentRequestConfig with a headers object doesn't really do anything (although the get of that function returns the values I've put).

setContentControllerUpdateContentRequestConfig({ headers: { 'Accept-language': 'en', }, });

The headers of useContentControllerUpdateContentMutationWithParameters remain unchanged.

What I've ended up doing is changing the headers using getAxios().defaults.headers.common and using the hook as mutateAsync to await it before changing the header again,

getAxios().defaults.headers.common['Accept-Language'] = 'en'; await updateSettingsMutation(.... getAxios().defaults.headers.common['Accept-Language'] = 'fr'; await updateSettingsMutation(....

It works, but it's not very clean as it's changing the headers globally and the default language will be the one used last (I'm resetting the headers manually after the second call).

Any input on the above would be very appreciated.

joehannouch commented 10 months ago

I followed through the calls and noticed that _requestConfig*(requestConfigContentControllerGetContents in my case) inside the options is being added at the beginning and the headers are built after:

let options_: AxiosRequestConfig = { ...requestConfigContentControllerGetContents, ...config, method: 'GET', url: url, headers: { Accept: 'application/json', }, };

Which makes it not possible to send headers from the setQueryNameRequestConfig function.

On another note, the third parameter of the use hooks (which can hold headers) is not being sent to the useQuery call since only context.queryKey[2] is being filled in the client (although the controller is also expecting a config):

function __contentControllerGetContents(context: QueryFunctionContext) { return Client.contentControllerGetContents( context.queryKey[2] as string | undefined); }

export function contentControllerGetContents( filter?: string | undefined, config?: AxiosRequestConfig | undefined, )

However, removing the headers from let options_: AxiosRequestConfig... would make it possible to update the headers using setQueryNameRequestConfig.

Although I still find the 3rd parameter of the use hooks more elegant, what would be the best way to clean this up, better yet not mess with the Query.ts and Client.ts files since we're still in early development and I'm generating from Swagger on the daily (which would override all react-query-swagger manual fixes.

Would really appreciate some insight on what/how to approach this.

Thanks

Shaddix commented 10 months ago

yes, it wasn't possible to override headers via set**RequestConfig, it's fixed in the latest