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(tokenData.refreshToken)
console.log() 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 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. * GET request to mastodon REST API.
* @param path relative path from baseUrl * @param path relative path from baseUrl

View file

@ -104,28 +104,28 @@ export default class Misskey implements MegalodonInterface {
"secret": "string" "secret": "string"
} }
*/ */
return MisskeyAPI.Client.post<MisskeyAPI.Entity.App>('/api/app/create', params, this.baseUrl, this.proxyConfig).then( return this.client.post<MisskeyAPI.Entity.App>('/api/app/create', params).then((res: Response<MisskeyAPI.Entity.App>) => {
(res: Response<MisskeyAPI.Entity.App>) => { const appData: OAuth.AppDataFromServer = {
const appData: OAuth.AppDataFromServer = { id: res.data.id,
id: res.data.id, name: res.data.name,
name: res.data.name, website: null,
website: null, redirect_uri: res.data.callbackUrl,
redirect_uri: res.data.callbackUrl, client_id: '',
client_id: '', client_secret: res.data.secret
client_secret: res.data.secret
}
return OAuth.AppData.from(appData)
} }
) return OAuth.AppData.from(appData)
})
} }
/** /**
* POST /api/auth/session/generate * POST /api/auth/session/generate
*/ */
public async generateAuthUrlAndToken(clientSecret: string): Promise<MisskeyAPI.Entity.Session> { public async generateAuthUrlAndToken(clientSecret: string): Promise<MisskeyAPI.Entity.Session> {
return MisskeyAPI.Client.post<MisskeyAPI.Entity.Session>('/api/auth/session/generate', { return this.client
appSecret: clientSecret .post<MisskeyAPI.Entity.Session>('/api/auth/session/generate', {
}).then((res: Response<MisskeyAPI.Entity.Session>) => res.data) appSecret: clientSecret
})
.then((res: Response<MisskeyAPI.Entity.Session>) => res.data)
} }
// ====================================== // ======================================
@ -155,13 +155,15 @@ export default class Misskey implements MegalodonInterface {
session_token: string, session_token: string,
_redirect_uri?: string _redirect_uri?: string
): Promise<OAuth.TokenData> { ): Promise<OAuth.TokenData> {
return MisskeyAPI.Client.post<MisskeyAPI.Entity.UserKey>('/api/auth/session/userkey', { return this.client
appSecret: client_secret, .post<MisskeyAPI.Entity.UserKey>('/api/auth/session/userkey', {
token: session_token appSecret: client_secret,
}).then(res => { token: session_token
const token = new OAuth.TokenData(res.data.accessToken, 'misskey', '', 0, null, null) })
return 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> { 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. * Usign axios for request, you will handle promises.
*/ */
export class Client implements Interface { export class Client implements Interface {
static DEFAULT_URL = 'https://misskey.io'
private accessToken: string | null private accessToken: string | null
private baseUrl: string private baseUrl: string
private userAgent: string private userAgent: string
@ -400,37 +398,6 @@ namespace MisskeyAPI {
this.proxyConfig = proxyConfig 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. * POST request to mastodon REST API.
* @param path relative path from baseUrl * @param path relative path from baseUrl
@ -438,17 +405,14 @@ namespace MisskeyAPI {
*/ */
public async post<T>(path: string, params = {}): Promise<Response<T>> { public async post<T>(path: string, params = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = { let options: AxiosRequestConfig = {
cancelToken: this.cancelTokenSource.token, cancelToken: this.cancelTokenSource.token
headers: {
'User-Agent': this.userAgent
}
} }
if (this.proxyConfig) { if (this.proxyConfig) {
options = Object.assign(options, { options = Object.assign(options, {
httpsAgent: proxyAgent(this.proxyConfig) httpsAgent: proxyAgent(this.proxyConfig)
}) })
} }
let bodyParams = {} let bodyParams = params
if (this.accessToken) { if (this.accessToken) {
bodyParams = Object.assign(params, { bodyParams = Object.assign(params, {
i: this.accessToken i: this.accessToken