lib/src/http/routing/Middleware.ts

31 lines
884 B
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {Request} from '../lifecycle/Request'
import {ResponseObject} from './Route'
import {Container} from '../../di'
import {CanonicalItemClass} from '../../support/CanonicalReceiver'
2021-03-09 15:42:19 +00:00
2021-03-25 13:50:13 +00:00
/**
* Base class representing a middleware handler that can be applied to routes.
*/
export abstract class Middleware extends CanonicalItemClass {
2021-03-09 15:42:19 +00:00
constructor(
2021-03-25 13:50:13 +00:00
/** The request that will be handled by this middleware. */
2021-06-03 03:36:25 +00:00
protected readonly request: Request,
) {
super()
}
2021-03-09 15:42:19 +00:00
2021-06-03 03:36:25 +00:00
protected container(): Container {
2021-03-09 15:42:19 +00:00
return this.request
}
2021-03-25 13:50:13 +00:00
/**
* Apply the middleware to the request.
* If this returns a response factory or similar item, that will be sent
* as a response.
*
* If this returns `void | Promise<void>`, the request will continue to the
* next handler.
*/
2021-03-09 15:42:19 +00:00
public abstract apply(): ResponseObject
}