diff --git a/__tests__/router.test.ts b/__tests__/router.test.ts index 4ce535a..14c885d 100644 --- a/__tests__/router.test.ts +++ b/__tests__/router.test.ts @@ -70,18 +70,19 @@ describe("Test the routing capabilities", () => { expect(resp.getBody().data.message).toEqual("hello james from rocket team of pokemon"); }); - it("should test path matching with predefined param", async () => { + it("should test path matching with predefined param and query params", async () => { const app = new App(); const resp = await app.consumeEvent(new Request({ headers: { "accept": "application/json" }, - path: "/hello/pokemon/cascade/james", + path: "/hello/pokemon/cascade/james?teamSize=4", method: HttpMethod.GET })); expect(resp.statusCode).toEqual(200); expect(resp.getBody().data.message).toEqual("cascade team"); + expect(resp.getBody().data.teamSize).toEqual(4) }); it("should test handler with body", async () => { diff --git a/example/app.ts b/example/app.ts index d9ceb00..a2f9884 100644 --- a/example/app.ts +++ b/example/app.ts @@ -200,29 +200,33 @@ export class App extends LambdaRouter { schema: { type: "string" } - }, { - description: "The user team", + },{ + description: "The user name", in: "path", - name: "team", + name: "name", required: true, schema: { type: "string" } }, { - description: "The user name", - in: "path", - name: "name", + description: "The team member size", + in: "query", + name: "teamSize", required: true, schema: { - type: "string" + type: "integer" } }] }) - async sayHelloFooNameSimilarHandler(_: Request): Promise> { + async sayHelloFooNameSimilarHandler(r: Request): Promise> { + console.log("Query params::", r.queryParams); + const data = { + message: `cascade team`, + teamSize: Number(r.queryParams?.teamSize) + }; + console.log("Response data::", data); return new Response().setBody({ - data: { - message: `cascade team` - } + data }); } diff --git a/example/model/model/hello-data.ts b/example/model/model/hello-data.ts index 235b669..5797e9d 100644 --- a/example/model/model/hello-data.ts +++ b/example/model/model/hello-data.ts @@ -2,7 +2,14 @@ import { DocProperty } from "../../../src"; export class HelloData { @DocProperty({ - type: "string" + type: "string", + isRequired: true }) message: string; + + @DocProperty({ + type: "number", + isRequired: false + }) + teamSize?: number; } diff --git a/package.json b/package.json index ff6444d..e3da067 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typescript-openapi-router", - "version": "2.0.4", + "version": "2.0.5", "description": "Multi purpose yet simple nodejs router with OpenAPI 3 compatibility for REST APIs", "author": { "name": "Vali Draganescu", diff --git a/src/router/request.ts b/src/router/request.ts index 67c8d4f..a20ae38 100644 --- a/src/router/request.ts +++ b/src/router/request.ts @@ -18,6 +18,7 @@ import { HttpMethod } from ".."; import { IPathParams } from "./path-params.interface"; import { IQueryParams } from "./query-param.interface"; import { IRequestOpts } from "./request-opts.interface"; +import { Logger } from "../logger"; export class Request { headers?: { [key: string]: string }; @@ -38,14 +39,18 @@ export class Request { } private getQueryParams(): IQueryParams | null { + Logger.log("Getting query params from", this.path); const paramPath = this.path.split("?")[1]; if (paramPath) { + Logger.log("Got param path::", paramPath); const params: IQueryParams = {}; const paramPairs = paramPath.split("&"); + Logger.log(`There are ${paramPairs.length} params`); for (const paramPair of paramPairs) { const paramPairComponents = paramPair.split("="); params[paramPairComponents[0]] = paramPairComponents[1]; } + Logger.log("Got query params::", params); return params; } return null; diff --git a/src/router/router.ts b/src/router/router.ts index 8c14e8e..38ec6ce 100644 --- a/src/router/router.ts +++ b/src/router/router.ts @@ -158,7 +158,7 @@ class Router { return a.split("{").length - b.split("{").length; }); pathParams = {}; - console.warn("Route keys::", JSON.stringify(routeKeys)); + Logger.log("Route keys::", JSON.stringify(routeKeys)); for (const routeKey of routeKeys) { const routeComponents = routeKey.split("/");