add cookiestorage, add better immuteable handing
This commit is contained in:
parent
4ed1c7c812
commit
11e7ff63f8
|
@ -3,8 +3,11 @@ import { HTTPServer } from "../http_server.ts";
|
||||||
|
|
||||||
const httpServer = new HTTPServer();
|
const httpServer = new HTTPServer();
|
||||||
httpServer.add("GET", "/", (_req, rep) => {
|
httpServer.add("GET", "/", (_req, rep) => {
|
||||||
rep.statusCode = Status.Teapot;
|
rep.status(Status.Teapot)
|
||||||
rep.addHeader("working", "true");
|
.header("working", "true")
|
||||||
|
.cookie("working", "true");
|
||||||
|
|
||||||
|
console.log(_req.cookie("working"));
|
||||||
return JSON.stringify(
|
return JSON.stringify(
|
||||||
{
|
{
|
||||||
code: Status.Teapot,
|
code: Status.Teapot,
|
||||||
|
|
|
@ -3,6 +3,8 @@ import {
|
||||||
STATUS_TEXT,
|
STATUS_TEXT,
|
||||||
} from "https://deno.land/std@0.186.0/http/http_status.ts";
|
} from "https://deno.land/std@0.186.0/http/http_status.ts";
|
||||||
import * as path from "https://deno.land/std@0.185.0/path/mod.ts";
|
import * as path from "https://deno.land/std@0.185.0/path/mod.ts";
|
||||||
|
import * as cookie from "https://deno.land/std@0.185.0/http/cookie.ts";
|
||||||
|
|
||||||
type ListenOptions = {
|
type ListenOptions = {
|
||||||
port: number;
|
port: number;
|
||||||
host?: string;
|
host?: string;
|
||||||
|
@ -11,7 +13,7 @@ type ListenOptions = {
|
||||||
};
|
};
|
||||||
type HTTPMethod = "GET" | "POST" | "PUSH" | "DELETE";
|
type HTTPMethod = "GET" | "POST" | "PUSH" | "DELETE";
|
||||||
type RouteHandler = (
|
type RouteHandler = (
|
||||||
req: Request,
|
req: RouteRequest,
|
||||||
rep: RouteReply,
|
rep: RouteReply,
|
||||||
) =>
|
) =>
|
||||||
| Promise<unknown>
|
| Promise<unknown>
|
||||||
|
@ -85,7 +87,7 @@ export class HTTPServer {
|
||||||
if (route) {
|
if (route) {
|
||||||
const routeReply: RouteReply = new RouteReply();
|
const routeReply: RouteReply = new RouteReply();
|
||||||
const handler = await route.handler(
|
const handler = await route.handler(
|
||||||
requestEvent.request,
|
new RouteRequest(requestEvent.request),
|
||||||
routeReply,
|
routeReply,
|
||||||
);
|
);
|
||||||
await requestEvent.respondWith(
|
await requestEvent.respondWith(
|
||||||
|
@ -153,14 +155,74 @@ export class Route {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class RouteRequest {
|
||||||
|
headers: Headers;
|
||||||
|
|
||||||
|
constructor(request: Request) {
|
||||||
|
this.headers = request.headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
header(name: string) {
|
||||||
|
const matchingHeader = Array.from(this.headers.keys()).find((headerName) =>
|
||||||
|
headerName === name
|
||||||
|
);
|
||||||
|
return matchingHeader ? this.headers.get(matchingHeader) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie(name: string) {
|
||||||
|
const allCookies = cookie.getCookies(this.headers);
|
||||||
|
const allCookieNames = Object.keys(allCookies);
|
||||||
|
return allCookieNames.includes(name) ? allCookies[name] : undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class RouteReply {
|
export class RouteReply {
|
||||||
headers: Headers = new Headers();
|
headers: Headers = new Headers();
|
||||||
statusCode: Status = Status.OK;
|
statusCode: Status = Status.OK;
|
||||||
|
|
||||||
addHeader(name: string, value: string) {
|
header(name: string, value: string): RouteReply {
|
||||||
this.headers.append(name, value);
|
this.headers.set(name, value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export class RouteProcessor {
|
status(code: Status): RouteReply {
|
||||||
}
|
this.statusCode = code;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
type(type: string): RouteReply {
|
||||||
|
this.header("Content-Type", type);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie(name: string, value: string, attributes?: {
|
||||||
|
expires?: Date | number;
|
||||||
|
maxAge?: number;
|
||||||
|
domain?: string;
|
||||||
|
path?: string;
|
||||||
|
secure?: boolean;
|
||||||
|
httpOnly?: boolean;
|
||||||
|
sameSite?: "Strict" | "Lax" | "None";
|
||||||
|
unparsed?: string[];
|
||||||
|
}) {
|
||||||
|
if (!value) {
|
||||||
|
cookie.deleteCookie(this.headers, name, {
|
||||||
|
domain: attributes?.domain,
|
||||||
|
path: attributes?.path,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cookie.setCookie(this.headers, {
|
||||||
|
name: name,
|
||||||
|
value: value,
|
||||||
|
expires: attributes?.expires,
|
||||||
|
maxAge: attributes?.maxAge,
|
||||||
|
domain: attributes?.domain,
|
||||||
|
path: attributes?.path,
|
||||||
|
secure: attributes?.secure,
|
||||||
|
httpOnly: attributes?.httpOnly,
|
||||||
|
sameSite: attributes?.sameSite,
|
||||||
|
unparsed: attributes?.unparsed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user