Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: use request wrapper for erequest #60

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/lib/routing/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import { appendFn, setFn } from "../common";
import { CookieMap } from "./cookie-map";
import { EConfig } from "..";

export function wrapERequest (req: Request, event: FetchEvent, config: EConfig) {
const request: ERequest = req as any;
Object.setPrototypeOf(request, ERequest.prototype);

request.set = setFn(request);
request.append = appendFn(request);

request.params = {};
(request as any).waitUntil = event.waitUntil.bind(event);
(request as any).clientInfo = event.client;
request.urlObj = new URL(request.url);
request.query = request.urlObj.searchParams;

Object.defineProperty(request, "url", {
get() {
return request.urlObj.toString();
},
});

// Parse cookies.
if (config.parseCookie) {
request.cookies = new CookieMap(request.headers);
}
return request;
}

export class ERequest extends Request {
readonly clientInfo: ClientInfo;
readonly waitUntil: (promise: Promise<any>) => void;
Expand All @@ -15,25 +41,11 @@ export class ERequest extends Request {
private readonly event: FetchEvent,
) {
super(event.request);
this.waitUntil = event.waitUntil.bind(event);
this.clientInfo = this.event.client;
this.urlObj = new URL(this.url);
this.query = this.urlObj.searchParams;

Object.defineProperty(this, "url", {
get() {
return this.urlObj.toString();
},
});

// Parse cookies.
if (this.config.parseCookie) {
this.cookies = new CookieMap(this.headers);
}
wrapERequest(this, event, config);
}

set = setFn(this);
append = appendFn(this);
set: ReturnType<typeof setFn>;
append: ReturnType<typeof appendFn>;

// Express-like URL helpers.
public get path(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/routing/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EConfig, Method } from ".";
import { RequestHandler, RequestHandlerCallback } from "./request-handler";
import { ErrorMiddleware, ErrorMiddlewareCallback } from "./error-middleware";
import { ErrorNotFound, ErrorMethodNotAllowed, EErr } from "./errors";
import { ERequest, EReq } from "./request";
import { wrapERequest, EReq } from "./request";
import { EResponse, ERes } from "./response";

const pathMatcherCache: Map<string, Function> = new Map();
Expand Down Expand Up @@ -39,7 +39,7 @@ export class Router<
}

private async handler(event: FetchEvent): Promise<Response> {
const req = new ERequest(this.config, event);
const req = wrapERequest(event.request, event, this.config);
const res = new EResponse(this.config);
try {
// Run middleware and request handler stack.
Expand Down
Loading