@eang/core
Version:
eang - model driven enterprise event processing
317 lines • 9.04 kB
JavaScript
import { Obj, parseCombinedId } from './entity.js';
export class OrganizationObj extends Obj {
typeOf = 'Organization';
constructor(opts) {
super({ ...opts, typeOf: 'Organization' });
}
}
export function isOrganizationObj(obj) {
return obj.typeOf === 'Organization';
}
export class ItemObj extends Obj {
typeOf = 'Item';
constructor(opts) {
super({ ...opts, typeOf: 'Item' });
}
}
export function isItemObj(obj) {
return obj.typeOf === 'Item';
}
export class GroupObj extends Obj {
typeOf = 'Group';
constructor(opts) {
super({ ...opts, typeOf: 'Group' });
}
}
export function isGroupObj(obj) {
return obj.typeOf === 'Group';
}
export class SystemObj extends Obj {
typeOf = 'System';
constructor(opts) {
super({ ...opts, typeOf: 'System' });
}
}
export function isSystemObj(obj) {
return obj.typeOf === 'System';
}
export class FolderObj extends Obj {
typeOf = 'Folder';
constructor(opts) {
super({ ...opts, typeOf: 'Folder' });
}
}
export function isFolderObj(obj) {
return obj.typeOf === 'Folder';
}
export class PageObj extends Obj {
typeOf = 'Page';
constructor(opts) {
super({ ...opts, typeOf: 'Page' });
}
}
export function isPageObj(obj) {
return obj.typeOf === 'Page';
}
export class SecurityPolicyObj extends Obj {
typeOf = 'SecurityPolicy';
constructor(opts) {
super({ ...opts, typeOf: 'SecurityPolicy' });
}
}
export function isSecurityPolicyObj(obj) {
return obj.typeOf === 'SecurityPolicy';
}
/**
* PolicyRuleObj - A policy rule object using declarative rules.
*/
export class PolicyRuleObj extends Obj {
typeOf = 'PolicyRule';
effect;
overrideable;
applicableActions;
applicableEntityTypes;
rules;
constructor(opts) {
super({ ...opts, typeOf: 'PolicyRule' });
this.effect = opts.effect;
this.overrideable = opts.overrideable;
if (opts.applicableActions !== undefined) {
this.applicableActions = opts.applicableActions;
}
if (opts.applicableEntityTypes !== undefined) {
this.applicableEntityTypes = opts.applicableEntityTypes;
}
if (opts.rules !== undefined) {
this.rules = opts.rules;
}
}
}
export function isPolicyRuleObj(obj) {
return obj.typeOf === 'PolicyRule';
}
export class PersonObj extends Obj {
typeOf = 'Person';
username;
get displayName() {
return this.name || this.username || this.key;
}
constructor(opts) {
super({ ...opts, typeOf: 'Person' });
this.username = opts.username ?? undefined;
}
}
export function isPersonObj(obj) {
return obj.typeOf === 'Person';
}
export class ServiceAccountObj extends Obj {
typeOf = 'ServiceAccount';
username;
constructor(opts) {
super({ ...opts, typeOf: 'ServiceAccount' });
this.username = opts.username;
}
}
export function isServiceAccountObj(obj) {
return obj.typeOf === 'ServiceAccount';
}
export class EventInstanceObj extends Obj {
typeOf = 'EventInstance';
constructor(opts) {
super({ ...opts, typeOf: 'EventInstance' });
}
}
export function isEventInstanceObj(obj) {
return obj.typeOf === 'EventInstance';
}
export class ErrorEventInstanceObj extends Obj {
typeOf = 'ErrorEventInstance';
message;
code;
stack;
cause;
severity = 'error';
/** Error occurrence time is tracked via createdAt */
constructor(opts) {
super({ ...opts, typeOf: 'ErrorEventInstance' });
this.message = opts.message;
if (opts.code !== undefined) {
this.code = opts.code;
}
if (opts.stack !== undefined) {
this.stack = opts.stack;
}
if (opts.cause !== undefined) {
this.cause = opts.cause;
}
this.severity = opts.severity || 'error';
}
}
export function isErrorEventInstanceObj(obj) {
return obj.typeOf === 'ErrorEventInstance';
}
export class ProcessInstanceObj extends Obj {
typeOf = 'ProcessInstance';
startedAt;
stoppedAt;
constructor(opts) {
super({ ...opts, typeOf: 'ProcessInstance' });
if (opts.startedAt !== undefined) {
this.startedAt = opts.startedAt;
}
if (opts.stoppedAt !== undefined) {
this.stoppedAt = opts.stoppedAt;
}
}
}
export function isProcessInstanceObj(obj) {
return obj.typeOf === 'ProcessInstance';
}
export class FunctionInstanceObj extends Obj {
typeOf = 'FunctionInstance';
startedAt;
stoppedAt;
constructor(opts) {
super({ ...opts, typeOf: 'FunctionInstance' });
this.startedAt = opts.startedAt || Date.now();
if (opts.stoppedAt !== undefined) {
this.stoppedAt = opts.stoppedAt;
}
}
}
export function isFunctionInstanceObj(obj) {
return obj.typeOf === 'FunctionInstance';
}
export class FunctionObj extends Obj {
typeOf = 'Function';
/** Function timeout in milliseconds */
timeout;
/** Memory limit in MB */
memoryLimit;
/** Environment variables specific to this function */
env;
/** Key-value pairs of secrets available to this object */
secrets;
constructor(opts) {
super({ ...opts, typeOf: 'Function' });
if (opts.timeout !== undefined) {
this.timeout = opts.timeout;
}
if (opts.memoryLimit !== undefined) {
this.memoryLimit = opts.memoryLimit;
}
if (opts.env !== undefined) {
this.env = opts.env;
}
if (opts.secrets !== undefined) {
this.secrets = opts.secrets;
}
}
}
export function isFunctionObj(obj) {
return obj.typeOf === 'Function';
}
export class ServiceObj extends Obj {
typeOf = 'Service';
/** Service type (e.g., 'http', 'grpc', 'websocket') */
//type!: string
/** Service port */
//port?: number
/** Service host */
//host?: string
/** Service health check endpoint */
//healthCheckEndpoint?: string
/** Service version */
//version?: string
/** Environment variables specific to this service */
env;
/** Secrets available to this service */
secrets;
/** Service scaling configuration */
// scaling?: {
// minInstances?: number
// maxInstances?: number
// targetCPUUtilization?: number
// }
/** Service deployment configuration */
// deployment?: {
// strategy?: 'rolling' | 'recreate' | 'blue-green'
// replicas?: number
// resources?: {
// cpu?: string
// memory?: string
// }
// }
constructor(opts) {
super({ ...opts, typeOf: 'Service' });
if (opts.env !== undefined) {
this.env = opts.env;
}
if (opts.secrets !== undefined) {
this.secrets = opts.secrets;
}
}
}
export function isServiceObj(obj) {
return obj.typeOf === 'Service';
}
// Generic type guard
export function isObjOfType(obj, typeOf) {
return obj.typeOf === typeOf;
}
export const typeOfToClassMap = {
EventInstance: EventInstanceObj,
ErrorEventInstance: ErrorEventInstanceObj,
ProcessInstance: ProcessInstanceObj,
FunctionInstance: FunctionInstanceObj,
Function: FunctionObj,
Service: ServiceObj,
Item: ItemObj,
Group: GroupObj,
System: SystemObj,
Folder: FolderObj,
Page: PageObj,
SecurityPolicy: SecurityPolicyObj,
PolicyRule: PolicyRuleObj,
Organization: OrganizationObj,
Person: PersonObj,
ServiceAccount: ServiceAccountObj
};
export function registerTypeOfClass(typeOf, cls) {
;
typeOfToClassMap[typeOf] =
cls;
}
export function createObj(data, typeOf, key) {
if (data.typeOf &&
typeof data.typeOf === 'string' &&
data.key &&
typeof data.key === 'string') {
// If data already has typeOf and key, use them directly
}
else if (data.typeOf && typeof data.typeOf === 'string' && !data.key) {
// If only typeOf is provided, key will be auto-generated by the Obj constructor
// No need to set data.key here
}
else if (typeOf && typeof typeOf === 'string' && key && typeof key === 'string') {
data.typeOf = typeOf;
data.key = key;
}
else if (typeOf && typeof typeOf === 'string' && !key) {
// If only typeOf is provided as parameter, key will be auto-generated
data.typeOf = typeOf;
}
else if (data.id && typeof data.id === 'string') {
const { key, typeOf } = parseCombinedId(data.id);
data.typeOf = typeOf;
data.key = key;
}
else {
throw new Error('Either id, or typeOf (with optional key) must be provided as strings to create an object');
}
const Cls = typeOfToClassMap[data.typeOf];
if (!Cls)
throw new Error(`Unknown typeOf: ${data.typeOf}`);
return new Cls(data);
}
//# sourceMappingURL=objects.js.map