From 8dcb0d5e09bc1cb0fe220ccd44adfe5d3e5e151d Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 25 Mar 2025 13:55:58 +0100 Subject: [PATCH] reverts commit 5c8a4c1edc3236788bba4dd17e8018e3bf5549f3. --- dist/container.d.ts | 5 ----- dist/container.js | 11 ----------- dist/index.d.ts | 9 +-------- dist/index.js | 37 ++++--------------------------------- dist/router.d.ts | 2 +- src/container.ts | 28 ---------------------------- src/index.ts | 42 ++---------------------------------------- src/router.ts | 6 +----- 8 files changed, 9 insertions(+), 131 deletions(-) delete mode 100644 src/container.ts diff --git a/dist/container.d.ts b/dist/container.d.ts index 33869f7..4c5fe34 100644 --- a/dist/container.d.ts +++ b/dist/container.d.ts @@ -1,10 +1,5 @@ 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 9588594..5a20c95 100644 --- a/dist/container.js +++ b/dist/container.js @@ -2,7 +2,6 @@ export default class Container { constructor() { this.services = new Map(); } -<<<<<<< HEAD set(type, instance) { this.services.set(type, instance); } @@ -11,15 +10,5 @@ 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 fd6c089..bc3830b 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -3,17 +3,10 @@ import Tpl from "./template.js"; export { Router, Tpl, Request, Response, Handler }; export default class Flummpress { private server?; - private container; 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) + use(plugin: Router | Handler): this; private processPipeline; listen(...args: any[]): this; private parseRequest; diff --git a/dist/index.js b/dist/index.js index 4171f69..bec4dfd 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11,49 +11,20 @@ import http from "node:http"; import { URL } from "node:url"; import querystring from "node:querystring"; import Router from "./router.js"; -import Container from "./container.js"; import Tpl from "./template.js"; export { Router, Tpl }; export default class Flummpress { constructor() { - this.container = new Container(); this.router = new Router(); this.middleware = []; } -<<<<<<< HEAD - use(plugin, factory) { - if (typeof plugin === "function" && factory) - this.container.set(plugin, factory()); - else if (plugin instanceof Router) + use(plugin) { + if (plugin instanceof Router) this.router.use(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()"); - } -======= - 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()"); + else if (typeof plugin === "function") + this.middleware.push(plugin); 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* () { for (const handler of handlers) { diff --git a/dist/router.d.ts b/dist/router.d.ts index 4383b54..d8f4e34 100644 --- a/dist/router.d.ts +++ b/dist/router.d.ts @@ -17,7 +17,7 @@ export interface Response extends ServerResponse { body: string; }) => void; status: (code: number) => Response; - json: (body: any, code?: number) => void; + json: (body: JSON, code?: number) => void; html: (body: string, code?: number) => void; redirect: (target: string, code?: number) => void; } diff --git a/src/container.ts b/src/container.ts deleted file mode 100644 index ddc72ae..0000000 --- a/src/container.ts +++ /dev/null @@ -1,28 +0,0 @@ -export default class Container { -<<<<<<< HEAD - private services: Map = new Map(); - - set(type: new (...args: any[]) => T, instance: T): void { - this.services.set(type, instance); - } - - 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; -======= - 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 03fc81b..f2884a8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,64 +3,26 @@ import { URL } from "node:url"; import querystring from "node:querystring"; import Router, { Request, Response, Handler } from "./router.js"; -import Container from "./container.js"; import Tpl from "./template.js"; export { Router, Tpl, Request, Response, Handler }; export default class Flummpress { private server?: http.Server; - private container: Container; private middleware: Handler[]; public router: Router; constructor() { - this.container = new Container(); this.router = new Router(); 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()); - else if(plugin instanceof Router) - this.router.use(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()"); -======= - /*public use(plugin: Router | Handler): this { + 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); - if(!instance) - throw new Error(`Service of type "${type.name}" not found.`); - return instance; } private async processPipeline(handlers: Handler[], req: Request, res: Response) { @@ -171,7 +133,7 @@ export default class Flummpress { return res.writeHead(code); }; - res.json = (body: any, code = 200) => { + res.json = (body: JSON, code = 200) => { res.reply({ code, type: "application/json", body: JSON.stringify(body) }); }; diff --git a/src/router.ts b/src/router.ts index 2b79da4..a334201 100644 --- a/src/router.ts +++ b/src/router.ts @@ -17,7 +17,7 @@ export interface Request extends Omit { export interface Response extends ServerResponse { reply: (options: { code?: number; type?: string; body: string }) => void; status: (code: number) => Response; - json: (body: any, code?: number) => void; + json: (body: JSON, code?: number) => void; html: (body: string, code?: number) => void; redirect: (target: string, code?: number) => void; } @@ -25,14 +25,10 @@ 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() {