Skip to content

Instantly share code, notes, and snippets.

@jqn
Created April 16, 2021 06:40
Show Gist options
  • Save jqn/8bac0237af9602b122fb32030d3e5b55 to your computer and use it in GitHub Desktop.
Save jqn/8bac0237af9602b122fb32030d3e5b55 to your computer and use it in GitHub Desktop.
Example of api class
class Api {
static headers() {
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
'dataType': 'json',
}
}
static get(route) {
return this.xhr(route, null, 'GET');
}
static put(route, params) {
return this.xhr(route, params, 'PUT')
}
static post(route, params) {
return this.xhr(route, params, 'POST')
}
static delete(route, params) {
return this.xhr(route, params, 'DELETE')
}
static xhr(route, params, verb) {
const host = 'http://www.recipepuppy.com'
const url = `${host}${route}`
let options = Object.assign({ method: verb }, params ? { body: JSON.stringify(params) } : null );
options.headers = Api.headers()
return fetch(url, options).then( resp => {
let json = resp.json();
if (resp.ok) {
return json
}
return json.then(err => {throw err});
}).then( json => json.results );
}
}
export default Api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment