A Mastodon API Client library for node.js. It provides REST API and streaming methods.
$ npm install -S megalodon
or
$ yarn add megalodon
I prepared examples.
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))
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)
})
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)
})
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.')
})
The software is available as open source under the terms of the MIT License.