Skip to content

Instantly share code, notes, and snippets.

@jadjoubran
Created November 17, 2017 13:50
Show Gist options
  • Save jadjoubran/0f984f0eb19c6ab46057cced09440d42 to your computer and use it in GitHub Desktop.
Save jadjoubran/0f984f0eb19c6ab46057cced09440d42 to your computer and use it in GitHub Desktop.
Fetch wrapper for laravel-angular
class API{
constructur(){
//you could do advanced logic to determine the baseUrl
//based on the environment. Or you could simply pass it
//as an argument
this.baseUrl = 'http://laravel.dev/';//keep the slash at the end
}
fetch(url, options){
return fetch(`${this.baseUrl}${url}`, options);
}
//or you could define even better helpers for GET, POST, PUT, DELETE..
get(url){
return fetch(`${this.baseUrl}${url}`);
}
post(url, data = {}){
return fetch(`${this.baseUrl}${url}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment