Add example for typescript
This commit is contained in:
parent
8d0f52b048
commit
4190587dc1
|
@ -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,
|
|
@ -1,4 +1,4 @@
|
|||
const Mastodon = require( '../lib/mastodon')
|
||||
const Mastodon = require( '../../lib/mastodon')
|
||||
|
||||
const BASE_URL = 'https://friends.nico'
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
const Mastodon = require( '../lib/mastodon')
|
||||
const Mastodon = require( '../../lib/mastodon')
|
||||
|
||||
const BASE_URL = 'https://friends.nico'
|
||||
|
|
@ -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,
|
38
example/typescript/authorization.ts
Normal file
38
example/typescript/authorization.ts
Normal 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))
|
||||
|
24
example/typescript/streaming.ts
Normal file
24
example/typescript/streaming.ts
Normal 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.')
|
||||
})
|
||||
|
13
example/typescript/timeline.ts
Normal file
13
example/typescript/timeline.ts
Normal 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))
|
32
example/typescript/toot.ts
Normal file
32
example/typescript/toot.ts
Normal 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()
|
||||
})
|
||||
})
|
||||
})
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue