diff --git a/dist/container.d.ts b/dist/container.d.ts index 4c5fe34..33869f7 100644 --- a/dist/container.d.ts +++ b/dist/container.d.ts @@ -1,5 +1,10 @@ export default class Container { private services; +<<<<<<< HEAD set(type: new (...args: any[]) => T, instance: T): void; get(type: new (...args: any[]) => T): T; +======= + register(name: string, factory: () => T): void; + resolve(name: string): T; +>>>>>>> 277f5a3 (test) } diff --git a/dist/container.js b/dist/container.js index 5a20c95..9588594 100644 --- a/dist/container.js +++ b/dist/container.js @@ -2,6 +2,7 @@ export default class Container { constructor() { this.services = new Map(); } +<<<<<<< HEAD set(type, instance) { this.services.set(type, instance); } @@ -10,5 +11,15 @@ export default class Container { if (!instance) throw new Error(`Service of type "${type.name}" not found.`); return instance; +======= + register(name, factory) { + this.services.set(name, factory); + } + resolve(name) { + const factory = this.services.get(name); + if (!factory) + throw new Error(`Service "${name}" not found.`); + return factory(); +>>>>>>> 277f5a3 (test) } } diff --git a/dist/index.d.ts b/dist/index.d.ts index 88c1c3a..fd6c089 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -7,8 +7,13 @@ export default class Flummpress { private middleware; router: Router; constructor(); +<<<<<<< HEAD use(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this; get(type: new (...args: any[]) => T): T; +======= + use(nameOrRouter: string | Router, factory?: () => T): this; + resolve(name: string): T; +>>>>>>> 277f5a3 (test) private processPipeline; listen(...args: any[]): this; private parseRequest; diff --git a/dist/index.js b/dist/index.js index 6433f86..4171f69 100644 --- a/dist/index.js +++ b/dist/index.js @@ -20,6 +20,7 @@ export default class Flummpress { this.router = new Router(); this.middleware = []; } +<<<<<<< HEAD use(plugin, factory) { if (typeof plugin === "function" && factory) this.container.set(plugin, factory()); @@ -31,15 +32,27 @@ export default class Flummpress { else throw new TypeError("Invalid plugin provided to use()"); } +======= + use(nameOrRouter, factory) { + if (typeof nameOrRouter === "string" && factory) + this.container.register(nameOrRouter, factory); + else if (nameOrRouter instanceof Router) + this.router.use(nameOrRouter); +>>>>>>> 277f5a3 (test) else throw new TypeError("Invalid arguments provided to use()"); return this; } +<<<<<<< HEAD get(type) { const instance = this.container.get(type); if (!instance) throw new Error(`Service of type "${type.name}" not found.`); return instance; +======= + resolve(name) { + return this.container.resolve(name); +>>>>>>> 277f5a3 (test) } processPipeline(handlers, req, res) { return __awaiter(this, void 0, void 0, function* () { diff --git a/src/container.ts b/src/container.ts index 5ac7c28..ddc72ae 100644 --- a/src/container.ts +++ b/src/container.ts @@ -1,4 +1,5 @@ export default class Container { +<<<<<<< HEAD private services: Map = new Map(); set(type: new (...args: any[]) => T, instance: T): void { @@ -10,5 +11,18 @@ export default class Container { if(!instance) throw new Error(`Service of type "${type.name}" not found.`); return instance; +======= + private services: Map any> = new Map(); + + register(name: string, factory: () => T): void { + this.services.set(name, factory); + } + + resolve(name: string): T { + const factory = this.services.get(name); + if(!factory) + throw new Error(`Service "${name}" not found.`); + return factory(); +>>>>>>> 277f5a3 (test) } } diff --git a/src/index.ts b/src/index.ts index 621a88f..03fc81b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ export default class Flummpress { this.middleware = []; } +<<<<<<< HEAD public use(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this { if(typeof plugin === "function" && factory) this.container.set(plugin, factory()); @@ -33,7 +34,27 @@ export default class Flummpress { } else throw new TypeError("Invalid arguments provided to use()"); +======= + /*public use(plugin: Router | Handler): this { + if(plugin instanceof Router) + this.router.use(plugin); + else if(typeof plugin === "function") + this.middleware.push(plugin); +>>>>>>> 277f5a3 (test) return this; + }*/ + + public use(nameOrRouter: string | Router, factory?: () => T): this { + if(typeof nameOrRouter === "string" && factory) + this.container.register(nameOrRouter, factory); + else if(nameOrRouter instanceof Router) + this.router.use(nameOrRouter); + 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); diff --git a/src/router.ts b/src/router.ts index d214ab7..2b79da4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -25,10 +25,14 @@ export interface Response extends ServerResponse { export type Handler = (req: Request, res: Response, next?: () => void) => void | Promise; export default class Router { +<<<<<<< HEAD private routes: Array<{ path: string | RegExp; methods: { [method: string]: Handler[] } }> = []; +======= + private routes: Array<{ path: string | RegExp; methods: { [method: string]: Handler[] } }> = []; +>>>>>>> 277f5a3 (test) private mimes: Map; constructor() {