move query and params to request, aswell as make method and accepts multistringable

This commit is contained in:
creations 2025-03-15 18:59:01 -04:00
parent 0a881c354b
commit f25fe6fab5
Signed by: creations
GPG key ID: 8F553AA4320FC711
4 changed files with 54 additions and 27 deletions

View file

@ -10,22 +10,22 @@
"cleanup": "rm -rf logs node_modules bun.lockdb" "cleanup": "rm -rf logs node_modules bun.lockdb"
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.21.0", "@eslint/js": "^9.22.0",
"@types/bun": "^1.2.4", "@types/bun": "^1.2.5",
"@types/ejs": "^3.1.5", "@types/ejs": "^3.1.5",
"@typescript-eslint/eslint-plugin": "^8.25.0", "@typescript-eslint/eslint-plugin": "^8.26.1",
"@typescript-eslint/parser": "^8.25.0", "@typescript-eslint/parser": "^8.26.1",
"eslint": "^9.21.0", "eslint": "^9.22.0",
"eslint-plugin-prettier": "^5.2.3", "eslint-plugin-prettier": "^5.2.3",
"eslint-plugin-promise": "^7.2.1", "eslint-plugin-promise": "^7.2.1",
"eslint-plugin-simple-import-sort": "^12.1.1", "eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-unicorn": "^56.0.1", "eslint-plugin-unicorn": "^56.0.1",
"eslint-plugin-unused-imports": "^4.1.4", "eslint-plugin-unused-imports": "^4.1.4",
"globals": "^15.15.0", "globals": "^15.15.0",
"prettier": "^3.5.2" "prettier": "^3.5.3"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.7.3" "typescript": "^5.8.2"
}, },
"dependencies": { "dependencies": {
"ejs": "^3.1.10" "ejs": "^3.1.10"

View file

@ -20,6 +20,7 @@ class ServerHandler {
this.router = new FileSystemRouter({ this.router = new FileSystemRouter({
style: "nextjs", style: "nextjs",
dir: "./src/routes", dir: "./src/routes",
fileExtensions: [".ts"],
origin: `http://${this.host}:${this.port}`, 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( response = Response.json(
{ {
success: false, success: false,
code: 405, 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 }, { status: 405 },
); );
} else { } else {
const expectedContentType: string | null = const expectedContentType: string | string[] | null =
routeModule.routeDef.accepts; routeModule.routeDef.accepts;
const matchesAccepts: boolean = let matchesAccepts: boolean;
expectedContentType === "*/*" ||
actualContentType === expectedContentType; if (Array.isArray(expectedContentType)) {
matchesAccepts =
expectedContentType.includes("*/*") ||
expectedContentType.includes(
actualContentType || "",
);
} else {
matchesAccepts =
expectedContentType === "*/*" ||
actualContentType === expectedContentType;
}
if (!matchesAccepts) { if (!matchesAccepts) {
response = Response.json( response = Response.json(
{ {
success: false, success: false,
code: 406, 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 }, { status: 406 },
); );
} else { } else {
request.params = params;
request.query = query;
response = await routeModule.handler( response = await routeModule.handler(
request, request,
server,
requestBody, requestBody,
query, server,
params,
); );
if (routeModule.routeDef.returns !== "*/*") { if (routeModule.routeDef.returns !== "*/*") {

9
types/bun.d.ts vendored
View file

@ -1,9 +1,14 @@
import type { Server } from "bun"; import type { Server } from "bun";
type Query = Record<string, string>;
type Params = Record<string, string>;
declare global { declare global {
type BunServer = Server; type BunServer = Server;
type ExtendedRequest = Request & { interface ExtendedRequest extends Request {
startPerf: number; startPerf: number;
}; query: Query;
params: Params;
}
} }

11
types/routes.d.ts vendored
View file

@ -1,20 +1,15 @@
type RouteDef = { type RouteDef = {
method: string; method: string | string[];
accepts: string | null; accepts: string | null | string[];
returns: string; returns: string;
needsBody?: "multipart" | "json"; needsBody?: "multipart" | "json";
}; };
type Query = Record<string, string>;
type Params = Record<string, string>;
type RouteModule = { type RouteModule = {
handler: ( handler: (
request: Request, request: Request,
server: BunServer,
requestBody: unknown, requestBody: unknown,
query: Query, server: BunServer,
params: Params,
) => Promise<Response> | Response; ) => Promise<Response> | Response;
routeDef: RouteDef; routeDef: RouteDef;
}; };