Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Created October 20, 2019 06:18
Show Gist options
  • Save qetr1ck-op/108336f6ef0006eba29f5005f158a7ff to your computer and use it in GitHub Desktop.
Save qetr1ck-op/108336f6ef0006eba29f5005f158a7ff to your computer and use it in GitHub Desktop.
const api = {
get(p) {return p},
post() {},
patch() {},
put() {},
dele() {},
}
class Resource {
constructor(private api: any, private resourceUri: string) {}
private withId(id) {
return `${this.resourceUri}/${id}`
}
fetchAll() {
return this.api.get(this.resourceUri)
}
fetch(id: string) {
return this.api.get(this.withId(id))
}
create(data: any) {
return this.api.post(this.resourceUri, data)
}
save(data: any) {
return this.api.put(this.resourceUri, data)
}
update(data: any) {
return this.api.patch(this.resourceUri, data)
}
destroy(data: any) {
return this.api.delete(this.resourceUri, data)
}
}
class Customer extends Resource {
constructor(api, resourceUri) {
super(api, resourceUri)
}
}
const customerModel = new Customer(api, 'customers')
console.log(customerModel.fetchAll())
console.log(customerModel.fetch('1'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment