E
E
Enma2022-01-19 10:28:25
JavaScript
Enma, 2022-01-19 10:28:25

Dynamic method in AxiosInstance with Typescript?

There is an instance of axios

AxiosInstance
export default function apiClient(app?: ClientApplication<any>): AxiosInstance {
    const baseUrl = 'blablabla'

    const apiClient = axios.create({
        baseURL: baseUrl,
    })

    apiClient.interceptors.request.use(
        function (config) {
            if (app !== null) {
                return getSessionToken(app!)
                    .then((token) => {
                        config.headers!['Authorization'] = `Bearer ${token}`;
                        return config;
                    });
            } else {
                return config;
            }
        }
    );

    return apiClient;
}


the question is:
how can i dynamically select a method?
i.e. conditionally not
spoiler
await apiClient.post(...)

or
spoiler
await apiClient.get(...)

a
spoiler
await apiClient[workMode]
in workMode just a string - post, get and so on


at the moment it's just not clear what type "workMode" should be.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-01-19
@Enma

await apiClient.request({
   method: workMode
 ...
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question