| 45 | * A route backed by a controller |
| 46 | */ |
| 47 | export class ControllerRoute<T extends object> extends BaseRoute { |
| 48 | protected readonly _controllerCtor: ControllerClass<T>; |
| 49 | protected readonly _controllerName: string; |
| 50 | protected readonly _methodName: string; |
| 51 | protected readonly _controllerFactory: ControllerFactory<T>; |
| 52 | |
| 53 | /** |
| 54 | * Construct a controller based route |
| 55 | * @param verb - http verb |
| 56 | * @param path - http request path |
| 57 | * @param spec - OpenAPI operation spec |
| 58 | * @param controllerCtor - Controller class |
| 59 | * @param controllerFactory - A factory function to create a controller instance |
| 60 | * @param methodName - Controller method name, default to `x-operation-name` |
| 61 | */ |
| 62 | constructor( |
| 63 | verb: string, |
| 64 | path: string, |
| 65 | spec: OperationObject, |
| 66 | controllerCtor: ControllerClass<T>, |
| 67 | controllerFactory?: ControllerFactory<T>, |
| 68 | methodName?: string, |
| 69 | ) { |
| 70 | const controllerName = spec['x-controller-name'] || controllerCtor.name; |
| 71 | methodName = methodName ?? spec['x-operation-name']; |
| 72 | |
| 73 | if (!methodName) { |
| 74 | throw new Error( |
| 75 | 'methodName must be provided either via the ControllerRoute argument ' + |
| 76 | 'or via "x-operation-name" extension field in OpenAPI spec. ' + |
| 77 | `Operation: "${verb} ${path}" ` + |
| 78 | `Controller: ${controllerName}.`, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | super( |
| 83 | verb, |
| 84 | path, |
| 85 | // Add x-controller-name and x-operation-name if not present |
| 86 | Object.assign( |
| 87 | { |
| 88 | 'x-controller-name': controllerName, |
| 89 | 'x-operation-name': methodName, |
| 90 | tags: [controllerName], |
| 91 | }, |
| 92 | spec, |
| 93 | ), |
| 94 | ); |
| 95 | |
| 96 | this._controllerFactory = |
| 97 | controllerFactory ?? createControllerFactoryForClass(controllerCtor); |
| 98 | this._controllerCtor = controllerCtor; |
| 99 | this._controllerName = controllerName || controllerCtor.name; |
| 100 | this._methodName = methodName; |
| 101 | } |
| 102 | |
| 103 | describe(): string { |
| 104 | return `${super.describe()} => ${this._controllerName}.${this._methodName}`; |
nothing calls this directly
no outgoing calls
no test coverage detected