2021-06-03 03:36:25 +00:00
|
|
|
import {Request} from '../lifecycle/Request'
|
|
|
|
import {ResponseObject} from './Route'
|
|
|
|
import {Container} from '../../di'
|
2021-08-24 04:51:53 +00:00
|
|
|
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.
|
|
|
|
*/
|
2021-08-24 04:51:53 +00:00
|
|
|
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()
|
2022-01-19 19:24:59 +00:00
|
|
|
if ( !request ) {
|
|
|
|
throw new Error('Middleware constructed without request')
|
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
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
|
|
|
|
}
|