import * as express from "express"; type PathParams = string | RegExp | (string | RegExp)[]; // TODO: Update this repo's lint tooling so that we can use full TypeScript syntax and make this an // enum export const HttpMethod = { DELETE: "delete", GET: "get", PATCH: "patch", POST: "post", PUT: "put", }; /** * The Endpoint class allows us to define types of endpoints with specific middlewares. For * example: * * export class AuthenticatedEndpoint extends Endpoint { * constructor() { * super(); * this.addMiddleware(authenticationCheckMiddleware); * } * } * * export class SpecificEndpoint extends AuthenticatedEndpoint { * handler(req, res, next) { * ... * } * } */ export class Endpoint { middlewares: express.Handler[] = []; addMiddleware(middleware: express.Handler) { this.middlewares.push(middleware); } addMiddlewareAtStart(middleware: express.Handler) { this.middlewares.unshift(middleware); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async handler(_req: express.Request, _res: express.Response, _next: express.NextFunction) { throw new Error(`handler() not implemented for ${this.constructor.name}`); } install(router: express.IRouter, httpMethod: string, path: PathParams) { router[httpMethod](path, ...this.middlewares, this.handler); } }