OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.59k stars 6.52k forks source link

[REQ] Overwrite axios instance globally for all endpoints of typescript-axios sdk #11101

Open mklueh opened 2 years ago

mklueh commented 2 years ago

Hi,

I'm using the typescript-axios in my Nuxt project.

My middleware should be called for every axios request, but this is not the case for the typescript-axios calls, as they are using another instance.

//middleware
export default function ({$axios, $fire}) {

  $axios.onRequest(async config => {
    const token = "some-token";
    config.headers["Authorization"] = "Bearer " + token;
  })

}

typescript-axios is using the global axios instance, which I believe is a singleton provided by axios and declared as a const within axios itself.

//api.ts
import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';
//axios index.d.ts
export interface AxiosStatic extends AxiosInstance {
  create(config?: AxiosRequestConfig): AxiosInstance;
  Cancel: CancelStatic;
  CancelToken: CancelTokenStatic;
  Axios: typeof Axios;
  readonly VERSION: string;
  isCancel(value: any): boolean;
  all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
  spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
  isAxiosError(payload: any): payload is AxiosError;
}

declare const axios: AxiosStatic;

export default axios;

I have seen, it would be possible to create endpoints with their related factory, to pass an axios instance. But I would need to do that for every single endpoint, which is not an option. So my idea was, to overwrite the instance typescript-axios is using.

Is it somehow possible to use the Nuxt.js axios instance for all endpoints?

mklueh commented 2 years ago

I'm currently injecting the $axios instance into each API of the SDK, which is currently enough for me:

const api = new ProjectResourceApi(undefined, undefined, this.$axios);

However, it would still be nice to hear if there is a solution that allows this on a global level for all endpoints