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"
},
"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"

View file

@ -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 =
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 !== "*/*") {

9
types/bun.d.ts vendored
View file

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

11
types/routes.d.ts vendored
View file

@ -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<string, string>;
type Params = Record<string, string>;
type RouteModule = {
handler: (
request: Request,
server: BunServer,
requestBody: unknown,
query: Query,
params: Params,
server: BunServer,
) => Promise<Response> | Response;
routeDef: RouteDef;
};