Skip to content

Commit

Permalink
let the code more beautiful without changing the performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diluka committed Jun 12, 2017
1 parent 051a824 commit 177143a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
31 changes: 15 additions & 16 deletions src/driver/express/ExpressDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,24 @@ export class ExpressDriver extends BaseDriver implements Driver {

const action: Action = {request, response, next};
const checkResult = this.authorizationChecker(action, actionMetadata.authorizedRoles);
if (isPromiseLike(checkResult)) {
checkResult.then(result => {
if (!result) {
return this.handleError((actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action) : new AccessDeniedError(action)),
actionMetadata, action);

} else {
next();
}
});
} else {
if (!checkResult) {
this.handleError((actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action) : new AccessDeniedError(action))
, actionMetadata, action);

const handleError = (result: any) => {
if (!result) {
return this.handleError((
actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action)
: new AccessDeniedError(action)),
actionMetadata, action);

} else {
next();
}
};

if (isPromiseLike(checkResult)) {
checkResult.then(result => handleError(result));
} else {
return handleError(checkResult);
}
});
}
Expand Down
32 changes: 15 additions & 17 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,23 @@ export class KoaDriver extends BaseDriver implements Driver {
getFromContainer<RoleChecker>(actionMetadata.authorizedRoles).check(action) :
this.authorizationChecker(action, actionMetadata.authorizedRoles);

if (isPromiseLike(checkResult)) {
return checkResult.then(result => {
if (!result) {
return this.handleError((actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action) : new AccessDeniedError(action))
, actionMetadata, action);

} else {
return next();
}
});
} else {
if (!checkResult) {
return this.handleError((actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action) : new AccessDeniedError(action))
, actionMetadata, action);
const handleError = (result: any) => {
if (!result) {
return this.handleError((
actionMetadata.authorizedRoles.length === 0
? new AuthorizationRequiredError(action)
: new AccessDeniedError(action)),
actionMetadata, action);

} else {
return next();
next();
}
};

if (isPromiseLike(checkResult)) {
checkResult.then(result => handleError(result));
} else {
return handleError(checkResult);
}
});
}
Expand Down

0 comments on commit 177143a

Please sign in to comment.