Refaktoriere Anfrage- und Antworttypen in separate Datei, verbessere die Typen für Middleware und RouteCallback
This commit is contained in:
		
							
								
								
									
										31
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/index.ts
									
									
									
									
									
								
							@@ -1,33 +1,13 @@
 | 
			
		||||
import http, { IncomingMessage, ServerResponse } from "node:http";
 | 
			
		||||
import { URL } from "node:url";
 | 
			
		||||
import { Buffer } from "node:buffer";
 | 
			
		||||
import querystring from "node:querystring";
 | 
			
		||||
 | 
			
		||||
import Router from "./router.js";
 | 
			
		||||
import Tpl from "./template.js";
 | 
			
		||||
 | 
			
		||||
export { Router, Tpl, Request, Response, Middleware };
 | 
			
		||||
import { Request, Response, Middleware } from "./types.js";
 | 
			
		||||
 | 
			
		||||
type Middleware = (req: Request, res: Response, next: () => void) => void;
 | 
			
		||||
 | 
			
		||||
interface Request extends IncomingMessage {
 | 
			
		||||
  parsedUrl: {
 | 
			
		||||
    pathname: string;
 | 
			
		||||
    split: string[];
 | 
			
		||||
    searchParams: URLSearchParams;
 | 
			
		||||
    qs: Record<string, string>;
 | 
			
		||||
  };
 | 
			
		||||
  cookies: Record<string, string>;
 | 
			
		||||
  params?: Record<string, string>;
 | 
			
		||||
  post?: Record<string, string>;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
interface Response extends ServerResponse {
 | 
			
		||||
  reply: (options: { code?: number; type?: string; body: string }) => void;
 | 
			
		||||
  json: (body: string, code?: number) => void;
 | 
			
		||||
  html: (body: string, code?: number) => void;
 | 
			
		||||
  redirect: (target: string, code?: number) => void;
 | 
			
		||||
}
 | 
			
		||||
export { Router, Tpl };
 | 
			
		||||
 | 
			
		||||
export default class Flummpress {
 | 
			
		||||
  private server?: http.Server;
 | 
			
		||||
@@ -74,7 +54,7 @@ export default class Flummpress {
 | 
			
		||||
    this.server = http
 | 
			
		||||
      .createServer(async (request: IncomingMessage, response: ServerResponse) => {
 | 
			
		||||
        const req = request as Request;
 | 
			
		||||
        const res = this.createResponse(req, response);
 | 
			
		||||
        const res = this.createResponse(response);
 | 
			
		||||
        const start = process.hrtime();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
@@ -173,10 +153,7 @@ export default class Flummpress {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private createResponse(
 | 
			
		||||
    req: Request,
 | 
			
		||||
    response: ServerResponse
 | 
			
		||||
  ): Response {
 | 
			
		||||
  private createResponse(response: ServerResponse): Response {
 | 
			
		||||
    const res = response as Response;
 | 
			
		||||
 | 
			
		||||
    res.reply = ({ code = 200, type = "text/html", body }) => {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,9 @@ import fs from "node:fs";
 | 
			
		||||
import path from "node:path";
 | 
			
		||||
import Tpl from "./template.js";
 | 
			
		||||
 | 
			
		||||
import { Request } from "./index.js";
 | 
			
		||||
import { ServerResponse } from "node:http";
 | 
			
		||||
import { RouteCallback } from "./types.js";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type RouteCallback = (req: Request, res: ServerResponse) => void;
 | 
			
		||||
 | 
			
		||||
export default class Router {
 | 
			
		||||
  private routes: Map<RegExp | string, { [key: string]: RouteCallback | RouteCallback[] }>;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								src/types.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/types.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
import { IncomingMessage, ServerResponse } from "node:http";
 | 
			
		||||
 | 
			
		||||
export interface Request extends IncomingMessage {
 | 
			
		||||
  parsedUrl: {
 | 
			
		||||
    pathname: string;
 | 
			
		||||
    split: string[];
 | 
			
		||||
    searchParams: URLSearchParams;
 | 
			
		||||
    qs: Record<string, string>;
 | 
			
		||||
  };
 | 
			
		||||
  cookies: Record<string, string>;
 | 
			
		||||
  params?: Record<string, string>;
 | 
			
		||||
  post?: Record<string, string>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface Response extends ServerResponse {
 | 
			
		||||
  reply: (options: { code?: number; type?: string; body: string }) => void;
 | 
			
		||||
  json: (body: string, code?: number) => void;
 | 
			
		||||
  html: (body: string, code?: number) => void;
 | 
			
		||||
  redirect: (target: string, code?: number) => void;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type Middleware = (req: Request, res: Response, next: () => void) => void;
 | 
			
		||||
export type RouteCallback = (req: Request, res: Response) => void;
 | 
			
		||||
		Reference in New Issue
	
	Block a user