Support global middleware
This commit is contained in:
parent
4f23ac7156
commit
5f5b12a5be
@ -6,9 +6,11 @@ import {ResponseFactory} from "../response/ResponseFactory";
|
|||||||
import {Response} from "../lifecycle/Response";
|
import {Response} from "../lifecycle/Response";
|
||||||
import {Controllers} from "../../service/Controllers";
|
import {Controllers} from "../../service/Controllers";
|
||||||
import {ErrorWithContext, Collection} from "@extollo/util";
|
import {ErrorWithContext, Collection} from "@extollo/util";
|
||||||
|
import {Container} from "@extollo/di";
|
||||||
import {Controller} from "../Controller";
|
import {Controller} from "../Controller";
|
||||||
import {Middlewares} from "../../service/Middlewares";
|
import {Middlewares} from "../../service/Middlewares";
|
||||||
import {Middleware} from "./Middleware";
|
import {Middleware} from "./Middleware";
|
||||||
|
import {Config} from "../../service/Config";
|
||||||
|
|
||||||
export type ResponseObject = ResponseFactory | string | number | void | any | Promise<ResponseObject>
|
export type ResponseObject = ResponseFactory | string | number | void | any | Promise<ResponseObject>
|
||||||
export type RouteHandler = ((request: Request, response: Response) => ResponseObject) | ((request: Request) => ResponseObject) | (() => ResponseObject) | string
|
export type RouteHandler = ((request: Request, response: Response) => ResponseObject) | ((request: Request) => ResponseObject) | (() => ResponseObject) | string
|
||||||
@ -34,6 +36,9 @@ export class Route extends AppClass {
|
|||||||
this.registeredRoutes = []
|
this.registeredRoutes = []
|
||||||
this.registeredGroups = []
|
this.registeredGroups = []
|
||||||
|
|
||||||
|
const configService = <Config> Container.getContainer().make(Config)
|
||||||
|
const globalMiddleware = configService.get('server.middleware.global', {})
|
||||||
|
|
||||||
const stack = [...this.compiledGroupStack].reverse()
|
const stack = [...this.compiledGroupStack].reverse()
|
||||||
for ( const route of registeredRoutes ) {
|
for ( const route of registeredRoutes ) {
|
||||||
for ( const group of stack ) {
|
for ( const group of stack ) {
|
||||||
@ -47,6 +52,39 @@ export class Route extends AppClass {
|
|||||||
.each(def => route.appendMiddleware(def))
|
.each(def => route.appendMiddleware(def))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the global pre- and post- middleware
|
||||||
|
if ( Array.isArray(globalMiddleware?.pre) ) {
|
||||||
|
const globalPre = [...globalMiddleware.pre].reverse()
|
||||||
|
for ( const item of globalPre ) {
|
||||||
|
if ( typeof item !== 'string' ) {
|
||||||
|
throw new ErrorWithContext(`Invalid global pre-middleware definition. Global middleware must be string-references.`, {
|
||||||
|
configKey: 'server.middleware.global.pre',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
route.prependMiddleware({
|
||||||
|
stage: 'pre',
|
||||||
|
handler: item,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( Array.isArray(globalMiddleware?.post) ) {
|
||||||
|
const globalPost = [...globalMiddleware.post]
|
||||||
|
for ( const item of globalPost ) {
|
||||||
|
if ( typeof item !== 'string' ) {
|
||||||
|
throw new ErrorWithContext(`Invalid global post-middleware definition. Global middleware must be string-references.`, {
|
||||||
|
configKey: 'server.middleware.global.post',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
route.appendMiddleware({
|
||||||
|
stage: 'post',
|
||||||
|
handler: item,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
route.resolvePreflight() // Try to resolve here to catch any errors at boot-time and pre-compile
|
route.resolvePreflight() // Try to resolve here to catch any errors at boot-time and pre-compile
|
||||||
route.resolveHandler()
|
route.resolveHandler()
|
||||||
route.resolvePostflight()
|
route.resolvePostflight()
|
||||||
|
Loading…
Reference in New Issue
Block a user