Skip to content

Instantly share code, notes, and snippets.

@DanielCoulbourne
Created June 16, 2017 20:04
Show Gist options
  • Save DanielCoulbourne/9b9126d5ac5ca83ea5fd1c8ce1af5223 to your computer and use it in GitHub Desktop.
Save DanielCoulbourne/9b9126d5ac5ca83ea5fd1c8ce1af5223 to your computer and use it in GitHub Desktop.
ES6 Resource Client
/***
*
* EXAMPLE USAGE
*
* Resource.index('users')
* Resource.store('users', { email: 'daniel@tighten.co' })
* Resource.show('users', 1)
* Resource.update('users', { id: 1, email: 'daniel.coulbourne@gmail.com' })
* Resource.destroy('users', 1)
*
***/
class Resource {
constructor() {
this.url = '/api';
}
index(resource) {
return this.http('GET', resource);
}
store(resource, payload) {
return this.http('POST', resource, payload);
}
show(resource, id) {
return this.http('GET', resource, null, id);
}
update(resource, payload) {
return this.http('PUT', resource, payload, payload.id);
}
destroy(resource, id) {
return this.http('DELETE', resource, null, payload.id);
}
http(method, resource, payload = null, path = '') {
return new Promise((resolve, reject) => {
axios({
method: method,
url: `${ this.url }/${ resource }/${ path }`,
headers: {
'Accept': 'application/json', // I'm assuming your token is in App.apiToken
'Authorization': 'Bearer ' + App.apiToken // Do what you gotta do for your own Auth.
},
data: payload
}).then(response => {
return resolve(response.data);
}).catch(error => {
reject({ error });
});
});
}
}
export default new Resource();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment