vue-resource icon indicating copy to clipboard operation
vue-resource copied to clipboard

How use promise in new interceptors format (version 1.4.0)?

Open KhristenkoYura opened this issue 7 years ago • 6 comments

How change this code in new format?

/**
 * @param request
 * @param next
 */
export default (request, next)  => {
    loadToken()
        .then((token) => {
            // continue to next interceptor
            request.headers.set('Authorization', token.token_type + ' ' + token.access_token);
            next();
        })
        .catch(error => {
            return next(request.respondWith({
              message: 'Access token not fetch',
            }, {
              status: 401,
              statusText: 'Unauthorized'
            }))
        })
    ;
};

KhristenkoYura avatar Feb 26 '18 09:02 KhristenkoYura

Having the same issue here. Have you found a solution, @KhristenkoYura ?

stgogm avatar Mar 12 '18 15:03 stgogm

Spent all day and couldn't find similar solution without next() method calling.

The algorithm was:

  1. Check token validity before each request
  2. Stop all new requests if token expired (make queue array [{ request, next }, ...])
  3. Wait new token
  4. Release queue

But now we can't stop new requests and wait for a new token.

ver1000000 avatar Mar 27 '18 13:03 ver1000000

I've finally given up and started using vue-axios. It behaves the way you'd expect and is pretty much a drop-in replacement.

This project has poor documentation and with things like this is unusable in real/production scenarios.

stgogm avatar Mar 27 '18 16:03 stgogm

@stgogm , I'm using version 1.3

KhristenkoYura avatar Mar 28 '18 22:03 KhristenkoYura

@KhristenkoYura Yes, I was too, but having to do an extra request when you can refresh the token beforehand if needed is unnecessary and not optimal. Axios allows you to do this by being able to add an async or Promise interceptor.

stgogm avatar Mar 31 '18 13:03 stgogm

I'm using version 1.5.1

star1105 avatar Sep 15 '18 12:09 star1105