change HTTPMethod to enum

This commit is contained in:
HorizonCode 2023-05-12 13:55:26 +02:00
parent ca6d23f5ef
commit ec0df0de60

19
mod.ts
View File

@ -11,7 +11,14 @@ type ListenOptions = {
staticLocalDir?: string; staticLocalDir?: string;
staticServePath?: string; staticServePath?: string;
}; };
type HTTPMethod = "GET" | "POST" | "PUSH" | "DELETE";
enum HTTPMethod {
GET = "GET",
POST = "POST",
PUSH = "PUSH",
DELETE = "DELETE",
}
type RouteHandler = ( type RouteHandler = (
req: RouteRequest, req: RouteRequest,
rep: RouteReply, rep: RouteReply,
@ -267,22 +274,22 @@ export class HTTPServer {
} }
get(path: string, handler: RouteHandler) { get(path: string, handler: RouteHandler) {
this.add("GET", path, handler); this.add(HTTPMethod.GET, path, handler);
} }
post(path: string, handler: RouteHandler) { post(path: string, handler: RouteHandler) {
this.add("POST", path, handler); this.add(HTTPMethod.POST, path, handler);
} }
push(path: string, handler: RouteHandler) { push(path: string, handler: RouteHandler) {
this.add("PUSH", path, handler); this.add(HTTPMethod.PUSH, path, handler);
} }
delete(path: string, handler: RouteHandler) { delete(path: string, handler: RouteHandler) {
this.add("DELETE", path, handler); this.add(HTTPMethod.DELETE, path, handler);
} }
add(method: HTTPMethod, path: string, handler: RouteHandler) { private add(method: HTTPMethod, path: string, handler: RouteHandler) {
const route = new Route(path, method, handler); const route = new Route(path, method, handler);
if (this.routes.has(route.routeName)) { if (this.routes.has(route.routeName)) {
console.log(`${route.routeName} already registered!`); console.log(`${route.routeName} already registered!`);