-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: poc inversify plugin completed
- Loading branch information
1 parent
5fb364e
commit 183d324
Showing
4 changed files
with
42 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { Container } from 'inversify'; | ||
import { DataService } from './services/data.service'; | ||
import { HomeController } from './controllers/home.controller'; | ||
|
||
export const container = new Container(); | ||
container.bind<DataService>(DataService).toSelf(); | ||
|
||
container.bind<HomeController>(HomeController).toSelf(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import type { Container, interfaces } from 'inversify'; | ||
import type { NammathamApp, BaseHandlerResolver } from '@nammatham/core'; | ||
|
||
import { type NammathamApp, type BaseHandlerResolver, type BaseHandler, logger } from '@nammatham/core'; | ||
|
||
export function inverisfyPlugin(options: { | ||
container: Container; | ||
services: interfaces.ServiceIdentifier[]; | ||
}): (app: NammathamApp, handlerResolver: BaseHandlerResolver) => void { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => { | ||
for (const service of options.services) { | ||
const controller = options.container.get(service); | ||
// app.addFunctions(controller, handlerResolver); | ||
const instance = options.container.get(service); | ||
if (typeof instance !== 'object') { | ||
throw new Error(`Service ${service.toString()} is not an object`); | ||
} | ||
if (!instance) { | ||
throw new Error(`Service ${service.toString()} not found`); | ||
} | ||
app.addFunctions(...extractClassHandlers(instance)); | ||
} | ||
}; | ||
} | ||
|
||
function extractClassHandlers(classInstance: object) { | ||
const handlers: BaseHandler<any>[] = []; | ||
const fields = Object.entries(classInstance); | ||
fields.forEach(([methodName, method]) => { | ||
const handler = method as BaseHandler<any>; | ||
if (handler.__baseHandler) { | ||
logger.debug( | ||
`Adding handler from ${classInstance.constructor.name}.${methodName} with function name "${ | ||
handler.build().name | ||
}"` | ||
); | ||
handlers.push(handler); | ||
} | ||
}); | ||
return handlers; | ||
} |