Skip to content

Instantly share code, notes, and snippets.

@bernardodiasc
Last active September 18, 2017 18:06
Show Gist options
  • Save bernardodiasc/d274c00fa65a7b4df64ac7f0100bce57 to your computer and use it in GitHub Desktop.
Save bernardodiasc/d274c00fa65a7b4df64ac7f0100bce57 to your computer and use it in GitHub Desktop.
const axios = require('axios')
class API {
constructor({ url }){
this.url = url
this.endpoints = {}
}
/**
* Create and store a single entity's endpoints
* @param {A entity Object} entity
*/
createEntity(entity) {
this.endpoints[entity.name] = this.createBasicCRUDEndpoints(entity)
}
createEntities(arrayOfEntity) {
arrayOfEntity.forEach(this.createEntity.bind(this))
}
/**
* Create the basic endpoints handlers for CRUD operations
* @param {A entity Object} entity
*/
createBasicCRUDEndpoints( {name} ) {
var endpoints = {}
const resourceURL = `${this.url}/${name}`
endpoints.getAll = ({ query }={}) => axios.get(resourceURL, { params: { query } })
endpoints.getOne = ({ id }) => axios.get(`${resourceURL}/${id}`)
endpoints.create = (toCreate) => axios.post(resourceURL, toCreate)
endpoints.update = (toUpdate) => axios.put(`${resourceURL}/${toUpdate.id}`, toUpdate)
endpoints.delete = ({ id }) => axios.delete(`${resourceURL}/${id}`)
return endpoints
}
}
export default API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment