Skip to content

Commit

Permalink
Fixed issues when no pram metadata is available (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen authored Dec 7, 2017
1 parent c610ba7 commit 9dc3948
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inversify-express-utils",
"version": "5.1.1",
"version": "5.1.2",
"description": "Some utilities for the development of express applications with Inversify",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
67 changes: 35 additions & 32 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,43 @@ export function getRouteInfo(container: inversifyInterfaces.Container) {
const method = m.method.toUpperCase();
const controllerPath = r.controllerMetadata.path;
const actionPath = m.path;
const paramMetadata = r.parameterMetadata[m.key] || undefined;
const paramMetadata = r.parameterMetadata;
let args: string[] | undefined = undefined;

if (paramMetadata) {
args = (r.parameterMetadata[m.key] || []).map(a => {
let type = "";
switch (a.type) {
case PARAMETER_TYPE.RESPONSE:
type = "@response";
break;
case PARAMETER_TYPE.REQUEST:
type = "@request";
break;
case PARAMETER_TYPE.NEXT:
type = "@next";
break;
case PARAMETER_TYPE.PARAMS:
type = "@requestParam";
break;
case PARAMETER_TYPE.QUERY:
type = "queryParam";
break;
case PARAMETER_TYPE.BODY:
type = "@requestBody";
break;
case PARAMETER_TYPE.HEADERS:
type = "@requestHeaders";
break;
case PARAMETER_TYPE.COOKIES:
type = "@cookies";
break;
}
return `${type} ${a.parameterName}`;
});
if (paramMetadata !== undefined ) {
const paramMetadataForKey = paramMetadata[m.key] || undefined;
if (paramMetadataForKey) {
args = (r.parameterMetadata[m.key] || []).map(a => {
let type = "";
switch (a.type) {
case PARAMETER_TYPE.RESPONSE:
type = "@response";
break;
case PARAMETER_TYPE.REQUEST:
type = "@request";
break;
case PARAMETER_TYPE.NEXT:
type = "@next";
break;
case PARAMETER_TYPE.PARAMS:
type = "@requestParam";
break;
case PARAMETER_TYPE.QUERY:
type = "queryParam";
break;
case PARAMETER_TYPE.BODY:
type = "@requestBody";
break;
case PARAMETER_TYPE.HEADERS:
type = "@requestHeaders";
break;
case PARAMETER_TYPE.COOKIES:
type = "@cookies";
break;
}
return `${type} ${a.parameterName}`;
});
}
}

const details = {
Expand Down
39 changes: 39 additions & 0 deletions test/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,43 @@ describe("Debug utils", () => {

});

it("should be able to handle missig parameter metadata", () => {

let container = new Container();

@controller("/api/order")
class OrderController extends BaseHttpController {
@httpGet("/")
public get() {
return {};
}
@httpPost("/")
public post() {
return {};
}
}

const TYPES = {
OrderController: OrderController.name
};

let server = new InversifyExpressServer(container);
let app = server.build();

const routeInfo = getRouteInfo(container);

console.log(
prettyjson.render({ CONTROLLERS: routeInfo })
);

console.log(routeInfo);

expect(routeInfo[0].controller).to.eq(TYPES.OrderController);
expect(routeInfo[0].endpoints[0].route).to.eq("GET /api/order/");
expect(routeInfo[0].endpoints[0].args).to.eq(undefined);
expect(routeInfo[0].endpoints[1].route).to.eq("POST /api/order/");
expect(routeInfo[0].endpoints[1].args).to.eq(undefined);

});

});

0 comments on commit 9dc3948

Please sign in to comment.