2021-06-03 03:36:25 +00:00
|
|
|
import {CanonicalStatic} from './CanonicalStatic'
|
|
|
|
import {Singleton, Instantiable, StaticClass} from '../di'
|
|
|
|
import {CanonicalDefinition} from './Canonical'
|
|
|
|
import {Middleware} from '../http/routing/Middleware'
|
2021-03-09 15:42:19 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* A canonical unit that loads the middleware classes from `app/http/middlewares`.
|
|
|
|
*/
|
2021-03-09 15:42:19 +00:00
|
|
|
@Singleton()
|
2021-06-05 18:24:12 +00:00
|
|
|
export class Middlewares extends CanonicalStatic<Middleware, Instantiable<Middleware>> {
|
2021-03-09 15:42:19 +00:00
|
|
|
protected appPath = ['http', 'middlewares']
|
2021-06-03 03:36:25 +00:00
|
|
|
|
2021-03-09 15:42:19 +00:00
|
|
|
protected canonicalItem = 'middleware'
|
2021-06-03 03:36:25 +00:00
|
|
|
|
2021-03-09 15:42:19 +00:00
|
|
|
protected suffix = '.middleware.js'
|
|
|
|
|
2021-06-05 18:24:12 +00:00
|
|
|
public async initCanonicalItem(definition: CanonicalDefinition): Promise<StaticClass<Middleware, Instantiable<Middleware>>> {
|
2021-03-09 15:42:19 +00:00
|
|
|
const item = await super.initCanonicalItem(definition)
|
|
|
|
if ( !(item.prototype instanceof Middleware) ) {
|
|
|
|
throw new TypeError(`Invalid middleware definition: ${definition.originalName}. Controllers must extend from @extollo/lib.http.routing.Middleware.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|