Skip to content

Commit

Permalink
feat: poc inversify plugin completed
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Apr 18, 2024
1 parent 5fb364e commit 183d324
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion examples/azure-functions-with-inversify/src/container.ts
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();
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { inject } from 'inversify';
import { inject, injectable } from 'inversify';
import { func } from '../nammatham';
import { DataService } from '../services/data.service';

@injectable()
export class HomeController {
constructor(@inject(DataService) public dataService: DataService) {}

hello = func
.httpGet('hello', {
.httpGet('myHello', {
route: 'hello-world',
})
.handler(async c => {
Expand All @@ -16,4 +17,12 @@ export class HomeController {
data: 'hello world' + this.dataService.getData(),
});
});

timer = func
.timer('myTimer', {
schedule: '0 */5 * * * *',
})
.handler(async c => {
c.context.log('Timer trigger function processed a request.');
});
}
1 change: 1 addition & 0 deletions packages/core/src/bases/base-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NammamthamEndpoint } from '../types';

export abstract class BaseHandler<Handler extends (...args: any[]) => any> {
public readonly __baseHandler: boolean = true;
abstract build(): NammamthamEndpoint;
abstract handler(func: Handler): this;
abstract getHandler(): Handler;
Expand Down
31 changes: 28 additions & 3 deletions packages/inversify/src/plugin.ts
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;
}

0 comments on commit 183d324

Please sign in to comment.