import { clone , isPlainObject } from ".." ; import { AnnotationStatus , AnnotationType } from "./annotation" ; import { IoSourceBackpressureStrategy } from "./io-point" ; import { LogLevel } from "./log" ; import { CoatyObject } from "./object" ; import { TaskStatus } from "./task" ; /**
 * Type alias for all framework core types. This type is used for the
 * coreType property of objects based on the CoatyObject interface.
 */ export type CoreType = "CoatyObject" | "User" | "Annotation" | "Task" | "IoContext" | "IoNode" | "IoSource" | "IoActor" | "Identity" | "Log" | "Location" | "Snapshot" ; /**
 * Provides utility methods and constants for core object types.
 */ export class CoreTypes { static readonly OBJECT_TYPE_PREFIX = "coaty." ; static readonly OBJECT_TYPE_OBJECT = CoreTypes . OBJECT_TYPE_PREFIX + "CoatyObject" ; static readonly OBJECT_TYPE_USER = CoreTypes . OBJECT_TYPE_PREFIX + "User" ; static readonly OBJECT_TYPE_ANNOTATION = CoreTypes . OBJECT_TYPE_PREFIX + "Annotation" ; static readonly OBJECT_TYPE_TASK = CoreTypes . OBJECT_TYPE_PREFIX + "Task" ; static readonly OBJECT_TYPE_IO_CONTEXT = CoreTypes . OBJECT_TYPE_PREFIX + "IoContext" ; static readonly OBJECT_TYPE_IO_NODE = CoreTypes . OBJECT_TYPE_PREFIX + "IoNode" ; static readonly OBJECT_TYPE_IO_SOURCE = CoreTypes . OBJECT_TYPE_PREFIX + "IoSource" ; static readonly OBJECT_TYPE_IO_ACTOR = CoreTypes . OBJECT_TYPE_PREFIX + "IoActor" ; static readonly OBJECT_TYPE_IDENTITY = CoreTypes . OBJECT_TYPE_PREFIX + "Identity" ; static readonly OBJECT_TYPE_LOG = CoreTypes . OBJECT_TYPE_PREFIX + "Log" ; static readonly OBJECT_TYPE_LOCATION = CoreTypes . OBJECT_TYPE_PREFIX + "Location" ; static readonly OBJECT_TYPE_SNAPSHOT = CoreTypes . OBJECT_TYPE_PREFIX + "Snapshot" ; private static readonly _coreTypesMap = { CoatyObject : CoreTypes . OBJECT_TYPE_OBJECT , User : CoreTypes . OBJECT_TYPE_USER , Annotation : CoreTypes . OBJECT_TYPE_ANNOTATION , Task : CoreTypes . OBJECT_TYPE_TASK , IoContext : CoreTypes . OBJECT_TYPE_IO_CONTEXT , IoNode : CoreTypes . OBJECT_TYPE_IO_NODE , IoSource : CoreTypes . OBJECT_TYPE_IO_SOURCE , IoActor : CoreTypes . OBJECT_TYPE_IO_ACTOR , Identity : CoreTypes . OBJECT_TYPE_IDENTITY , Log : CoreTypes . OBJECT_TYPE_LOG , Location : CoreTypes . OBJECT_TYPE_LOCATION , Snapshot : CoreTypes . OBJECT_TYPE_SNAPSHOT , } ; param obj param coreType static isObject ( obj , coreType ? ) { if ( ! isPlainObject ( obj ) ) { return false ; } if ( coreType ) { return CoreTypes . _isAssignable ( obj , coreType ) ; } return typeof obj . coreType === "string" && CoreTypes . _isAssignable ( obj , obj . coreType ) ; } param obj param coreType static asObject ( obj , coreType ? ) { if ( ! isPlainObject ( obj ) ) { return undefined ; } if ( coreType ) { return CoreTypes . _assignableAs ( obj , coreType ) ; } if ( typeof obj . coreType === "string" ) { return CoreTypes . _assignableAs ( obj , obj . coreType ) ; } return undefined ; } param obj static isStringArray ( obj ) { return Array . isArray ( obj ) && ( < string [ ] > obj ) . every ( o => o && typeof o === "string" && o . length > 0 ) ; } param obj static isCoreTypeArray ( obj ) { return this . isStringArray ( obj ) && ( < string [ ] > obj ) . every ( t => ! ! CoreTypes . _coreTypesMap [ t ] ) ; } param obj param coreType static isObjectArray ( obj , coreType ? ) { const isTypesArray = Array . isArray ( coreType ) ; return Array . isArray ( obj ) && ( < any [ ] > obj ) . every ( o => { if ( isTypesArray ) { return ( < any [ ] > coreType ) . some ( t => CoreTypes . isObject ( o , t ) ) ; } return CoreTypes . isObject ( o , coreType as CoreType ) ; } ) ; } /**
     * Gets an array of all defined framework core types.
     */ static getCoreTypes ( ) { const coreTypes = [ ] ; Object . keys ( CoreTypes . _coreTypesMap ) . forEach ( t => coreTypes . push ( t ) ) ; return coreTypes ; } param coreType static getObjectTypeFor ( coreType ) { return CoreTypes . _coreTypesMap [ coreType ] ; } param objectType static getCoreTypeFor ( objectType ) { for ( const coreType in CoreTypes . _coreTypesMap ) { if ( CoreTypes . _coreTypesMap . hasOwnProperty ( coreType ) ) { if ( CoreTypes . _coreTypesMap [ coreType ] === objectType ) { return coreType ; } } } return undefined ; } param type static isCoreType ( type ) { return ! ! CoreTypes . _coreTypesMap [ type ] ; } param type static isCoreObjectType ( type ) { return Object . keys ( CoreTypes . _coreTypesMap ) . some ( coreType => CoreTypes . _coreTypesMap [ coreType ] === type ) ; } param obj static clone < T extends CoatyObject > ( obj , shouldSkipValidation = false ) { if ( obj === undefined ) { return undefined ; } if ( ! shouldSkipValidation && ! CoreTypes . isObject ( obj ) ) { throw new TypeError ( `CoreTypes.clone: argument 'obj' is not a valid CoatyObject: ${ JSON . stringify ( obj ) }` ) ; } return clone ( obj ) ; } param obj static isDate ( obj ) { return obj && ( ( obj instanceof Date && isFinite ( obj . getTime ( ) ) ) || typeof obj === "string" ) ; } param obj param coreType private static _isAssignable ( obj , coreType ) { switch ( coreType ) { case "CoatyObject" : return CoreTypes . _isObject ( obj ) ; case "User" : return CoreTypes . _isUser ( obj ) ; case "Annotation" : return CoreTypes . _isAnnotation ( obj ) ; case "Task" : return CoreTypes . _isTask ( obj ) ; case "IoContext" : return CoreTypes . _isIoContext ( obj ) ; case "IoNode" : return CoreTypes . _isIoNode ( obj ) ; case "IoSource" : return CoreTypes . _isIoSource ( obj ) ; case "IoActor" : return CoreTypes . _isIoActor ( obj ) ; case "Identity" : return CoreTypes . _isIdentity ( obj ) ; case "Log" : return CoreTypes . _isLog ( obj ) ; case "Location" : return CoreTypes . _isLocation ( obj ) ; case "Snapshot" : return CoreTypes . _isSnapshot ( obj ) ; default : return false ; } } param obj param coreType private static _assignableAs ( obj , coreType ) { if ( CoreTypes . _isAssignable ( obj , coreType ) ) { return obj as CoatyObject ; } return undefined ; } private static _isObject ( obj ) { return typeof obj . name === "string" && typeof obj . objectType === "string" && obj . objectType . length > 0 && typeof obj . coreType === "string" && CoreTypes . isCoreType ( obj . coreType ) && typeof obj . objectId === "string" && obj . objectId . length > 0 && ( obj . parentObjectId === undefined || ( typeof obj . parentObjectId === "string" && obj . parentObjectId . length > 0 ) ) && ( obj . externalId === undefined || ( typeof obj . externalId === "string" && obj . externalId . length > 0 ) ) && ( obj . locationId === undefined || ( typeof obj . locationId === "string" && obj . locationId . length > 0 ) ) && ( obj . isDeactivated === undefined || typeof obj . isDeactivated === "boolean" ) ; } private static _isUser ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "User" && CoreTypes . _isScimNames ( obj . names ) && ( obj . displayName === undefined || typeof obj . displayName === "string" ) && ( obj . nickName === undefined || typeof obj . nickName === "string" ) && ( obj . title === undefined || typeof obj . title === "string" ) && ( obj . userType === undefined || typeof obj . userType === "string" ) && ( obj . preferredLanguage === undefined || typeof obj . preferredLanguage === "string" ) && ( obj . locale === undefined || typeof obj . locale === "string" ) && ( obj . timezone === undefined || typeof obj . timezone === "string" ) && ( obj . active === undefined || typeof obj . active === "boolean" ) && ( obj . password === undefined || typeof obj . password === "string" ) && ( obj . emails === undefined || CoreTypes . _isScimMultiValuedAttributes ( obj . emails ) ) && ( obj . phoneNumbers === undefined || CoreTypes . _isScimMultiValuedAttributes ( obj . phoneNumbers ) ) && ( obj . ims === undefined || CoreTypes . _isScimMultiValuedAttributes ( obj . ims ) ) && ( obj . photos === undefined || CoreTypes . _isScimMultiValuedAttributes ( obj . photos ) ) && ( obj . addresses === undefined || CoreTypes . _isScimAddresses ( obj . addresses ) ) && ( obj . groups === undefined || CoreTypes . _isScimMultiValuedAttributes ( obj . groups ) ) && ( obj . entitlements === undefined || Array . isArray ( obj . entitlements ) ) && ( obj . roles === undefined || CoreTypes . isStringArray ( obj . roles ) ) && ( obj . x509Certificates === undefined || CoreTypes . isStringArray ( obj . x509Certificates ) ) ; } private static _isScimNames ( names ) { return isPlainObject ( names ) && ( names . formatted === undefined || ( typeof names . formatted === "string" && names . formatted . length > 0 ) ) && ( names . familyName === undefined || typeof names . familyName === "string" ) && ( names . givenName === undefined || typeof names . givenName === "string" ) && ( ( names . familyName !== undefined && names . givenName !== undefined ) || ( names . familyName === undefined && names . givenName === undefined ) ) && ( ! ( names . formatted === undefined && names . familyName === undefined ) ) && ( names . middleName === undefined || typeof names . middleName === "string" ) && ( names . honorificPrefix === undefined || typeof names . honorificPrefix === "string" ) && ( names . honorificSuffix === undefined || typeof names . honorificSuffix === "string" ) ; } private static _isScimMultiValuedAttribute ( attr ) { return isPlainObject ( attr ) && typeof attr . type === "string" && typeof attr . value === "string" && ( attr . primary === undefined || typeof attr . primary === "boolean" ) && ( attr . display === undefined || typeof attr . display === "string" ) ; } private static _isScimMultiValuedAttributes ( attrs ) { return Array . isArray ( attrs ) && ( < any [ ] > attrs ) . every ( attr => CoreTypes . _isScimMultiValuedAttribute ( attr ) ) ; } private static _isScimAddresses ( obj ) { return Array . isArray ( obj ) && ( < any [ ] > obj ) . every ( o => { return CoreTypes . _isScimMultiValuedAttribute ( o ) && ( o . formatted === undefined || typeof o . formatted === "string" ) && ( o . streetAddress === undefined || typeof o . streetAddress === "string" ) && ( o . locality === undefined || typeof o . locality === "string" ) && ( o . region === undefined || typeof o . region === "string" ) && ( o . postalCode === undefined || typeof o . postalCode === "string" ) && ( o . country === undefined || typeof o . country === "string" ) ; } ) ; } private static _isIoContext ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "IoContext" ; } private static _isIoNode ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "IoNode" && ( obj . ioSources === undefined || CoreTypes . isObjectArray ( obj . ioSources , "IoSource" ) ) && ( obj . ioActors === undefined || CoreTypes . isObjectArray ( obj . ioActors , "IoActor" ) ) && ( obj . characteristics === undefined || isPlainObject ( obj . characteristics ) ) ; } private static _isAnnotation ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "Annotation" && typeof obj . type === "number" && AnnotationType [ obj . type ] !== undefined && typeof obj . creatorId === "string" && obj . creatorId . length > 0 && typeof obj . creationTimestamp === "number" && typeof obj . status === "number" && AnnotationStatus [ obj . status ] !== undefined && ( obj . variants === undefined || ( Array . isArray ( obj . variants ) && ( < any [ ] > obj . variants ) . every ( o => isPlainObject ( o ) && ( typeof o . variant === "string" ) && o . variant . length > 0 && ( typeof o . downloadUrl === "string" ) ) ) ) ; } private static _isTask ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "Task" && typeof obj . creatorId === "string" && obj . creatorId . length > 0 && typeof obj . creationTimestamp === "number" && ( obj . startTimestamp === undefined || typeof obj . startTimestamp === "number" ) && ( obj . duration === undefined || typeof obj . duration === "number" ) && typeof obj . status === "number" && TaskStatus [ obj . status ] !== undefined && ( obj . requirements === undefined || isPlainObject ( obj . requirements ) ) && ( obj . description === undefined || typeof obj . description === "string" ) && ( obj . assigneeObjectId === undefined || ( typeof obj . assigneeObjectId === "string" && obj . assigneeObjectId . length > 0 ) ) ; } private static _isIoSource ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "IoSource" && typeof obj . valueType === "string" && obj . valueType . length > 0 && ( obj . externalRoute === undefined || typeof obj . externalRoute === "string" ) && ( obj . updateRate === undefined || ( typeof obj . updateRate === "number" && obj . updateRate >= 0 ) ) && ( obj . updateStrategy === undefined || ( typeof obj . updateStrategy === "number" && IoSourceBackpressureStrategy [ obj . updateStrategy ] !== undefined ) ) && ( obj . useRawIoValues === undefined || typeof obj . useRawIoValues === "boolean" ) ; } private static _isIoActor ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "IoActor" && typeof obj . valueType === "string" && obj . valueType . length > 0 && ( obj . externalRoute === undefined || typeof obj . externalRoute === "string" ) && ( obj . updateRate === undefined || ( typeof obj . updateRate === "number" && obj . updateRate >= 0 ) ) && ( obj . useRawIoValues === undefined || typeof obj . useRawIoValues === "boolean" ) ; } private static _isIdentity ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "Identity" ; } private static _isLog ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "Log" && typeof obj . logLevel === "number" && LogLevel [ obj . logLevel ] !== undefined && typeof obj . logMessage === "string" && typeof obj . logDate === "string" && ( obj . logTags === undefined || CoreTypes . isStringArray ( obj . logTags ) ) && ( obj . logLabels === undefined || isPlainObject ( obj . logLabels ) ) && ( obj . logHost === undefined || CoreTypes . _isLogHost ( obj . logHost ) ) ; } private static _isLogHost ( obj ) { return isPlainObject ( obj ) && ( obj . agentInfo === undefined || CoreTypes . _isAgentInfo ( obj . agentInfo ) ) && ( obj . pid === undefined || typeof obj . pid === "number" ) && ( obj . hostname === undefined || typeof obj . hostname === "string" ) && ( obj . userAgent === undefined || typeof obj . userAgent === "string" ) ; } private static _isAgentInfo ( obj ) { return isPlainObject ( obj ) && ( isPlainObject ( obj . packageInfo ) && typeof obj . packageInfo . name === "string" && typeof obj . packageInfo . version === "string" ) && ( isPlainObject ( obj . buildInfo ) && typeof obj . buildInfo . buildDate === "string" && typeof obj . buildInfo . buildMode === "string" ) && ( isPlainObject ( obj . configInfo ) && typeof obj . configInfo . serviceHost === "string" ) ; } private static _isLocation ( obj ) { return CoreTypes . _isObject ( obj ) && obj . coreType === "Location" && isPlainObject ( obj . geoLocation ) && typeof obj . geoLocation . timestamp === "number" && isPlainObject ( obj . geoLocation . coords ) && typeof obj . geoLocation . coords . latitude === "number" && typeof obj . geoLocation . coords . longitude === "number" && typeof obj . geoLocation . coords . accuracy === "number" && ( obj . geoLocation . coords . altitude === undefined || typeof obj . geoLocation . coords . altitude === "number" ) && ( obj . geoLocation . coords . altitudeAccuracy === undefined || typeof obj . geoLocation . coords . altitudeAccuracy === "number" ) && ( obj . geoLocation . coords . heading === undefined || typeof obj . geoLocation . coords . heading === "number" ) && ( obj . geoLocation . coords . speed === undefined || typeof obj . geoLocation . coords . speed === "number" ) ; } private static _isSnapshot ( obj ) { return CoreTypes . _isObject ( obj ) && ( typeof obj . parentObjectId === "string" && obj . parentObjectId . length > 0 ) && obj . coreType === "Snapshot" && obj . creationTimestamp && typeof obj . creationTimestamp === "number" && obj . creatorId && typeof obj . creatorId === "string" && obj . creatorId . length > 0 && ( obj . tags === undefined || CoreTypes . isStringArray ( obj . tags ) ) && CoreTypes . isObject ( obj . object ) ; } }