Add example for misskey and fix scopes

This commit is contained in:
AkiraFukushima 2020-03-10 23:12:54 +09:00
parent ba43bf9b82
commit d5a6d8b32e
9 changed files with 147 additions and 21 deletions

View file

@ -0,0 +1,18 @@
import generator, { MegalodonInterface } from 'megalodon'
declare var process: {
env: {
MISSKEY_ACCESS_TOKEN: string
}
}
const BASE_URL: string = 'https://misskey.io'
const access_token: string = process.env.MISSKEY_ACCESS_TOKEN
const client: MegalodonInterface = generator('misskey', BASE_URL, access_token)
client
.verifyAccountCredentials()
.then(res => console.log(res.data))
.catch(err => console.error(err))

View file

@ -6,7 +6,6 @@ const rl: readline.ReadLine = readline.createInterface({
output: process.stdout
})
const SCOPES: Array<string> = ['read', 'write', 'follow']
const BASE_URL: string = 'https://misskey.io'
let clientId: string
@ -15,9 +14,7 @@ let clientSecret: string
const client = new Misskey(BASE_URL)
client
.registerApp('Test App', {
scopes: SCOPES
})
.registerApp('Test App')
.then(appData => {
clientId = appData.clientId
clientSecret = appData.clientSecret

View file

@ -0,0 +1,17 @@
import generator, { isCancel } from 'megalodon'
const access_token: string = process.env.MISSKEY_ACCESS_TOKEN!
const client = generator('misskey', 'https://misskey.io', access_token)
client
.search('h3poteto', 'accounts')
.then(res => console.log(res.data))
.catch(err => {
if (isCancel(err)) {
console.log('Request was canceled')
}
})
setTimeout(() => {
client.cancel()
}, 1000)

View file

@ -0,0 +1,18 @@
import generator, { Entity, Response } from 'megalodon'
declare var process: {
env: {
MISSKEY_ACCESS_TOKEN: string
}
}
const BASE_URL = 'https://misskey.io'
const access_token = process.env.MISSKEY_ACCESS_TOKEN
const client = generator('misskey', BASE_URL, access_token)
client
.getNotifications()
.then((res: Response<Array<Entity.Notification>>) => {
console.log(res.headers)
console.log(res.data)
})
.catch(err => console.error(err))

View file

@ -0,0 +1,12 @@
import generator, { Entity, Response } from 'megalodon'
const BASE_URL = 'https://misskey.io'
const client = generator('misskey', BASE_URL)
client
.getInstance()
.then((res: Response<Entity.Instance>) => {
console.log(res)
})
.catch(err => console.error(err))

View file

@ -0,0 +1,20 @@
import generator, { MegalodonInterface, Entity, Response } from 'megalodon'
declare var process: {
env: {
MISSKEY_ACCESS_TOKEN: string
}
}
const BASE_URL: string = 'https://misskey.io'
const access_token: string = process.env.MISSKEY_ACCESS_TOKEN
const client: MegalodonInterface = generator('misskey', BASE_URL, access_token)
client
.getHomeTimeline()
.then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)
})
.catch(err => console.error(err))

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://misskey.io'
const access_token: string = process.env.MISSKEY_ACCESS_TOKEN as string
const client = generator('misskey', 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

@ -4,7 +4,8 @@ import Pleroma from './pleroma'
import proxyAgent, { ProxyConfig } from './proxy_config'
import Mastodon from './mastodon'
import Entity from './entity'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import axios, { AxiosRequestConfig } from 'axios'
import Misskey from './misskey'
export interface WebSocketInterface {
start(): void
@ -1212,14 +1213,18 @@ type Instance = {
* @param proxyConfig Proxy setting, or set false if don't use proxy.
* @return SNS name.
*/
export const detector = async (url: string, proxyConfig: ProxyConfig | false = false): Promise<'mastodon' | 'pleroma' | 'pixelfed'> => {
export const detector = async (
url: string,
proxyConfig: ProxyConfig | false = false
): Promise<'mastodon' | 'pleroma' | 'pixelfed' | 'misskey'> => {
let options: AxiosRequestConfig = {}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
return axios.get<Instance>(url + '/api/v1/instance', options).then((res: AxiosResponse<Instance>) => {
try {
const res = await axios.get<Instance>(url + '/api/v1/instance', options)
if (res.data.version.includes('Pleroma')) {
return 'pleroma'
} else if (res.data.version.includes('Pixelfed')) {
@ -1227,7 +1232,10 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
} else {
return 'mastodon'
}
})
} catch (err) {
await axios.post<{}>(url + '/api/meta', options)
return 'misskey'
}
}
/**
@ -1241,7 +1249,7 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
* @return Client instance for each SNS you specified.
*/
const generator = (
sns: 'mastodon' | 'pleroma',
sns: 'mastodon' | 'pleroma' | 'misskey',
baseUrl: string,
accessToken: string | null = null,
userAgent: string | null = null,
@ -1252,6 +1260,10 @@ const generator = (
const pleroma = new Pleroma(baseUrl, accessToken, userAgent, proxyConfig)
return pleroma
}
case 'misskey': {
const misskey = new Misskey(baseUrl, accessToken, userAgent, proxyConfig)
return misskey
}
default: {
const mastodon = new Mastodon(baseUrl, accessToken, userAgent, proxyConfig)
return mastodon

View file

@ -337,15 +337,15 @@ namespace MisskeyAPI {
'write:account',
'read:blocks',
'write:blocks',
'read:favourites',
'write:favourites',
'read:drive',
'write:drive',
'read:favorites',
'write:favorites',
'read:following',
'write:following',
'read:messaging',
'write:messaging',
'read:mute',
'write:mute',
'write:note',
'read:mutes',
'write:mutes',
'write:notes',
'read:notifications',
'write:notifications',
'read:reactions',
@ -366,7 +366,7 @@ namespace MisskeyAPI {
export class Client implements Interface {
static DEFAULT_URL = 'https://misskey.io'
private accessToken: string
private accessToken: string | null
private baseUrl: string
private userAgent: string
private cancelTokenSource: CancelTokenSource
@ -378,7 +378,7 @@ namespace MisskeyAPI {
* @param userAgent UserAgent is specified in header on request.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
*/
constructor(baseUrl: string, accessToken: string, userAgent: string = DEFAULT_UA, proxyConfig: ProxyConfig | false = false) {
constructor(baseUrl: string, accessToken: string | null, userAgent: string = DEFAULT_UA, proxyConfig: ProxyConfig | false = false) {
this.accessToken = accessToken
this.baseUrl = baseUrl
this.userAgent = userAgent
@ -434,9 +434,12 @@ namespace MisskeyAPI {
httpsAgent: proxyAgent(this.proxyConfig)
})
}
const bodyParams = Object.assign(params, {
i: this.accessToken
})
let bodyParams = {}
if (this.accessToken) {
bodyParams = Object.assign(params, {
i: this.accessToken
})
}
return axios.post<T>(this.baseUrl + path, bodyParams, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,