routing-controllers
Advanced tools
Comparing version 0.7.0-alpha.6 to 0.7.0-alpha.7
@@ -9,7 +9,7 @@ "use strict"; | ||
function Authorized(roleOrRoles) { | ||
return function (object, methodName) { | ||
return function (clsOrObject, method) { | ||
index_1.getMetadataArgsStorage().responseHandlers.push({ | ||
type: "authorized", | ||
target: object.constructor, | ||
method: methodName, | ||
target: method ? clsOrObject.constructor : clsOrObject, | ||
method: method, | ||
value: roleOrRoles | ||
@@ -16,0 +16,0 @@ }); |
@@ -56,2 +56,6 @@ import { ControllerMetadataArgs } from "../metadata/args/ControllerMetadataArgs"; | ||
/** | ||
* Filters response handlers by a given class. | ||
*/ | ||
findResponseHandlersWithTarget(target: Function): ResponseHandlerMetadataArgs[]; | ||
/** | ||
* Filters response handlers by a given classes. | ||
@@ -58,0 +62,0 @@ */ |
@@ -78,2 +78,10 @@ "use strict"; | ||
/** | ||
* Filters response handlers by a given class. | ||
*/ | ||
MetadataArgsStorage.prototype.findResponseHandlersWithTarget = function (target) { | ||
return this.responseHandlers.filter(function (property) { | ||
return property.target === target; | ||
}); | ||
}; | ||
/** | ||
* Filters response handlers by a given classes. | ||
@@ -80,0 +88,0 @@ */ |
@@ -36,6 +36,10 @@ import { ControllerMetadata } from "../metadata/ControllerMetadata"; | ||
/** | ||
* Creates response handler metadatas. | ||
* Creates response handler metadatas for action. | ||
*/ | ||
protected createResponseHandlers(action: ActionMetadata): ResponseHandlerMetadata[]; | ||
protected createActionResponseHandlers(action: ActionMetadata): ResponseHandlerMetadata[]; | ||
/** | ||
* Creates response handler metadatas for controller. | ||
*/ | ||
protected createControllerResponseHandlers(controller: ControllerMetadata): ResponseHandlerMetadata[]; | ||
/** | ||
* Creates use metadatas for actions. | ||
@@ -42,0 +46,0 @@ */ |
@@ -49,2 +49,3 @@ "use strict"; | ||
var controller = new ControllerMetadata_1.ControllerMetadata(controllerArgs); | ||
controller.build(_this.createControllerResponseHandlers(controller)); | ||
controller.actions = _this.createActions(controller); | ||
@@ -65,5 +66,4 @@ controller.uses = _this.createControllerUses(controller); | ||
action.params = _this.createParams(action); | ||
action.responseHandlers = _this.createResponseHandlers(action); | ||
action.uses = _this.createActionUses(action); | ||
action.build(); | ||
action.build(_this.createActionResponseHandlers(action)); | ||
return action; | ||
@@ -81,10 +81,18 @@ }); | ||
/** | ||
* Creates response handler metadatas. | ||
* Creates response handler metadatas for action. | ||
*/ | ||
MetadataBuilder.prototype.createResponseHandlers = function (action) { | ||
MetadataBuilder.prototype.createActionResponseHandlers = function (action) { | ||
return index_1.getMetadataArgsStorage() | ||
.findResponseHandlersWithTargetAndMethod(action.target, action.method) | ||
.map(function (handlerArgs) { return new ResponseHandleMetadata_1.ResponseHandlerMetadata(action, handlerArgs); }); | ||
.map(function (handlerArgs) { return new ResponseHandleMetadata_1.ResponseHandlerMetadata(handlerArgs); }); | ||
}; | ||
/** | ||
* Creates response handler metadatas for controller. | ||
*/ | ||
MetadataBuilder.prototype.createControllerResponseHandlers = function (controller) { | ||
return index_1.getMetadataArgsStorage() | ||
.findResponseHandlersWithTarget(controller.target) | ||
.map(function (handlerArgs) { return new ResponseHandleMetadata_1.ResponseHandlerMetadata(handlerArgs); }); | ||
}; | ||
/** | ||
* Creates use metadatas for actions. | ||
@@ -91,0 +99,0 @@ */ |
@@ -26,6 +26,2 @@ import { ParamMetadata } from "./ParamMetadata"; | ||
/** | ||
* Action's response handlers. | ||
*/ | ||
responseHandlers: ResponseHandlerMetadata[]; | ||
/** | ||
* Class on which's method this action is attached. | ||
@@ -113,2 +109,6 @@ */ | ||
appendParams?: (actionProperties: ActionProperties) => any[]; | ||
/** | ||
* Special function that will be called instead of orignal method of the target. | ||
*/ | ||
methodOverride?: (actionMetadata: ActionMetadata, actionProperties: ActionProperties, params: any[]) => Promise<any> | any; | ||
constructor(controllerMetadata: ControllerMetadata, args: ActionMetadataArgs); | ||
@@ -119,3 +119,3 @@ /** | ||
*/ | ||
build(): void; | ||
build(responseHandlers: ResponseHandlerMetadata[]): void; | ||
/** | ||
@@ -128,3 +128,3 @@ * Builds full action route. | ||
*/ | ||
private buildHeaders(); | ||
private buildHeaders(responseHandlers); | ||
/** | ||
@@ -131,0 +131,0 @@ * Calls action method. |
@@ -17,2 +17,3 @@ "use strict"; | ||
this.appendParams = args.appendParams; | ||
this.methodOverride = args.methodOverride; | ||
} | ||
@@ -26,10 +27,10 @@ // ------------------------------------------------------------------------- | ||
*/ | ||
ActionMetadata.prototype.build = function () { | ||
var classTransformerResponseHandler = this.responseHandlers.find(function (handler) { return handler.type === "response-class-transform-options"; }); | ||
var undefinedResultHandler = this.responseHandlers.find(function (handler) { return handler.type === "on-undefined"; }); | ||
var nullResultHandler = this.responseHandlers.find(function (handler) { return handler.type === "on-null"; }); | ||
var successCodeHandler = this.responseHandlers.find(function (handler) { return handler.type === "success-code"; }); | ||
var redirectHandler = this.responseHandlers.find(function (handler) { return handler.type === "redirect"; }); | ||
var renderedTemplateHandler = this.responseHandlers.find(function (handler) { return handler.type === "rendered-template"; }); | ||
var authorizedHandler = this.responseHandlers.find(function (handler) { return handler.type === "authorized"; }); | ||
ActionMetadata.prototype.build = function (responseHandlers) { | ||
var classTransformerResponseHandler = responseHandlers.find(function (handler) { return handler.type === "response-class-transform-options"; }); | ||
var undefinedResultHandler = responseHandlers.find(function (handler) { return handler.type === "on-undefined"; }); | ||
var nullResultHandler = responseHandlers.find(function (handler) { return handler.type === "on-null"; }); | ||
var successCodeHandler = responseHandlers.find(function (handler) { return handler.type === "success-code"; }); | ||
var redirectHandler = responseHandlers.find(function (handler) { return handler.type === "redirect"; }); | ||
var renderedTemplateHandler = responseHandlers.find(function (handler) { return handler.type === "rendered-template"; }); | ||
var authorizedHandler = responseHandlers.find(function (handler) { return handler.type === "authorized"; }); | ||
var bodyParam = this.params.find(function (param) { return param.type === "body"; }); | ||
@@ -49,4 +50,2 @@ if (classTransformerResponseHandler) | ||
this.bodyExtraOptions = bodyParam ? bodyParam.extraOptions : undefined; | ||
this.isAuthorizedUsed = !!authorizedHandler; | ||
this.authorizedRoles = authorizedHandler ? authorizedHandler.value : []; | ||
this.isBodyUsed = !!this.params.find(function (param) { return param.type === "body" || param.type === "body-param"; }); | ||
@@ -57,3 +56,5 @@ this.isFilesUsed = !!this.params.find(function (param) { return param.type === "files"; }); | ||
this.fullRoute = this.buildFullRoute(); | ||
this.headers = this.buildHeaders(); | ||
this.headers = this.buildHeaders(responseHandlers); | ||
this.isAuthorizedUsed = this.controllerMetadata.isAuthorizedUsed || !!authorizedHandler; | ||
this.authorizedRoles = this.controllerMetadata.authorizedRoles.concat(authorizedHandler ? authorizedHandler.value : []); | ||
}; | ||
@@ -83,5 +84,5 @@ // ------------------------------------------------------------------------- | ||
*/ | ||
ActionMetadata.prototype.buildHeaders = function () { | ||
var contentTypeHandler = this.responseHandlers.find(function (handler) { return handler.type === "content-type"; }); | ||
var locationHandler = this.responseHandlers.find(function (handler) { return handler.type === "location"; }); | ||
ActionMetadata.prototype.buildHeaders = function (responseHandlers) { | ||
var contentTypeHandler = responseHandlers.find(function (handler) { return handler.type === "content-type"; }); | ||
var locationHandler = responseHandlers.find(function (handler) { return handler.type === "location"; }); | ||
var headers = {}; | ||
@@ -92,3 +93,3 @@ if (locationHandler) | ||
headers["Content-type"] = contentTypeHandler.value; | ||
var headerHandlers = this.responseHandlers.filter(function (handler) { return handler.type === "header"; }); | ||
var headerHandlers = responseHandlers.filter(function (handler) { return handler.type === "header"; }); | ||
if (headerHandlers) | ||
@@ -95,0 +96,0 @@ headerHandlers.map(function (handler) { return headers[handler.value] = handler.secondaryValue; }); |
import { ActionType } from "../types/ActionType"; | ||
import { ActionProperties } from "../../ActionProperties"; | ||
import { ActionMetadata } from "../ActionMetadata"; | ||
/** | ||
@@ -28,2 +29,6 @@ * Action metadata used to storage information about registered action. | ||
appendParams?: (actionProperties: ActionProperties) => any[]; | ||
/** | ||
* Special function that will be called instead of orignal method of the target. | ||
*/ | ||
methodOverride?: (actionMetadata: ActionMetadata, actionProperties: ActionProperties, params: any[]) => Promise<any> | any; | ||
} |
import { ActionMetadata } from "./ActionMetadata"; | ||
import { ControllerMetadataArgs } from "./args/ControllerMetadataArgs"; | ||
import { UseMetadata } from "./UseMetadata"; | ||
import { ResponseHandlerMetadata } from "./ResponseHandleMetadata"; | ||
/** | ||
@@ -28,2 +29,10 @@ * Controller metadata. | ||
uses: UseMetadata[]; | ||
/** | ||
* Indicates if this action uses Authorized decorator. | ||
*/ | ||
isAuthorizedUsed: boolean; | ||
/** | ||
* Roles set by @Authorized decorator. | ||
*/ | ||
authorizedRoles: any[]; | ||
constructor(args: ControllerMetadataArgs); | ||
@@ -34,2 +43,7 @@ /** | ||
readonly instance: any; | ||
/** | ||
* Builds everything controller metadata needs. | ||
* Controller metadata should be used only after its build. | ||
*/ | ||
build(responseHandlers: ResponseHandlerMetadata[]): void; | ||
} |
@@ -29,2 +29,14 @@ "use strict"; | ||
}); | ||
// ------------------------------------------------------------------------- | ||
// Public Methods | ||
// ------------------------------------------------------------------------- | ||
/** | ||
* Builds everything controller metadata needs. | ||
* Controller metadata should be used only after its build. | ||
*/ | ||
ControllerMetadata.prototype.build = function (responseHandlers) { | ||
var authorizedHandler = responseHandlers.find(function (handler) { return handler.type === "authorized" && !handler.method; }); | ||
this.isAuthorizedUsed = !!authorizedHandler; | ||
this.authorizedRoles = authorizedHandler ? authorizedHandler.value : []; | ||
}; | ||
return ControllerMetadata; | ||
@@ -31,0 +43,0 @@ }()); |
@@ -1,2 +0,1 @@ | ||
import { ActionMetadata } from "./ActionMetadata"; | ||
import { ResponseHandlerMetadataArgs } from "./args/ResponseHandleMetadataArgs"; | ||
@@ -9,6 +8,2 @@ import { ResponseHandlerType } from "./types/ResponseHandlerType"; | ||
/** | ||
* Response handler's action. | ||
*/ | ||
actionMetadata: ActionMetadata; | ||
/** | ||
* Class on which's method decorator is set. | ||
@@ -33,3 +28,3 @@ */ | ||
secondaryValue: any; | ||
constructor(actionMetadata: ActionMetadata, args: ResponseHandlerMetadataArgs); | ||
constructor(args: ResponseHandlerMetadataArgs); | ||
} |
@@ -10,4 +10,3 @@ "use strict"; | ||
// ------------------------------------------------------------------------- | ||
function ResponseHandlerMetadata(actionMetadata, args) { | ||
this.actionMetadata = actionMetadata; | ||
function ResponseHandlerMetadata(args) { | ||
this.target = args.target; | ||
@@ -14,0 +13,0 @@ this.method = args.method; |
{ | ||
"name": "routing-controllers", | ||
"private": false, | ||
"version": "0.7.0-alpha.6", | ||
"version": "0.7.0-alpha.7", | ||
"description": "Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage for Express / Koa using TypeScript.", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -73,3 +73,3 @@ "use strict"; | ||
var allParams = action.appendParams ? action.appendParams(actionProperties).concat(params) : params; | ||
var result = action.callMethod(allParams); | ||
var result = action.methodOverride ? action.methodOverride(action, actionProperties, allParams) : action.callMethod(allParams); | ||
return _this.handleCallMethodResult(result, action, actionProperties); | ||
@@ -76,0 +76,0 @@ }).catch(function (error) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
457102
4973