Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ritch
Created May 31, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/2631e8800735e331ee8e7af29da4dd67 to your computer and use it in GitHub Desktop.
Save ritch/2631e8800735e331ee8e7af29da4dd67 to your computer and use it in GitHub Desktop.
sequence.ts
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