Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.