feat: refactor Container and Flummpress classes to use set/get methods for service management
This commit is contained in:
		
							
								
								
									
										4
									
								
								dist/container.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								dist/container.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -1,5 +1,5 @@
 | 
			
		||||
export default class Container {
 | 
			
		||||
    private services;
 | 
			
		||||
    register<T>(name: string, factory: () => T): void;
 | 
			
		||||
    resolve<T>(name: string): T;
 | 
			
		||||
    set<T>(type: new (...args: any[]) => T, instance: T): void;
 | 
			
		||||
    get<T>(type: new (...args: any[]) => T): T;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								dist/container.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										14
									
								
								dist/container.js
									
									
									
									
										vendored
									
									
								
							@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								dist/index.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								dist/index.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -7,8 +7,8 @@ export default class Flummpress {
 | 
			
		||||
    private middleware;
 | 
			
		||||
    router: Router;
 | 
			
		||||
    constructor();
 | 
			
		||||
    use<T>(plugin: string | Router | Handler, factory?: () => T): this;
 | 
			
		||||
    resolve<T>(name: string): T;
 | 
			
		||||
    use<T>(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this;
 | 
			
		||||
    get<T>(type: new (...args: any[]) => T): T;
 | 
			
		||||
    private processPipeline;
 | 
			
		||||
    listen(...args: any[]): this;
 | 
			
		||||
    private parseRequest;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										19
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							@@ -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* () {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user