Merge pull request #307 from h3poteto/fix/class_methods

fix: Use instance method instead of class method in misskey
This commit is contained in:
AkiraFukushima 2020-04-02 01:35:32 +09:00 committed by GitHub
commit 85402e63bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 125 deletions

View file

@ -43,4 +43,6 @@ client
console.log(tokenData.refreshToken)
console.log()
})
.catch((err: Error) => console.error(err))
.catch((err: any) => {
console.error(err)
})

View file

@ -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<T>(
path: string,
params = {},
baseUrl = this.DEFAULT_URL,
proxyConfig: ProxyConfig | false = false
): Promise<Response<T>> {
const apiUrl = baseUrl
let options: AxiosRequestConfig = {
params: params
}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
return axios.get<T>(apiUrl + path, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
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<T>(
path: string,
params = {},
baseUrl = this.DEFAULT_URL,
proxyConfig: ProxyConfig | false = false
): Promise<Response<T>> {
let options: AxiosRequestConfig = {}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
const apiUrl = baseUrl
return axios.post<T>(apiUrl + path, params, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
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

View file

@ -104,28 +104,28 @@ export default class Misskey implements MegalodonInterface {
"secret": "string"
}
*/
return MisskeyAPI.Client.post<MisskeyAPI.Entity.App>('/api/app/create', params, this.baseUrl, this.proxyConfig).then(
(res: Response<MisskeyAPI.Entity.App>) => {
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<MisskeyAPI.Entity.App>('/api/app/create', params).then((res: Response<MisskeyAPI.Entity.App>) => {
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<MisskeyAPI.Entity.Session> {
return MisskeyAPI.Client.post<MisskeyAPI.Entity.Session>('/api/auth/session/generate', {
appSecret: clientSecret
}).then((res: Response<MisskeyAPI.Entity.Session>) => res.data)
return this.client
.post<MisskeyAPI.Entity.Session>('/api/auth/session/generate', {
appSecret: clientSecret
})
.then((res: Response<MisskeyAPI.Entity.Session>) => res.data)
}
// ======================================
@ -155,13 +155,15 @@ export default class Misskey implements MegalodonInterface {
session_token: string,
_redirect_uri?: string
): Promise<OAuth.TokenData> {
return MisskeyAPI.Client.post<MisskeyAPI.Entity.UserKey>('/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<MisskeyAPI.Entity.UserKey>('/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<OAuth.TokenData> {

View file

@ -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<T>(
path: string,
params = {},
baseUrl = this.DEFAULT_URL,
proxyConfig: ProxyConfig | false = false
): Promise<Response<T>> {
let options: AxiosRequestConfig = {}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
const apiUrl = baseUrl
return axios.post<T>(apiUrl + path, params, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
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<T>(path: string, params = {}): Promise<Response<T>> {
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