From 1381f3ee65f4bb366e1156c4f0dccc64e3058a21 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 25 Mar 2025 11:05:07 +0100 Subject: [PATCH] feat: refactor Container and Flummpress classes to use set/get methods for service management --- dist/container.d.ts | 4 ++-- dist/container.js | 14 +++++++------- dist/index.d.ts | 4 ++-- dist/index.js | 19 +++++++++++++------ src/container.ts | 16 ++++++++-------- src/index.ts | 21 ++++++++++++++------- src/router.ts | 5 ++++- 7 files changed, 50 insertions(+), 33 deletions(-) diff --git a/dist/container.d.ts b/dist/container.d.ts index 973622e..4c5fe34 100644 --- a/dist/container.d.ts +++ b/dist/container.d.ts @@ -1,5 +1,5 @@ export default class Container { private services; - register(name: string, factory: () => T): void; - resolve(name: string): T; + set(type: new (...args: any[]) => T, instance: T): void; + get(type: new (...args: any[]) => T): T; } diff --git a/dist/container.js b/dist/container.js index d8d73a3..5a20c95 100644 --- a/dist/container.js +++ b/dist/container.js @@ -2,13 +2,13 @@ export default class Container { constructor() { this.services = new Map(); } - register(name, factory) { - this.services.set(name, factory); + set(type, instance) { + this.services.set(type, instance); } - resolve(name) { - const factory = this.services.get(name); - if (!factory) - throw new Error(`Service "${name}" not found.`); - return factory(); + get(type) { + const instance = this.services.get(type); + if (!instance) + throw new Error(`Service of type "${type.name}" not found.`); + return instance; } } diff --git a/dist/index.d.ts b/dist/index.d.ts index 503f0f9..88c1c3a 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -7,8 +7,8 @@ export default class Flummpress { private middleware; router: Router; constructor(); - use(plugin: string | Router | Handler, factory?: () => T): this; - resolve(name: string): T; + use(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this; + get(type: new (...args: any[]) => T): T; private processPipeline; listen(...args: any[]): this; private parseRequest; diff --git a/dist/index.js b/dist/index.js index 55a16c7..6433f86 100644 --- a/dist/index.js +++ b/dist/index.js @@ -21,18 +21,25 @@ export default class Flummpress { this.middleware = []; } use(plugin, factory) { - if (typeof plugin === "string" && factory) - this.container.register(plugin, factory); + if (typeof plugin === "function" && factory) + this.container.set(plugin, factory()); else if (plugin instanceof Router) this.router.use(plugin); - else if (typeof plugin === "function") - this.middleware.push(plugin); + else if (typeof plugin === "function") { + if (typeof plugin === "function" && plugin.length >= 2) + this.middleware.push(plugin); + else + throw new TypeError("Invalid plugin provided to use()"); + } else throw new TypeError("Invalid arguments provided to use()"); return this; } - resolve(name) { - return this.container.resolve(name); + get(type) { + const instance = this.container.get(type); + if (!instance) + throw new Error(`Service of type "${type.name}" not found.`); + return instance; } processPipeline(handlers, req, res) { return __awaiter(this, void 0, void 0, function* () { diff --git a/src/container.ts b/src/container.ts index 04f4964..5ac7c28 100644 --- a/src/container.ts +++ b/src/container.ts @@ -1,14 +1,14 @@ export default class Container { - private services: Map any> = new Map(); + private services: Map = new Map(); - register(name: string, factory: () => T): void { - this.services.set(name, factory); + set(type: new (...args: any[]) => T, instance: T): void { + this.services.set(type, instance); } - resolve(name: string): T { - const factory = this.services.get(name); - if(!factory) - throw new Error(`Service "${name}" not found.`); - return factory(); + get(type: new (...args: any[]) => T): T { + const instance = this.services.get(type); + if(!instance) + throw new Error(`Service of type "${type.name}" not found.`); + return instance; } } diff --git a/src/index.ts b/src/index.ts index 6f7cb61..621a88f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,19 +20,26 @@ export default class Flummpress { this.middleware = []; } - public use(plugin: string | Router | Handler, factory?: () => T): this { - if(typeof plugin === "string" && factory) - this.container.register(plugin, factory); + public use(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this { + if(typeof plugin === "function" && factory) + this.container.set(plugin, factory()); else if(plugin instanceof Router) this.router.use(plugin); - else if(typeof plugin === "function") - this.middleware.push(plugin); + else if(typeof plugin === "function") { + if(typeof plugin === "function" && plugin.length >= 2) + this.middleware.push(plugin as unknown as Handler); + else + throw new TypeError("Invalid plugin provided to use()"); + } else throw new TypeError("Invalid arguments provided to use()"); return this; } - public resolve(name: string): T { - return this.container.resolve(name); + public get(type: new (...args: any[]) => T): T { + const instance = this.container.get(type); + if(!instance) + throw new Error(`Service of type "${type.name}" not found.`); + return instance; } private async processPipeline(handlers: Handler[], req: Request, res: Response) { diff --git a/src/router.ts b/src/router.ts index 1f06978..d214ab7 100644 --- a/src/router.ts +++ b/src/router.ts @@ -25,7 +25,10 @@ export interface Response extends ServerResponse { export type Handler = (req: Request, res: Response, next?: () => void) => void | Promise; export default class Router { - private routes: Array<{ path: string | RegExp; methods: { [method: string]: Handler[] } }> = []; + private routes: Array<{ + path: string | RegExp; + methods: { [method: string]: Handler[] } + }> = []; private mimes: Map; constructor() {