Created
March 18, 2019 16:10
-
-
Save Markonis/639307d02bd9e7550eddc7d10aeef49a to your computer and use it in GitHub Desktop.
This file contains 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 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