From a4e1d3ad61f4d72698b573dab9acbb867f2c0756 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Thu, 2 Apr 2020 01:28:52 +0900
Subject: [PATCH] fix: Use instance method instead of class method in misskey
---
.../typescript/src/misskey/authorization.ts | 4 +-
src/mastodon/api_client.ts | 64 -------------------
src/misskey.ts | 46 ++++++-------
src/misskey/api_client.ts | 40 +-----------
4 files changed, 29 insertions(+), 125 deletions(-)
diff --git a/example/typescript/src/misskey/authorization.ts b/example/typescript/src/misskey/authorization.ts
index 855edf3..4a66236 100644
--- a/example/typescript/src/misskey/authorization.ts
+++ b/example/typescript/src/misskey/authorization.ts
@@ -43,4 +43,6 @@ client
console.log(tokenData.refreshToken)
console.log()
})
- .catch((err: Error) => console.error(err))
+ .catch((err: any) => {
+ console.error(err)
+ })
diff --git a/src/mastodon/api_client.ts b/src/mastodon/api_client.ts
index a0f2d2f..e2fbd6b 100644
--- a/src/mastodon/api_client.ts
+++ b/src/mastodon/api_client.ts
@@ -58,70 +58,6 @@ namespace MastodonAPI {
this.proxyConfig = proxyConfig
}
- /**
- * Unauthorized GET request to mastodon REST API.
- * @param path relative path from baseUrl
- * @param params Query parameters
- * @param baseUrl base URL of the target
- * @param proxyConfig Proxy setting, or set false if don't use proxy.
- */
- public static async get(
- path: string,
- params = {},
- baseUrl = this.DEFAULT_URL,
- proxyConfig: ProxyConfig | false = false
- ): Promise> {
- const apiUrl = baseUrl
- let options: AxiosRequestConfig = {
- params: params
- }
- if (proxyConfig) {
- options = Object.assign(options, {
- httpsAgent: proxyAgent(proxyConfig)
- })
- }
- return axios.get(apiUrl + path, options).then((resp: AxiosResponse) => {
- const res: Response = {
- data: resp.data,
- status: resp.status,
- statusText: resp.statusText,
- headers: resp.headers
- }
- return res
- })
- }
-
- /**
- * Unauthorized POST request to mastodon REST API.
- * @param path relative path from baseUrl
- * @param params Body parameters
- * @param baseUrl base URL of the target
- * @param proxyConfig Proxy setting, or set false if don't use proxy.
- */
- public static async post(
- path: string,
- params = {},
- baseUrl = this.DEFAULT_URL,
- proxyConfig: ProxyConfig | false = false
- ): Promise> {
- let options: AxiosRequestConfig = {}
- if (proxyConfig) {
- options = Object.assign(options, {
- httpsAgent: proxyAgent(proxyConfig)
- })
- }
- const apiUrl = baseUrl
- return axios.post(apiUrl + path, params, options).then((resp: AxiosResponse) => {
- const res: Response = {
- data: resp.data,
- status: resp.status,
- statusText: resp.statusText,
- headers: resp.headers
- }
- return res
- })
- }
-
/**
* GET request to mastodon REST API.
* @param path relative path from baseUrl
diff --git a/src/misskey.ts b/src/misskey.ts
index 19cf022..fcb8a76 100644
--- a/src/misskey.ts
+++ b/src/misskey.ts
@@ -104,28 +104,28 @@ export default class Misskey implements MegalodonInterface {
"secret": "string"
}
*/
- return MisskeyAPI.Client.post('/api/app/create', params, this.baseUrl, this.proxyConfig).then(
- (res: Response) => {
- const appData: OAuth.AppDataFromServer = {
- id: res.data.id,
- name: res.data.name,
- website: null,
- redirect_uri: res.data.callbackUrl,
- client_id: '',
- client_secret: res.data.secret
- }
- return OAuth.AppData.from(appData)
+ return this.client.post('/api/app/create', params).then((res: Response) => {
+ const appData: OAuth.AppDataFromServer = {
+ id: res.data.id,
+ name: res.data.name,
+ website: null,
+ redirect_uri: res.data.callbackUrl,
+ client_id: '',
+ client_secret: res.data.secret
}
- )
+ return OAuth.AppData.from(appData)
+ })
}
/**
* POST /api/auth/session/generate
*/
public async generateAuthUrlAndToken(clientSecret: string): Promise {
- return MisskeyAPI.Client.post('/api/auth/session/generate', {
- appSecret: clientSecret
- }).then((res: Response) => res.data)
+ return this.client
+ .post('/api/auth/session/generate', {
+ appSecret: clientSecret
+ })
+ .then((res: Response) => res.data)
}
// ======================================
@@ -155,13 +155,15 @@ export default class Misskey implements MegalodonInterface {
session_token: string,
_redirect_uri?: string
): Promise {
- return MisskeyAPI.Client.post('/api/auth/session/userkey', {
- appSecret: client_secret,
- token: session_token
- }).then(res => {
- const token = new OAuth.TokenData(res.data.accessToken, 'misskey', '', 0, null, null)
- return token
- })
+ return this.client
+ .post('/api/auth/session/userkey', {
+ appSecret: client_secret,
+ token: session_token
+ })
+ .then(res => {
+ const token = new OAuth.TokenData(res.data.accessToken, 'misskey', '', 0, null, null)
+ return token
+ })
}
public async refreshToken(_client_id: string, _client_secret: string, _refresh_token: string): Promise {
diff --git a/src/misskey/api_client.ts b/src/misskey/api_client.ts
index a311cf1..2df4fdf 100644
--- a/src/misskey/api_client.ts
+++ b/src/misskey/api_client.ts
@@ -378,8 +378,6 @@ namespace MisskeyAPI {
* Usign axios for request, you will handle promises.
*/
export class Client implements Interface {
- static DEFAULT_URL = 'https://misskey.io'
-
private accessToken: string | null
private baseUrl: string
private userAgent: string
@@ -400,37 +398,6 @@ namespace MisskeyAPI {
this.proxyConfig = proxyConfig
}
- /**
- * Unauthorized POST request to mastodon REST API.
- * @param path relative path from baseUrl
- * @param params Body parameters
- * @param baseUrl base URL of the target
- * @param proxyConfig Proxy setting, or set false if don't use proxy.
- */
- public static async post(
- path: string,
- params = {},
- baseUrl = this.DEFAULT_URL,
- proxyConfig: ProxyConfig | false = false
- ): Promise> {
- let options: AxiosRequestConfig = {}
- if (proxyConfig) {
- options = Object.assign(options, {
- httpsAgent: proxyAgent(proxyConfig)
- })
- }
- const apiUrl = baseUrl
- return axios.post(apiUrl + path, params, options).then((resp: AxiosResponse) => {
- const res: Response = {
- data: resp.data,
- status: resp.status,
- statusText: resp.statusText,
- headers: resp.headers
- }
- return res
- })
- }
-
/**
* POST request to mastodon REST API.
* @param path relative path from baseUrl
@@ -438,17 +405,14 @@ namespace MisskeyAPI {
*/
public async post(path: string, params = {}): Promise> {
let options: AxiosRequestConfig = {
- cancelToken: this.cancelTokenSource.token,
- headers: {
- 'User-Agent': this.userAgent
- }
+ cancelToken: this.cancelTokenSource.token
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(this.proxyConfig)
})
}
- let bodyParams = {}
+ let bodyParams = params
if (this.accessToken) {
bodyParams = Object.assign(params, {
i: this.accessToken