kopia lustrzana https://github.com/activitypub-js/activitypub-models
update base model & refactor
rodzic
1be5e6a340
commit
b806943b70
|
@ -55,6 +55,7 @@ export type LanguageTag = string;
|
|||
export type MediaType = string;
|
||||
export type Duration = string;
|
||||
|
||||
export type ContextValue = string | Record<string, string>;
|
||||
export type UrlValue = string | Link;
|
||||
export type IdValue = string;
|
||||
export type TypeValue = string;
|
|
@ -1 +1,13 @@
|
|||
export type Modify<T, R> = Omit<T, keyof R> & R;
|
||||
export type Modify<T, R> = Omit<T, keyof R> & R;
|
||||
|
||||
export const cloneObjDeep = <T>(obj: T): T => {
|
||||
const duplicateObj = {} as T;
|
||||
for (const key in obj) {
|
||||
if(typeof obj[key] === 'object') {
|
||||
duplicateObj[key] = cloneObjDeep(obj[key]);
|
||||
} else {
|
||||
duplicateObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
return duplicateObj;
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import { ActivityFields } from "./Activity.types";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
|
||||
/**
|
||||
* An Activity is a subtype of Object that describes some form of action that may happen,
|
||||
|
|
|
@ -6,7 +6,7 @@ import {
|
|||
OriginValue,
|
||||
ResultValue,
|
||||
TargetValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
export interface ActivityFields extends APObjectFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ActorFields} from "./Actor.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
ProxyUrlValue, SharedInboxValue,
|
||||
SignClientKeyValue,
|
||||
StreamsValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
export interface EndpointsValue {
|
||||
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
import { contexts } from "../../common/common.constants";
|
||||
import { contexts } from "../../common/constants";
|
||||
import { ContextValue } from "../../common/types";
|
||||
import { cloneObjDeep } from "../../common/utils";
|
||||
|
||||
// TODO: types
|
||||
// TODO: renaming/refactor
|
||||
export type Context = string | any[];
|
||||
|
||||
export interface ModelBaseAPWithContext {
|
||||
'@context'?: Context;
|
||||
interface ContextField {
|
||||
'@context'?: ContextValue | ContextValue[];
|
||||
}
|
||||
|
||||
export class APBase<T> {
|
||||
public fields: T & ModelBaseAPWithContext;
|
||||
public fields: T & ContextField;
|
||||
|
||||
constructor(fields: T) {
|
||||
// TODO: make recursive copy
|
||||
constructor(fields: T & ContextField) {
|
||||
const obj = cloneObjDeep<T & ContextField>(fields);
|
||||
this.fields = {
|
||||
...fields,
|
||||
} as T & ModelBaseAPWithContext;
|
||||
...obj,
|
||||
} as T & ContextField;
|
||||
}
|
||||
|
||||
// TODO: rewrite (better option to handle multiple contexts)
|
||||
// some comment
|
||||
public setContext(context?: Context): this {
|
||||
private parseValue(value: any): any {
|
||||
return value instanceof APBase ? value.toPlain() : value;
|
||||
}
|
||||
|
||||
public withContext(context?: ContextValue): this {
|
||||
this.fields = {
|
||||
['@context']: context || contexts.activityStreamsV2,
|
||||
...this.fields
|
||||
|
@ -28,20 +28,33 @@ export class APBase<T> {
|
|||
return this;
|
||||
}
|
||||
|
||||
public plain(): Record<string, any> {
|
||||
public toPlain(): Record<string, any> {
|
||||
const result = {} as Record<string, any>;
|
||||
for (const [key, node] of Object.entries(this.fields)) {
|
||||
if (node instanceof APBase) {
|
||||
result[key] = node.plain();
|
||||
for (const [key, value] of Object.entries(this.fields)) {
|
||||
const isFunction = typeof value === 'function';
|
||||
const isArray = Array.isArray(value);
|
||||
const isNull = value === null;
|
||||
const isUndefined = value === undefined;
|
||||
const isObject = !isArray && !isNull && typeof value === 'object';
|
||||
const isPlain = !isArray && !isNull && !isUndefined && !isObject && !isFunction;
|
||||
|
||||
if (isArray) {
|
||||
result[key] = value.map(v => this.parseValue(v));
|
||||
} else if (isNull || isUndefined) {
|
||||
result[key] = null;
|
||||
} else if (isObject) {
|
||||
result[key] = {};
|
||||
Object.entries(value).forEach(([k, v]) => result[key][k] = this.parseValue(v));
|
||||
} else if (isPlain) {
|
||||
result[key] = this.parseValue(value);
|
||||
} else {
|
||||
result[key] = node;
|
||||
throw new Error(`Unable to convert key ${key} with value ${value}. Type of value: ${typeof value}`);
|
||||
}
|
||||
}
|
||||
// TODO make recursive copy
|
||||
return result;
|
||||
}
|
||||
|
||||
public json() {
|
||||
return JSON.stringify(this.plain());
|
||||
public toJSON(): string {
|
||||
return JSON.stringify(this.toPlain());
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {APEventFields} from "./APEvent.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import { APObjectFields } from "./APObject.types";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
|
||||
/**
|
||||
* Describes an object of any kind. The Object type serves as the base type for most of
|
||||
|
|
|
@ -29,7 +29,7 @@ import {
|
|||
SummaryMapValue,
|
||||
UpdatedValue,
|
||||
DurationValue, SourceValue, LikesValue, SharesValue,
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
export interface APObjectFields {
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ApplicationFields} from "./Application.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ArticleFields} from "./Article.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {AudioFields} from "./Audio.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {CollectionFields} from "./Collection.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
CollectionFirstValue, CollectionItemsValue,
|
||||
CollectionLastValue,
|
||||
TotalItemsValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
export interface CollectionFields extends APObjectFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {CollectionPageFields} from "./CollectionPage.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {CollectionFields} from "../collection/Collection.types";
|
||||
import {CollectionPageNextValue, CollectionPagePartOfValue, CollectionPagePrevValue} from "../../common/common.types";
|
||||
import {CollectionPageNextValue, CollectionPagePartOfValue, CollectionPagePrevValue} from "../../common/types";
|
||||
|
||||
export interface CollectionPageFields extends CollectionFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {DocumentFields} from "./Document.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {GroupFields} from "./Group.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ImageFields} from "./Image.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {IntransitiveActivityFields} from './IntransitiveActivity.types';
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
|
||||
/**
|
||||
* Instances of intransitiveActivity are a subtype of
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import { LinkFields } from "./Link.types";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
|
||||
/**
|
||||
* A Link is an indirect, qualified reference to a resource identified by a URL.
|
||||
|
|
|
@ -2,12 +2,11 @@ import {
|
|||
HeightValue,
|
||||
HreflangValue,
|
||||
HrefValue,
|
||||
IdValue,
|
||||
MediaTypeValue,
|
||||
NameMapValue,
|
||||
NameValue, PreviewValue,
|
||||
RelValue, TypeValue, WidthValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
export interface LinkFields {
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {MentionFields} from "./Mention.types";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
/**
|
||||
* A specialized Link that represents an @mention.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {NoteFields} from "./Note.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {OrderedCollectionFields} from "./OrderedCollection.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,7 +2,7 @@ import {
|
|||
OrderedCollectionCurrentValue,
|
||||
OrderedCollectionFirstValue, OrderedCollectionItemsValue,
|
||||
OrderedCollectionLastValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
import {CollectionFields} from "../collection/Collection.types";
|
||||
import {Modify} from "../../common/utils";
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {OrderedCollectionPageFields} from "./OrderedCollectionPage.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
OrderedCollectionPagePartOfValue,
|
||||
OrderedCollectionPagePrevValue,
|
||||
StartIndexValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
import {CollectionPageFields} from "../collectionPage/CollectionPage.types";
|
||||
import {Modify} from "../../common/utils";
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {OrganizationFields} from "./Organization.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {PageFields} from "./Page.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {PersonFields} from "./Person.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {PlaceFields} from "./Place.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,7 @@ import {
|
|||
LongitudeValue,
|
||||
RadiusValue,
|
||||
UnitsValue
|
||||
} from "../../common/common.types";
|
||||
} from "../../common/types";
|
||||
|
||||
|
||||
export interface PlaceFields extends APObjectFields {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ProfileFields} from "./Profile.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
import {APObjectFields} from "../apObject/APObject.types";
|
||||
import {DescribesValue} from "../../common/common.types";
|
||||
import {DescribesValue} from "../../common/types";
|
||||
|
||||
export interface ProfileFields extends APObjectFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {QuestionFields} from "./Question.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {IntransitiveActivityFields} from "../intransitiveActivity/IntransitiveActivity.types";
|
||||
import {AnyOfValue, ClosedValue, OneOfValue} from "../../common/common.types";
|
||||
import {AnyOfValue, ClosedValue, OneOfValue} from "../../common/types";
|
||||
|
||||
export interface QuestionFields extends IntransitiveActivityFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {RelationshipFields} from "./Relationship.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APObjectFields} from "../apObject/APObject.types";
|
||||
import {SubjectValue, ObjectValue, RelationshipValue} from "../../common/common.types";
|
||||
import {SubjectValue, ObjectValue, RelationshipValue} from "../../common/types";
|
||||
|
||||
export interface RelationshipFields extends APObjectFields {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {ServiceFields} from "./Service.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {TombstoneFields} from "./Tombstone.types";
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {DeletedValue, FormerTypeValue} from "../../common/common.types";
|
||||
import {DeletedValue, FormerTypeValue} from "../../common/types";
|
||||
import {APObjectFields} from "../apObject/APObject.types";
|
||||
|
||||
export interface TombstoneFields extends APObjectFields {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {APBase} from "../apBase/APBase.model";
|
||||
import {ASModelType} from "../../common/common.types";
|
||||
import {ASModelType} from "../../common/types";
|
||||
import {VideoFields} from "./Video.types";
|
||||
|
||||
/**
|
||||
|
|
Ładowanie…
Reference in New Issue