orval icon indicating copy to clipboard operation
orval copied to clipboard

Extra axios options are not generated for react-query

Open denisorehovsky opened this issue 2 years ago • 8 comments

Hi, I need to pass additional options to axios, but can't find a way to do this with react-query client (haven't tested with other clients).

I have this config:

import { defineConfig } from 'orval';

export default defineConfig({
  server: {
    input: 'http://server:8000/api/v1/schema.json',
    output: {
      target: './src/restTypes.ts',
      client: 'react-query',
      override: {
        mutator: {
          path: './src/hooks/useServer.ts',
          name: 'useServer',
          default: true,
        },
      },
    }
  },
});

And a hook for axios:

import { useContext } from 'react';
import { AxiosRequestConfig } from 'axios';
import { AxiosContext } from '../components/AxiosProvider';

const useServer = <T>(): ((
    config: AxiosRequestConfig,
    options?: AxiosRequestConfig,
) => Promise<T>) => {
    const { axios } = useContext(AxiosContext);

    return (config: AxiosRequestConfig, options?: AxiosRequestConfig) => {
        return axios({
            ...config,
            ...options,
        }).then(({ data }) => data);
    };
};

export default useServer;

However, when I generate the code using orval, looks like it doesn't add the second parameter:

image

Maybe I'm doing something wrong. Let me know if I'm missing something.

Thanks.

denisorehovsky avatar Jul 17 '22 08:07 denisorehovsky

Hello @denisorehovsky, It's not possible for the moment with the hook. I will need to take a look at it

anymaniax avatar Jul 17 '22 14:07 anymaniax

@anymaniax When will have time to take a look? If not soon, maybe I will take a look myself.

denisorehovsky avatar Jul 18 '22 03:07 denisorehovsky

If you have the time go ahead. It's always a pleasure to receive some help 🥹

anymaniax avatar Jul 18 '22 06:07 anymaniax

@anymaniax Hi. I'm looking at the examples again, and I see this: image

This is react query example and it looks like it provides extra axios options. However, when I generate types locally, I don't see this option in the code: image

What am I doing wrong?

denisorehovsky avatar Jul 23 '22 11:07 denisorehovsky

The problem is the hook that returns a function. Behind the implementation is not the same.

anymaniax avatar Jul 23 '22 12:07 anymaniax

@anymaniax I really don't have a lot of free time right now for contributing and understanding the codebase. But I would love to see this improvement merged. Is it possible to increase the priority of this issue? Maybe donate some money. Let me know what you think. Thanks.

denisorehovsky avatar Aug 03 '22 09:08 denisorehovsky

@denisorehovsky I will try to take a look at it but I am doing that during my free time so it's not always easy to find time so I prioritize bugs first. And for the support, I want to invest more time to open source in a long term and it can help for sure but I prefer that you do it because you like the project and want to support it.

anymaniax avatar Aug 03 '22 09:08 anymaniax

should be fixed with 6.9.5can you verify, please?

anymaniax avatar Aug 04 '22 19:08 anymaniax

@anymaniax Works perfectly. Thank you.

denisorehovsky avatar Aug 29 '22 15:08 denisorehovsky

Great! Also thanks for the support 😊

anymaniax avatar Aug 29 '22 15:08 anymaniax