refactor: front end code to unified design

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-28 12:21:09 -04:00
parent 08c1de9723
commit 80a8dac2fd
21 changed files with 1087 additions and 1151 deletions
+27
View File
@@ -0,0 +1,27 @@
import { isProxy, toRaw } from 'vue';
function normalizeCloneable<T>(value: T): T {
if (value === null || value === undefined) {
return value;
}
if (typeof value !== 'object') {
return value;
}
const rawValue = isProxy(value) ? toRaw(value) : value;
if (Array.isArray(rawValue)) {
return rawValue.map(item => normalizeCloneable(item)) as T;
}
const plainObject = Object.fromEntries(
Object.entries(rawValue).map(([key, nestedValue]) => [key, normalizeCloneable(nestedValue)])
);
return plainObject as T;
}
export function clonePlain<T>(value: T): T {
return structuredClone(normalizeCloneable(value));
}
+54 -63
View File
@@ -1,47 +1,52 @@
/**
* Class model for Collection Interface
* Class models for Collection interfaces
*/
import type { CollectionContentTypes, CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
import type {
CollectionContentTypes,
CollectionInterface,
CollectionModelInterface,
CollectionPropertiesInterface,
CollectionPropertiesModelInterface
} from '@/types/collection';
import type {
CollectionIdentifier,
ServiceIdentifier
} from '@/types/common';
import { clonePlain } from './clone-plain';
export class CollectionObject implements CollectionInterface {
export class CollectionObject implements CollectionModelInterface {
_data!: CollectionInterface;
private _data: CollectionInterface<CollectionPropertiesInterface>;
private _properties: CollectionPropertiesObject | undefined = undefined;
constructor() {
this._data = {
'@type': 'people:collection',
version: 1,
provider: '',
service: '',
collection: null,
identifier: '',
signature: null,
created: null,
modified: null,
properties: new CollectionPropertiesObject(),
service: '' as ServiceIdentifier,
collection: null as CollectionIdentifier | null,
identifier: '' as CollectionIdentifier,
properties: new CollectionPropertiesObject().toJson(),
};
}
fromJson(data: CollectionInterface): CollectionObject {
this._data = data;
if (data.properties) {
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
}
this._data = clonePlain(data);
this._properties = undefined;
return this;
}
toJson(): CollectionInterface {
const json = { ...this._data };
if (this._data.properties instanceof CollectionPropertiesObject) {
json.properties = this._data.properties.toJson();
}
return json;
const json = this._properties
? { ...this._data, properties: this._properties.toJson() }
: this._data;
return clonePlain(json);
}
clone(): CollectionObject {
const cloned = new CollectionObject();
cloned._data = { ...this._data };
cloned._data.properties = this.properties.clone();
return cloned;
return new CollectionObject().fromJson(this.toJson());
}
/** Immutable Properties */
@@ -50,16 +55,16 @@ export class CollectionObject implements CollectionInterface {
return this._data.provider;
}
get service(): string | number {
return this._data.service;
get service(): ServiceIdentifier {
return this._data.service as ServiceIdentifier;
}
get collection(): string | number | null {
return this._data.collection;
get collection(): CollectionIdentifier | null {
return this._data.collection as CollectionIdentifier | null;
}
get identifier(): string | number {
return this._data.identifier;
get identifier(): CollectionIdentifier {
return this._data.identifier as CollectionIdentifier;
}
get signature(): string | null | undefined {
@@ -74,37 +79,33 @@ export class CollectionObject implements CollectionInterface {
return this._data.modified;
}
/** Mutable Properties */
get properties(): CollectionPropertiesObject {
if (this._data.properties instanceof CollectionPropertiesObject) {
return this._data.properties;
if (this._properties) {
return this._properties;
}
else if (this._data.properties) {
const properties = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
this._properties = properties;
return properties;
}
if (this._data.properties) {
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
this._data.properties = hydrated;
return hydrated;
}
return new CollectionPropertiesObject();
}
set properties(value: CollectionPropertiesObject) {
if (value instanceof CollectionPropertiesObject) {
this._data.properties = value as any;
} else {
this._data.properties = value;
}
this._properties = value;
}
}
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
_data!: CollectionPropertiesInterface;
export class CollectionPropertiesObject implements CollectionPropertiesModelInterface {
private _data: CollectionPropertiesInterface;
constructor() {
this._data = {
'@type': 'chrono:collection',
version: 1,
'@type': 'chrono:calendar',
content: [],
label: '',
description: null,
@@ -115,32 +116,22 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
}
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
this._data = data;
this._data = clonePlain(data);
return this;
}
toJson(): CollectionPropertiesInterface {
return this._data;
return clonePlain(this._data);
}
clone(): CollectionPropertiesObject {
const cloned = new CollectionPropertiesObject();
cloned._data = { ...this._data };
return cloned;
return new CollectionPropertiesObject().fromJson(this.toJson());
}
/** Immutable Properties */
get '@type'(): string {
return this._data['@type'];
}
get version(): number {
return this._data.version;
}
get content(): CollectionContentTypes[] {
return this._data.content || [];
return this._data.content ?? [];
}
/** Mutable Properties */
@@ -185,4 +176,4 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
this._data.color = value;
}
}
}
+51 -55
View File
@@ -1,106 +1,102 @@
/**
* Class model for Entity Interface
*/
import type { EntityInterface } from "@/types/entity";
import type { EventInterface } from "@/types/event";
import type { TaskInterface } from "@/types/task";
import type { JournalInterface } from "@/types/journal";
import { EventObject } from "./event";
import { TaskObject } from "./task";
import { JournalObject } from "./journal";
export class EntityObject implements EntityInterface {
import type { CollectionIdentifier, EntityIdentifier } from '@/types/common';
import type { EntityInterface, EntityModelInterface, EntityPropertiesInterface } from '@/types/entity';
import type { EventInterface } from '@/types/event';
import type { JournalInterface } from '@/types/journal';
import type { TaskInterface } from '@/types/task';
import { EventObject } from './event';
import { JournalObject } from './journal';
import { TaskObject } from './task';
import { clonePlain } from './clone-plain';
_data!: EntityInterface;
export type EntityPropertiesObject = EventObject | TaskObject | JournalObject;
export class EntityObject implements EntityModelInterface {
private _data: EntityInterface<EntityPropertiesInterface>;
private _properties: EntityPropertiesObject | undefined;
constructor() {
this._data = {
'@type': 'chrono:entity',
version: 1,
provider: '',
service: '',
collection: '',
identifier: '',
collection: '' as CollectionIdentifier,
identifier: '' as EntityIdentifier,
signature: null,
created: null,
modified: null,
properties: new EventObject(),
properties: new EventObject().toJson(),
};
}
fromJson(data: EntityInterface): EntityObject {
this._data = data
if (data.properties) {
const type = data.properties.type
if (type === 'task') {
this._data.properties = new TaskObject().fromJson(data.properties as TaskInterface);
} else if (type === 'journal') {
this._data.properties = new JournalObject().fromJson(data.properties as JournalInterface);
} else {
this._data.properties = new EventObject().fromJson(data.properties as EventInterface);
}
}
this._data = clonePlain(data);
this._properties = undefined;
return this;
}
toJson(): EntityInterface {
const json = { ...this._data }
if (this._data.properties instanceof EventObject ||
this._data.properties instanceof TaskObject ||
this._data.properties instanceof JournalObject) {
json.properties = this._data.properties.toJson();
}
return json as EntityInterface
const json = this._properties
? { ...this._data, properties: this._properties.toJson() }
: this._data;
return clonePlain(json);
}
clone(): EntityObject {
const cloned = new EntityObject()
cloned._data = { ...this._data }
return cloned
return new EntityObject().fromJson(this.toJson());
}
/** Immutable Properties */
/** Metadata Properties */
get provider(): string {
return this._data.provider
return this._data.provider;
}
get service(): string {
return this._data.service
return this._data.service;
}
get collection(): string | number {
return this._data.collection
get collection(): CollectionIdentifier {
return this._data.collection;
}
get identifier(): string | number {
return this._data.identifier
get identifier(): EntityIdentifier {
return this._data.identifier;
}
get signature(): string | null {
return this._data.signature
return this._data.signature;
}
get created(): string | null {
return this._data.created
return this._data.created;
}
get modified(): string | null {
return this._data.modified
return this._data.modified;
}
get properties(): EventObject | TaskObject | JournalObject {
if (this._data.properties instanceof EventObject ||
this._data.properties instanceof TaskObject ||
this._data.properties instanceof JournalObject) {
return this._data.properties
/** Entity Properties (individual | organization | group) */
get properties(): EntityPropertiesObject {
if (this._properties) return this._properties;
const raw = this._data.properties;
if (raw.type === 'task') {
this._properties = new TaskObject().fromJson(raw as TaskInterface);
} else if (raw.type === 'journal') {
this._properties = new JournalObject().fromJson(raw as JournalInterface);
} else {
this._properties = new EventObject().fromJson(raw as EventInterface);
}
const defaultProperties = new EventObject();
this._data.properties = defaultProperties;
return defaultProperties
return this._properties;
}
set properties(value: EventObject | TaskObject | JournalObject) {
this._data.properties = value
set properties(value: EntityPropertiesObject) {
this._properties = value;
}
}
+103 -175
View File
@@ -1,196 +1,124 @@
/**
* Identity implementation classes for Mail Manager services
* Identity models for Chrono Manager services
*/
import type {
ServiceIdentity,
ServiceIdentityNone,
ServiceIdentityBasic,
ServiceIdentityToken,
ServiceIdentityOAuth,
ServiceIdentityCertificate
ServiceIdentity,
ServiceIdentityBasic,
ServiceIdentityCertificate,
ServiceIdentityNone,
ServiceIdentityOAuth,
ServiceIdentityToken
} from '@/types/service';
import { clonePlain } from './clone-plain';
import { MutationProxy } from './mutation-proxy';
/**
* Base Identity class
*/
export abstract class Identity {
abstract toJson(): ServiceIdentity;
static fromJson(data: ServiceIdentity): Identity {
switch (data.type) {
case 'NA':
return IdentityNone.fromJson(data);
case 'BA':
return IdentityBasic.fromJson(data);
case 'TA':
return IdentityToken.fromJson(data);
case 'OA':
return IdentityOAuth.fromJson(data);
case 'CC':
return IdentityCertificate.fromJson(data);
default:
throw new Error(`Unknown identity type: ${(data as any).type}`);
export abstract class Identity<T extends ServiceIdentity = ServiceIdentity> {
protected _original: T;
protected _mutated: Partial<T>;
protected _mutationProxy: MutationProxy<T>;
protected _data: T;
protected constructor(initial: T) {
this._original = clonePlain(initial);
this._mutated = {};
this._mutationProxy = new MutationProxy<T>(() => this._original, () => this._mutated);
this._data = this._mutationProxy.create();
}
protected load(data: T): this {
this._original = clonePlain(data);
this._mutated = {};
this._data = this._mutationProxy.create();
return this;
}
toJSON(): ServiceIdentity { return this.toJson(); }
toJson(): T;
toJson(delta: true): Partial<T>;
toJson(delta?: boolean): T | Partial<T> {
return delta
? clonePlain(this._mutated)
: { ...clonePlain(this._original), ...clonePlain(this._mutated) };
}
abstract clone(): Identity;
mutated(): boolean { return Reflect.ownKeys(this._mutated).length > 0; }
static fromJson(data: ServiceIdentity): Identity {
switch (data.type) {
case 'NA': return IdentityNone.fromJson(data);
case 'BA': return IdentityBasic.fromJson(data);
case 'TA': return IdentityToken.fromJson(data);
case 'OA': return IdentityOAuth.fromJson(data);
case 'CC': return IdentityCertificate.fromJson(data);
default: throw new Error(`Unknown identity type: ${(data as any).type}`);
}
}
}
}
/**
* No authentication
*/
export class IdentityNone extends Identity {
readonly type = 'NA' as const;
static fromJson(_data: ServiceIdentityNone): IdentityNone {
return new IdentityNone();
}
toJson(): ServiceIdentityNone {
return {
type: this.type
};
}
export class IdentityNone extends Identity<ServiceIdentityNone> {
constructor() { super({ type: 'NA' }); }
static fromJson(_data: ServiceIdentityNone): IdentityNone { return new IdentityNone(); }
clone(): IdentityNone { return IdentityNone.fromJson(this.toJson()); }
get type(): 'NA' { return this._data.type; }
}
/**
* Basic authentication (username/password)
*/
export class IdentityBasic extends Identity {
readonly type = 'BA' as const;
identity: string;
secret: string;
constructor(identity: string = '', secret: string = '') {
super();
this.identity = identity;
this.secret = secret;
}
static fromJson(data: ServiceIdentityBasic): IdentityBasic {
return new IdentityBasic(data.identity, data.secret);
}
toJson(): ServiceIdentityBasic {
return {
type: this.type,
identity: this.identity,
secret: this.secret
};
}
export class IdentityBasic extends Identity<ServiceIdentityBasic> {
constructor(identity: string = '', secret: string = '') { super({ type: 'BA', identity, secret }); }
static fromJson(data: ServiceIdentityBasic): IdentityBasic { return new IdentityBasic().load(data); }
clone(): IdentityBasic { return IdentityBasic.fromJson(this.toJson()); }
get type(): 'BA' { return this._data.type; }
get identity(): string { return this._data.identity; }
set identity(value: string) { this._data.identity = value; }
get secret(): string { return this._data.secret; }
set secret(value: string) { this._data.secret = value; }
}
/**
* Token authentication (API key, static token)
*/
export class IdentityToken extends Identity {
readonly type = 'TA' as const;
token: string;
constructor(token: string = '') {
super();
this.token = token;
}
static fromJson(data: ServiceIdentityToken): IdentityToken {
return new IdentityToken(data.token);
}
toJson(): ServiceIdentityToken {
return {
type: this.type,
token: this.token
};
}
export class IdentityToken extends Identity<ServiceIdentityToken> {
constructor(token: string = '') { super({ type: 'TA', token }); }
static fromJson(data: ServiceIdentityToken): IdentityToken { return new IdentityToken().load(data); }
clone(): IdentityToken { return IdentityToken.fromJson(this.toJson()); }
get type(): 'TA' { return this._data.type; }
get token(): string { return this._data.token; }
set token(value: string) { this._data.token = value; }
}
/**
* OAuth authentication
*/
export class IdentityOAuth extends Identity {
readonly type = 'OA' as const;
accessToken: string;
accessScope?: string[];
accessExpiry?: number;
refreshToken?: string;
refreshLocation?: string;
export class IdentityOAuth extends Identity<ServiceIdentityOAuth> {
constructor(accessToken: string = '', accessScope?: string[], accessExpiry?: number, refreshToken?: string, refreshLocation?: string) {
super({ type: 'OA', accessToken, accessScope, accessExpiry, refreshToken, refreshLocation });
}
constructor(
accessToken: string = '',
accessScope?: string[],
accessExpiry?: number,
refreshToken?: string,
refreshLocation?: string
) {
super();
this.accessToken = accessToken;
this.accessScope = accessScope;
this.accessExpiry = accessExpiry;
this.refreshToken = refreshToken;
this.refreshLocation = refreshLocation;
}
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth { return new IdentityOAuth().load(data); }
clone(): IdentityOAuth { return IdentityOAuth.fromJson(this.toJson()); }
isExpired(): boolean { return this.accessExpiry ? Date.now() / 1000 >= this.accessExpiry : false; }
expiresIn(): number { return this.accessExpiry ? Math.max(0, this.accessExpiry - Date.now() / 1000) : Infinity; }
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth {
return new IdentityOAuth(
data.accessToken,
data.accessScope,
data.accessExpiry,
data.refreshToken,
data.refreshLocation
);
}
toJson(): ServiceIdentityOAuth {
return {
type: this.type,
accessToken: this.accessToken,
...(this.accessScope && { accessScope: this.accessScope }),
...(this.accessExpiry && { accessExpiry: this.accessExpiry }),
...(this.refreshToken && { refreshToken: this.refreshToken }),
...(this.refreshLocation && { refreshLocation: this.refreshLocation })
};
}
isExpired(): boolean {
if (!this.accessExpiry) return false;
return Date.now() / 1000 >= this.accessExpiry;
}
expiresIn(): number {
if (!this.accessExpiry) return Infinity;
return Math.max(0, this.accessExpiry - Date.now() / 1000);
}
get type(): 'OA' { return this._data.type; }
get accessToken(): string { return this._data.accessToken; }
set accessToken(value: string) { this._data.accessToken = value; }
get accessScope(): string[] | undefined { return this._data.accessScope ? [...this._data.accessScope] : undefined; }
set accessScope(value: string[] | undefined) { this._data.accessScope = value ? [...value] : undefined; }
get accessExpiry(): number | undefined { return this._data.accessExpiry; }
set accessExpiry(value: number | undefined) { this._data.accessExpiry = value; }
get refreshToken(): string | undefined { return this._data.refreshToken; }
set refreshToken(value: string | undefined) { this._data.refreshToken = value; }
get refreshLocation(): string | undefined { return this._data.refreshLocation; }
set refreshLocation(value: string | undefined) { this._data.refreshLocation = value; }
}
/**
* Client certificate authentication (mTLS)
*/
export class IdentityCertificate extends Identity {
readonly type = 'CC' as const;
certificate: string;
privateKey: string;
passphrase?: string;
export class IdentityCertificate extends Identity<ServiceIdentityCertificate> {
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
super({ type: 'CC', certificate, privateKey, passphrase });
}
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
super();
this.certificate = certificate;
this.privateKey = privateKey;
this.passphrase = passphrase;
}
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate {
return new IdentityCertificate(
data.certificate,
data.privateKey,
data.passphrase
);
}
toJson(): ServiceIdentityCertificate {
return {
type: this.type,
certificate: this.certificate,
privateKey: this.privateKey,
...(this.passphrase && { passphrase: this.passphrase })
};
}
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate { return new IdentityCertificate().load(data); }
clone(): IdentityCertificate { return IdentityCertificate.fromJson(this.toJson()); }
get type(): 'CC' { return this._data.type; }
get certificate(): string { return this._data.certificate; }
set certificate(value: string) { this._data.certificate = value; }
get privateKey(): string { return this._data.privateKey; }
set privateKey(value: string) { this._data.privateKey = value; }
get passphrase(): string | undefined { return this._data.passphrase; }
set passphrase(value: string | undefined) { this._data.passphrase = value; }
}
+2 -3
View File
@@ -1,6 +1,6 @@
export { ProviderObject } from './provider';
export { ServiceObject } from './service';
export { CollectionObject } from './collection';
export { CollectionObject, CollectionPropertiesObject } from './collection';
export { EntityObject } from './entity';
export { EventObject } from './event';
export { TaskObject } from './task';
@@ -16,7 +16,6 @@ export {
export {
Location,
LocationUri,
LocationSocketSole,
LocationSocketSplit,
LocationFile
} from './location';
export { MutationProxy } from './mutation-proxy';
+78 -227
View File
@@ -1,240 +1,91 @@
/**
* Location implementation classes for Mail Manager services
* Location models for Chrono Manager services
*/
import type {
ServiceLocation,
ServiceLocationUri,
ServiceLocationSocketSole,
ServiceLocationSocketSplit,
ServiceLocationFile
} from '@/types/service';
import type { ServiceLocation, ServiceLocationFile, ServiceLocationUri } from '@/types/service';
import { clonePlain } from './clone-plain';
import { MutationProxy } from './mutation-proxy';
/**
* Base Location class
*/
export abstract class Location {
abstract toJson(): ServiceLocation;
static fromJson(data: ServiceLocation): Location {
switch (data.type) {
case 'URI':
return LocationUri.fromJson(data);
case 'SOCKET_SOLE':
return LocationSocketSole.fromJson(data);
case 'SOCKET_SPLIT':
return LocationSocketSplit.fromJson(data);
case 'FILE':
return LocationFile.fromJson(data);
default:
throw new Error(`Unknown location type: ${(data as any).type}`);
export abstract class Location<T extends ServiceLocation = ServiceLocation> {
protected _original: T;
protected _mutated: Partial<T>;
protected _mutationProxy: MutationProxy<T>;
protected _data: T;
protected constructor(initial: T) {
this._original = clonePlain(initial);
this._mutated = {};
this._mutationProxy = new MutationProxy<T>(() => this._original, () => this._mutated);
this._data = this._mutationProxy.create();
}
protected load(data: T): this {
this._original = clonePlain(data);
this._mutated = {};
this._data = this._mutationProxy.create();
return this;
}
toJson(): T;
toJson(delta: true): Partial<T>;
toJson(delta?: boolean): T | Partial<T> {
return delta
? clonePlain(this._mutated)
: { ...clonePlain(this._original), ...clonePlain(this._mutated) };
}
abstract clone(): Location;
mutated(): boolean {
return Reflect.ownKeys(this._mutated).length > 0;
}
static fromJson(data: ServiceLocation): Location {
switch (data.type) {
case 'URI': return LocationUri.fromJson(data);
case 'FILE': return LocationFile.fromJson(data);
default: throw new Error(`Unknown location type: ${(data as any).type}`);
}
}
}
}
/**
* URI-based service location for API and web services
* Used by: JMAP, Gmail API, etc.
*/
export class LocationUri extends Location {
readonly type = 'URI' as const;
scheme: string;
host: string;
port: number;
path?: string;
verifyPeer: boolean;
verifyHost: boolean;
export class LocationUri extends Location<ServiceLocationUri> {
constructor(
scheme: string = 'https',
host: string = '',
port: number = 443,
path?: string,
verifyPeer: boolean = true,
verifyHost: boolean = true
) {
super({ type: 'URI', scheme, host, port, ...(path !== undefined && { path }), verifyPeer, verifyHost });
}
constructor(
scheme: string = 'https',
host: string = '',
port: number = 443,
path?: string,
verifyPeer: boolean = true,
verifyHost: boolean = true
) {
super();
this.scheme = scheme;
this.host = host;
this.port = port;
this.path = path;
this.verifyPeer = verifyPeer;
this.verifyHost = verifyHost;
}
static fromJson(data: ServiceLocationUri): LocationUri { return new LocationUri().load(data); }
clone(): LocationUri { return LocationUri.fromJson(this.toJson()); }
getUrl(): string { return `${this.scheme}://${this.host}:${this.port}${this.path || ''}`; }
static fromJson(data: ServiceLocationUri): LocationUri {
return new LocationUri(
data.scheme,
data.host,
data.port,
data.path,
data.verifyPeer ?? true,
data.verifyHost ?? true
);
}
toJson(): ServiceLocationUri {
return {
type: this.type,
scheme: this.scheme,
host: this.host,
port: this.port,
...(this.path && { path: this.path }),
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
};
}
getUrl(): string {
const path = this.path || '';
return `${this.scheme}://${this.host}:${this.port}${path}`;
}
get type(): 'URI' { return this._data.type; }
get scheme(): string { return this._data.scheme; }
set scheme(value: string) { this._data.scheme = value; }
get host(): string { return this._data.host; }
set host(value: string) { this._data.host = value; }
get port(): number { return this._data.port; }
set port(value: number) { this._data.port = value; }
get path(): string | undefined { return this._data.path; }
set path(value: string | undefined) { this._data.path = value; }
get verifyPeer(): boolean { return this._data.verifyPeer ?? true; }
set verifyPeer(value: boolean) { this._data.verifyPeer = value; }
get verifyHost(): boolean { return this._data.verifyHost ?? true; }
set verifyHost(value: boolean) { this._data.verifyHost = value; }
}
/**
* Single socket-based service location
* Used by: services using a single host/port combination
*/
export class LocationSocketSole extends Location {
readonly type = 'SOCKET_SOLE' as const;
host: string;
port: number;
encryption: 'none' | 'ssl' | 'tls' | 'starttls';
verifyPeer: boolean;
verifyHost: boolean;
export class LocationFile extends Location<ServiceLocationFile> {
constructor(path: string = '') { super({ type: 'FILE', path }); }
static fromJson(data: ServiceLocationFile): LocationFile { return new LocationFile().load(data); }
clone(): LocationFile { return LocationFile.fromJson(this.toJson()); }
constructor(
host: string = '',
port: number = 993,
encryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
verifyPeer: boolean = true,
verifyHost: boolean = true
) {
super();
this.host = host;
this.port = port;
this.encryption = encryption;
this.verifyPeer = verifyPeer;
this.verifyHost = verifyHost;
}
static fromJson(data: ServiceLocationSocketSole): LocationSocketSole {
return new LocationSocketSole(
data.host,
data.port,
data.encryption,
data.verifyPeer ?? true,
data.verifyHost ?? true
);
}
toJson(): ServiceLocationSocketSole {
return {
type: this.type,
host: this.host,
port: this.port,
encryption: this.encryption,
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
};
}
}
/**
* Split socket-based service location
* Used by: traditional IMAP/SMTP configurations
*/
export class LocationSocketSplit extends Location {
readonly type = 'SOCKET_SPLIT' as const;
inboundHost: string;
inboundPort: number;
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
outboundHost: string;
outboundPort: number;
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
inboundVerifyPeer: boolean;
inboundVerifyHost: boolean;
outboundVerifyPeer: boolean;
outboundVerifyHost: boolean;
constructor(
inboundHost: string = '',
inboundPort: number = 993,
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
outboundHost: string = '',
outboundPort: number = 465,
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
inboundVerifyPeer: boolean = true,
inboundVerifyHost: boolean = true,
outboundVerifyPeer: boolean = true,
outboundVerifyHost: boolean = true
) {
super();
this.inboundHost = inboundHost;
this.inboundPort = inboundPort;
this.inboundEncryption = inboundEncryption;
this.outboundHost = outboundHost;
this.outboundPort = outboundPort;
this.outboundEncryption = outboundEncryption;
this.inboundVerifyPeer = inboundVerifyPeer;
this.inboundVerifyHost = inboundVerifyHost;
this.outboundVerifyPeer = outboundVerifyPeer;
this.outboundVerifyHost = outboundVerifyHost;
}
static fromJson(data: ServiceLocationSocketSplit): LocationSocketSplit {
return new LocationSocketSplit(
data.inboundHost,
data.inboundPort,
data.inboundEncryption,
data.outboundHost,
data.outboundPort,
data.outboundEncryption,
data.inboundVerifyPeer ?? true,
data.inboundVerifyHost ?? true,
data.outboundVerifyPeer ?? true,
data.outboundVerifyHost ?? true
);
}
toJson(): ServiceLocationSocketSplit {
return {
type: this.type,
inboundHost: this.inboundHost,
inboundPort: this.inboundPort,
inboundEncryption: this.inboundEncryption,
outboundHost: this.outboundHost,
outboundPort: this.outboundPort,
outboundEncryption: this.outboundEncryption,
...(this.inboundVerifyPeer !== undefined && { inboundVerifyPeer: this.inboundVerifyPeer }),
...(this.inboundVerifyHost !== undefined && { inboundVerifyHost: this.inboundVerifyHost }),
...(this.outboundVerifyPeer !== undefined && { outboundVerifyPeer: this.outboundVerifyPeer }),
...(this.outboundVerifyHost !== undefined && { outboundVerifyHost: this.outboundVerifyHost })
};
}
}
/**
* File-based service location
* Used by: local file system providers
*/
export class LocationFile extends Location {
readonly type = 'FILE' as const;
path: string;
constructor(path: string = '') {
super();
this.path = path;
}
static fromJson(data: ServiceLocationFile): LocationFile {
return new LocationFile(data.path);
}
toJson(): ServiceLocationFile {
return {
type: this.type,
path: this.path
};
}
get type(): 'FILE' { return this._data.type; }
get path(): string { return this._data.path; }
set path(value: string) { this._data.path = value; }
}
+36
View File
@@ -0,0 +1,36 @@
import { clonePlain } from './clone-plain';
export class MutationProxy<T extends object> {
constructor(
private readonly getOriginal: () => T,
private readonly getMutated: () => Partial<T>,
) {}
create(): T {
return new Proxy({} as T, {
get: (_target, prop: string | symbol) => {
if (typeof prop !== 'string') return undefined;
const key = prop as keyof T;
const mutated = this.getMutated();
return key in mutated ? mutated[key] : this.getOriginal()[key];
},
set: (_target, prop: string | symbol, value: unknown) => {
if (typeof prop === 'string') {
const key = prop as keyof T;
(this.getMutated() as Record<keyof T, unknown>)[key] = clonePlain(value);
}
return true;
},
has: (_target, prop: string | symbol) => {
if (typeof prop !== 'string') return false;
return prop in this.getMutated() || prop in this.getOriginal();
},
ownKeys: () => Array.from(new Set([
...Reflect.ownKeys(this.getOriginal()),
...Reflect.ownKeys(this.getMutated()),
])),
getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true }),
});
}
}
+11 -9
View File
@@ -4,10 +4,12 @@
import type {
ProviderInterface,
ProviderCapabilitiesInterface
ProviderCapabilitiesInterface,
ProviderModelInterface
} from "@/types/provider";
import { clonePlain } from './clone-plain';
export class ProviderObject implements ProviderInterface {
export class ProviderObject implements ProviderModelInterface {
_data!: ProviderInterface;
@@ -21,12 +23,16 @@ export class ProviderObject implements ProviderInterface {
}
fromJson(data: ProviderInterface): ProviderObject {
this._data = data;
this._data = clonePlain(data);
return this;
}
toJson(): ProviderInterface {
return this._data;
return clonePlain(this._data);
}
clone(): ProviderObject {
return new ProviderObject().fromJson(this.toJson());
}
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
@@ -43,10 +49,6 @@ export class ProviderObject implements ProviderInterface {
/** Immutable Properties */
get '@type'(): string {
return this._data['@type'];
}
get identifier(): string {
return this._data.identifier;
}
@@ -56,7 +58,7 @@ export class ProviderObject implements ProviderInterface {
}
get capabilities(): ProviderCapabilitiesInterface {
return this._data.capabilities;
return clonePlain(this._data.capabilities);
}
}
+86 -81
View File
@@ -5,18 +5,26 @@
import type {
ServiceInterface,
ServiceCapabilitiesInterface,
ServiceIdentity,
ServiceLocation
ServiceLocation,
ServiceModelInterface
} from "@/types/service";
import { Identity } from './identity';
import { Location } from './location';
import { MutationProxy } from './mutation-proxy';
import { clonePlain } from './clone-plain';
export class ServiceObject implements ServiceInterface {
export class ServiceObject implements ServiceModelInterface {
private _original: ServiceInterface;
private _mutated: Partial<ServiceInterface>;
private _mutationProxy = new MutationProxy<ServiceInterface>(() => this._original, () => this._mutated);
_data!: ServiceInterface;
private _location: Location | null | undefined = undefined;
private _identity: Identity | null | undefined = undefined;
private _locationAssigned = false;
private _identityAssigned = false;
constructor() {
this._data = {
this._original = {
'@type': 'chrono:service',
provider: '',
identifier: null,
@@ -24,15 +32,59 @@ export class ServiceObject implements ServiceInterface {
enabled: false,
capabilities: {}
};
this._mutated = {};
this._data = this._mutationProxy.create();
}
fromJson(data: ServiceInterface): ServiceObject {
this._data = data;
this._original = clonePlain(data);
this._mutated = {};
this._data = this._mutationProxy.create();
this._location = undefined;
this._identity = undefined;
this._locationAssigned = false;
this._identityAssigned = false;
return this;
}
toJson(): ServiceInterface {
return this._data;
toJson(): ServiceInterface;
toJson(delta: true): Partial<ServiceInterface>;
toJson(delta?: boolean): ServiceInterface | Partial<ServiceInterface> {
if (delta) {
const json = clonePlain(this._mutated);
if (this._locationAssigned) {
json.location = this._location ? this._location.toJson() : null;
} else if (this._location?.mutated()) {
json.location = this._location.toJson(true) as ServiceInterface['location'];
}
if (this._identityAssigned) {
json.identity = this._identity ? this._identity.toJson() : null;
} else if (this._identity?.mutated()) {
json.identity = this._identity.toJson(true) as ServiceInterface['identity'];
}
return json;
}
const json: ServiceInterface = {
...clonePlain(this._original),
...clonePlain(this._mutated),
};
if (this._location !== undefined) json.location = this._location ? this._location.toJson() : null;
if (this._identity !== undefined) json.identity = this._identity ? this._identity.toJson() : null;
return json;
}
clone(): ServiceObject {
return new ServiceObject().fromJson(this.toJson());
}
mutated(): boolean {
return Reflect.ownKeys(this._mutated).length > 0
|| this._locationAssigned
|| this._identityAssigned
|| (this._location?.mutated() ?? false)
|| (this._identity?.mutated() ?? false);
}
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
@@ -41,88 +93,41 @@ export class ServiceObject implements ServiceInterface {
}
capability(capability: keyof ServiceCapabilitiesInterface): any | null {
if (this._data.capabilities) {
return this._data.capabilities[capability];
}
return null;
return this._data.capabilities?.[capability] ?? null;
}
/** Immutable Properties */
get provider(): string { return this._data.provider; }
get identifier(): string | number | null { return this._data.identifier; }
get capabilities(): ServiceCapabilitiesInterface { return this._data.capabilities ?? {}; }
get '@type'(): string {
return this._data['@type'];
get label(): string | null { return this._data.label; }
set label(value: string | null) { this._data.label = value; }
get enabled(): boolean { return this._data.enabled; }
set enabled(value: boolean) { this._data.enabled = value; }
get location(): Location | null {
if (this._location !== undefined) return this._location;
this._location = this._data.location ? Location.fromJson(this._data.location as ServiceLocation) : null;
return this._location;
}
get provider(): string {
return this._data.provider;
set location(value: Location | null) {
this._location = value;
this._locationAssigned = true;
}
get identifier(): string | number | null {
return this._data.identifier;
get identity(): Identity | null {
if (this._identity !== undefined) return this._identity;
this._identity = this._data.identity ? Identity.fromJson(this._data.identity) : null;
return this._identity;
}
get capabilities(): ServiceCapabilitiesInterface | undefined {
return this._data.capabilities;
}
/** Mutable Properties */
get label(): string | null {
return this._data.label;
}
set label(value: string | null) {
this._data.label = value;
}
get enabled(): boolean {
return this._data.enabled;
}
set enabled(value: boolean) {
this._data.enabled = value;
}
get location(): ServiceLocation | null {
return this._data.location ?? null;
}
set location(value: ServiceLocation | null) {
this._data.location = value;
}
get identity(): ServiceIdentity | null {
return this._data.identity ?? null;
}
set identity(value: ServiceIdentity | null) {
this._data.identity = value;
}
get auxiliary(): Record<string, any> {
return this._data.auxiliary ?? {};
}
set auxiliary(value: Record<string, any>) {
this._data.auxiliary = value;
}
/** Helper Methods */
/**
* Get identity as a class instance for easier manipulation
*/
getIdentity(): Identity | null {
if (!this._data.identity) return null;
return Identity.fromJson(this._data.identity);
}
/**
* Get location as a class instance for easier manipulation
*/
getLocation(): Location | null {
if (!this._data.location) return null;
return Location.fromJson(this._data.location);
set identity(value: Identity | null) {
this._identity = value;
this._identityAssigned = true;
}
get auxiliary(): Record<string, any> { return this._data.auxiliary ?? {}; }
set auxiliary(value: Record<string, any>) { this._data.auxiliary = value; }
}