refactor: front end code to unified manager api

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-20 18:56:35 -04:00
parent 053c272fe4
commit c4cb1512d2
21 changed files with 1484 additions and 927 deletions
+54 -61
View File
@@ -2,46 +2,55 @@
* Class model for Collection Interface
*/
import type { CollectionContentTypes, CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
import type {
CollectionContentTypes,
CollectionInterface,
CollectionModelInterface,
CollectionPropertiesInterface,
CollectionPropertiesModelInterface
} from "@/types/collection";
import { clonePlain } from './clone-plain';
import type {
CollectionIdentifier,
ServiceIdentifier
} from "@/types/common";
export class CollectionObject implements CollectionInterface {
export class CollectionObject implements CollectionModelInterface {
_data!: CollectionInterface;
_data!: CollectionInterface<CollectionPropertiesInterface>;
_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: { '@type': 'people:addressbook', content: [], label: '', description: null, rank: null, visibility: null, color: null },
};
}
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 +59,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 {
@@ -75,36 +84,30 @@ export class CollectionObject implements CollectionInterface {
}
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 {
export class CollectionPropertiesObject implements CollectionPropertiesModelInterface {
_data!: CollectionPropertiesInterface;
private _data!: CollectionPropertiesInterface;
constructor() {
this._data = {
'@type': 'people:collection',
version: 1,
'@type': 'people:addressbook',
content: [],
label: '',
description: null,
@@ -115,32 +118,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 +178,4 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
this._data.color = value;
}
}
}