Merge pull request #214 from h3poteto/refactor/example

Divide typescript example for each SNS
This commit is contained in:
AkiraFukushima 2020-02-23 16:35:10 +09:00 committed by GitHub
commit 91632e1bf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 203 additions and 2628 deletions

View file

@ -10,5 +10,4 @@ script:
- yarn run build
- yarn run test
- cd example/browser && yarn install && yarn build
- cd ../../example/javascript && yarn install && yarn build
- cd ../../example/typescript && yarn install && yarn build

View file

@ -1,5 +0,0 @@
{
"presets": [
"@babel/preset-env"
]
}

View file

@ -1,46 +0,0 @@
import readline from 'readline'
import { Mastodon } from 'megalodon'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const SCOPES = ['read', 'write', 'follow']
const BASE_URL = 'https://mastodon.social'
let clientId
let clientSecret
const client = new Mastodon(BASE_URL)
client
.registerApp('Test App', {
scopes: SCOPES
})
.then(appData => {
clientId = appData.clientId
clientSecret = appData.clientSecret
console.log('\nclient_id:')
console.log(clientId)
console.log('\nclient_secret:')
console.log(clientSecret)
console.log('\nAuthorization URL is generated.')
console.log(appData.url)
console.log()
return new Promise(resolve => {
rl.question('Enter the authorization code from website: ', code => {
resolve(code)
rl.close()
})
})
})
.then(code => Mastodon.fetchAccessToken(clientId, clientSecret, code, BASE_URL))
.then(tokenData => {
console.log('\naccess_token:')
console.log(tokenData.accessToken)
console.log('\nrefresh_token:')
console.log(tokenData.refreshToken)
console.log()
})
.catch(err => console.error(err))

View file

@ -1,12 +0,0 @@
import { Mastodon } from 'megalodon'
const BASE_URL = 'https://mastodon.social'
const access_token = process.env.MASTODON_ACCESS_TOKEN
const client = new Mastodon(BASE_URL, access_token)
client.getFavourites().then(res => {
console.log(res.headers)
console.log(res.data)
})

View file

@ -1,14 +0,0 @@
{
"name": "example",
"author": "h3poteto",
"scripts": {
"build": "babel -d ./dist . -s --ignore 'node_modules/**/*.js' --ignore 'dist/*'"
},
"license": "MIT",
"dependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"megalodon": "file:../../"
}
}

View file

@ -1,23 +0,0 @@
// Please use this function after authorization.js
// Now mastodon and pleroma don't have refersh token method.
// So this example is failed.
import { Mastodon } from 'megalodon'
const BASE_URL = 'https://pleroma.io'
const clientId = ''
const clientSecret = ''
const refreshToken = ''
const client = new Mastodon(BASE_URL)
client
.refreshToken(clientId, clientSecret, refreshToken)
.then(tokenData => {
console.log('\naccess_token:')
console.log(tokenData.accessToken)
console.log('\nrefresh_token:')
console.log(tokenData.refreshToken)
console.log()
})
.catch(err => console.error(err))

View file

@ -1,40 +0,0 @@
import { Mastodon } from 'megalodon'
const BASE_URL = 'https://mastodon.social'
const access_token = process.env.MASTODON_ACCESS_TOKEN
const client = new Mastodon(BASE_URL, access_token)
const stream = client.publicStream()
stream.on('connect', _ => {
console.log('connect')
})
stream.on('not-event-stream', mes => {
console.log(mes)
})
stream.on('update', status => {
console.log(status)
})
stream.on('notification', notification => {
console.log(notification)
})
stream.on('delete', id => {
console.log(`delete: ${id}`)
})
stream.on('error', err => {
console.error(err)
})
stream.on('heartbeat', msg => {
console.log('thump.')
})
stream.on('connection-limit-exceeded', err => {
console.error(err)
})

View file

@ -1,9 +0,0 @@
import { Mastodon } from 'megalodon'
const BASE_URL = 'https://mastodon.social'
const access_token = process.env.MASTODON_ACCESS_TOKEN
const client = new Mastodon(BASE_URL, access_token)
client.getHomeTimeline().then(res => console.log(res.data))

View file

@ -1,27 +0,0 @@
import readline from 'readline'
import { Mastodon } from 'megalodon'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const BASE_URL = 'https://mastodon.social'
const access_token = process.env.MASTODON_ACCESS_TOKEN
const client = new Mastodon(BASE_URL, access_token)
new Promise(resolve => {
rl.question('Toot: ', status => {
client
.postStatus(status)
.then(res => {
console.log(res)
rl.close()
})
.catch(err => {
console.error(err)
rl.close()
})
})
})

View file

@ -1,40 +0,0 @@
import { Mastodon } from 'megalodon'
const BASE_URL = 'wss://pleroma.io'
const access_token = process.env.PLEROMA_ACCESS_TOKEN
const client = new Mastodon(BASE_URL, access_token)
const stream = client.userSocket()
stream.on('connect', event => {
console.log('connect')
})
stream.on('update', status => {
console.log(status)
})
stream.on('notification', notification => {
console.log(notification)
})
stream.on('delete', id => {
console.log(`delete: ${id}`)
})
stream.on('error', err => {
console.error(err)
})
stream.on('heartbeat', msg => {
console.log('thump.')
})
stream.on('close', () => {
console.log('close')
})
stream.on('parser-error', err => {
console.error(err)
})

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,28 @@
import generator, { Response, Entity, isCancel } from 'megalodon'
declare var process: {
env: {
MASTODON_ACCESS_TOKEN: string
}
}
const BASE_URL: string = 'https://mastodon.social'
const access_token: string = process.env.MASTODON_ACCESS_TOKEN
const client = generator('mastodon', BASE_URL, access_token)
client
.search('whalebird', 'hashtags', null, null, null, true)
.then((resp: Response<Entity.Results>) => {
console.log(resp.data.hashtags)
})
.catch((err: Error) => {
if (isCancel(err)) {
console.log('Request was canceled')
}
})
setTimeout(() => {
client.cancel()
}, 5000)

View file

@ -3,7 +3,6 @@ import generator, { Entity, Response } from 'megalodon'
declare var process: {
env: {
MASTODON_ACCESS_TOKEN: string
SNS: 'mastodon' | 'pleroma'
}
}
@ -11,7 +10,7 @@ const BASE_URL: string = 'https://mastodon.social'
const access_token: string = process.env.MASTODON_ACCESS_TOKEN
const client = generator(process.env.SNS, BASE_URL, access_token)
const client = generator('mastodon', BASE_URL, access_token)
client.getFavourites().then((res: Response<Array<Entity.Status>>) => {
console.log(res.headers)

View file

@ -5,7 +5,6 @@ declare var process: {
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
SNS: 'mastodon' | 'pleroma'
}
}
@ -17,7 +16,7 @@ const proxy: ProxyConfig = {
protocol: process.env.PROXY_PROTOCOL
}
const client = generator(process.env.SNS, BASE_URL, '', null, proxy)
const client = generator('mastodon', BASE_URL, '', null, proxy)
client.getInstance().then((res: Response<Entity.Instance>) => {
console.log(res)

View file

@ -6,7 +6,6 @@ declare var process: {
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
SNS: 'mastodon' | 'pleroma'
}
}
@ -20,7 +19,7 @@ const proxy: ProxyConfig = {
protocol: process.env.PROXY_PROTOCOL
}
const client = generator(process.env.SNS, BASE_URL, access_token, null, proxy)
const client = generator('mastodon', BASE_URL, access_token, null, proxy)
const stream: StreamListener = client.userStream()
stream.on('connect', _ => {

View file

@ -6,7 +6,6 @@ declare var process: {
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
SNS: 'mastodon' | 'pleroma'
}
}
@ -20,7 +19,7 @@ const proxy: ProxyConfig = {
protocol: process.env.PROXY_PROTOCOL
}
const client = generator(process.env.SNS, BASE_URL, access_token, null, proxy)
const client = generator('mastodon', BASE_URL, access_token, null, proxy)
client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)

View file

@ -3,7 +3,6 @@ import generator, { Entity, StreamListener } from 'megalodon'
declare var process: {
env: {
MASTODON_ACCESS_TOKEN: string
SNS: 'mastodon' | 'pleroma'
}
}
@ -11,7 +10,7 @@ const BASE_URL: string = 'https://mastodon.social'
const access_token: string = process.env.MASTODON_ACCESS_TOKEN
const client = generator(process.env.SNS, BASE_URL, access_token)
const client = generator('mastodon', BASE_URL, access_token)
const stream: StreamListener = client.publicStream()
stream.on('connect', _ => {

View file

@ -3,7 +3,6 @@ import generator, { MegalodonInterface, Entity, Response } from 'megalodon'
declare var process: {
env: {
MASTODON_ACCESS_TOKEN: string
SNS: 'mastodon' | 'pleroma'
}
}
@ -11,7 +10,7 @@ const BASE_URL: string = 'https://mastodon.social'
const access_token: string = process.env.MASTODON_ACCESS_TOKEN
const client: MegalodonInterface = generator(process.env.SNS, BASE_URL, access_token)
const client: MegalodonInterface = generator('mastodon', BASE_URL, access_token)
client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)

View file

@ -0,0 +1,29 @@
import * as readline from 'readline'
import generator, { Entity, Response } from 'megalodon'
const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const BASE_URL: string = 'https://mastodon.social'
const access_token: string = process.env.MASTODON_ACCESS_TOKEN as string
const client = generator('mastodon', BASE_URL, access_token)
new Promise(resolve => {
rl.question('Toot: ', status => {
client
.postStatus(status)
.then((res: Response<Entity.Status>) => {
console.log(res)
rl.close()
resolve(res)
})
.catch(err => {
console.error(err)
rl.close()
})
})
})

View file

@ -0,0 +1,44 @@
import * as readline from 'readline'
import generator, { OAuth } from 'megalodon'
const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const SCOPES: Array<string> = ['read', 'write', 'follow']
const BASE_URL: string = 'https://pleroma.io'
let clientId: string
let clientSecret: string
const client = generator('pleroma', BASE_URL)
client
.registerApp('Test App', {
scopes: SCOPES
})
.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 client.fetchAccessToken(clientId, clientSecret, code, BASE_URL)
})
.then((tokenData: OAuth.TokenData) => {
console.log('\naccess_token:')
console.log(tokenData.accessToken)
console.log('\nrefresh_token:')
console.log(tokenData.refreshToken)
console.log()
})
.catch((err: Error) => console.error(err))

View file

@ -0,0 +1,18 @@
import generator, { Entity, Response } from 'megalodon'
declare var process: {
env: {
PLEROMA_ACCESS_TOKEN: string
}
}
const BASE_URL: string = 'https://pleroma.io'
const access_token: string = process.env.PLEROMA_ACCESS_TOKEN
const client = generator('pleroma', BASE_URL, access_token)
client.getFavourites().then((res: Response<Array<Entity.Status>>) => {
console.log(res.headers)
console.log(res.data)
})

View file

@ -0,0 +1,9 @@
import generator, { Entity, Response } from 'megalodon'
const BASE_URL: string = 'http://pleroma.io'
const client = generator('pleroma', BASE_URL)
client.getInstance().then((res: Response<Entity.Instance>) => {
console.log(res)
})

View file

@ -0,0 +1,23 @@
import generator, { Entity, ProxyConfig, Response } from 'megalodon'
declare var process: {
env: {
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
}
}
const BASE_URL: string = 'http://pleroma.io'
const proxy: ProxyConfig = {
host: process.env.PROXY_HOST,
port: process.env.PROXY_PORT,
protocol: process.env.PROXY_PROTOCOL
}
const client = generator('pleroma', BASE_URL, '', null, proxy)
client.getInstance().then((res: Response<Entity.Instance>) => {
console.log(res)
})

View file

@ -0,0 +1,26 @@
import generator, { Entity, Response, ProxyConfig } from 'megalodon'
declare var process: {
env: {
PLEROMA_ACCESS_TOKEN: string
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
}
}
const BASE_URL: string = 'https://pleroma.io'
const access_token: string = process.env.PLEROMA_ACCESS_TOKEN
const proxy: ProxyConfig = {
host: process.env.PROXY_HOST,
port: process.env.PROXY_PORT,
protocol: process.env.PROXY_PROTOCOL
}
const client = generator('pleroma', BASE_URL, access_token, null, proxy)
client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)
})

View file

@ -4,11 +4,9 @@ import log4js from 'log4js'
declare var process: {
env: {
PLEROMA_ACCESS_TOKEN: string
MASTODON_ACCESS_TOKEN: string
PROXY_HOST: string
PROXY_PORT: number
PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
SNS: 'mastodon' | 'pleroma'
}
}
@ -22,7 +20,7 @@ const proxy: ProxyConfig = {
protocol: process.env.PROXY_PROTOCOL
}
const client = generator(process.env.SNS, BASE_URL, access_token, null, proxy)
const client = generator('pleroma', BASE_URL, access_token, null, proxy)
const stream: WebSocket = client.userSocket()

View file

@ -0,0 +1,17 @@
import generator, { MegalodonInterface, Entity, Response } from 'megalodon'
declare var process: {
env: {
PLEROMA_ACCESS_TOKEN: string
}
}
const BASE_URL: string = 'https://pleroma.io'
const access_token: string = process.env.PLEROMA_ACCESS_TOKEN
const client: MegalodonInterface = generator('pleroma', BASE_URL, access_token)
client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)
})

View file

@ -4,8 +4,6 @@ import log4js from 'log4js'
declare var process: {
env: {
PLEROMA_ACCESS_TOKEN: string
MASTODON_ACCESS_TOKEN: string
SNS: 'mastodon' | 'pleroma'
}
}
@ -13,7 +11,7 @@ const BASE_URL: string = 'wss://pleroma.io'
const access_token: string = process.env.PLEROMA_ACCESS_TOKEN
const client = generator(process.env.SNS, BASE_URL, access_token)
const client = generator('pleroma', BASE_URL, access_token)
const stream: WebSocket = client.userSocket()

View file

@ -56,5 +56,5 @@
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": ["./"]
"include": ["./src"]
}