Go to file
AkiraFukushima 88e7771db5
Merge pull request #6 from h3poteto/types
Cast each types in parser when streaming
2018-07-16 13:13:02 +09:00
example Cast each types in parser when streaming 2018-07-16 12:32:31 +09:00
lib Create base REST API client 2018-06-09 01:13:19 +09:00
src Cast each types in parser when streaming 2018-07-16 12:32:31 +09:00
.gitignore Update definitely types for entities 2018-07-16 00:44:11 +09:00
.npmignore Update npmignore 2018-06-10 16:40:25 +09:00
.travis.yml Add travis setting 2018-06-10 16:27:29 +09:00
LICENSE.txt Add LICENSE 2018-06-10 14:27:55 +09:00
package-lock.json Add basic tslint settings 2018-06-10 16:15:04 +09:00
package.json Update definitely types for entities 2018-07-16 00:44:11 +09:00
README.md Cast each types in parser when streaming 2018-07-16 12:32:31 +09:00
sideci.yml Add basic tslint settings 2018-06-10 16:15:04 +09:00
tsconfig.json Generate d.ts files 2018-06-09 19:43:00 +09:00
tslint.json Add basic tslint settings 2018-06-10 16:15:04 +09:00

Megalodon

Build Status NPM Version

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

Install

$ npm install -S megalodon

or

$ yarn add megalodon

Useage

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 } 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: [Status]) => {
    console.log(resp)
  })

Post toot

import Mastodon, { Status } 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: Status) => {
    console.log(res)
  })

Streaming

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.')
})

License

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