Skip to content

Instantly share code, notes, and snippets.

@cmnstmntmn
Created August 15, 2018 21:30
Show Gist options
  • Save cmnstmntmn/fe42bb0e535dc0766fd236c8d0adc9d1 to your computer and use it in GitHub Desktop.
Save cmnstmntmn/fe42bb0e535dc0766fd236c8d0adc9d1 to your computer and use it in GitHub Desktop.
m.request wrapper
// -- 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