reverts commit 5c8a4c1edc3236788bba4dd17e8018e3bf5549f3.

This commit is contained in:
Flummi 2025-03-25 13:55:58 +01:00
parent b5d488b0b8
commit 8dcb0d5e09
8 changed files with 9 additions and 131 deletions

5
dist/container.d.ts vendored
View File

@ -1,10 +1,5 @@
export default class Container { export default class Container {
private services; private services;
<<<<<<< HEAD
set<T>(type: new (...args: any[]) => T, instance: T): void; set<T>(type: new (...args: any[]) => T, instance: T): void;
get<T>(type: new (...args: any[]) => T): T; get<T>(type: new (...args: any[]) => T): T;
=======
register<T>(name: string, factory: () => T): void;
resolve<T>(name: string): T;
>>>>>>> 277f5a3 (test)
} }

11
dist/container.js vendored
View File

@ -2,7 +2,6 @@ export default class Container {
constructor() { constructor() {
this.services = new Map(); this.services = new Map();
} }
<<<<<<< HEAD
set(type, instance) { set(type, instance) {
this.services.set(type, instance); this.services.set(type, instance);
} }
@ -11,15 +10,5 @@ export default class Container {
if (!instance) if (!instance)
throw new Error(`Service of type "${type.name}" not found.`); throw new Error(`Service of type "${type.name}" not found.`);
return instance; 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)
} }
} }

9
dist/index.d.ts vendored
View File

@ -3,17 +3,10 @@ import Tpl from "./template.js";
export { Router, Tpl, Request, Response, Handler }; export { Router, Tpl, Request, Response, Handler };
export default class Flummpress { export default class Flummpress {
private server?; private server?;
private container;
private middleware; private middleware;
router: Router; router: Router;
constructor(); constructor();
<<<<<<< HEAD use(plugin: Router | Handler): this;
use<T>(plugin: new (...args: any[]) => T | Router | Handler, factory?: () => T): this;
get<T>(type: new (...args: any[]) => T): T;
=======
use<T>(nameOrRouter: string | Router, factory?: () => T): this;
resolve<T>(name: string): T;
>>>>>>> 277f5a3 (test)
private processPipeline; private processPipeline;
listen(...args: any[]): this; listen(...args: any[]): this;
private parseRequest; private parseRequest;

37
dist/index.js vendored
View File

@ -11,49 +11,20 @@ import http from "node:http";
import { URL } from "node:url"; import { URL } from "node:url";
import querystring from "node:querystring"; import querystring from "node:querystring";
import Router from "./router.js"; import Router from "./router.js";
import Container from "./container.js";
import Tpl from "./template.js"; import Tpl from "./template.js";
export { Router, Tpl }; export { Router, Tpl };
export default class Flummpress { export default class Flummpress {
constructor() { constructor() {
this.container = new Container();
this.router = new Router(); this.router = new Router();
this.middleware = []; this.middleware = [];
} }
<<<<<<< HEAD use(plugin) {
use(plugin, factory) { if (plugin instanceof Router)
if (typeof plugin === "function" && factory)
this.container.set(plugin, factory());
else if (plugin instanceof Router)
this.router.use(plugin); this.router.use(plugin);
else if (typeof plugin === "function") { else if (typeof plugin === "function")
if (typeof plugin === "function" && plugin.length >= 2) this.middleware.push(plugin);
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()");
return this; 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) { processPipeline(handlers, req, res) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
for (const handler of handlers) { for (const handler of handlers) {

2
dist/router.d.ts vendored
View File

@ -17,7 +17,7 @@ export interface Response extends ServerResponse {
body: string; body: string;
}) => void; }) => void;
status: (code: number) => Response; status: (code: number) => Response;
json: (body: any, code?: number) => void; json: (body: JSON, code?: number) => void;
html: (body: string, code?: number) => void; html: (body: string, code?: number) => void;
redirect: (target: string, code?: number) => void; redirect: (target: string, code?: number) => void;
} }

View File

@ -1,28 +0,0 @@
export default class Container {
<<<<<<< HEAD
private services: Map<Function, any> = new Map();
set<T>(type: new (...args: any[]) => T, instance: T): void {
this.services.set(type, instance);
}
get<T>(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<string, () => any> = new Map();
register<T>(name: string, factory: () => T): void {
this.services.set(name, factory);
}
resolve<T>(name: string): T {
const factory = this.services.get(name);
if(!factory)
throw new Error(`Service "${name}" not found.`);
return factory();
>>>>>>> 277f5a3 (test)
}
}

View File

@ -3,64 +3,26 @@ import { URL } from "node:url";
import querystring from "node:querystring"; import querystring from "node:querystring";
import Router, { Request, Response, Handler } from "./router.js"; import Router, { Request, Response, Handler } from "./router.js";
import Container from "./container.js";
import Tpl from "./template.js"; import Tpl from "./template.js";
export { Router, Tpl, Request, Response, Handler }; export { Router, Tpl, Request, Response, Handler };
export default class Flummpress { export default class Flummpress {
private server?: http.Server; private server?: http.Server;
private container: Container;
private middleware: Handler[]; private middleware: Handler[];
public router: Router; public router: Router;
constructor() { constructor() {
this.container = new Container();
this.router = new Router(); this.router = new Router();
this.middleware = []; this.middleware = [];
} }
<<<<<<< HEAD public use(plugin: Router | Handler): this {
public use<T>(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 {
if(plugin instanceof Router) if(plugin instanceof Router)
this.router.use(plugin); this.router.use(plugin);
else if(typeof plugin === "function") else if(typeof plugin === "function")
this.middleware.push(plugin); this.middleware.push(plugin);
>>>>>>> 277f5a3 (test)
return this; return this;
}*/
public use<T>(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<T>(name: string): T {
return this.container.resolve(name);
}
public get<T>(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) { private async processPipeline(handlers: Handler[], req: Request, res: Response) {
@ -171,7 +133,7 @@ export default class Flummpress {
return res.writeHead(code); 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) }); res.reply({ code, type: "application/json", body: JSON.stringify(body) });
}; };

View File

@ -17,7 +17,7 @@ export interface Request extends Omit<IncomingMessage, 'url'> {
export interface Response extends ServerResponse { export interface Response extends ServerResponse {
reply: (options: { code?: number; type?: string; body: string }) => void; reply: (options: { code?: number; type?: string; body: string }) => void;
status: (code: number) => Response; status: (code: number) => Response;
json: (body: any, code?: number) => void; json: (body: JSON, code?: number) => void;
html: (body: string, code?: number) => void; html: (body: string, code?: number) => void;
redirect: (target: 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<void>; export type Handler = (req: Request, res: Response, next?: () => void) => void | Promise<void>;
export default class Router { export default class Router {
<<<<<<< HEAD
private routes: Array<{ private routes: Array<{
path: string | RegExp; path: string | RegExp;
methods: { [method: string]: Handler[] } methods: { [method: string]: Handler[] }
}> = []; }> = [];
=======
private routes: Array<{ path: string | RegExp; methods: { [method: string]: Handler[] } }> = [];
>>>>>>> 277f5a3 (test)
private mimes: Map<string, string>; private mimes: Map<string, string>;
constructor() { constructor() {