lib/src/service/Middlewares.ts

26 lines
989 B
TypeScript
Raw Normal View History

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()
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'
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
}
}