Last active
May 20, 2016 00:50
-
-
Save stewhouston/7c6c57ae13ae08b0acd3375c03165808 to your computer and use it in GitHub Desktop.
Class factory decorator pattern in TypeScript. Used for dependency injection in Express.
This file contains hidden or 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
export default function RouteController(decoratorParams:Object) { | |
var providers:any[]; | |
if (Array.isArray(decoratorParams['providers'])) { | |
providers = decoratorParams['providers'].map((provider:string) => { | |
let p = require(provider); | |
return p; | |
}); | |
} | |
return function(decoratedClass:any) { | |
function wrapConstructor(app) { | |
return new decoratedClass(app, ...providers); | |
} | |
wrapConstructor.prototype = decoratedClass.prototype; | |
return <typeof decoratedClass> wrapConstructor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment