woocommerce / woocommerce-rest-api-js-lib

New JavaScript library for WooCommerce REST API
https://www.npmjs.com/package/@woocommerce/woocommerce-rest-api
MIT License
273 stars 76 forks source link

Possiblity to configure non global axios instance #100

Open lbroman opened 3 years ago

lbroman commented 3 years ago

To be able to have different axios configurations beyond axiosConfig (ie. interceptors) it should be possible to assign a an axios instance which will be used in place of the global axios, which remains the default.

One example of such use would be to use a separate logger and interceptors for logging WC requests.

` let wcAxiosInstance = axios.create();

wcAxiosInstance.interceptors.request.use(req => {
    wcApiLogger.debug(`Request   ${req.method} ${req.url}`);
    wcApiLogger.debug(`Headers > ${JSON.stringify(req.headers)}`)
    return req;
});

wcAxiosInstance.interceptors.response.use(res => {
    wcApiLogger.debug(`Response ${res.config.method} ${res.config.url} ${res.status}`);
    wcApiLogger.debug(`Headers  < ${JSON.stringify(res.headers)}`)
    return res;
});

let wcApi = new WooCommerceRestApi({ url: ..., consumerKey: ..., consumerSecret: ..., axiosInstance: wcAxiosInstance });

`