diff --git a/src/engine/datatype/ComplexDataType.ts b/src/engine/datatype/ComplexDataType.ts index 5563bf77..9e014713 100644 --- a/src/engine/datatype/ComplexDataType.ts +++ b/src/engine/datatype/ComplexDataType.ts @@ -1,5 +1,5 @@ export class ComplexDataType {} -export const isComplexDataType = obj => { +export const isComplexDataType = (obj: any): boolean => { return obj instanceof ComplexDataType; }; diff --git a/src/engine/datatype/DataType.ts b/src/engine/datatype/DataType.ts index a38f016f..2607a396 100644 --- a/src/engine/datatype/DataType.ts +++ b/src/engine/datatype/DataType.ts @@ -25,7 +25,7 @@ export class DataType { defaultValue?: () => any; enforceIndex?: boolean; - constructor(setup: DataTypeSetup = {}) { + constructor(setup: DataTypeSetup = {} as DataTypeSetup) { const { name, description, @@ -78,17 +78,17 @@ export class DataType { } } - validate = async (value, context) => { + validate = async (value: any, context: any): Promise => { if (value && this.validator) { await this.validator(value, context); } }; - toString() { + toString(): string { return this.name; } } -export const isDataType = obj => { +export const isDataType = (obj: any): boolean => { return obj instanceof DataType; }; diff --git a/src/engine/datatype/DataTypeEnum.ts b/src/engine/datatype/DataTypeEnum.ts index 59a2a6f9..5504196b 100644 --- a/src/engine/datatype/DataTypeEnum.ts +++ b/src/engine/datatype/DataTypeEnum.ts @@ -1,7 +1,7 @@ +import { uniq } from 'lodash'; import { passOrThrow, isMap } from '../util'; import { enumValueRegex, ENUM_VALUE_PATTERN } from '../constants'; import { DataType } from './DataType'; -import * as _ from 'lodash'; export type ValueType = { [key: string]: number; @@ -16,7 +16,7 @@ export type DataTypeEnumSetupType = { export class DataTypeEnum extends DataType { values: ValueType; - constructor(setup: DataTypeEnumSetupType = {}) { + constructor(setup: DataTypeEnumSetupType = {} as DataTypeEnumSetupType) { const { name, description, values } = setup; passOrThrow( @@ -45,7 +45,7 @@ export class DataTypeEnum extends DataType { }); passOrThrow( - uniqueIds.length === _.uniq(uniqueIds).length, + uniqueIds.length === uniq(uniqueIds).length, () => `Each value defined for data type '${name}' needs to have a unique ID`, ); @@ -63,11 +63,11 @@ export class DataTypeEnum extends DataType { this.values = values; } - toString() { + toString(): string { return this.name; } } -export const isDataTypeEnum = obj => { +export const isDataTypeEnum = (obj: any): boolean => { return obj instanceof DataTypeEnum; }; diff --git a/src/engine/datatype/DataTypeState.ts b/src/engine/datatype/DataTypeState.ts index f45ff7af..c8e26edd 100644 --- a/src/engine/datatype/DataTypeState.ts +++ b/src/engine/datatype/DataTypeState.ts @@ -1,9 +1,6 @@ +import { uniq } from 'lodash'; import { passOrThrow, isMap } from '../util'; - import { stateNameRegex, STATE_NAME_PATTERN } from '../constants'; - -import * as _ from 'lodash'; - import { DataType } from './DataType'; export type StateType = { @@ -18,7 +15,7 @@ export type DataTypeStateSetupType = { export class DataTypeState extends DataType { states: StateType; - constructor(setup: DataTypeStateSetupType = {}) { + constructor(setup: DataTypeStateSetupType = {} as DataTypeStateSetupType) { const { name, description, states } = setup; passOrThrow( @@ -47,7 +44,7 @@ export class DataTypeState extends DataType { }); passOrThrow( - uniqueIds.length === _.uniq(uniqueIds).length, + uniqueIds.length === uniq(uniqueIds).length, () => `Each state defined for data type '${name}' needs to have a unique ID`, ); @@ -65,11 +62,11 @@ export class DataTypeState extends DataType { this.states = states; } - toString() { + toString(): string { return this.name; } } -export const isDataTypeState = obj => { +export const isDataTypeState = (obj: any): boolean => { return obj instanceof DataTypeState; }; diff --git a/src/engine/datatype/DataTypeUser.ts b/src/engine/datatype/DataTypeUser.ts index b6346136..58e44041 100644 --- a/src/engine/datatype/DataTypeUser.ts +++ b/src/engine/datatype/DataTypeUser.ts @@ -2,6 +2,6 @@ import { DataType } from './DataType'; export class DataTypeUser extends DataType {} -export const isDataTypeUser = obj => { +export const isDataTypeUser = (obj: any): boolean => { return obj instanceof DataTypeUser; }; diff --git a/src/engine/datatype/ListDataType.ts b/src/engine/datatype/ListDataType.ts index 29de7f52..2d1569d9 100644 --- a/src/engine/datatype/ListDataType.ts +++ b/src/engine/datatype/ListDataType.ts @@ -1,5 +1,4 @@ import { passOrThrow, isFunction, isArray } from '../util'; - import { Entity, isEntity } from '../entity/Entity'; import { DataType, isDataType, DataTypeFunction } from './DataType'; import { ComplexDataType, isComplexDataType } from './ComplexDataType'; @@ -20,7 +19,7 @@ export class ListDataType extends ComplexDataType { minItems?: number; maxItems?: number; - constructor(setup: ListDataTypeSetupType = {}) { + constructor(setup: ListDataTypeSetupType = {} as ListDataTypeSetupType) { super(); const { name, description, itemType, minItems, maxItems } = setup; @@ -78,7 +77,8 @@ export class ListDataType extends ComplexDataType { _processItemType(): DataType | ComplexDataType { if (isFunction(this.itemType)) { - const itemTypeBuilder: DataTypeFunction = this.itemType; + const itemTypeBuilder: DataTypeFunction = this + .itemType as DataTypeFunction; const itemType = itemTypeBuilder({ name: this.name, description: this.description, @@ -109,7 +109,7 @@ export class ListDataType extends ComplexDataType { return ret; } - validate = payload => { + validate = (payload: any): void => { if (payload) { passOrThrow( isArray(payload), @@ -119,32 +119,30 @@ export class ListDataType extends ComplexDataType { passOrThrow( payload.length >= this.minItems, () => - `List data type '${this.name}' requires a minimum of ${ - this.minItems - } items`, + `List data type '${this.name}' requires a minimum of ${this.minItems} items`, ); passOrThrow( this.maxItems === 0 || payload.length <= this.maxItems, () => - `List data type '${this.name}' requires a maximum of ${ - this.maxItems - } items`, + `List data type '${this.name}' requires a maximum of ${this.maxItems} items`, ); } }; - toString() { + toString(): string { return this.name; } } -export const isListDataType = obj => { +export const isListDataType = (obj: any): boolean => { return obj instanceof ListDataType; }; -export const buildListDataType = obj => { - return ({ name, description }) => +export const buildListDataType = (obj: { + itemType: Entity | ComplexDataType | DataTypeFunction; +}): Function => { + return ({ name, description }): ListDataType => new ListDataType({ description, ...obj, diff --git a/src/engine/datatype/ObjectDataType.ts b/src/engine/datatype/ObjectDataType.ts index 149ebf74..b27490dc 100644 --- a/src/engine/datatype/ObjectDataType.ts +++ b/src/engine/datatype/ObjectDataType.ts @@ -2,7 +2,11 @@ import { passOrThrow, resolveFunctionMap, isMap, isFunction } from '../util'; import { isEntity } from '../entity/Entity'; import { isDataType } from './DataType'; import { ComplexDataType, isComplexDataType } from './ComplexDataType'; -import { AttributesMap, AttributesMapGenerator } from '../attribute/Attribute'; +import { + AttributesMap, + AttributesMapGenerator, + AttributeBase, +} from '../attribute/Attribute'; export type ObjectDataTypeSetupType = { name: string; @@ -53,9 +57,13 @@ export class ObjectDataType extends ComplexDataType { return ret; } - _processAttribute(rawAttribute, attributeName) { + _processAttribute( + rawAttribute: AttributeBase, + attributeName: string, + ): AttributeBase { if (isFunction(rawAttribute.type)) { - rawAttribute.type = rawAttribute.type({ + const rawAttributeTypeFn = rawAttribute.type as Function; + rawAttribute.type = rawAttributeTypeFn({ name: attributeName, description: rawAttribute.description, }); @@ -112,7 +120,7 @@ export class ObjectDataType extends ComplexDataType { return attribute; } - _processAttributeMap() { + _processAttributeMap(): AttributesMap { // if it's a function, resolve it to get that map const attributeMap = resolveFunctionMap(this._attributesMap); @@ -140,7 +148,7 @@ export class ObjectDataType extends ComplexDataType { return resultAttributes; } - validate = value => { + validate = (value: any): void => { if (value) { passOrThrow( isMap(value), @@ -149,17 +157,19 @@ export class ObjectDataType extends ComplexDataType { } }; - toString() { + toString(): string { return this.name; } } -export const isObjectDataType = obj => { +export const isObjectDataType = (obj: any): boolean => { return obj instanceof ObjectDataType; }; -export const buildObjectDataType = obj => { - return ({ name, description }) => +export const buildObjectDataType = (obj: { + attributes: AttributesMap | AttributesMapGenerator; +}): Function => { + return ({ name, description }): ObjectDataType => new ObjectDataType({ description, ...obj,