Skip to content

Commit

Permalink
feat: comman http headers enum
Browse files Browse the repository at this point in the history
  • Loading branch information
aaydin-tr committed Jul 11, 2024
1 parent 522c9ee commit 094af3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server } from "bun";
import { HttpMethod } from "./utils";
import { HTTPMethod } from "./utils";
import { parse } from "fast-querystring";
import { Routes } from "./route";

Expand Down Expand Up @@ -205,7 +205,7 @@ export class Context {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async body<T = FormData | string | ArrayBuffer>(): Promise<T | null> {
if (this.method === HttpMethod.GET || this.method === HttpMethod.HEAD)
if (this.method === HTTPMethod.GET || this.method === HTTPMethod.HEAD)
return null;
if (this.req.bodyUsed) return this._body;

Expand Down
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ServeValidator } from "./serve-validator";
import { Context } from "./context";
import { Routes } from "./route";
import {
HttpMethod,
HTTPHeaders,
HTTPMethod,
NotFound,
StatusCode,
createPath,
Expand Down Expand Up @@ -79,12 +80,12 @@ export function Serve(config: Config) {

let route = findRoute(router, url.pathname, request.method);
if (!route) {
route = findRoute(router, url.pathname, HttpMethod.ALL);
route = findRoute(router, url.pathname, HTTPMethod.ALL);
}

if (!route && request.method === HttpMethod.OPTIONS) {
if (!route && request.method === HTTPMethod.OPTIONS) {
const allowedMethods = new Set<string>();
for (const method of Object.keys(HttpMethod)) {
for (const method of Object.keys(HTTPMethod)) {
const r = findRoute(router, url.pathname, method);
if (r?.data) {
allowedMethods.add(r.data.method);
Expand All @@ -99,8 +100,8 @@ export function Serve(config: Config) {
}
}

if (!route && request.method === HttpMethod.HEAD) {
route = findRoute(router, url.pathname, HttpMethod.GET);
if (!route && request.method === HTTPMethod.HEAD) {
route = findRoute(router, url.pathname, HTTPMethod.GET);
}

let handlers: Handlers;
Expand All @@ -124,7 +125,7 @@ export function Serve(config: Config) {

const ctx = new Context(server, request, params, url, routes);
if (allowHeader) {
ctx.set("Allow", allowHeader);
ctx.set(HTTPHeaders.Allow, allowHeader);
}

if (status) {
Expand Down
38 changes: 27 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ export function createPath(str: string) {
return str;
}

export const representationHeaders = [
"Content-Type",
"Content-Encoding",
"Content-Language",
"Content-Location",
];

export enum HttpMethod {
export enum HTTPMethod {
GET = "GET",
POST = "POST",
PUT = "PUT",
Expand Down Expand Up @@ -62,6 +55,29 @@ export enum StatusCode {
SERVICE_UNAVAILABLE = 503,
}

export enum HTTPHeaders {
ContentType = "Content-Type",
ContentLength = "Content-Length",
ContentEncoding = "Content-Encoding",
Accept = "Accept",
AcceptEncoding = "Accept-Encoding",
Authorization = "Authorization",
UserAgent = "User-Agent",
Connection = "Connection",
Host = "Host",
Origin = "Origin",
Allow = "Allow",
Vary = "Vary",
AccessControlAllowOrigin = "Access-Control-Allow-Origin",
AccessControlAllowMethods = "Access-Control-Allow-Methods",
AccessControlAllowHeaders = "Access-Control-Allow-Headers",
AccessControlExposeHeaders = "Access-Control-Expose-Headers",
AccessControlAllowCredentials = "Access-Control-Allow-Credentials",
AccessControlMaxAge = "Access-Control-Max-Age",
AccessControlRequestHeaders = "Access-Control-Request-Headers",
AccessControlRequestMethod = "Access-Control-Request-Method",
}

export const resetColor = "\x1b[0m";
export const greenColor = "\x1b[32m";
export const blueColor = "\x1b[34m";
Expand All @@ -78,7 +94,7 @@ export function startupMessage(
const { version } = JSON.parse(
fs.readFileSync(import.meta.dir + "/../package.json", "utf-8")
);

const hostMsg = `${bunServe.url.toString()}`;
const handlersMsg = `Handlers ...... ${routes.length}`;
const pidMsg = `PID ...... ${process.pid}`;
Expand Down Expand Up @@ -119,15 +135,15 @@ export function startupMessage(
}

export function returnContextResponse(ctx: Context) {
if (ctx.method === HttpMethod.HEAD || ctx.method === HttpMethod.OPTIONS) {
if (ctx.method === HTTPMethod.HEAD || ctx.method === HTTPMethod.OPTIONS) {
return ctx.getResWithoutBody();
}
return ctx.res;
}

export function NotFound(ctx: Context) {
// TODO not sure about this
if (ctx.method !== HttpMethod.OPTIONS) {
if (ctx.method !== HTTPMethod.OPTIONS) {
ctx.status(StatusCode.NOT_FOUND);
}
return ctx.json({ message: "Not Found" });
Expand Down

0 comments on commit 094af3f

Please sign in to comment.