npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mstdn-entities

v0.0.1

Published

mastodon entities package

Downloads

5

Readme

mstdn-entities

Build Status Coverage Status Dependency Status Maintainability

This library provides Mastodon Entities Class (e.g. Account, Status) and API in order to generate them.

Note

This library uses Decorators and Reflection-API (polyfill), so transpiling is highly recommended, but Babel is untested :(
Currently, Typescript is recommended.

Install and Usage of Presets

$ npm install -S mstdn-entities will be available some time soon

The presets of Mastodon Entities Class are avalable.

import { Account } from 'mstdn-entities'
YOUR_HTTP_CLIENT.get('/api/v1/accounts/verify_credentials') // as Promise<Object>
  .then((data: Object) => {
    const account = new Account(data) // Type-safe
  })

All propertyNames are converted from snake_case to camelCase.

const plainObj = {
  display_name: 'name'
}

const account = new Account(plainObj)
console.log(account.displayName) // === "name"
console.log(account.display_name) // === undefined

/*
  Account {
    displayName: 'name' // converted to camelCase automatically
  }
*/

const plainAccountObj = account.toJSON()
/*
  {
    display_name: 'name' // return the original in this case
  }
*/

"created_at" property, its ISOString will be converted to Date object.

If you use your own Date API (e.g. moment), you can create your custom class. (explained later)

const plainObj = {
  created_at: '2011-10-05T14:48:00.000Z'
}

const account = new Account(plainObj)

/*
  Account {
    createdAt: new Date('2011-10-05T14:48:00.000Z') // converted to Date object automatically
  }
*/

const plainAccountObj = account.toJSON()
/*
  {
    created_at: '2011-10-05T14:48:00.000Z' // using Date#toJSON() method
  }
*/

List of Presets

  • Account
  • Application (untested)
  • Attachment (untested)
  • Card (untested)
  • Context (untested)
  • Emoji (untested)
  • Instance (untested)
  • List (untested)
  • MastodonError (untested)
  • Mention (untested)
  • Notification (untested)
  • Relationship (untested)
  • Report (untested)
  • Results (untested)
  • Status (untested)
  • Tag

are available. I will test all the above some time.

MastodonError is just an object. (not built-in Error)

Mastodon Entities API

You can create your own custom class.

You should decorate all properties.

import * as moment from 'moment'
import { Entity, convert, param } from 'mstdn-entities'
class CustomAccount extends Entity {
  @param id: number
  @param username: string
  @convert(value => moment(value)) createdAt: any
  /*
    ...
  */
}

or babel user, (untested)

import moment from 'moment'
import { Entity, convert, is, param } from 'mstdn-entities'
class CustomAccount extends Entity {
  @is(Number) id
  @param username
  @convert(value => moment(value)) createdAt
  /*
    ...
  */
}

Decorators

convert

arg[0]: callback return: PropertyDecorator

@is, @are and @param decorators are all syntax-sugar of @convert.

is

arg[0]: Function (e.g. String, Number, UserDefinedClass) return: PropertyDecorator

If your property type is Date, use @is(Date) decorator explicitly.

are

arg[0]: Function return: PropertyDecorator

If your property type is Array, use @are(Function) decorator explicitly.

If string[], then use @are(String).

If Account[], then use @are(Account).

param

This is a property decorator itself.

If your property is Number, String, Boolean, Object, or YourOwnClass, you do not have to use @is. (JSON.parse will work properly.)

If your property is Array, use @are decorator.

If your property is Date or JSON.parse will not work, use @is decorator.