Skip to content

Commit

Permalink
Merge branch 'ivanpadavan-controller-inheritance' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
jotamorais committed May 28, 2020
2 parents b4bf77c + ddee6cd commit b34f47e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
11 changes: 11 additions & 0 deletions sample/sample17-controllers-inheritance/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "reflect-metadata";
import * as express from "express";
import {useExpressServer} from "../../src/index";

let app = express(); // create express server
useExpressServer(app, {
controllers: [__dirname + "/controllers/*{.js,.ts}"] // register controllers routes in our express app
});
app.listen(3001); // run express app

console.log("Express server is running on port 3001. Open http://localhost:3001/blogs/ or http://localhost:3002/posts/");
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {Request} from "express";
import {Get} from "../../../src/decorator/Get";
import {Req} from "../../../src/index";
import {Post} from "../../../src/decorator/Post";
import {Put} from "../../../src/decorator/Put";
import {Patch} from "../../../src/decorator/Patch";
import {Delete} from "../../../src/decorator/Delete";

export class BaseControllerClass {
name: string;
constructor(name: string) {
this.name = name;
}

@Get()
getAll() {
return [
{id: 1, name: `First ${this.name}!`},
{id: 2, name: `Second ${this.name}!`}
];
}

@Get("/:id")
getOne() {
return {id: 1, name: `First ${this.name}!`};
}

@Post("")
post(@Req() request: Request) {
let entity = JSON.stringify(request.body);
return `${this.name} ${entity} !saved!`;
}

@Put("/:id")
put(@Req() request: Request) {
return `${this.name} # ${request.params.id} has been putted!`;
}

@Patch("/:id")
patch(@Req() request: Request) {
return `${this.name} # ${request.params.id} has been patched!`;
}

@Delete("/:id")
remove(@Req() request: Request) {
return `${this.name} # ${request.params.id} has been removed!`;
}
}

export function BaseController(name: string): { new(): BaseControllerClass } {
return class extends BaseControllerClass {
constructor() {
super(name);
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {JsonController} from "../../../src/decorator/JsonController";
import {BaseController, BaseControllerClass} from "./BaseController";
import {Get} from "../../../src/decorator/Get";

@JsonController("/blogs")
export class BlogController extends BaseController("blog") {
constructor() {
super();
}

@Get()
getAll() {
return [
{id: 1, name: `First ${this.name}!`},
{id: 2, name: `Second ${this.name}!`},
{id: 3, name: `Third ${this.name}!`}
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {JsonController} from "../../../src/decorator/JsonController";
import {BaseController, BaseControllerClass} from "./BaseController";

@JsonController("/posts")
export class PostController extends BaseController("post") {}
18 changes: 16 additions & 2 deletions src/metadata-builder/MetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ResponseHandlerMetadata} from "../metadata/ResponseHandleMetadata";
import { RoutingControllersOptions } from "../RoutingControllersOptions";
import {UseMetadata} from "../metadata/UseMetadata";
import {getMetadataArgsStorage} from "../index";
import {ActionMetadataArgs} from "../metadata/args/ActionMetadataArgs";

/**
* Builds metadata from the given metadata arguments.
Expand Down Expand Up @@ -84,8 +85,21 @@ export class MetadataBuilder {
* Creates action metadatas.
*/
protected createActions(controller: ControllerMetadata): ActionMetadata[] {
return getMetadataArgsStorage()
.filterActionsWithTarget(controller.target)
let target = controller.target;
let actionsWithTarget: ActionMetadataArgs[] = [];
while (target) {
actionsWithTarget.push(
...getMetadataArgsStorage()
.filterActionsWithTarget(target)
.filter(action => {
return actionsWithTarget
.map(a => a.method)
.indexOf(action.method) === -1;
})
);
target = Object.getPrototypeOf(target);
}
return actionsWithTarget
.map(actionArgs => {
const action = new ActionMetadata(controller, actionArgs, this.options);
action.options = {...controller.options, ...actionArgs.options};
Expand Down

0 comments on commit b34f47e

Please sign in to comment.