refactor: front end
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,95 +1,144 @@
|
||||
/**
|
||||
* Class model for FileCollection Interface
|
||||
* Class model for Collection Interface
|
||||
*/
|
||||
import type { FileCollection } from "@/types/node";
|
||||
|
||||
export class FileCollectionObject implements FileCollection {
|
||||
import type { CollectionContentTypes, CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||
|
||||
_data!: FileCollection;
|
||||
export class CollectionObject implements CollectionModelInterface {
|
||||
|
||||
_data!: CollectionInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'files.collection',
|
||||
in: null,
|
||||
id: '',
|
||||
createdBy: '',
|
||||
createdOn: '',
|
||||
modifiedBy: '',
|
||||
modifiedOn: '',
|
||||
'@type': 'documents:collection',
|
||||
schema: 1,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: null,
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new CollectionPropertiesObject(),
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionInterface): CollectionObject {
|
||||
this._data = data;
|
||||
if (data.properties) {
|
||||
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): CollectionObject {
|
||||
const cloned = new CollectionObject();
|
||||
cloned._data = { ...this._data };
|
||||
cloned._data.properties = this.properties.clone();
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get schema(): number {
|
||||
return this._data.schema;
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get service(): string | number {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get collection(): string | number | null {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature || null;
|
||||
}
|
||||
|
||||
get created(): Date | null {
|
||||
return this._data.created ? new Date(this._data.created) : null;
|
||||
}
|
||||
|
||||
get modified(): Date | null {
|
||||
return this._data.modified ? new Date(this._data.modified) : null;
|
||||
}
|
||||
|
||||
get properties(): CollectionPropertiesObject {
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
return this._data.properties;
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
const defaultProperties = new CollectionPropertiesObject();
|
||||
this._data.properties = defaultProperties;
|
||||
return defaultProperties;
|
||||
}
|
||||
|
||||
set properties(value: CollectionPropertiesObject) {
|
||||
if (value instanceof CollectionPropertiesObject) {
|
||||
this._data.properties = value as any;
|
||||
} else {
|
||||
this._data.properties = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
|
||||
|
||||
_data!: CollectionPropertiesInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
content: [],
|
||||
owner: '',
|
||||
signature: '',
|
||||
label: '',
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: FileCollection): FileCollectionObject {
|
||||
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): FileCollection {
|
||||
toJson(): CollectionPropertiesInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): FileCollectionObject {
|
||||
const cloned = new FileCollectionObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
clone(): CollectionPropertiesObject {
|
||||
const cloned = new CollectionPropertiesObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): 'files.collection' {
|
||||
return this._data['@type'];
|
||||
get content(): CollectionContentTypes[] {
|
||||
return this._data.content || [];
|
||||
}
|
||||
|
||||
get in(): string | null {
|
||||
return this._data.in;
|
||||
}
|
||||
|
||||
set in(value: string | null) {
|
||||
this._data.in = value;
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
set id(value: string) {
|
||||
this._data.id = value;
|
||||
}
|
||||
|
||||
get createdBy(): string {
|
||||
return this._data.createdBy;
|
||||
}
|
||||
|
||||
set createdBy(value: string) {
|
||||
this._data.createdBy = value;
|
||||
}
|
||||
|
||||
get createdOn(): string {
|
||||
return this._data.createdOn;
|
||||
}
|
||||
|
||||
set createdOn(value: string) {
|
||||
this._data.createdOn = value;
|
||||
}
|
||||
|
||||
get modifiedBy(): string {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string) {
|
||||
this._data.modifiedBy = value;
|
||||
}
|
||||
|
||||
get modifiedOn(): string {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
set modifiedOn(value: string) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
/** Mutable Properties */
|
||||
|
||||
get owner(): string {
|
||||
return this._data.owner;
|
||||
@@ -99,34 +148,12 @@ export class FileCollectionObject implements FileCollection {
|
||||
this._data.owner = value;
|
||||
}
|
||||
|
||||
get signature(): string {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
set signature(value: string) {
|
||||
this._data.signature = value;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
return this._data.label;
|
||||
return this._data.label || '';
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
|
||||
get isRoot(): boolean {
|
||||
return this._data.id === '00000000-0000-0000-0000-000000000000';
|
||||
}
|
||||
|
||||
get createdOnDate(): Date | null {
|
||||
return this._data.createdOn ? new Date(this._data.createdOn) : null;
|
||||
}
|
||||
|
||||
get modifiedOnDate(): Date | null {
|
||||
return this._data.modifiedOn ? new Date(this._data.modifiedOn) : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
79
src/models/document.ts
Normal file
79
src/models/document.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { DocumentInterface, DocumentModelInterface } from "@/types/document";
|
||||
|
||||
export class DocumentObject implements DocumentModelInterface {
|
||||
|
||||
_data!: DocumentInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'documents:document',
|
||||
urid: null,
|
||||
size: 0,
|
||||
label: '',
|
||||
mime: null,
|
||||
format: null,
|
||||
encoding: null,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: DocumentInterface): DocumentObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): DocumentInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): DocumentObject {
|
||||
const cloned = new DocumentObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get urid(): string | null {
|
||||
return this._data.urid;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
const parsed = typeof this._data.size === 'number' ? this._data.size : Number(this._data.size);
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
get label(): string | null {
|
||||
return this._data.label || null;
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get mime(): string | null {
|
||||
return this._data.mime;
|
||||
}
|
||||
|
||||
set mime(value: string) {
|
||||
this._data.mime = value;
|
||||
}
|
||||
|
||||
get format(): string | null {
|
||||
return this._data.format;
|
||||
}
|
||||
|
||||
set format(value: string) {
|
||||
this._data.format = value;
|
||||
}
|
||||
|
||||
get encoding(): string | null {
|
||||
return this._data.encoding;
|
||||
}
|
||||
|
||||
set encoding(value: string) {
|
||||
this._data.encoding = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,200 +1,104 @@
|
||||
/**
|
||||
* Class model for FileEntity Interface
|
||||
* Class model for Entity Interface
|
||||
*/
|
||||
import type { FileEntity } from "@/types/node";
|
||||
import type { EntityInterface, EntityModelInterface } from "@/types/entity";
|
||||
import type { DocumentInterface, DocumentModelInterface } from "@/types/document";
|
||||
import { DocumentObject } from "./document";
|
||||
|
||||
export class FileEntityObject implements FileEntity {
|
||||
export class EntityObject implements EntityModelInterface {
|
||||
|
||||
_data!: FileEntity;
|
||||
_data!: EntityInterface<DocumentInterface|DocumentModelInterface>;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'files.entity',
|
||||
in: null,
|
||||
id: '',
|
||||
createdBy: '',
|
||||
createdOn: '',
|
||||
modifiedBy: '',
|
||||
modifiedOn: '',
|
||||
owner: '',
|
||||
signature: '',
|
||||
label: '',
|
||||
size: 0,
|
||||
mime: '',
|
||||
format: '',
|
||||
encoding: '',
|
||||
'@type': '',
|
||||
schema: 1,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: '',
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new DocumentObject(),
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: FileEntity): FileEntityObject {
|
||||
this._data = data;
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = data
|
||||
if (data.properties) {
|
||||
this._data.properties = new DocumentObject().fromJson(data.properties as DocumentInterface);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): FileEntity {
|
||||
return this._data;
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data }
|
||||
if (this._data.properties instanceof DocumentObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json as EntityInterface
|
||||
}
|
||||
|
||||
clone(): FileEntityObject {
|
||||
const cloned = new FileEntityObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
return cloned;
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject()
|
||||
cloned._data = { ...this._data }
|
||||
cloned._data.properties = this.properties.clone();
|
||||
return cloned
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): 'files.entity' {
|
||||
return this._data['@type'];
|
||||
get provider(): string {
|
||||
return this._data.provider
|
||||
}
|
||||
|
||||
get in(): string | null {
|
||||
return this._data.in;
|
||||
get schema(): number {
|
||||
return this._data.schema
|
||||
}
|
||||
|
||||
set in(value: string | null) {
|
||||
this._data.in = value;
|
||||
get service(): string {
|
||||
return this._data.service
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this._data.id;
|
||||
get collection(): string | number {
|
||||
return this._data.collection
|
||||
}
|
||||
|
||||
set id(value: string) {
|
||||
this._data.id = value;
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier
|
||||
}
|
||||
|
||||
get createdBy(): string {
|
||||
return this._data.createdBy;
|
||||
get signature(): string | null {
|
||||
return this._data.signature
|
||||
}
|
||||
|
||||
set createdBy(value: string) {
|
||||
this._data.createdBy = value;
|
||||
get created(): Date | null {
|
||||
return this._data.created ? new Date(this._data.created) : null
|
||||
}
|
||||
|
||||
get createdOn(): string {
|
||||
return this._data.createdOn;
|
||||
get modified(): Date | null {
|
||||
return this._data.modified ? new Date(this._data.modified) : null
|
||||
}
|
||||
|
||||
set createdOn(value: string) {
|
||||
this._data.createdOn = value;
|
||||
get properties(): DocumentObject {
|
||||
if (this._data.properties instanceof DocumentObject) {
|
||||
return this._data.properties
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = new DocumentObject().fromJson(this._data.properties as DocumentInterface)
|
||||
this._data.properties = hydrated
|
||||
return hydrated
|
||||
}
|
||||
|
||||
const defaultProperties = new DocumentObject()
|
||||
this._data.properties = defaultProperties
|
||||
return defaultProperties
|
||||
}
|
||||
|
||||
get modifiedBy(): string {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string) {
|
||||
this._data.modifiedBy = value;
|
||||
}
|
||||
|
||||
get modifiedOn(): string {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
set modifiedOn(value: string) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
|
||||
get owner(): string {
|
||||
return this._data.owner;
|
||||
}
|
||||
|
||||
set owner(value: string) {
|
||||
this._data.owner = value;
|
||||
}
|
||||
|
||||
get signature(): string {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
set signature(value: string) {
|
||||
this._data.signature = value;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this._data.size;
|
||||
}
|
||||
|
||||
set size(value: number) {
|
||||
this._data.size = value;
|
||||
}
|
||||
|
||||
get mime(): string {
|
||||
return this._data.mime;
|
||||
}
|
||||
|
||||
set mime(value: string) {
|
||||
this._data.mime = value;
|
||||
}
|
||||
|
||||
get format(): string {
|
||||
return this._data.format;
|
||||
}
|
||||
|
||||
set format(value: string) {
|
||||
this._data.format = value;
|
||||
}
|
||||
|
||||
get encoding(): string {
|
||||
return this._data.encoding;
|
||||
}
|
||||
|
||||
set encoding(value: string) {
|
||||
this._data.encoding = value;
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
|
||||
get createdOnDate(): Date | null {
|
||||
return this._data.createdOn ? new Date(this._data.createdOn) : null;
|
||||
}
|
||||
|
||||
get modifiedOnDate(): Date | null {
|
||||
return this._data.modifiedOn ? new Date(this._data.modifiedOn) : null;
|
||||
}
|
||||
|
||||
get extension(): string {
|
||||
const parts = this._data.label.split('.');
|
||||
return parts.length > 1 ? parts[parts.length - 1] : '';
|
||||
}
|
||||
|
||||
get sizeFormatted(): string {
|
||||
const bytes = this._data.size;
|
||||
if (bytes === 0) return '0 B';
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
get isImage(): boolean {
|
||||
return this._data.mime.startsWith('image/');
|
||||
}
|
||||
|
||||
get isVideo(): boolean {
|
||||
return this._data.mime.startsWith('video/');
|
||||
}
|
||||
|
||||
get isAudio(): boolean {
|
||||
return this._data.mime.startsWith('audio/');
|
||||
}
|
||||
|
||||
get isText(): boolean {
|
||||
return this._data.mime.startsWith('text/') ||
|
||||
this._data.mime === 'application/json' ||
|
||||
this._data.mime === 'application/xml';
|
||||
}
|
||||
|
||||
get isPdf(): boolean {
|
||||
return this._data.mime === 'application/pdf';
|
||||
set properties(value: DocumentObject) {
|
||||
this._data.properties = value
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
196
src/models/identity.ts
Normal file
196
src/models/identity.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* Identity implementation classes for Mail Manager services
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServiceIdentity,
|
||||
ServiceIdentityNone,
|
||||
ServiceIdentityBasic,
|
||||
ServiceIdentityToken,
|
||||
ServiceIdentityOAuth,
|
||||
ServiceIdentityCertificate
|
||||
} from '@/types/service';
|
||||
|
||||
/**
|
||||
* 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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth authentication
|
||||
*/
|
||||
export class IdentityOAuth extends Identity {
|
||||
readonly type = 'OA' as const;
|
||||
accessToken: string;
|
||||
accessScope?: string[];
|
||||
accessExpiry?: number;
|
||||
refreshToken?: string;
|
||||
refreshLocation?: string;
|
||||
|
||||
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(
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Client certificate authentication (mTLS)
|
||||
*/
|
||||
export class IdentityCertificate extends Identity {
|
||||
readonly type = 'CC' as const;
|
||||
certificate: string;
|
||||
privateKey: string;
|
||||
passphrase?: string;
|
||||
|
||||
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 })
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
/**
|
||||
* Central export point for all File Manager models
|
||||
*/
|
||||
|
||||
export { FileCollectionObject } from './collection';
|
||||
export { FileEntityObject } from './entity';
|
||||
export { CollectionObject } from './collection';
|
||||
export { EntityObject } from './entity';
|
||||
export { ProviderObject } from './provider';
|
||||
export { ServiceObject } from './service';
|
||||
|
||||
240
src/models/location.ts
Normal file
240
src/models/location.ts
Normal file
@@ -0,0 +1,240 @@
|
||||
/**
|
||||
* Location implementation classes for Mail Manager services
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServiceLocation,
|
||||
ServiceLocationUri,
|
||||
ServiceLocationSocketSole,
|
||||
ServiceLocationSocketSplit,
|
||||
ServiceLocationFile
|
||||
} from '@/types/service';
|
||||
|
||||
/**
|
||||
* 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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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(
|
||||
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}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
/**
|
||||
* Class model for Provider Interface
|
||||
*/
|
||||
import type { ProviderCapabilitiesInterface, ProviderInterface } from "@/types/provider";
|
||||
|
||||
import type {
|
||||
ProviderInterface,
|
||||
ProviderCapabilitiesInterface
|
||||
} from "@/types/provider";
|
||||
|
||||
export class ProviderObject implements ProviderInterface {
|
||||
|
||||
@@ -9,8 +13,8 @@ export class ProviderObject implements ProviderInterface {
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'files:provider',
|
||||
id: '',
|
||||
'@type': 'documents:provider',
|
||||
identifier: '',
|
||||
label: '',
|
||||
capabilities: {},
|
||||
};
|
||||
@@ -25,21 +29,16 @@ export class ProviderObject implements ProviderInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): ProviderObject {
|
||||
const cloned = new ProviderObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
return cloned;
|
||||
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
||||
const value = this._data.capabilities?.[capability];
|
||||
return value !== undefined && value !== false;
|
||||
}
|
||||
|
||||
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
||||
return !!(this._data.capabilities && this._data.capabilities[capability]);
|
||||
}
|
||||
|
||||
capability(capability: keyof ProviderCapabilitiesInterface): boolean | string[] | Record<string, string> | Record<string, string[]> | undefined {
|
||||
capability(capability: keyof ProviderCapabilitiesInterface): any | null {
|
||||
if (this._data.capabilities) {
|
||||
return this._data.capabilities[capability];
|
||||
}
|
||||
return undefined;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
@@ -48,8 +47,8 @@ export class ProviderObject implements ProviderInterface {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this._data.id;
|
||||
get identifier(): string {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
/**
|
||||
* Class model for Service Interface
|
||||
*/
|
||||
import type { ServiceInterface } from "@/types/service";
|
||||
|
||||
import type {
|
||||
ServiceInterface,
|
||||
ServiceCapabilitiesInterface,
|
||||
ServiceIdentity,
|
||||
ServiceLocation
|
||||
} from "@/types/service";
|
||||
import { Identity } from './identity';
|
||||
import { Location } from './location';
|
||||
|
||||
export class ServiceObject implements ServiceInterface {
|
||||
|
||||
@@ -9,11 +17,12 @@ export class ServiceObject implements ServiceInterface {
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'files:service',
|
||||
id: '',
|
||||
'@type': 'documents:service',
|
||||
provider: '',
|
||||
label: '',
|
||||
rootId: '',
|
||||
identifier: null,
|
||||
label: null,
|
||||
enabled: false,
|
||||
capabilities: {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,10 +35,16 @@ export class ServiceObject implements ServiceInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): ServiceObject {
|
||||
const cloned = new ServiceObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
return cloned;
|
||||
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
||||
const value = this._data.capabilities?.[capability];
|
||||
return value !== undefined && value !== false;
|
||||
}
|
||||
|
||||
capability(capability: keyof ServiceCapabilitiesInterface): any | null {
|
||||
if (this._data.capabilities) {
|
||||
return this._data.capabilities[capability];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
@@ -38,20 +53,76 @@ export class ServiceObject implements ServiceInterface {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
get identifier(): string | number | null {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
||||
return this._data.capabilities;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
get label(): string | null {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
get rootId(): string {
|
||||
return this._data.rootId;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user