question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

resolve requests immediately on request interceptor?

See original GitHub issue

Summary

I’m using a global request interceptor. I want to be able to decide whether to send the request or to resolve them with a particular data immediately from the interceptor (or maybe else where).

Snipet
axios.interceptors.request.use(config => {
  if (shouldNotSend) {
    // I don't want to continue sending this request
    // instead I want to resolve this immediately
    return Promise.resolve(data);
  }

  return {
    ...config,
    headers: {
      ...config.headers,
      customHeader: 'custom header value'
    }
  };
});

Context

  • axios version: 0.18.0
  • Environment: node v10.5.0, chrome 67.0.3396.99, MacOS High Sierra 10.13.5

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:13

github_iconTop GitHub Comments

2reactions
Feamancommented, Sep 17, 2019

I made it with adapter. If there is some redis cache - api get cached data, otherwise starts real request.

const self = this
const defaultAdapter = axios.defaults.adapter
axios.defaults.adapter = function (config) {
  return new Promise((resolve, reject) => {
    function fetch () {
      const axiosWithoutAdapter = axios.create(Object.assign(config, { adapter: defaultAdapter }))
      axiosWithoutAdapter.request(config)
        .then(response => {
          resolve(response)
        })
        .catch(error => reject(error))
    }
​
    return self.$apiCache.get(config.url + JSON.stringify(config.params))
      .then(data => {
        if (data) {
          return Promise.resolve({
            data,
            status: 200,
            statusText: 'OK',
            headers: {},
            config: config,
            request: {}
          })
        } else {
          return fetch()
        }
      })
      .catch(fetch)
  })
}
1reaction
zaklampertcommented, Mar 21, 2019

I want to conditionally either return custom data (from a redis cache) or perform the request. Do I do this with an interceptor or an adapter?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Implement a Request Interceptor Like Axios
Step 1: Interceptor manager · Step 2: Wrap the fetch · Step 3: Intercept the request · Step 4: Test the interceptors.
Read more >
Axios: How to intercept and respond to axios request
Here's how i was able to resolve the problem axios.interceptors.request.use( request => { if (localResponse) { throw { isLocal: true, ...
Read more >
Setting up Axios Interceptors for all HTTP calls in an application
Interceptors are a feature that allows an application to intercept requests or responses before they are handled by the .then() or the .catch() ......
Read more >
4 ways to use Axios interceptors | Khaled Garbaya
Log every request and response using interceptors.​​ Logging requests can be beneficial, especially when you have a large app and you don't know ......
Read more >
Intercepting Requests & Responses Using Axios
In short, you can intercept the requests or responses before they are handled by then or catch.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found