Merge pull request #250 from h3poteto/access_token/null

Allow null as access token in mastodon client
This commit is contained in:
AkiraFukushima 2020-03-13 01:09:42 +09:00 committed by GitHub
commit 77c3b0f36f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 22 deletions

View file

@ -33,7 +33,7 @@ namespace MastodonAPI {
static DEFAULT_URL = 'https://mastodon.social'
static NO_REDIRECT = NO_REDIRECT
private accessToken: string
private accessToken: string | null
private baseUrl: string
private userAgent: string
private cancelTokenSource: CancelTokenSource
@ -45,7 +45,12 @@ namespace MastodonAPI {
* @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 = null,
userAgent: string = DEFAULT_UA,
proxyConfig: ProxyConfig | false = false
) {
this.accessToken = accessToken
this.baseUrl = baseUrl
this.userAgent = userAgent
@ -125,11 +130,15 @@ namespace MastodonAPI {
public async get<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
headers: {
Authorization: `Bearer ${this.accessToken}`
},
params: params
}
if (this.accessToken) {
options = Object.assign(options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(this.proxyConfig)
@ -162,10 +171,14 @@ namespace MastodonAPI {
*/
public async put<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
headers: {
Authorization: `Bearer ${this.accessToken}`
}
cancelToken: this.cancelTokenSource.token
}
if (this.accessToken) {
options = Object.assign(options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
@ -199,10 +212,14 @@ namespace MastodonAPI {
*/
public async patch<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
headers: {
Authorization: `Bearer ${this.accessToken}`
}
cancelToken: this.cancelTokenSource.token
}
if (this.accessToken) {
options = Object.assign(options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
@ -236,10 +253,14 @@ namespace MastodonAPI {
*/
public async post<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
headers: {
Authorization: `Bearer ${this.accessToken}`
}
cancelToken: this.cancelTokenSource.token
}
if (this.accessToken) {
options = Object.assign(options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
@ -265,10 +286,14 @@ namespace MastodonAPI {
public async del<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token,
data: params,
headers: {
Authorization: `Bearer ${this.accessToken}`
}
data: params
}
if (this.accessToken) {
options = Object.assign(options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
@ -312,6 +337,9 @@ namespace MastodonAPI {
* @returns streamListener, which inherits from EventEmitter
*/
public stream(path: string, reconnectInterval = 1000): StreamListener {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const headers = {
'Cache-Control': 'no-cache',
Accept: 'text/event-stream',
@ -335,6 +363,9 @@ namespace MastodonAPI {
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(path: string, stream: string, params: string | null = null): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + path
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
process.nextTick(() => {

View file

@ -167,7 +167,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
headers: headers
}
if (proxyConfig) {
options = Object.assign(proxyConfig, {
options = Object.assign(options, {
agent: proxyAgent(proxyConfig)
})
}