| 29 | }; |
| 30 | |
| 31 | export class Lb3AppBooter implements Booter { |
| 32 | options: Lb3AppBooterOptions; |
| 33 | appPath: string; |
| 34 | |
| 35 | constructor( |
| 36 | @inject(CoreBindings.APPLICATION_INSTANCE) |
| 37 | public app: RestApplication, |
| 38 | @inject(BootBindings.PROJECT_ROOT) |
| 39 | public projectRoot: string, |
| 40 | @inject(`${BootBindings.BOOT_OPTIONS}#lb3app`) |
| 41 | options: Partial<Lb3AppBooterOptions> = {}, |
| 42 | ) { |
| 43 | this.options = Object.assign({}, DefaultOptions, options); |
| 44 | |
| 45 | if (typeof app.mountExpressRouter !== 'function') { |
| 46 | throw new Error( |
| 47 | 'Lb3AppBooter requires RestApplication with mountExpressRouter API', |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | async configure?(): Promise<void> { |
| 53 | this.appPath = path.join(this.projectRoot, this.options.path); |
| 54 | } |
| 55 | |
| 56 | async load?(): Promise<void> { |
| 57 | const lb3App = await this.loadAndBootTheApp(); |
| 58 | const spec = await this.buildOpenApiSpec(lb3App); |
| 59 | if (this.options.mode === 'fullApp') { |
| 60 | this.mountFullApp(lb3App, spec); |
| 61 | } else { |
| 62 | this.mountRoutesOnly(lb3App, spec); |
| 63 | } |
| 64 | |
| 65 | const dataSources = lb3App.dataSources; |
| 66 | if (dataSources) { |
| 67 | const visitedDs = new Set(); |
| 68 | for (const k in dataSources) { |
| 69 | const ds = dataSources[k]; |
| 70 | if (visitedDs.has(ds)) continue; |
| 71 | this.app.bind(`lb3-datasources.${k}`).to(ds).tag('lb3-datasource'); |
| 72 | visitedDs.add(ds); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | const models = lb3App.models; |
| 77 | if (models) { |
| 78 | const visitedModels = new Set(); |
| 79 | for (const k in models) { |
| 80 | const model = models[k]; |
| 81 | if (visitedModels.has(model)) continue; |
| 82 | this.app.bind(`lb3-models.${k}`).to(model).tag('lb3-model'); |
| 83 | visitedModels.add(model); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // TODO(bajtos) Listen for the following events to update the OpenAPI spec: |
| 88 | // - modelRemoted |
nothing calls this directly
no outgoing calls
no test coverage detected