1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Posting using formData

This commit is contained in:
Zhiyuan Zheng
2020-11-17 23:57:23 +01:00
parent 5cf6eaa8d9
commit c0d7f379b3
8 changed files with 345 additions and 66 deletions

View File

@ -18,28 +18,29 @@ const client = async ({
query?: {
[key: string]: string | number | boolean
}
body?: object
body?: FormData
}): Promise<any> => {
const state: RootState['instanceInfo'] = store.getState().instanceInfo
let response
try {
response = await ky(endpoint, {
method: method,
prefixUrl: `https://${state[instance]}/api/${version}`,
searchParams: query,
headers: {
'Content-Type': 'application/json',
...headers,
...(instance === 'local' && {
Authorization: `Bearer ${state.localToken}`
})
},
...(body && { json: body })
})
} catch (error) {
return Promise.reject('ky error: ' + error)
}
// try {
response = await ky(endpoint, {
method: method,
prefixUrl: `https://${state[instance]}/api/${version}`,
searchParams: query,
headers: {
'Content-Type': 'application/json',
...headers,
...(instance === 'local' && {
Authorization: `Bearer ${state.localToken}`
})
},
...(body && { body: body }),
throwHttpErrors: false
})
// } catch (error) {
// return Promise.reject('ky error: ' + error.json())
// }
if (response.ok) {
return Promise.resolve({
@ -47,8 +48,9 @@ const client = async ({
body: await response.json()
})
} else {
console.error(response.status + ': ' + response.statusText)
return Promise.reject({ body: response.statusText })
const errorResponse = await response.json()
console.error(response.status + ': ' + errorResponse.error)
return Promise.reject({ body: errorResponse.error })
}
}