Created
November 17, 2017 13:50
-
-
Save jadjoubran/0f984f0eb19c6ab46057cced09440d42 to your computer and use it in GitHub Desktop.
Fetch wrapper for laravel-angular
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
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