Vue-resource: How use promise in new interceptors format (version 1.4.0)?

Created on 26 Feb 2018  路  6Comments  路  Source: pagekit/vue-resource

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'
            }))
        })
    ;
};

Most helpful comment

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.

All 6 comments

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

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.

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 , I'm using version 1.3

@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.

I'm using version 1.5.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

christophwolff picture christophwolff  路  6Comments

jiyifun picture jiyifun  路  3Comments

Dreampie picture Dreampie  路  3Comments

ionutvelicu picture ionutvelicu  路  5Comments

okandavut picture okandavut  路  3Comments