Skip to content

Instantly share code, notes, and snippets.

@hejrobin
Last active May 6, 2016 15:10
Show Gist options
  • Save hejrobin/df4b2908dc371ac5188081bf6f733bbb to your computer and use it in GitHub Desktop.
Save hejrobin/df4b2908dc371ac5188081bf6f733bbb to your computer and use it in GitHub Desktop.
class RestfulController {
static validRequestMethods = [
'get', 'post', 'put', 'patch', 'delete'
];
constructor() {
let implementedRequestMethods = [];
if(new.target === RestfulController) {
throw new TypeError('Cannot instantiate abstract class');
}
RestfulController.validRequestMethods.map((requestMethod) => {
if(typeof this[requestMethod] === 'function') {
implementedRequestMethods.push(requestMethod);
}
});
if(implementedRequestMethods.length === 0) {
throw new TypeError(`Class must implement at least one of; ${RestfulController.validRequestMethods.join(', ')}`);
}
}
}
class ResourceController extends RestfulController {
constructor() {
super();
}
get() {
// HTTP GET
}
}
let fooResourceController = new RestfulController; // Throws TypeError
let barResourceController = new ResourceController; // OK!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment