Extendable ActivityPub JS/TS models with official docs
 
Go to file
SiRanWeb 21dd474678 1.0.1 2022-11-26 14:59:35 +03:00
src Release 1.0.0 (#5) 2022-11-26 14:53:36 +03:00
.gitignore prepare to npm publish 2022-10-23 19:27:10 +03:00
LICENSE Release 1.0.0 (#5) 2022-11-26 14:53:36 +03:00
README.md Release 1.0.0 (#5) 2022-11-26 14:53:36 +03:00
package-lock.json 1.0.1 2022-11-26 14:59:35 +03:00
package.json 1.0.1 2022-11-26 14:59:35 +03:00
tsconfig.json Release 1.0.0 (#5) 2022-11-26 14:53:36 +03:00

README.md

ActivityPub Models

Extendable ActivityPub JS/TS models with official docs

Table of Contents

Features

  • All models implemented
  • In-code documentation
  • Ability to create/extend own models
  • TypeScript and JavaScript support

Docs

Install with npm:

npm i activitypub-models

Install with yarn:

yarn add activitypub-models

Use of models:

import { Note, Link, contexts } from 'activitypub-models';

// "type" already provided, but can be rewrited
const imageLink = Link.create({
    href: 'https://example.org/martin/image.jpg',
    mediaType: 'image/jpeg'
});

const note = Note.create({
    '@context': contexts.activityStreamsV2,
    mediaType: 'text/plain',
    image: imageLink, // supports nested objects
});

note.content = 'some content';
note.content = 123; // TypeError: only string available for "content"

const obj = note.toPlain(); // convert to simple JS object
const json = note.toJSON(); // convert to JSON

Creating own models with TypeScript:

import { NoteFields, APBase, ASModelType } from 'activitypub-models';

interface CustomNoteFields extends NoteFields {
    myField: string | string[];
}

class CustomNote extends APBase<CustomNoteFields> {
    static create(fields: CustomNoteFields) {
        return APBase._create<CustomNoteFields>({
            type: ASModelType.Note,
            ...fields,
        });
    }
}

const customNote = CustomNote.create({
    myField: 'I am a new field!'
});

You also can create models with plain JS, but you will lose types. Can be fixed with .d.ts and jsdoc:

// CustomNote.d.ts
import { APBase, NoteFields, WithContext } from "activitypub-models";
export interface CustomNotesFields extends NoteFields {
    myField: string | string[];
}
export type CustomNoteType = APBase<CustomNotesFields> & CustomNotesFields & WithContext;

// CustomNote.js
import {APBase, ASModelType} from "activitypub-models";

class CustomNote extends APBase {
    static create(fields) {
        return APBase._create({
            type: ASModelType.Note,
            ...fields,
        });
    }
}

/** @type {CustomNoteType} */
const customNote = CustomNote.create({
    myField: 'I am a new field!'
});

Future plans

You can check current tasks here

Contact

You can contact me via matrix or telegram

License

MIT