From f25fe6fab57094df30f15d40d8f0928cdcf4e6ce Mon Sep 17 00:00:00 2001 From: creations Date: Sat, 15 Mar 2025 18:59:01 -0400 Subject: [PATCH] move query and params to request, aswell as make method and accepts multistringable --- package.json | 14 +++++++------- src/server.ts | 47 +++++++++++++++++++++++++++++++++++++---------- types/bun.d.ts | 9 +++++++-- types/routes.d.ts | 11 +++-------- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 1931822..4288d28 100644 --- a/package.json +++ b/package.json @@ -10,22 +10,22 @@ "cleanup": "rm -rf logs node_modules bun.lockdb" }, "devDependencies": { - "@eslint/js": "^9.21.0", - "@types/bun": "^1.2.4", + "@eslint/js": "^9.22.0", + "@types/bun": "^1.2.5", "@types/ejs": "^3.1.5", - "@typescript-eslint/eslint-plugin": "^8.25.0", - "@typescript-eslint/parser": "^8.25.0", - "eslint": "^9.21.0", + "@typescript-eslint/eslint-plugin": "^8.26.1", + "@typescript-eslint/parser": "^8.26.1", + "eslint": "^9.22.0", "eslint-plugin-prettier": "^5.2.3", "eslint-plugin-promise": "^7.2.1", "eslint-plugin-simple-import-sort": "^12.1.1", "eslint-plugin-unicorn": "^56.0.1", "eslint-plugin-unused-imports": "^4.1.4", "globals": "^15.15.0", - "prettier": "^3.5.2" + "prettier": "^3.5.3" }, "peerDependencies": { - "typescript": "^5.7.3" + "typescript": "^5.8.2" }, "dependencies": { "ejs": "^3.1.10" diff --git a/src/server.ts b/src/server.ts index d448edc..1f8985c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -20,6 +20,7 @@ class ServerHandler { this.router = new FileSystemRouter({ style: "nextjs", dir: "./src/routes", + fileExtensions: [".ts"], origin: `http://${this.host}:${this.port}`, }); } @@ -137,39 +138,65 @@ class ServerHandler { } } - if (routeModule.routeDef.method !== request.method) { + if ( + (Array.isArray(routeModule.routeDef.method) && + !routeModule.routeDef.method.includes( + request.method, + )) || + (!Array.isArray(routeModule.routeDef.method) && + routeModule.routeDef.method !== request.method) + ) { response = Response.json( { success: false, code: 405, - error: `Method ${request.method} Not Allowed, expected ${routeModule.routeDef.method}`, + error: `Method ${request.method} Not Allowed, expected ${ + Array.isArray(routeModule.routeDef.method) + ? routeModule.routeDef.method.join(", ") + : routeModule.routeDef.method + }`, }, { status: 405 }, ); } else { - const expectedContentType: string | null = + const expectedContentType: string | string[] | null = routeModule.routeDef.accepts; - const matchesAccepts: boolean = - expectedContentType === "*/*" || - actualContentType === expectedContentType; + let matchesAccepts: boolean; + + if (Array.isArray(expectedContentType)) { + matchesAccepts = + expectedContentType.includes("*/*") || + expectedContentType.includes( + actualContentType || "", + ); + } else { + matchesAccepts = + expectedContentType === "*/*" || + actualContentType === expectedContentType; + } if (!matchesAccepts) { response = Response.json( { success: false, code: 406, - error: `Content-Type ${contentType} Not Acceptable, expected ${expectedContentType}`, + error: `Content-Type ${actualContentType} Not Acceptable, expected ${ + Array.isArray(expectedContentType) + ? expectedContentType.join(", ") + : expectedContentType + }`, }, { status: 406 }, ); } else { + request.params = params; + request.query = query; + response = await routeModule.handler( request, - server, requestBody, - query, - params, + server, ); if (routeModule.routeDef.returns !== "*/*") { diff --git a/types/bun.d.ts b/types/bun.d.ts index 240b1d7..018bf35 100644 --- a/types/bun.d.ts +++ b/types/bun.d.ts @@ -1,9 +1,14 @@ import type { Server } from "bun"; +type Query = Record; +type Params = Record; + declare global { type BunServer = Server; - type ExtendedRequest = Request & { + interface ExtendedRequest extends Request { startPerf: number; - }; + query: Query; + params: Params; + } } diff --git a/types/routes.d.ts b/types/routes.d.ts index 031ea9c..5517e0d 100644 --- a/types/routes.d.ts +++ b/types/routes.d.ts @@ -1,20 +1,15 @@ type RouteDef = { - method: string; - accepts: string | null; + method: string | string[]; + accepts: string | null | string[]; returns: string; needsBody?: "multipart" | "json"; }; -type Query = Record; -type Params = Record; - type RouteModule = { handler: ( request: Request, - server: BunServer, requestBody: unknown, - query: Query, - params: Params, + server: BunServer, ) => Promise | Response; routeDef: RouteDef; };