From 2805448950fb7560aa49e5bb6260f5d607c8d1bb Mon Sep 17 00:00:00 2001 From: Enubia Date: Mon, 7 Jun 2021 09:37:50 +0200 Subject: [PATCH] fix type mappings and review comments --- src/engine/action/Action.ts | 16 ++++++++-------- src/engine/datatype/ObjectDataType.ts | 2 +- src/graphqlProtocol/action.ts | 10 +++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/engine/action/Action.ts b/src/engine/action/Action.ts index 49700b00..daed9573 100644 --- a/src/engine/action/Action.ts +++ b/src/engine/action/Action.ts @@ -49,7 +49,7 @@ export type ActionSetup = { output?: any; resolve: ActionResolver; type?: string; - permissions?: Function | Permission | Permission[]; + permissions?: ((...args) => Permission | Permission[]) | Permission | Permission[]; // preProcessor?: Function; // postProcessor?: Function; preProcessor?: ActionPreProcessor; @@ -70,7 +70,7 @@ export class Action { resolve: ActionResolver; type: string; permissions: Permission | Permission[]; - private _permissions: Function | Permission | Permission[]; + private readonly _permissions: ((...args) => Permission | Permission[]) | Permission | Permission[]; private _defaultPermissions: Permission | Permission[]; descriptionPermissions: string | boolean; preProcessor?: ActionPreProcessor; @@ -152,7 +152,7 @@ export class Action { } if (isFunction(this.input)) { - const inputFn = this.input as Function; + const inputFn = this.input as (...args) => any; this.input = inputFn(); passOrThrow( @@ -198,7 +198,7 @@ export class Action { } if (isFunction(this.output)) { - const outputFn = this.output as Function; + const outputFn = this.output as (...args) => any; this.output = outputFn(); passOrThrow( @@ -237,8 +237,8 @@ export class Action { _processPermissions(): null | Permission | Permission[] { if (this._permissions) { if (isFunction(this._permissions)) { - const permissionsFn = this._permissions as Function; - const permissions: Permission | Permission[] = permissionsFn(); + const permissionsFn = this._permissions as (...args) => Permission | Permission[]; + const permissions = permissionsFn(); return processActionPermissions(this, permissions); } const permissions = this._permissions as Permission | Permission[]; @@ -254,7 +254,7 @@ export class Action { if (this.permissions) { let permissions: Permission | Permission[]; if (isFunction(this._permissions)) { - const permissionsFn = this._permissions as Function; + const permissionsFn = this._permissions as (...args) => Permission | Permission[]; permissions = permissionsFn(); } else { permissions = this._permissions as Permission | Permission[]; @@ -268,7 +268,7 @@ export class Action { this._defaultPermissions = defaultPermissions; } - getPermissions(): Permission | Permission[] { + getPermissions(): ((...args) => Permission | Permission[]) | Permission | Permission[] { if ((!this._permissions && !this._defaultPermissions) || this.permissions) { return this.permissions; } diff --git a/src/engine/datatype/ObjectDataType.ts b/src/engine/datatype/ObjectDataType.ts index bd2e5bf8..ae826dbd 100644 --- a/src/engine/datatype/ObjectDataType.ts +++ b/src/engine/datatype/ObjectDataType.ts @@ -151,7 +151,7 @@ export class ObjectDataType extends ComplexDataType { return resultAttributes; } - validate: DataTypeValidateType = (value) => { + validate: DataTypeValidateType = ({ value }): void => { if (value) { if (!isMap(value)) { throw new Error(`Object data type '${this.name}' expects an object`); diff --git a/src/graphqlProtocol/action.ts b/src/graphqlProtocol/action.ts index 1f9185c6..15ea348f 100644 --- a/src/graphqlProtocol/action.ts +++ b/src/graphqlProtocol/action.ts @@ -18,6 +18,7 @@ import { buildActionPermissionFilter } from '../engine/permission/Permission'; import { CustomError } from '../engine/CustomError'; import { Attribute } from '../engine/attribute/Attribute'; import { GraphRegistryType } from './graphRegistry'; +import { isFunction } from '../index'; const AccessDeniedError = new CustomError( 'Access denied', @@ -95,11 +96,18 @@ export const handlePermission = async ( const { userId, userRoles } = context; + let _permission; + if (!isFunction(permission)) { + _permission = () => permission; + } else { + _permission = permission; + } + const { where: permissionWhere, lookupPermissionEntity, } = await buildActionPermissionFilter( - () => permission, + _permission, userId, userRoles, action,