From dda8be976dac503d204d4a000524742a6d97de29 Mon Sep 17 00:00:00 2001 From: SiRanWeb Date: Mon, 7 Nov 2022 19:45:34 +0300 Subject: [PATCH] implement proxy --- package.json | 4 +- src/common/types.ts | 13 +++- src/common/utils.ts | 2 + src/index.ts | 1 - src/models/activity/Activity.model.ts | 8 +-- src/models/actor/Actor.model.ts | 10 ++-- src/models/apBase/APBase.model.ts | 60 +++++++++++-------- src/models/apEvent/APEvent.model.ts | 8 +-- src/models/apObject/APObject.model.ts | 8 +-- src/models/apObject/APObject.types.ts | 4 +- src/models/application/Application.model.ts | 8 +-- src/models/article/Article.model.ts | 8 +-- src/models/audio/Audio.model.ts | 8 +-- src/models/collection/Collection.model.ts | 8 +-- .../collectionPage/CollectionPage.model.ts | 8 +-- src/models/document/Document.model.ts | 8 +-- src/models/group/Group.model.ts | 8 +-- src/models/image/Image.model.ts | 8 +-- .../IntransitiveActivity.model.ts | 8 +-- src/models/link/Link.model.ts | 8 +-- src/models/link/Link.types.ts | 1 + src/models/mention/Mention.model.ts | 9 +-- src/models/note/Note.model.ts | 8 +-- .../OrderedCollection.model.ts | 8 +-- .../OrderedCollectionPage.model.ts | 8 +-- src/models/organization/Organization.model.ts | 8 +-- src/models/page/Page.model.ts | 8 +-- src/models/place/Place.model.ts | 8 +-- src/models/profile/Profile.model.ts | 8 +-- src/models/question/Question.model.ts | 8 +-- src/models/relationship/Relationship.model.ts | 8 +-- src/models/service/Service.model.ts | 8 +-- src/models/tombstone/Tombstone.model.ts | 8 +-- src/models/video/Video.model.ts | 8 +-- 34 files changed, 165 insertions(+), 139 deletions(-) diff --git a/package.json b/package.json index e2a9209..e0c5122 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "scripts": { "tsc": "tsc -p ./tsconfig.json", "tsc:watch": "tsc -w -p ./tsconfig.json", - "start": "node --es-module-specifier-resolution=node build/index.js", - "start:dev": "nodemon --es-module-specifier-resolution=node build/index.js" + "start": "node --es-module-specifier-resolution=node dist/index.js", + "start:dev": "nodemon --es-module-specifier-resolution=node dist/index.js" }, "files": [ "dist" diff --git a/src/common/types.ts b/src/common/types.ts index bc44762..b5ac258 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -39,6 +39,7 @@ export enum ASModelType { Question = 'Question', Tombstone = 'Tombstone', Profile = 'Profile', + Actor = 'Actor', Link = 'Link', Mention = 'Mention', Collection = 'Collection', @@ -146,4 +147,14 @@ export type OauthAuthorizationEndpointValue = string; export type OauthTokenEndpointValue = string; export type ProvideClientKeyValue = string; export type SignClientKeyValue = string; -export type SharedInboxValue = string; \ No newline at end of file +export type SharedInboxValue = string; + +export interface WithContext { + /** + * JSON-LD uses the special @context property to define the processing context. + * The value of the @context property is defined by the [JSON-LD] specification. + * + * {@link https://www.w3.org/TR/activitystreams-core/#jsonld Docs} + */ + '@context'?: ContextValue | ContextValue[]; +} \ No newline at end of file diff --git a/src/common/utils.ts b/src/common/utils.ts index 0f4ea36..2e7418c 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -1,5 +1,7 @@ export type Modify = Omit & R; +export const hasKey = (obj: T, k: keyof any): k is keyof T => k in obj; + export const cloneObjDeep = (obj: T): T => { const duplicateObj = {} as T; for (const key in obj) { diff --git a/src/index.ts b/src/index.ts index 8b13789..e69de29 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +0,0 @@ - diff --git a/src/models/activity/Activity.model.ts b/src/models/activity/Activity.model.ts index 456044d..491b9ca 100644 --- a/src/models/activity/Activity.model.ts +++ b/src/models/activity/Activity.model.ts @@ -11,10 +11,10 @@ import {ASModelType} from "../../common/types"; * {@link https://www.w3.org/ns/activitystreams#Activity Docs} */ export class Activity extends APBase{ - constructor(fields: ActivityFields) { - super({ + static create(fields: ActivityFields) { + return APBase._create({ type: ASModelType.Activity, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/actor/Actor.model.ts b/src/models/actor/Actor.model.ts index 1efaf94..acec776 100644 --- a/src/models/actor/Actor.model.ts +++ b/src/models/actor/Actor.model.ts @@ -13,10 +13,10 @@ import {ActorFields} from "./Actor.types"; * {@link https://www.w3.org/TR/activitypub/#actors Docs} */ export class Actor extends APBase{ - constructor(fields: ActorFields) { - super({ - type: ASModelType.Profile, - ...fields - }) + static create(fields: ActorFields) { + return APBase._create({ + type: ASModelType.Actor, + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/apBase/APBase.model.ts b/src/models/apBase/APBase.model.ts index df2fa6d..f249302 100644 --- a/src/models/apBase/APBase.model.ts +++ b/src/models/apBase/APBase.model.ts @@ -1,31 +1,43 @@ -import { contexts } from "../../common/constants"; -import { ContextValue } from "../../common/types"; -import { cloneObjDeep } from "../../common/utils"; +import {cloneObjDeep, hasKey} from "../../common/utils"; +import { WithContext } from "../../common/types"; -interface ContextField { - '@context'?: ContextValue | ContextValue[]; -} -export class APBase { - public fields: T & ContextField; +export class APBase { + public fields: T; - constructor(fields: T & ContextField) { - const obj = cloneObjDeep(fields); - this.fields = { - ...obj, - } as T & ContextField; - } - - 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 + protected constructor(fields: T) { + // TODO move ordering to toPlain + const obj = cloneObjDeep(fields); + if ('@context' in obj) { + this.fields = { + '@context': obj['@context'], + } as T; + delete obj["@context"]; + } else { + this.fields = {} as T; } - return this; + + this.fields = { + ...this.fields, + ...obj + }; + } + + protected static _create(fields: T) { + const data = new APBase(fields); + return new Proxy(data, { + get(target: APBase, p: keyof APBase & keyof T) { + return hasKey(target, p) ? target[p] : target.fields[p]; + }, + set(target: APBase, p: keyof APBase & keyof T, newValue: any): boolean { + target.fields[p] = newValue; + return true; + } + }) as APBase & T & WithContext; + } + + protected parseValue(value: any): any { + return value instanceof APBase ? value.toPlain() : value; } public toPlain(): Record { diff --git a/src/models/apEvent/APEvent.model.ts b/src/models/apEvent/APEvent.model.ts index 346b317..6467fac 100644 --- a/src/models/apEvent/APEvent.model.ts +++ b/src/models/apEvent/APEvent.model.ts @@ -8,10 +8,10 @@ import {APEventFields} from "./APEvent.types"; * {@link https://www.w3.org/ns/activitystreams#Event Docs} */ export class APEvent extends APBase{ - constructor(fields: APEventFields) { - super({ + static create(fields: APEventFields) { + return APBase._create({ type: ASModelType.Event, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/apObject/APObject.model.ts b/src/models/apObject/APObject.model.ts index b213610..1e48cde 100644 --- a/src/models/apObject/APObject.model.ts +++ b/src/models/apObject/APObject.model.ts @@ -10,10 +10,10 @@ import {ASModelType} from "../../common/types"; * {@link https://www.w3.org/ns/activitystreams#Object Docs} */ export class APObject extends APBase{ - constructor(fields: APObjectFields) { - super({ + static create(fields: APObjectFields) { + return APBase._create({ type: ASModelType.Object, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/apObject/APObject.types.ts b/src/models/apObject/APObject.types.ts index 5d9225d..0891c82 100644 --- a/src/models/apObject/APObject.types.ts +++ b/src/models/apObject/APObject.types.ts @@ -28,10 +28,10 @@ import { SummaryValue, SummaryMapValue, UpdatedValue, - DurationValue, SourceValue, LikesValue, SharesValue, + DurationValue, SourceValue, LikesValue, SharesValue, ContextValue, WithContext, } from "../../common/types"; -export interface APObjectFields { +export interface APObjectFields extends WithContext { /** * Provides the globally unique identifier for * an {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Object} diff --git a/src/models/application/Application.model.ts b/src/models/application/Application.model.ts index 44260e7..c21fc7a 100644 --- a/src/models/application/Application.model.ts +++ b/src/models/application/Application.model.ts @@ -8,10 +8,10 @@ import {ApplicationFields} from "./Application.types"; * {@link https://www.w3.org/ns/activitystreams#Application Docs} */ export class Application extends APBase{ - constructor(fields: ApplicationFields) { - super({ + static create(fields: ApplicationFields) { + return APBase._create({ type: ASModelType.Application, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/article/Article.model.ts b/src/models/article/Article.model.ts index e43094e..e182ee4 100644 --- a/src/models/article/Article.model.ts +++ b/src/models/article/Article.model.ts @@ -8,10 +8,10 @@ import {ArticleFields} from "./Article.types"; * {@link https://www.w3.org/ns/activitystreams#Article Docs} */ export class Article extends APBase{ - constructor(fields: ArticleFields) { - super({ + static create(fields: ArticleFields) { + return APBase._create({ type: ASModelType.Article, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/audio/Audio.model.ts b/src/models/audio/Audio.model.ts index f1ff133..ce74973 100644 --- a/src/models/audio/Audio.model.ts +++ b/src/models/audio/Audio.model.ts @@ -8,10 +8,10 @@ import {AudioFields} from "./Audio.types"; * {@link https://www.w3.org/ns/activitystreams#Audio Docs} */ export class Audio extends APBase{ - constructor(fields: AudioFields) { - super({ + static create(fields: AudioFields) { + return APBase._create({ type: ASModelType.Audio, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/collection/Collection.model.ts b/src/models/collection/Collection.model.ts index 094ddf8..db90db1 100644 --- a/src/models/collection/Collection.model.ts +++ b/src/models/collection/Collection.model.ts @@ -9,10 +9,10 @@ import {CollectionFields} from "./Collection.types"; * {@link https://www.w3.org/ns/activitystreams#Collection Docs} */ export class Collection extends APBase{ - constructor(fields: CollectionFields) { - super({ + static create(fields: CollectionFields) { + return APBase._create({ type: ASModelType.Collection, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/collectionPage/CollectionPage.model.ts b/src/models/collectionPage/CollectionPage.model.ts index e694a9e..3f4f2e2 100644 --- a/src/models/collectionPage/CollectionPage.model.ts +++ b/src/models/collectionPage/CollectionPage.model.ts @@ -8,10 +8,10 @@ import {CollectionPageFields} from "./CollectionPage.types"; * {@link https://www.w3.org/ns/activitystreams#CollectionPage Docs} */ export class CollectionPage extends APBase{ - constructor(fields: CollectionPageFields) { - super({ + static create(fields: CollectionPageFields) { + return APBase._create({ type: ASModelType.CollectionPage, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/document/Document.model.ts b/src/models/document/Document.model.ts index b1b1aa2..474bb03 100644 --- a/src/models/document/Document.model.ts +++ b/src/models/document/Document.model.ts @@ -8,10 +8,10 @@ import {DocumentFields} from "./Document.types"; * {@link https://www.w3.org/ns/activitystreams#Document Docs} */ export class Document extends APBase{ - constructor(fields: DocumentFields) { - super({ + static create(fields: DocumentFields) { + return APBase._create({ type: ASModelType.Document, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/group/Group.model.ts b/src/models/group/Group.model.ts index 2af642d..c8081b8 100644 --- a/src/models/group/Group.model.ts +++ b/src/models/group/Group.model.ts @@ -8,10 +8,10 @@ import {GroupFields} from "./Group.types"; * {@link https://www.w3.org/ns/activitystreams#Group Docs} */ export class Group extends APBase{ - constructor(fields: GroupFields) { - super({ + static create(fields: GroupFields) { + return APBase._create({ type: ASModelType.Group, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/image/Image.model.ts b/src/models/image/Image.model.ts index 92e6e44..bbe3987 100644 --- a/src/models/image/Image.model.ts +++ b/src/models/image/Image.model.ts @@ -8,10 +8,10 @@ import {ImageFields} from "./Image.types"; * {@link https://www.w3.org/ns/activitystreams#Image Docs} */ export class Image extends APBase{ - constructor(fields: ImageFields) { - super({ + static create(fields: ImageFields) { + return APBase._create({ type: ASModelType.Image, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/intransitiveActivity/IntransitiveActivity.model.ts b/src/models/intransitiveActivity/IntransitiveActivity.model.ts index 008ffbe..a351119 100644 --- a/src/models/intransitiveActivity/IntransitiveActivity.model.ts +++ b/src/models/intransitiveActivity/IntransitiveActivity.model.ts @@ -10,10 +10,10 @@ import {ASModelType} from "../../common/types"; * {@link https://www.w3.org/ns/activitystreams#IntransitiveActivity Docs} */ export class IntransitiveActivity extends APBase{ - constructor(fields: IntransitiveActivityFields) { - super({ + static create(fields: IntransitiveActivityFields) { + return APBase._create({ type: ASModelType.Activity, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/link/Link.model.ts b/src/models/link/Link.model.ts index c97f3b4..a43029d 100644 --- a/src/models/link/Link.model.ts +++ b/src/models/link/Link.model.ts @@ -13,10 +13,10 @@ import {ASModelType} from "../../common/types"; * {@link https://www.w3.org/ns/activitystreams#Link Docs} */ export class Link extends APBase{ - constructor(fields: LinkFields) { - super({ + static create(fields: LinkFields) { + return APBase._create({ type: ASModelType.Link, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/link/Link.types.ts b/src/models/link/Link.types.ts index 785298e..83fe485 100644 --- a/src/models/link/Link.types.ts +++ b/src/models/link/Link.types.ts @@ -9,6 +9,7 @@ import { } from "../../common/types"; export interface LinkFields { + '@context': never; /** * Identifies the {@link https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object Object} diff --git a/src/models/mention/Mention.model.ts b/src/models/mention/Mention.model.ts index d2c9286..cb13596 100644 --- a/src/models/mention/Mention.model.ts +++ b/src/models/mention/Mention.model.ts @@ -1,16 +1,17 @@ import {APBase} from "../apBase/APBase.model"; import {MentionFields} from "./Mention.types"; import {ASModelType} from "../../common/types"; + /** * A specialized Link that represents an @mention. * * {@link https://www.w3.org/ns/activitystreams#Mention Docs} */ export class Mention extends APBase{ - constructor(fields: MentionFields) { - super({ + static create(fields: MentionFields) { + return APBase._create({ type: ASModelType.Mention, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/note/Note.model.ts b/src/models/note/Note.model.ts index f40b22a..914149e 100644 --- a/src/models/note/Note.model.ts +++ b/src/models/note/Note.model.ts @@ -8,10 +8,10 @@ import {NoteFields} from "./Note.types"; * {@link https://www.w3.org/ns/activitystreams#Note Docs} */ export class Note extends APBase{ - constructor(fields: NoteFields) { - super({ + static create(fields: NoteFields) { + return APBase._create({ type: ASModelType.Note, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/orderedCollection/OrderedCollection.model.ts b/src/models/orderedCollection/OrderedCollection.model.ts index fb2ec39..d66cfde 100644 --- a/src/models/orderedCollection/OrderedCollection.model.ts +++ b/src/models/orderedCollection/OrderedCollection.model.ts @@ -9,10 +9,10 @@ import {OrderedCollectionFields} from "./OrderedCollection.types"; * {@link https://www.w3.org/ns/activitystreams#OrderedCollection Docs} */ export class OrderedCollection extends APBase{ - constructor(fields: OrderedCollectionFields) { - super({ + static create(fields: OrderedCollectionFields) { + return APBase._create({ type: ASModelType.OrderedCollection, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/orderedCollectionPage/OrderedCollectionPage.model.ts b/src/models/orderedCollectionPage/OrderedCollectionPage.model.ts index c14a3c6..54ccae0 100644 --- a/src/models/orderedCollectionPage/OrderedCollectionPage.model.ts +++ b/src/models/orderedCollectionPage/OrderedCollectionPage.model.ts @@ -8,10 +8,10 @@ import {OrderedCollectionPageFields} from "./OrderedCollectionPage.types"; * {@link https://www.w3.org/ns/activitystreams#OrderedCollectionPage Docs} */ export class OrderedCollectionPage extends APBase{ - constructor(fields: OrderedCollectionPageFields) { - super({ + static create(fields: OrderedCollectionPageFields) { + return APBase._create({ type: ASModelType.OrderedCollectionPage, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/organization/Organization.model.ts b/src/models/organization/Organization.model.ts index 50d4b23..ada9070 100644 --- a/src/models/organization/Organization.model.ts +++ b/src/models/organization/Organization.model.ts @@ -8,10 +8,10 @@ import {OrganizationFields} from "./Organization.types"; * {@link https://www.w3.org/ns/activitystreams#Organization Docs} */ export class Organization extends APBase{ - constructor(fields: OrganizationFields) { - super({ + static create(fields: OrganizationFields) { + return APBase._create({ type: ASModelType.Organization, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/page/Page.model.ts b/src/models/page/Page.model.ts index 325672d..d0f4a17 100644 --- a/src/models/page/Page.model.ts +++ b/src/models/page/Page.model.ts @@ -8,10 +8,10 @@ import {PageFields} from "./Page.types"; * {@link https://www.w3.org/ns/activitystreams#Page Docs} */ export class Page extends APBase{ - constructor(fields: PageFields) { - super({ + static create(fields: PageFields) { + return APBase._create({ type: ASModelType.Page, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/place/Place.model.ts b/src/models/place/Place.model.ts index 2baa76d..64be315 100644 --- a/src/models/place/Place.model.ts +++ b/src/models/place/Place.model.ts @@ -10,10 +10,10 @@ import {PlaceFields} from "./Place.types"; * {@link https://www.w3.org/ns/activitystreams#Place Docs} */ export class Place extends APBase{ - constructor(fields: PlaceFields) { - super({ + static create(fields: PlaceFields) { + return APBase._create({ type: ASModelType.Place, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/profile/Profile.model.ts b/src/models/profile/Profile.model.ts index e23856c..2956559 100644 --- a/src/models/profile/Profile.model.ts +++ b/src/models/profile/Profile.model.ts @@ -10,10 +10,10 @@ import {ProfileFields} from "./Profile.types"; * {@link https://www.w3.org/ns/activitystreams#Profile Docs} */ export class Profile extends APBase{ - constructor(fields: ProfileFields) { - super({ + static create(fields: ProfileFields) { + return APBase._create({ type: ASModelType.Profile, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/question/Question.model.ts b/src/models/question/Question.model.ts index ab36fc8..fe12f2c 100644 --- a/src/models/question/Question.model.ts +++ b/src/models/question/Question.model.ts @@ -14,10 +14,10 @@ import {QuestionFields} from "./Question.types"; * {@link https://www.w3.org/ns/activitystreams#Question Docs} */ export class Question extends APBase{ - constructor(fields: QuestionFields) { - super({ + static create(fields: QuestionFields) { + return APBase._create({ type: ASModelType.Question, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/relationship/Relationship.model.ts b/src/models/relationship/Relationship.model.ts index d10c03e..b7bf7f0 100644 --- a/src/models/relationship/Relationship.model.ts +++ b/src/models/relationship/Relationship.model.ts @@ -11,10 +11,10 @@ import {RelationshipFields} from "./Relationship.types"; * {@link https://www.w3.org/ns/activitystreams#Relationship Docs} */ export class Relationship extends APBase{ - constructor(fields: RelationshipFields) { - super({ + static create(fields: RelationshipFields) { + return APBase._create({ type: ASModelType.Relationship, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/service/Service.model.ts b/src/models/service/Service.model.ts index 9c24a1c..68635c2 100644 --- a/src/models/service/Service.model.ts +++ b/src/models/service/Service.model.ts @@ -8,10 +8,10 @@ import {ServiceFields} from "./Service.types"; * {@link https://www.w3.org/ns/activitystreams#Service Docs} */ export class Service extends APBase{ - constructor(fields: ServiceFields) { - super({ + static create(fields: ServiceFields) { + return APBase._create({ type: ASModelType.Service, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/tombstone/Tombstone.model.ts b/src/models/tombstone/Tombstone.model.ts index c03fbcf..8c25e2e 100644 --- a/src/models/tombstone/Tombstone.model.ts +++ b/src/models/tombstone/Tombstone.model.ts @@ -10,10 +10,10 @@ import {TombstoneFields} from "./Tombstone.types"; * {@link https://www.w3.org/ns/activitystreams#Tombstone Docs} */ export class Tombstone extends APBase{ - constructor(fields: TombstoneFields) { - super({ + static create(fields: TombstoneFields) { + return APBase._create({ type: ASModelType.Tombstone, - ...fields - }) + ...fields, + }); } } \ No newline at end of file diff --git a/src/models/video/Video.model.ts b/src/models/video/Video.model.ts index 18c8405..59330cc 100644 --- a/src/models/video/Video.model.ts +++ b/src/models/video/Video.model.ts @@ -8,10 +8,10 @@ import {VideoFields} from "./Video.types"; * {@link https://www.w3.org/ns/activitystreams#Video Docs} */ export class Video extends APBase{ - constructor(fields: VideoFields) { - super({ + static create(fields: VideoFields) { + return APBase._create({ type: ASModelType.Video, - ...fields - }) + ...fields, + }); } } \ No newline at end of file