Last active
April 20, 2017 20:58
-
-
Save fabiancarlos/b49a945981a5aeaa648fedc40df72f88 to your computer and use it in GitHub Desktop.
Api REST
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
import * as CurrentUser from './current_user'; | |
class Api { | |
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 API_URL = 'http://www.domain.com/'; | |
const URL = `${API_URL}${route}` | |
let options = Object.assign({ method: verb }, | |
params ? { body: JSON.stringify(params) } : null ); | |
return fetch(URL, options) | |
.then( resp => { | |
let json = resp.json(); | |
if (resp.ok) { | |
return json; | |
} | |
return json.then(err => {throw err}); | |
}).then( json => json ); | |
} | |
} | |
export default Api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment