Define interceptors for multiple clients

I’m using multiple Axios clients.

const api1Client = axios.create({
    baseURL: process.env.VUE_APP_API1_HOST,
    // ...
});
const api2Client = axios.create({
    baseURL: process.env.VUE_APP_API2_HOST,
    // ...
});
// ...

There are some for which I need to specify common interceptors, and I’d like to avoid repeating the definitions.

api1Client.interceptors.request.use(config => {
    doingSomeMysteriousStuff();
    return config;
}, error => {
    doingSomeOtherMysteriousStuff();
    return Promise.reject(error);
});

api2Client.interceptors.request.use(config => {
    doingSomeMysteriousStuff();
    return config;
}, error => {
    doingSomeOtherMysteriousStuff();
    return Promise.reject(error);
});

I could still do this, but still too much repeats for me 🙂

api1Client.interceptors.request.use(
    doingSomeMysteriousStuffHandler,
    doingSomeOtherMysteriousStuffHandler
);
api2Client.interceptors.request.use(
    doingSomeMysteriousStuffHandler,
    doingSomeOtherMysteriousStuffHandler
);

How can I factorize that without specifying the interceptors globally ?

Answer

Here is what I ended up with based on @RyanLe & @Bergi suggestions, thanks guys.

[api1Client, api2Client].forEach((client) => {
    client.interceptors.request.use(
        (config) => {
            doingSomeMysteriousStuff();
            return config;
        },
        (error) => {
            doingSomeOtherMysteriousStuff();
            return Promise.reject(error);
        }
    );
});

This allows me to process not only interceptors.