feat: implement injectServices middleware and useService helper

This commit is contained in:
Stevan Freeborn
2025-05-24 22:29:46 -05:00
parent e14c0515e9
commit 1bc9776755
2 changed files with 197 additions and 2 deletions
+46 -1
View File
@@ -1 +1,46 @@
console.log("hono-netdi");
import {
ServiceScope,
type IServiceProvider,
type IServiceScope,
type ServiceIdentifier,
} from '@stevanfreeborn/netdi';
import type { Context, MiddlewareHandler } from 'hono';
const SERVICE_SCOPE_KEY = 'serviceScope';
declare module 'hono' {
interface ContextVariableMap {
[SERVICE_SCOPE_KEY]?: IServiceScope;
}
}
export function injectServices(serviceProvider: IServiceProvider): MiddlewareHandler {
return async (c: Context, next: () => Promise<void>) => {
const scope = serviceProvider.createScope();
c.set(SERVICE_SCOPE_KEY, scope);
try {
await next();
} finally {
scope.dispose();
}
};
}
export function useService<T>(c: Context, serviceType: ServiceIdentifier<T>): T {
const scope = c.get(SERVICE_SCOPE_KEY);
if (!scope) {
throw new Error(
'Service scope not found in context. Did you add the injectServices middleware?',
);
}
if (scope instanceof ServiceScope === false) {
throw new Error('Service scope is not an instance of ServiceScope.');
}
return scope.serviceProvider.getService(serviceType);
}