Node.js
Express
routing-controllers

(作業中) RoutingControllersのCurrentUserをテンプレートでレンダリングできるようにしたい

@CurrentUserデコレータの実装を確認する

https://github.com/typestack/routing-controllers/blob/4a56d176db77bc081dfcd3d8550e8433b5bc476e/src/ActionParameterHandler.ts


ActionParameterHandler.ts

    protected handleValue(value: any, action: Action, param: ParamMetadata): Promise<any>|any {

:
// if its current-user decorator then get its value
if (param.type === "current-user") {
if (!this.driver.currentUserChecker)
throw new CurrentUserCheckerNotDefinedError();

value = this.driver.currentUserChecker(action);
}
if (param.required) {
const isValueEmpty = value === null || value === undefined || value === "";
const isValueEmptyObject = value instanceof Object && Object.keys(value).length === 0;
:
} else if (param.type === "current-user") { // current user has a special check as well

if (isPromiseLike(value)) {
return value.then(currentUser => {
if (!currentUser)
return Promise.reject(new AuthorizationRequiredError(action));

return currentUser;
});

} else {
if (!value)
return Promise.reject(new AuthorizationRequiredError(action));
}

}
:
}

return value;



ActionParameterHandler.ts

    handle(action: Action, param: ParamMetadata): Promise<any>|any {

if (param.type === "request")
return action.request;

if (param.type === "response")
return action.response;

if (param.type === "context")
return action.context;

// get parameter value from request and normalize it
const value = this.normalizeParamValue(this.driver.getParamFromRequest(action, param), param);
if (isPromiseLike(value))
return value.then(value => this.handleValue(value, action, param));

return this.handleValue(value, action, param);
}



RoutingController.ts

    protected executeAction(actionMetadata: ActionMetadata, action: Action, interceptorFns: Function[]) {

// compute all parameters
const paramsPromises = actionMetadata.params
.sort((param1, param2) => param1.index - param2.index)
.map(param => this.parameterHandler.handle(action, param));

// after all parameters are computed
return Promise.all(paramsPromises).then(params => {

// execute action and handle result
const allParams = actionMetadata.appendParams ? actionMetadata.appendParams(action).concat(params) : params;
const result = actionMetadata.methodOverride ? actionMetadata.methodOverride(actionMetadata, action, allParams) : actionMetadata.callMethod(allParams);
return this.handleCallMethodResult(result, actionMetadata, action, interceptorFns);

}).catch(error => {

// otherwise simply handle error without action execution
return this.driver.handleError(error, actionMetadata, action);
});
}


このallParamsがどこかに引き渡されてたらいい

ただ上記のexecuteActionを見る限りactionMetadata.methodOverrideactionMetadata.callMethodにしか渡されていない

const result = actionMetadata.methodOverride 

? actionMetadata.methodOverride(actionMetadata, action, allParams)
: actionMetadata.callMethod(allParams);

方針をかえる


ActionParameterHandler.ts

    protected handleValue(value: any, action: Action, param: ParamMetadata): Promise<any>|any {

:
this.driver.currentUserChecker(action);

ということは

currentUserCheckerから呼び出されるフック内でrequestに書き込むという方針はいかがであろうか?


作業中