lib/src/http/routing/Middleware.ts

28 lines
798 B
TypeScript
Raw Normal View History

2021-03-09 15:42:19 +00:00
import {AppClass} from "../../lifecycle/AppClass"
import {Request} from "../lifecycle/Request"
import {ResponseObject} from "./Route"
2021-03-25 13:50:13 +00:00
/**
* Base class representing a middleware handler that can be applied to routes.
*/
2021-03-09 15:42:19 +00:00
export abstract class Middleware extends AppClass {
constructor(
2021-03-25 13:50:13 +00:00
/** The request that will be handled by this middleware. */
2021-03-09 15:42:19 +00:00
protected readonly request: Request
) { super() }
protected container() {
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
}