Created
May 31, 2017 23:04
-
-
Save ritch/2631e8800735e331ee8e7af29da4dd67 to your computer and use it in GitHub Desktop.
sequence.ts
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
interface Envelope { | |
writeToResponse() | |
} | |
class File extends Envelope { | |
writeToResponse(res) { | |
res.setHeader('content-type', this.getContentType()) | |
this.readableStream.pipe(res) | |
} | |
} | |
class MySequence { | |
constructor( | |
public @injectInvoke() invoke, | |
public @injectAuthenticate() authenticate, | |
public @injectAuthorize() authorize, | |
public @injectWrite() write, | |
public @injectParseArgs() parseArgs, | |
public @injectAuthorize() reject, | |
public @injectAuthorize() findRoute) {} | |
write(env : Envelope) { | |
// this is the impl of `write()` | |
return env.writeToResponse(this.res) | |
} | |
async run(request, response) { | |
try { | |
const {controllerName, methodName, spec, pathParams} = this.findRoute(request); | |
const args = this.parseArgs(spec, request, pathParams); | |
const user = await this.authenticate(request); | |
await this.authorize(controllerName, methodName, args, user); | |
const result = await this.invoke(controllerName, methodName, args, user); | |
await this.write(result); | |
} catch(err) { | |
await this.reject(err); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment