add queryParams

This commit is contained in:
HorizonCode 2023-05-11 07:03:10 +02:00
parent 129366c087
commit d4075a6bb5
2 changed files with 23 additions and 3 deletions

View File

@ -23,6 +23,8 @@ httpServer.add("GET", "/api/user/:userId", (req, rep) => {
rep.status(Status.Teapot)
.type("application/json");
console.log(req.queryParams);
return JSON.stringify(
{
code: Status.Teapot,

View File

@ -227,7 +227,8 @@ export class RouteRequest {
path: string;
headers: Headers;
method: string;
pathParams: { [key: string]: string | number };
queryParams: { [key: string]: string };
pathParams: { [key: string]: string };
constructor(request: Request) {
this.url = request.url;
@ -236,20 +237,37 @@ export class RouteRequest {
this.headers = request.headers;
this.method = request.method;
this.pathParams = {};
this.queryParams = this.paramsToObject(urlObj.searchParams.entries());
}
header(name: string) {
private paramsToObject(entries: IterableIterator<[string, string]>) {
const result: { [key: string]: string } = {};
for (const [key, value] of entries) {
result[key] = value;
}
return result;
}
header(name: string): unknown {
const matchingHeader = Array.from(this.headers.keys()).find((headerName) =>
headerName === name
);
return matchingHeader ? this.headers.get(matchingHeader) : undefined;
}
cookie(name: string) {
cookie(name: string): unknown {
const allCookies = cookie.getCookies(this.headers);
const allCookieNames = Object.keys(allCookies);
return allCookieNames.includes(name) ? allCookies[name] : undefined;
}
pathParam(name: string): string {
return this.pathParams[name];
}
queryParam(name: string): string {
return this.queryParams[name];
}
}
export class RouteReply {