|
|
@ -1,14 +1,13 @@ |
|
|
|
import { FastifyInstance, RouteHandlerMethod } from "fastify"; |
|
|
|
import { FastifyInstance } from "fastify"; |
|
|
|
import Application from "@server/Application"; |
|
|
|
import { handleMiddleware, MiddlewareMethod } from "../middleware"; |
|
|
|
import { handleMiddleware, HandlerMethodWithMiddleware, MiddlewareMethod } from "../middleware"; |
|
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
|
import { auth } from "../middleware/auth"; |
|
|
|
import WebServer from ".."; |
|
|
|
|
|
|
|
export type RouteFactory = ((factory: RouteRegisterFactory) => void) |
|
|
|
| ((factory: RouteRegisterFactory, app: Application) => void); |
|
|
|
export type RouteFactory<M extends MiddlewareMethod<any> = never> = ((factory: RouteRegisterFactory<M>) => void) |
|
|
|
| ((factory: RouteRegisterFactory<M>, app: Application) => void); |
|
|
|
|
|
|
|
export default class RouteRegisterFactory |
|
|
|
export default class RouteRegisterFactory<M extends MiddlewareMethod<any> = never> |
|
|
|
{ |
|
|
|
/** |
|
|
|
* The application instance |
|
|
@ -28,7 +27,7 @@ export default class RouteRegisterFactory |
|
|
|
/** |
|
|
|
* The list of middleware |
|
|
|
*/ |
|
|
|
protected middleware: MiddlewareMethod[] = []; |
|
|
|
protected middleware: M[] = []; |
|
|
|
|
|
|
|
/** |
|
|
|
* The current route prefix |
|
|
@ -47,7 +46,7 @@ export default class RouteRegisterFactory |
|
|
|
/** |
|
|
|
* Register a group of routes under a common prefix and middleware |
|
|
|
*/ |
|
|
|
public prefix(prefix: string, middleware: MiddlewareMethod[], factory: RouteFactory) { |
|
|
|
public prefix<T extends MiddlewareMethod<any>>(prefix: string, middleware: T[], factory: RouteFactory<(M|T)>) { |
|
|
|
let prefixBackup = this.pathPrefix; |
|
|
|
this.pathPrefix = prefix; |
|
|
|
this.group(middleware, factory); |
|
|
@ -57,44 +56,45 @@ export default class RouteRegisterFactory |
|
|
|
/** |
|
|
|
* Register a group of routes under common middleware |
|
|
|
*/ |
|
|
|
public group(middleware: MiddlewareMethod[], factory: RouteFactory) { |
|
|
|
public group<T extends MiddlewareMethod<any>>(middleware: T[], factory: RouteFactory<(M|T)>) { |
|
|
|
let middlewareBackup = this.middleware; |
|
|
|
this.middleware = this.middleware.concat(middleware); |
|
|
|
factory(this, this.app); |
|
|
|
this.middleware = this.middleware.concat(<any>middleware); |
|
|
|
factory(<RouteRegisterFactory<(M|T)>>this, this.app); |
|
|
|
this.middleware = middlewareBackup; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Register a GET request |
|
|
|
*/ |
|
|
|
public get(path: string, handler: RouteHandlerMethod): void; |
|
|
|
public get(path: string, middleware: MiddlewareMethod[], handler: RouteHandlerMethod): void; |
|
|
|
public get(path: string, middleware: RouteHandlerMethod|MiddlewareMethod[], handler?: RouteHandlerMethod) { |
|
|
|
this.log(`Registering GET: ${this.pathPrefix}${path}`); |
|
|
|
handler = (handler ?? <RouteHandlerMethod>middleware); |
|
|
|
middleware = (middleware instanceof Array) ? this.middleware.concat(middleware) : this.middleware; |
|
|
|
this.fastify.get(`${this.pathPrefix}${path}`, handleMiddleware(middleware, handler)); |
|
|
|
public get<T extends MiddlewareMethod<any>>(path: string, handler: HandlerMethodWithMiddleware<M[]>): void; |
|
|
|
public get<T extends MiddlewareMethod<any>>(path: string, middleware: T[], handler: HandlerMethodWithMiddleware<(T|M)[]>): void; |
|
|
|
public get<T extends MiddlewareMethod<any>>(path: string, middleware: HandlerMethodWithMiddleware<(T|M)[]>|T[], handler?: HandlerMethodWithMiddleware<(T|M)[]>) { |
|
|
|
type Handler = HandlerMethodWithMiddleware<(T|M)[]>; |
|
|
|
handler = (handler ?? <Handler>middleware); |
|
|
|
middleware = (middleware instanceof Array) ? <any>this.middleware.concat(<any>middleware) : this.middleware; |
|
|
|
this.fastify.get(`${this.pathPrefix}${path}`, handleMiddleware(<(T|M)[]>middleware, <Handler>handler)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Register a POST request |
|
|
|
*/ |
|
|
|
public post(path: string, handler: RouteHandlerMethod): void; |
|
|
|
public post(path: string, middleware: MiddlewareMethod[], handler: RouteHandlerMethod): void; |
|
|
|
public post(path: string, middleware: RouteHandlerMethod|MiddlewareMethod[], handler?: RouteHandlerMethod) { |
|
|
|
handler = (handler ?? <RouteHandlerMethod>middleware); |
|
|
|
middleware = (middleware instanceof Array) ? this.middleware.concat(middleware) : this.middleware; |
|
|
|
this.fastify.post(`${this.pathPrefix}${path}`, handleMiddleware(middleware, handler)); |
|
|
|
public post<T extends MiddlewareMethod<any>>(path: string, handler: HandlerMethodWithMiddleware<M[]>): void; |
|
|
|
public post<T extends MiddlewareMethod<any>>(path: string, middleware: T[], handler: HandlerMethodWithMiddleware<(T|M)[]>): void; |
|
|
|
public post<T extends MiddlewareMethod<any>>(path: string, middleware: HandlerMethodWithMiddleware<(T|M)[]>|T[], handler?: HandlerMethodWithMiddleware<(T|M)[]>) { |
|
|
|
type Handler = HandlerMethodWithMiddleware<(T|M)[]>; |
|
|
|
handler = (handler ?? <Handler>middleware); |
|
|
|
middleware = (middleware instanceof Array) ? <any>this.middleware.concat(<any>middleware) : this.middleware; |
|
|
|
this.fastify.post(`${this.pathPrefix}${path}`, handleMiddleware(<(T|M)[]>middleware, <Handler>handler)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Register a proxy route |
|
|
|
*/ |
|
|
|
public proxy(path: string, upstream: string, middleware?: MiddlewareMethod[]) { |
|
|
|
public proxy<T extends MiddlewareMethod<any>>(path: string, upstream: string, middleware?: T[]) { |
|
|
|
this.log(`Registering proxy: ${this.pathPrefix}${path} -> ${upstream}`); |
|
|
|
this.fastify.register(fastifyHttpProxy, { |
|
|
|
prefix: `${this.pathPrefix}${path}`, |
|
|
|
beforeHandler: middleware ? handleMiddleware(middleware) : undefined, |
|
|
|
beforeHandler: middleware ? handleMiddleware(this.middleware.concat(<any>middleware)) : undefined, |
|
|
|
upstream |
|
|
|
}); |
|
|
|
} |
|
|
|