Skip to content

Commit

Permalink
fix(query params mapping): fix for query params mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ValiDraganescu committed Apr 23, 2020
1 parent 20d246b commit 8efe921
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
5 changes: 3 additions & 2 deletions __tests__/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>({
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 () => {
Expand Down
26 changes: 15 additions & 11 deletions example/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response<HelloResponse>> {
async sayHelloFooNameSimilarHandler(r: Request): Promise<Response<HelloResponse>> {
console.log("Query params::", r.queryParams);
const data = {
message: `cascade team`,
teamSize: Number(r.queryParams?.teamSize)
};
console.log("Response data::", data);
return new Response<HelloResponse>().setBody({
data: {
message: `cascade team`
}
data
});
}

Expand Down
9 changes: 8 additions & 1 deletion example/model/model/hello-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/router/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestBody = any> {
headers?: { [key: string]: string };
Expand All @@ -38,14 +39,18 @@ export class Request<RequestBody = any> {
}

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;
Expand Down
2 changes: 1 addition & 1 deletion src/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("/");

Expand Down

0 comments on commit 8efe921

Please sign in to comment.