OpenAPITools / OpenAPITools/openapi-generator
[REQ] Overwrite axios instance globally for all endpoints of typescript-axios sdk
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated api.ts import of globalAxios and the axios index.d.ts declaration shown in the issue, then review how the endpoint factories accept an Axios instance. Done should mean there is a supported way for all generated endpoints to use the Nuxt axios instance without configuring each endpoint individually.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nuxtjs, typescript
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100