vue-resource
vue-resource copied to clipboard
How use promise in new interceptors format (version 1.4.0)?
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'
}))
})
;
};
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:
- Check token validity before each request
- Stop all new requests if token expired (make queue array
[{ request, next }, ...]
) - Wait new token
- 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