diff --git a/src/index.ts b/src/index.ts index c37e740..59bfb0b 100644 --- a/src/index.ts +++ b/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; - }; - cookies: Record; - params?: Record; - post?: Record; -}; - -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 }) => { diff --git a/src/router.ts b/src/router.ts index d74ba5f..110cfae 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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; diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..49a8a7f --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,23 @@ +import { IncomingMessage, ServerResponse } from "node:http"; + +export interface Request extends IncomingMessage { + parsedUrl: { + pathname: string; + split: string[]; + searchParams: URLSearchParams; + qs: Record; + }; + cookies: Record; + params?: Record; + post?: Record; +} + +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;