chore: bunch of improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+63
-36
@@ -5,19 +5,22 @@
|
||||
import type {
|
||||
ServiceInterface,
|
||||
ServiceCapabilitiesInterface,
|
||||
ServiceIdentity,
|
||||
ServiceLocation
|
||||
ServiceLocation,
|
||||
ServiceModelInterface
|
||||
} from "@/types/service";
|
||||
import { Identity } from './identity';
|
||||
import { Location } from './location';
|
||||
|
||||
export class ServiceObject implements ServiceInterface {
|
||||
export class ServiceObject implements ServiceModelInterface {
|
||||
|
||||
_data!: ServiceInterface;
|
||||
_location: Location | null = null;
|
||||
_identity: Identity | null = null;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:service',
|
||||
version: 1,
|
||||
provider: '',
|
||||
identifier: null,
|
||||
label: null,
|
||||
@@ -32,7 +35,35 @@ export class ServiceObject implements ServiceInterface {
|
||||
}
|
||||
|
||||
toJson(): ServiceInterface {
|
||||
return this._data;
|
||||
const json = {
|
||||
...this._data,
|
||||
capabilities: this._data.capabilities ? { ...this._data.capabilities } : this._data.capabilities,
|
||||
secondaryAddresses: this._data.secondaryAddresses ? [...this._data.secondaryAddresses] : this._data.secondaryAddresses,
|
||||
auxiliary: this._data.auxiliary ? { ...this._data.auxiliary } : this._data.auxiliary,
|
||||
};
|
||||
|
||||
if (this._location !== null) {
|
||||
json.location = this._location.toJson();
|
||||
}
|
||||
|
||||
if (this._identity !== null) {
|
||||
json.identity = this._identity.toJson();
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): ServiceObject {
|
||||
const cloned = new ServiceObject();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
capabilities: this._data.capabilities ? { ...this._data.capabilities } : this._data.capabilities,
|
||||
secondaryAddresses: this._data.secondaryAddresses ? [...this._data.secondaryAddresses] : this._data.secondaryAddresses,
|
||||
auxiliary: this._data.auxiliary ? { ...this._data.auxiliary } : this._data.auxiliary,
|
||||
};
|
||||
cloned._location = this._location ? this._location.clone() : null;
|
||||
cloned._identity = this._identity ? this._identity.clone() : null;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
||||
@@ -49,10 +80,6 @@ export class ServiceObject implements ServiceInterface {
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
@@ -61,8 +88,8 @@ export class ServiceObject implements ServiceInterface {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
||||
return this._data.capabilities;
|
||||
get capabilities(): ServiceCapabilitiesInterface {
|
||||
return this._data.capabilities ?? {};
|
||||
}
|
||||
|
||||
get primaryAddress(): string | null {
|
||||
@@ -91,20 +118,38 @@ export class ServiceObject implements ServiceInterface {
|
||||
this._data.enabled = value;
|
||||
}
|
||||
|
||||
get location(): ServiceLocation | null {
|
||||
return this._data.location ?? null;
|
||||
get location(): Location | null {
|
||||
if (this._location) {
|
||||
return this._location;
|
||||
}
|
||||
else if (this._location === null && this._data.location) {
|
||||
const location = Location.fromJson(this._data.location as ServiceLocation);
|
||||
this._location = location;
|
||||
return location;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set location(value: ServiceLocation | null) {
|
||||
this._data.location = value;
|
||||
set location(value: Location | null) {
|
||||
this._location = value;
|
||||
}
|
||||
|
||||
get identity(): ServiceIdentity | null {
|
||||
return this._data.identity ?? null;
|
||||
get identity(): Identity | null {
|
||||
if (this._identity) {
|
||||
return this._identity;
|
||||
}
|
||||
else if (this._data.identity) {
|
||||
const identity = Identity.fromJson(this._data.identity);
|
||||
this._identity = identity;
|
||||
return identity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set identity(value: ServiceIdentity | null) {
|
||||
this._data.identity = value;
|
||||
set identity(value: Identity | null) {
|
||||
this._identity = value;
|
||||
}
|
||||
|
||||
get auxiliary(): Record<string, any> {
|
||||
@@ -115,22 +160,4 @@ export class ServiceObject implements ServiceInterface {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user