Skip to content

Instantly share code, notes, and snippets.

@Markonis
Created March 18, 2019 16:10
Show Gist options
  • Save Markonis/639307d02bd9e7550eddc7d10aeef49a to your computer and use it in GitHub Desktop.
Save Markonis/639307d02bd9e7550eddc7d10aeef49a to your computer and use it in GitHub Desktop.
export default class ExpressApiMounter implements ApiMounter {
constructor(public readonly router: Router) { }
mountHandler<TInput, TOutput>(
endpoint: ApiEndpoint<TInput, TOutput>,
handler: ApiHandler<TInput, TOutput>): void {
this.router.post(endpoint.pathStr(), async (req, res) => {
const output = await handler(req.body as TInput);
res.json(output);
});
}
mountMiddleware<TInput, TOutput>(
namespace: any, ...handlers: RequestHandler[]) {
const path = '/' + resolvePath(namespace).join('/');
this.router.use(path, handlers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment