Add example for typescript

This commit is contained in:
AkiraFukushima 2018-06-10 15:14:08 +09:00
parent 8d0f52b048
commit 4190587dc1
9 changed files with 112 additions and 9 deletions

View file

@ -1,9 +1,5 @@
/**
* simple authorizaion tool via command line
*/
const readline = require('readline')
const Mastodon = require( '../lib/mastodon')
const Mastodon = require( '../../lib/mastodon')
const rl = readline.createInterface({
input: process.stdin,

View file

@ -1,4 +1,4 @@
const Mastodon = require( '../lib/mastodon')
const Mastodon = require( '../../lib/mastodon')
const BASE_URL = 'https://friends.nico'

View file

@ -1,4 +1,4 @@
const Mastodon = require( '../lib/mastodon')
const Mastodon = require( '../../lib/mastodon')
const BASE_URL = 'https://friends.nico'

View file

@ -1,5 +1,5 @@
const readline = require('readline')
const Mastodon = require('../lib/mastodon')
const Mastodon = require('../../lib/mastodon')
const rl = readline.createInterface({
input: process.stdin,

View file

@ -0,0 +1,38 @@
import readline from 'readline'
import Mastodon from '../../src/mastodon'
const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})
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)
console.log()
return new Promise<string>(resolve => {
rl.question('Enter the authorization code from website: ', code => {
resolve(code)
rl.close()
})
})
}).then((code: string) => {
return Mastodon.fetchAccessToken(clientId, clientSecret, code, BASE_URL)
})
.then((tokenData: Partial<{ accessToken: string }>) => {
console.log('\naccess_token:')
console.log(tokenData.accessToken)
console.log()
})
.catch((err: Error) => console.error(err))

View file

@ -0,0 +1,24 @@
import Mastodon from '../../src/mastodon'
const BASE_URL: string = 'https://friends.nico'
const access_token: string = '...'
const client = new Mastodon(
access_token,
BASE_URL + '/api/v1'
)
const stream = client.stream('/streaming/public')
stream.on('message', (data) => {
console.log(data)
})
stream.on('error', (err) => {
console.error(err)
})
stream.on('heartbeat', () => {
console.log('thump.')
})

View file

@ -0,0 +1,13 @@
import Mastodon from '../../src/mastodon'
const BASE_URL: string = 'https://friends.nico'
const access_token: string = '...'
const client = new Mastodon(
access_token,
BASE_URL + '/api/v1'
)
client.get('/timelines/home')
.then(resp => console.log(resp))

View file

@ -0,0 +1,32 @@
import readline from 'readline'
import Mastodon from '../../src/mastodon'
const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const BASE_URL: string = 'https://friends.nico'
const access_token: string = '...'
const client = new Mastodon(
access_token,
BASE_URL + '/api/v1'
)
new Promise<object>(resolve => {
rl.question('Toot: ', status => {
client.post('/statuses', {
status: status
})
.then(res => {
console.log(res)
rl.close()
resolve(res)
})
.catch(err => {
console.error(err)
rl.close()
})
})
})

View file

@ -13,7 +13,7 @@ const DEFAULT_SCOPE = 'read write follow'
*
* using superagent for request, you will handle promises
*/
class Mastodon {
export default class Mastodon {
static DEFAULT_SCOPE = DEFAULT_SCOPE
static DEFAULT_URL = DEFAULT_URL
static NO_REDIRECT = NO_REDIRECT