Created
October 20, 2019 06:18
-
-
Save qetr1ck-op/108336f6ef0006eba29f5005f158a7ff to your computer and use it in GitHub Desktop.
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
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