Skip to content

Commit

Permalink
Add req.path to KyukoRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
rikilele committed Jul 21, 2021
1 parent 2a3298d commit 5aad64c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const app = new Kyuko();
*/
let id = 0;
app.use((req, _res, defer) => {
const unique = `${id++} ${new URL(req.url).pathname}`;
const unique = `${id++} ${req.path}`;
console.time(unique);
defer(() => {
console.timeEnd(unique);
Expand Down
3 changes: 3 additions & 0 deletions src/Kyuko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ export class Kyuko {
req.query.append(key, value);
});

// Fill req.path
req.path = RoutePathHandler.splitPathSegments(pathname).join("/");

this.invokeHandlers(req, res, routeHandler);
}

Expand Down
8 changes: 8 additions & 0 deletions src/KyukoRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface KyukoRequest extends Request {
* Note that a single key may map to multiple different values.
*/
query: URLSearchParams;

/**
* Stores the sanitized path of the request, where leading and trailing
* slashes are stripped off accordingly.
*/
path: string;
}

/**
Expand All @@ -30,6 +36,7 @@ export interface KyukoRequest extends Request {
export class KyukoRequestImpl extends Request implements KyukoRequest {
params: { [key: string]: string };
query: URLSearchParams;
path: string;

/**
* Instantiates a `KyukoRequest` based on the original `fetchEvent` request.
Expand All @@ -39,5 +46,6 @@ export class KyukoRequestImpl extends Request implements KyukoRequest {
super(fetchEvent.request);
this.params = {};
this.query = new URLSearchParams();
this.path = "/";
}
}

0 comments on commit 5aad64c

Please sign in to comment.