Skip to content

Commit

Permalink
Merge pull request #307 from h3poteto/fix/class_methods
Browse files Browse the repository at this point in the history
fix: Use instance method instead of class method in misskey
  • Loading branch information
h3poteto authored Apr 1, 2020
2 parents 6447a67 + a4e1d3a commit 85402e6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 125 deletions.
4 changes: 3 additions & 1 deletion example/typescript/src/misskey/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ client
console.log(tokenData.refreshToken)
console.log()
})
.catch((err: Error) => console.error(err))
.catch((err: any) => {
console.error(err)
})
64 changes: 0 additions & 64 deletions src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 24 additions & 22 deletions src/misskey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// ======================================
Expand Down Expand Up @@ -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> {
Expand Down
40 changes: 2 additions & 38 deletions src/misskey/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -400,55 +398,21 @@ 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
* @param params Form data
*/
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
Expand Down

0 comments on commit 85402e6

Please sign in to comment.