refactor: documents interfaces
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -4,6 +4,19 @@
|
||||
|
||||
import type { CollectionContentTypes, CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||
|
||||
/**
|
||||
* Reduce a serialized resource identifier ("provider:service:collection")
|
||||
* to its own (last) segment; plain values pass through unchanged
|
||||
*/
|
||||
function plainIdentifier(value: string | number | null | undefined): string | number | null {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return value ?? null;
|
||||
}
|
||||
const text = String(value);
|
||||
const index = text.lastIndexOf(':');
|
||||
return index >= 0 ? text.slice(index + 1) : value;
|
||||
}
|
||||
|
||||
export class CollectionObject implements CollectionModelInterface {
|
||||
|
||||
_data!: CollectionInterface;
|
||||
@@ -24,7 +37,14 @@ export class CollectionObject implements CollectionModelInterface {
|
||||
}
|
||||
|
||||
fromJson(data: CollectionInterface): CollectionObject {
|
||||
this._data = data;
|
||||
// the wire format carries full resource identifiers (provider:service:collection);
|
||||
// reduce them to plain ids, which is what the stores and UI operate on
|
||||
this._data = {
|
||||
...data,
|
||||
service: plainIdentifier(data.service) ?? '',
|
||||
collection: plainIdentifier(data.collection),
|
||||
identifier: plainIdentifier(data.identifier) ?? '',
|
||||
};
|
||||
if (data.properties) {
|
||||
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||
}
|
||||
|
||||
+21
-1
@@ -5,6 +5,19 @@ import type { EntityInterface, EntityModelInterface } from "@/types/entity";
|
||||
import type { DocumentInterface, DocumentModelInterface } from "@/types/document";
|
||||
import { DocumentObject } from "./document";
|
||||
|
||||
/**
|
||||
* Reduce a serialized resource identifier ("provider:service:collection:entity")
|
||||
* to its own (last) segment; plain values pass through unchanged
|
||||
*/
|
||||
function plainIdentifier(value: string | number | null | undefined): string | number | null {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return value ?? null;
|
||||
}
|
||||
const text = String(value);
|
||||
const index = text.lastIndexOf(':');
|
||||
return index >= 0 ? text.slice(index + 1) : value;
|
||||
}
|
||||
|
||||
export class EntityObject implements EntityModelInterface {
|
||||
|
||||
_data!: EntityInterface<DocumentInterface|DocumentModelInterface>;
|
||||
@@ -25,7 +38,14 @@ export class EntityObject implements EntityModelInterface {
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = data
|
||||
// the wire format carries full resource identifiers (provider:service:collection:entity);
|
||||
// reduce them to plain ids, which is what the stores and UI operate on
|
||||
this._data = {
|
||||
...data,
|
||||
service: String(plainIdentifier(data.service) ?? ''),
|
||||
collection: plainIdentifier(data.collection) ?? '',
|
||||
identifier: plainIdentifier(data.identifier) ?? '',
|
||||
}
|
||||
if (data.properties) {
|
||||
this._data.properties = new DocumentObject().fromJson(data.properties as DocumentInterface);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user