Merge pull request #1510 from h3poteto/fix/axios-post-multipart

Update axios to 1.2.0 and fix multipart form posting
This commit is contained in:
AkiraFukushima 2022-12-25 15:00:09 +09:00 committed by GitHub
commit 1ffb42fbae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 270 additions and 28 deletions

View file

@ -57,7 +57,7 @@
"dependencies": {
"@types/oauth": "^0.9.0",
"@types/ws": "^8.2.3",
"axios": "1.1.3",
"axios": "1.2.1",
"dayjs": "^1.11.7",
"form-data": "^4.0.0",
"https-proxy-agent": "^5.0.1",

View file

@ -1309,11 +1309,7 @@ export default class Mastodon implements MegalodonInterface {
formData.append('focus', options.focus)
}
}
let headers: { [key: string]: string } = {}
if (typeof formData.getHeaders === 'function') {
headers = formData.getHeaders()
}
return this.client.post<MastodonAPI.Entity.AsyncAttachment>('/api/v2/media', formData, headers).then(res => {
return this.client.postForm<MastodonAPI.Entity.AsyncAttachment>('/api/v2/media', formData).then(res => {
return Object.assign(res, {
data: MastodonAPI.Converter.async_attachment(res.data)
})
@ -1348,11 +1344,7 @@ export default class Mastodon implements MegalodonInterface {
formData.append('focus', options.focus)
}
}
let headers: { [key: string]: string } = {}
if (typeof formData.getHeaders === 'function') {
headers = formData.getHeaders()
}
return this.client.put<MastodonAPI.Entity.Attachment>(`/api/v1/media/${id}`, formData, headers).then(res => {
return this.client.putForm<MastodonAPI.Entity.Attachment>(`/api/v1/media/${id}`, formData).then(res => {
return Object.assign(res, {
data: MastodonAPI.Converter.attachment(res.data)
})

View file

@ -18,8 +18,11 @@ namespace MastodonAPI {
export interface Interface {
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }, pathIsFullyQualified?: boolean): Promise<Response<T>>
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): WebSocket
@ -157,6 +160,51 @@ namespace MastodonAPI {
})
}
/**
* PUT request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.putForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PATCH request to mastodon REST API.
* @param path relative path from baseUrl
@ -202,6 +250,51 @@ namespace MastodonAPI {
})
}
/**
* PATCH request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.patchForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.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
@ -238,6 +331,42 @@ namespace MastodonAPI {
})
}
/**
* POST request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
*/
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios.postForm<T>(this.baseUrl + 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
})
}
/**
* DELETE request to mastodon REST API.
* @param path relative path from baseUrl

View file

@ -1295,11 +1295,7 @@ export default class Pleroma implements MegalodonInterface {
formData.append('focus', options.focus)
}
}
let headers: { [key: string]: string } = {}
if (typeof formData.getHeaders === 'function') {
headers = formData.getHeaders()
}
return this.client.post<PleromaAPI.Entity.AsyncAttachment>('/api/v2/media', formData, headers).then(res => {
return this.client.postForm<PleromaAPI.Entity.AsyncAttachment>('/api/v2/media', formData).then(res => {
return Object.assign(res, {
data: PleromaAPI.Converter.async_attachment(res.data)
})
@ -1334,11 +1330,7 @@ export default class Pleroma implements MegalodonInterface {
formData.append('focus', options.focus)
}
}
let headers: { [key: string]: string } = {}
if (typeof formData.getHeaders === 'function') {
headers = formData.getHeaders()
}
return this.client.put<PleromaAPI.Entity.Attachment>(`/api/v1/media/${id}`, formData, headers).then(res => {
return this.client.putForm<PleromaAPI.Entity.Attachment>(`/api/v1/media/${id}`, formData).then(res => {
return Object.assign(res, {
data: PleromaAPI.Converter.attachment(res.data)
})

View file

@ -248,8 +248,11 @@ namespace PleromaAPI {
export interface Interface {
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): WebSocket
@ -380,6 +383,51 @@ namespace PleromaAPI {
})
}
/**
* PUT request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.putForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PATCH request to mastodon REST API.
* @param path relative path from baseUrl
@ -425,6 +473,51 @@ namespace PleromaAPI {
})
}
/**
* PATCH request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.patchForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.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
@ -461,6 +554,42 @@ namespace PleromaAPI {
})
}
/**
* POST request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
*/
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios.postForm<T>(this.baseUrl + 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
})
}
/**
* DELETE request to mastodon REST API.
* @param path relative path from baseUrl

View file

@ -10,15 +10,15 @@ describe('detector', () => {
})
describe('pleroma', () => {
const url = 'https://pleroma.io'
const url = 'https://pleroma.soykaf.com'
it('should be pleroma', async () => {
const pleroma = await detector(url)
expect(pleroma).toEqual('pleroma')
})
})
describe('misskey', () => {
const url = 'https://misskey.io'
describe.skip('misskey', () => {
const url = 'https://sushi.ski'
it('should be misskey', async () => {
const misskey = await detector(url)
expect(misskey).toEqual('misskey')

View file

@ -1506,10 +1506,10 @@ available-typed-arrays@^1.0.5:
resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7"
integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw==
axios@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.1.3.tgz#8274250dada2edf53814ed7db644b9c2866c1e35"
integrity sha512-00tXVRwKx/FZr/IDVFt4C+f9FYairX517WoGCL6dpOntqLkZofjhu43F/Xl44UOpqa+9sLFDrG/XAnFsUYgkDA==
axios@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.1.tgz#44cf04a3c9f0c2252ebd85975361c026cb9f864a"
integrity sha512-I88cFiGu9ryt/tfVEi4kX2SITsvDddTajXTOFmt2uK1ZVA8LytjtdeyefdQWEf5PU8w+4SSJDoYnggflB5tW4A==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"