How to log all axios calls from one place in code

前端 未结 6 1210
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 12:17

I am using axios for a react application and I would like to log all axios calls that I\'m making anywhere in the app. I\'m already using a single global instance of axios via t

6条回答
  •  甜味超标
    2021-01-30 13:06

    You can try wrap the axios.request function in a Promise.

    function loggedRequest(config) {
      return new Promise((resolve, reject) => {
        axios.request(config)
        .then((res) => {
          // log success, config, res here
          resolve(res);      
        })
        .catch(err => {
          // same, log whatever you want here
          reject(err);
        })
      })
    }
    

提交回复
热议问题