Go to file
2020-02-01 15:30:58 +09:00
.github Create FUNDING.yml 2019-11-20 21:54:52 +09:00
example Merge pull request #165 from h3poteto/dependabot/npm_and_yarn/example/javascript/babel/core-7.8.4 2020-02-01 00:07:30 +09:00
lib Create base REST API client 2018-06-09 01:13:19 +09:00
src refs #164 Remove undefined Pleroma method 2020-02-01 15:29:06 +09:00
test refs #124 Move mastodon api methods under mastodon dir 2020-01-31 23:20:50 +09: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
.npmignore Fix npmignore for src directory 2019-08-26 22:58:36 +09:00
.prettierrc refs #41 Handle delete message from streaming 2019-05-24 01:37:02 +09:00
.travis.yml Disable travis cache 2020-01-25 00:23:17 +09:00
LICENSE.txt Add LICENSE 2018-06-10 14:27:55 +09:00
package.json Merge pull request #150 from h3poteto/dependabot/npm_and_yarn/jest-worker-25.1.0 2020-02-01 00:06:53 +09:00
README.md refs #102 Update description and readme for browser 2019-12-09 22:49:58 +09:00
sideci.yml Remove unused tslint 2019-07-08 23:05:50 +09:00
tsconfig.json Create browser example 2019-11-27 23:14:12 +09:00
yarn.lock Merge pull request #159 from h3poteto/dependabot/npm_and_yarn/eslint-config-prettier-6.10.0 2020-02-01 00:07:11 +09:00

Megalodon

Build Status NPM Version GitHub release npm NPM

A Mastodon and Pleroma API Client library for node.js and browser. It provides REST API and streaming methods.

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.

Authorization

First, you should register the application.

import Mastodon from 'megalodon'

const SCOPES: string = 'read write follow'
const BASE_URL: string = 'https://friends.nico'

let clientId: string
let clientSecret: string

Mastodon.registerApp('Test App', {
  scopes: SCOPES
}, BASE_URL).then(appData => {
  clientId = appData.clientId
  clientSecret = appData.clientSecret
  console.log('Authorization URL is generated.')
  console.log(appData.url)
})

And, get an access token.

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

Mastodon.fetchAccessToken(clientId, clientSecret, code, BASE_URL)
})
  .then((tokenData: Partial<{ accessToken: string }>) => {
    console.log(tokenData.accessToken)
  })
  .catch((err: Error) => console.error(err))

Get timeline

import Mastodon, { Status, Response } from 'megalodon'

const BASE_URL: string = 'https://friends.nico'

const access_token: string = '...'

const client = new Mastodon(
  access_token,
  BASE_URL + '/api/v1'
)

client.get<[Status]>('/timelines/home')
  .then((resp: Response<[Status]>) => {
    console.log(resp.data)
  })

Post toot

import Mastodon, { Status, Response } from 'megalodon'

const BASE_URL: string = 'https://friends.nico'

const access_token: string = '...'

const toot: string = 'test toot'

const client = new Mastodon(
  access_token,
  BASE_URL + '/api/v1'
)

client.post<Status>('/statuses', {
  status: toot
})
  .then((res: Response<Status>) => {
    console.log(res.data)
  })

Post medias

The POST method is wrapper of axios: https://github.com/h3poteto/megalodon/blob/master/src/mastodon.ts#L245-L266

So you can use the same way of axios to post medias.

import Mastodon, { Status, Response } from 'megalodon'
import fs from 'fs'

const BASE_URL: string = 'https://friends.nico'

const access_token: string = '...'

const client = new Mastodon(
  access_token,
  BASE_URL + '/api/v1'
)

const image = fs.readFileSync("test.image")
const formData = new FormData()
formData.append('file', image)

client.post<Attachment>('/media', formData)
  .then((res: Response<Attachment>) => {
    console.log(res.data)
  })

Streaming for Mastodon

This method provides streaming method for Mastodon. If you want to use Pleroma, please use WebSocket.

import Mastodon, { Status, Notification, StreamListener } from 'megalodon'

const BASE_URL: string = 'https://friends.nico'

const access_token: string = '...'

const client = new Mastodon(
  access_token,
  BASE_URL + '/api/v1'
)


const stream: StreamListener = client.stream('/streaming/public')
stream.on('update', (status: Status) => {
  console.log(status)
})

stream.on('notification', (notification: 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.')
})

WebSocket for Pleroma

This method provides streaming method for Pleroma.

import Mastodon, { Status, Notification, WebSocket } from 'megalodon'

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

const access_token: string = '...'

const client = new Mastodon(
  access_token,
  BASE_URL + '/api/v1'
)

const stream: WebSocket = client.socket('/streaming', 'user')
stream.on('connect', () => {
  console.log('connect')
})

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

stream.on('notification', (notification: 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)
})

License

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