question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass an additional option object in fetcher function when using custom axios instance

See original GitHub issue

What are the steps to reproduce this issue?

  1. The fetchcer function in generated code is correct with default config

orval.config.ts:

import { defineConfig } from 'orval'

export default defineConfig({
  test: {
    output: {
      mode: 'split',
      target: './swag.ts',
      client: 'react-query',
    },
    input: {
      target: './swagger.json',
    }
  }
})

result:

export const auth = (
     options?: AxiosRequestConfig // here we have an options here
 ): Promise<AxiosResponse<X>> => {
    return axios.get(
      `/api/v1/auth`,options
    );
  }
  1. But with custom axios instance config:
export default defineConfig({
  test: {
    output: {
      mode: 'split',
      target: './swag.ts',
      client: 'react-query',
+      override: {
+        mutator: {
+          path: './custom-instance.ts',
+         name: 'customInstance',
+       },
      },
    },
    input: {
      target: './swagger.json',
    }
  }
})

The options param no longer remains…

export const auth = ( // here no options can be passed in
     
 ) => {
      return customInstance<X>(
      {url: `/api/v1/auth`, method: 'get'
    },
      );
    }

What were you expecting to happen?

I want to pass some different option to some request, for example a longer timeout for certain requests. But with custom axios instance it behaves different than default one, it’ll need to make custom axios instance also have fetcher function accept an options object as param.

What versions are you using?

Package Version: v6.7.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zoubingwucommented, May 7, 2022

Damn you are right !!! 😂😂

// custom-instance
export const AXIOS_INSTANCE = Axios.create({ baseURL: '<BACKEND URL>' }); // use your own URL here or environment variable

export const customInstance = <T>(config: AxiosRequestConfig, options?: AxiosRequestConfig): Promise<T> => {
  const source = Axios.CancelToken.source();
  const promise = AXIOS_INSTANCE({ ...options, ...config, cancelToken: source.token }).then(
    ({ data }) => data,
  );

  // @ts-ignore
  promise.cancel = () => {
    source.cancel('Query was cancelled');
  };

  return promise;
};

//output
export const auth = (
    
 options?: SecondParameter<typeof customInstance>) => {
      return customInstance<CentralMetaData>(
      {url: `/api/v1/auth`, method: 'get'
    },
      options);
    }

Maybe we should add this to the doc.

1reaction
ristomatticommented, May 7, 2022

After some probing, I found out this is already supported from https://github.com/anymaniax/orval/issues/282. 🙂

The solution was maybe not the most obvious one but I was able to get this working by adding a second parameter to the custom client definition then regenerating the code, e.g.:

export const pricingApiClient = async <T>(
  config: AxiosRequestConfig,
  options?: AxiosRequestConfig
): Promise<T> => {
  const defaultConfig: AxiosRequestConfig = {
    ...config,
    baseURL: pricingBaseUrl,
  };
  return cancellableRequest<T>(defaultConfig, options);
};

const cancellableRequest = async <T>(
  config: AxiosRequestConfig,
  options?: AxiosRequestConfig
): Promise<T> => {
  const source = axios.CancelToken.source();
  const promise = axiosInstance({
    ...config,
    ...options,
    cancelToken: source.token,
  }).then(({ data }) => data);

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  promise.cancel = () => {
    source.cancel('Query was cancelled by React Query');
  };

  return promise;
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create custom fetch hook for multiple Axios instances - ITNEXT
Customize how to fetch data with React hooks.
Read more >
Passing custom props via axios config · Issue #2295 - GitHub
Describe the bug We use some custom parameters via axios configuration. They are used in the response interceptor.
Read more >
How to pass dynamic parameters to axios instance
To pass headers dynamically, you could export a function that takes options as an argument and returns a new instance of axios :...
Read more >
Axios vs. fetch(): Which is best for making HTTP requests?
Axios is not always an ideal solution; depending on your needs, there are sometimes better options for making HTTP requests.
Read more >
Arguments - SWR
In some scenarios, it's useful to pass multiple arguments (can be any value or object) to the fetcher function. For example an authorized...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found