Created
August 15, 2018 21:30
-
-
Save cmnstmntmn/fe42bb0e535dc0766fd236c8d0adc9d1 to your computer and use it in GitHub Desktop.
m.request wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -- wrapper around m.request using #next branch | |
import m from 'mithril' | |
const api = { | |
request: (options) => { | |
options.config = xhr => { | |
xhr.setRequestHeader('Authorization', 'Bearer ' + api.token()) | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') | |
} | |
options.url = '/api/v1' + options.url; | |
return m.request(options).catch((error) => { | |
if(error.code == 401) m.route.set("/login") | |
throw error | |
}); | |
}, | |
token: (value) => { | |
if (value) | |
localStorage.setItem('token', value) | |
return localStorage.getItem('token') | |
}, | |
tokenRemove: () => localStorage.removeItem('token') | |
} | |
export default api; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment