refs #215 Add a function to detect SNS

This commit is contained in:
AkiraFukushima 2020-02-23 19:39:16 +09:00
parent 9eeca7e61c
commit 5b80a3a961

View file

@ -3,9 +3,10 @@ import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import Pleroma from './pleroma'
import { ProxyConfig } from './proxy_config'
import proxyAgent, { ProxyConfig } from './proxy_config'
import Mastodon from './mastodon'
import Entity from './entity'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
export interface MegalodonInterface {
/**
@ -1142,6 +1143,41 @@ export class NoImplementedError extends Error {
}
}
type Instance = {
title: string
uri: string
urls: {
streaming_api: string
}
version: string
}
/**
* Detect SNS type.
* Now support Mastodon, Pleroma and Pixelfed.
*
* @param url Base URL of SNS.
* @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'> => {
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>) => {
if (res.data.version.includes('Pleroma')) {
return 'pleroma'
} else if (res.data.version.includes('Pixelfed')) {
return 'pixelfed'
} else {
return 'mastodon'
}
})
}
/**
* Get client for each SNS according to megalodon interface.
*