Go to file
2022-01-21 21:45:25 +09:00
.github Fix dependabot target for workspace 2022-01-02 22:04:38 +09:00
docs Use monorepo for examples 2022-01-02 16:28:11 +09:00
example Bump to version 4.0.0 2022-01-02 22:38:25 +09:00
megalodon Bump @typescript-eslint/eslint-plugin from 4.33.0 to 5.8.1 2022-01-02 13:51:55 +00:00
.eslintrc.js refs #164 Disable an eslint rule for promise param-names 2020-02-01 15:30:58 +09:00
.gitignore Create browser example 2019-11-27 23:14:12 +09:00
.prettierrc refs #330 Add emoji reaction API for Pleroma 2020-04-16 21:38:56 +09:00
CODEOWNERS Add codeowners 2022-01-21 21:45:25 +09:00
LICENSE.txt Add LICENSE 2018-06-10 14:27:55 +09:00
migration_guide.md Add migration guide for v3.0.0 2020-03-25 22:43:16 +09:00
package.json Use monorepo for examples 2022-01-02 16:28:11 +09:00
README.md Update status badge for github actions 2021-01-29 01:57:07 +09:00
sider.yml Rename sideci.yml to sider.yml 2020-08-21 20:05:25 +09:00
yarn.lock Bump @typescript-eslint/eslint-plugin from 4.33.0 to 5.8.1 2022-01-02 13:51:55 +00:00

Megalodon

Test NPM Version GitHub release npm NPM

A Mastodon, Pleroma and Misskey API Client library for node.js and browser. It provides REST API and streaming methods. By using this library, you can take Mastodon, Pleroma and Misskey with the same interface.

!!Migrate v2.x to v3.0.0

There are some breaking changes, so you can not update megalodon out of the box. Please refer migration guide before you update megalodon version.

Features

  • REST API
  • Streaming with Server-Sent Event
  • Streaming with WebSocket
  • Promisified methods
  • Proxy support
  • Support node.js and browser
  • Written in typescript

Install

$ npm install -S megalodon

or

$ yarn add megalodon

Build for browser

Important: In browser, you can not use proxy.

If you want to build for browser, please use Webpack and set empty value for these libraries.

  • net
  • tls
  • dns
  node: {
    net: 'empty',
    tls: 'empty',
    dns: 'empty'
  }

These libraries are for node.js, so can not use in browser.

Here is example Webpack configuration.

Usage

I prepared examples, and please refer documents about each methods.

I explain some typical methods. At first, please get your access token for a fediverse server. If you don't have access token, or you want to register applications and get access token programmably, please refer Authorization section.

Home timeline

import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'

const client = generator('mastodon', BASE_URL, access_token)
client.getHomeTimeline()
  .then((res: Response<Array<Entity.Status>>) => {
    console.log(res.data)
  })

Post toot

import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const toot: string = 'test toot'

const client = generator('mastodon', BASE_URL, access_token)
client.postStatus(toot)
  .then((res: Response<Entity.Status>) => {
    console.log(res.data)
  })

Post medias

Please provide a file to the argument.

import generator, { Entity, Response } from 'megalodon'
import fs from 'fs'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const image = fs.readFileSync("test.image")

const client = generator('mastodon', BASE_URL, access_token)
client.uploadMedia(image)
  .then((res: Response<Entity.Attachment>) => {
    console.log(res.data)
  })

WebSocket streaming

Mastodon, Pleroma and Misskey provide WebSocket for streaming.

import generator, { Entity, WebSocketInterface } from 'megalodon'

const BASE_URL: string = 'wss://pleroma.io'
const access_token: string = '...'

const client = generator('pleroma', BASE_URL, access_token)
const stream: WebSocketInterface = client.userSocket()

stream.on('connect', () => {
  console.log('connect')
})

stream.on('update', (status: Entity.Status) => {
  console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
  console.log(notification)
})

stream.on('delete', (id: number) => {
  console.log(id)
})

stream.on('error', (err: Error) => {
  console.error(err)
})

stream.on('heartbeat', () => {
  console.log('thump.')
})

stream.on('close', () => {
  console.log('close')
})

stream.on('parser-error', (err: Error) => {
  console.error(err)
})

HTTP Streaming

Mastodon provides HTTP streaming.

import generator, { Entity, StreamListenerInterface } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'

const client = generator('mastodon', BASE_URL, access_token)
const stream: StreamListenerInterface

stream.on('update', (status: Entity.Status) => {
  console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
  console.log(notification)
})

stream.on('delete', (id: number) => {
  console.log(id)
})

stream.on('error', (err: Error) => {
  console.error(err)
})

stream.on('heartbeat', () => {
  console.log('thump.')
})

Authorization

You can register applications, and get access tokens to use this method.

import generator, { OAuth } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'

let clientId: string
let clientSecret: string

const client = generator('mastodon', BASE_URL)

client.registerApp('Test App')
  .then(appData => {
    clientId = appData.clientId
    clientSecret = appData.clientSecret
    console.log('Authorization URL is generated.')
    console.log(appData.url)
  })

Please open Autorhization URL in your browser, and authorize this app. In this time, you can get authorization code.

After that, get an access token.

const code = '...' // Authorization code

client.fetchAccessToken(clientId, clientSecret, code)
})
  .then((tokenData: OAuth.TokenData) => {
    console.log(tokenData.accessToken)
    console.log(tokenData.refreshToken)
  })
  .catch((err: Error) => console.error(err))

Detect each SNS

You have to provide SNS name mastodon, pleroma or misskey to generator function. But when you only know the URL and not the SNS, detector function can detect the SNS.

import { detector } from 'megalodon'

const URL = 'https://misskey.io'

const sns = await detector(URL)
console.log(sns)

License

The software is available as open source under the terms of the MIT License.