kopia lustrzana https://github.com/activitypub-js/activitypub-models
21dd474678 | ||
---|---|---|
src | ||
.gitignore | ||
LICENSE | ||
README.md | ||
package-lock.json | ||
package.json | ||
tsconfig.json |
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