Last active
May 6, 2016 15:10
-
-
Save hejrobin/df4b2908dc371ac5188081bf6f733bbb 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
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