Skip to content

Commit

Permalink
Merge pull request #20 from kitesjs/refactor
Browse files Browse the repository at this point in the history
Refactor RestExtension Controller
  • Loading branch information
vunb authored Aug 15, 2019
2 parents bf42973 + c394a5a commit 6be8d77
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"lint": "tslint -p tsconfig.json -c tslint.json \"packages/**/*.ts\" -e \"*.spec.ts\"",
"format": "prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
"tsc": "tsc",
"start": "npm run start:dev",
"start:dev": "npm run todo",
"todo": "ts-node sample/01-todo-app/app.ts",
"test": "mocha -r ts-node/register packages/**/*.spec.ts --reporter spec --retries 3 --require node_modules/reflect-metadata/Reflect.js --exit",
"clean": "gulp clean:bundle",
"build": "npm run clean && gulp build",
Expand Down
6 changes: 3 additions & 3 deletions packages/rest/rest.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RestExtension implements KitesExtension {
if (parameterMetadata) {
paramList = parameterMetadata[metadata.key] || [];
}
const handler = this.handlerFactory(controllerMetadata.target, metadata.key, paramList);
const handler = this.handlerFactory(controller, metadata.key, paramList);
const routeMiddleware = this.resolveMiddleware(...metadata.middleware);
router[metadata.method](
`${controllerMetadata.path}${metadata.path}`,
Expand Down Expand Up @@ -117,7 +117,7 @@ class RestExtension implements KitesExtension {
}

private handlerFactory(
controller: any,
ctrl: any,
key: string,
parameterMetadata: ParameterMetadata[]
): express.RequestHandler {
Expand All @@ -126,7 +126,7 @@ class RestExtension implements KitesExtension {
let args = this.extractParameters(req, res, next, parameterMetadata);

// invoke controller's action
const ctrl = await this.kites.container.inject<any>(controller);
// const ctrl = await this.kites.container.inject<any>(controller);
const value = ctrl[key](...args);

if (value instanceof HttpResponseMessage) {
Expand Down
11 changes: 10 additions & 1 deletion sample/01-todo-app/todo/todo.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Inject } from '@kites/common';
import { KITES_INSTANCE, KitesInstance } from '@kites/core';
import { Controller, Get, Put, RequestParam } from '@kites/rest';
import { TodoService } from './todo.service';

@Controller('/todo')
export class TodoController {

constructor(public svTodo: TodoService) { }
constructor(
public svTodo: TodoService,
@Inject(KITES_INSTANCE) private kites: KitesInstance,
) {
kites.logger.info('Hello todo controller!!!');
kites.logger.info(svTodo.getAll());
}

@Get('/') list() {
this.kites.logger.info('get all todo!!');
return this.svTodo.getAll();
}

Expand Down

0 comments on commit 6be8d77

Please sign in to comment.