mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 12:36:57 +00:00
fix user-agent / authorization passing in megalodon
This commit is contained in:
parent
22bba7fe6d
commit
5815d2f537
1 changed files with 22 additions and 20 deletions
|
@ -1,6 +1,5 @@
|
||||||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import FormData from 'form-data'
|
|
||||||
|
|
||||||
import { DEFAULT_UA } from '../default'
|
import { DEFAULT_UA } from '../default'
|
||||||
import Response from '../response'
|
import Response from '../response'
|
||||||
|
@ -575,22 +574,26 @@ namespace MisskeyAPI {
|
||||||
this.accessToken = accessToken
|
this.accessToken = accessToken
|
||||||
this.baseUrl = baseUrl
|
this.baseUrl = baseUrl
|
||||||
this.userAgent = userAgent
|
this.userAgent = userAgent
|
||||||
this.abortController = new AbortController()
|
this.abortController = new AbortController();
|
||||||
axios.defaults.signal = this.abortController.signal
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET request to misskey API.
|
* GET request to misskey API.
|
||||||
**/
|
**/
|
||||||
public async get<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
public async get<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||||
|
if (!headers['Authorization'] && this.accessToken) {
|
||||||
|
headers['Authorization'] = `Bearer ${this.accessToken}`;
|
||||||
|
}
|
||||||
|
if (!headers['User-Agent']) {
|
||||||
|
headers['User-Agent'] = this.userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
let options: AxiosRequestConfig = {
|
let options: AxiosRequestConfig = {
|
||||||
params: params,
|
params: params,
|
||||||
headers: {
|
headers,
|
||||||
'User-Agent': this.userAgent,
|
|
||||||
...headers,
|
|
||||||
},
|
|
||||||
maxContentLength: Infinity,
|
maxContentLength: Infinity,
|
||||||
maxBodyLength: Infinity
|
maxBodyLength: Infinity,
|
||||||
|
signal: this.abortController.signal,
|
||||||
}
|
}
|
||||||
return axios.get<T>(this.baseUrl + path, options).then((resp: AxiosResponse<T>) => {
|
return axios.get<T>(this.baseUrl + path, options).then((resp: AxiosResponse<T>) => {
|
||||||
const res: Response<T> = {
|
const res: Response<T> = {
|
||||||
|
@ -610,22 +613,21 @@ namespace MisskeyAPI {
|
||||||
* @param headers Request header object
|
* @param headers Request header object
|
||||||
*/
|
*/
|
||||||
public async post<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
public async post<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||||
|
if (!headers['Authorization'] && this.accessToken) {
|
||||||
|
headers['Authorization'] = `Bearer ${this.accessToken}`;
|
||||||
|
}
|
||||||
|
if (!headers['User-Agent']) {
|
||||||
|
headers['User-Agent'] = this.userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
let options: AxiosRequestConfig = {
|
let options: AxiosRequestConfig = {
|
||||||
headers: headers,
|
headers: headers,
|
||||||
maxContentLength: Infinity,
|
maxContentLength: Infinity,
|
||||||
maxBodyLength: Infinity
|
maxBodyLength: Infinity,
|
||||||
|
signal: this.abortController.signal,
|
||||||
}
|
}
|
||||||
let bodyParams = params
|
|
||||||
if (this.accessToken) {
|
return axios.post<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||||
if (params instanceof FormData) {
|
|
||||||
bodyParams.append('i', this.accessToken)
|
|
||||||
} else {
|
|
||||||
bodyParams = Object.assign(params, {
|
|
||||||
i: this.accessToken
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return axios.post<T>(this.baseUrl + path, bodyParams, options).then((resp: AxiosResponse<T>) => {
|
|
||||||
const res: Response<T> = {
|
const res: Response<T> = {
|
||||||
data: resp.data,
|
data: resp.data,
|
||||||
status: resp.status,
|
status: resp.status,
|
||||||
|
|
Loading…
Add table
Reference in a new issue