refactor promisedRequest to always return whole response
This commit is contained in:
parent
c81813064b
commit
1ca0ffb1f0
25 changed files with 352 additions and 327 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { parseChat } from 'src/services/entity_normalizer/entity_normalizer.service.js'
|
||||
import { paramsString, promisedRequest } from './helpers.js'
|
||||
|
||||
import { parseChat } from 'src/services/entity_normalizer/entity_normalizer.service.js'
|
||||
|
||||
const PLEROMA_CHATS_URL = '/api/v1/pleroma/chats'
|
||||
const PLEROMA_CHAT_URL = (id) => `/api/v1/pleroma/chats/by-account-id/${id}`
|
||||
const PLEROMA_CHAT_MESSAGES_URL = (id, { maxId, sinceId, limit }) =>
|
||||
|
|
@ -13,7 +14,7 @@ export const chats = ({ credentials }) =>
|
|||
promisedRequest({
|
||||
url: PLEROMA_CHATS_URL,
|
||||
credentials,
|
||||
}).then((data) => ({
|
||||
}).then(({ data }) => ({
|
||||
chatList: data.map(parseChat).filter((c) => c),
|
||||
}))
|
||||
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ export const promisedRequest = async ({
|
|||
)
|
||||
.join('&')
|
||||
}
|
||||
|
||||
if (formData || payload) {
|
||||
options.body = formData || JSON.stringify(payload)
|
||||
}
|
||||
|
|
@ -109,33 +110,37 @@ export const promisedRequest = async ({
|
|||
}
|
||||
}
|
||||
|
||||
const response = await fetch(url, options)
|
||||
|
||||
// 204 is "No content", which fails to parse json (as you'd might think)
|
||||
if (response.ok && response.status === 204) return { _response: response }
|
||||
|
||||
let response = null
|
||||
try {
|
||||
const json = await response.json()
|
||||
response = await fetch(url, options)
|
||||
const data = await (async () => {
|
||||
const [contentType] = response.headers
|
||||
.get('content-type')
|
||||
.split(';')
|
||||
.map((x) => x.toLowerCase().trim())
|
||||
|
||||
if (typeof json !== 'object') {
|
||||
return {
|
||||
_response: response,
|
||||
_value: json,
|
||||
switch (contentType) {
|
||||
case 'text/plain':
|
||||
return await response.text()
|
||||
case 'application/json':
|
||||
return await response.json()
|
||||
default:
|
||||
return await response.bytes()
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
||||
json._response = response
|
||||
const { ok, status } = response
|
||||
|
||||
if (!response.ok) {
|
||||
if (ok) {
|
||||
return { response, status, data }
|
||||
} else {
|
||||
throw new StatusCodeError(
|
||||
response.status,
|
||||
json,
|
||||
data,
|
||||
{ url, options },
|
||||
response,
|
||||
)
|
||||
}
|
||||
|
||||
return json
|
||||
} catch (error) {
|
||||
throw new StatusCodeError(
|
||||
response.status,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { concat, each, last, map } from 'lodash'
|
||||
|
||||
import { paramsString, promisedRequest } from './helpers.js'
|
||||
|
||||
import {
|
||||
parseAttachment,
|
||||
parseChat,
|
||||
|
|
@ -9,8 +11,6 @@ import {
|
|||
parseStatus,
|
||||
parseUser,
|
||||
} from 'src/services/entity_normalizer/entity_normalizer.service.js'
|
||||
import { paramsString, promisedRequest } from './helpers.js'
|
||||
|
||||
import { RegistrationError, StatusCodeError } from 'src/services/errors/errors'
|
||||
|
||||
const SUGGESTIONS_URL = '/api/v1/suggestions'
|
||||
|
|
@ -102,7 +102,7 @@ export const fetchUser = ({ id, credentials }) =>
|
|||
promisedRequest({
|
||||
url: `${MASTODON_USER_URL}/${id}`,
|
||||
credentials,
|
||||
}).then((data) => parseUser(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
|
||||
export const fetchUserByName = ({ name, credentials }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -126,7 +126,7 @@ export const fetchFriends = ({ id, maxId, sinceId, limit = 20, credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_FOLLOWING_URL(id, { maxId, sinceId, limit }),
|
||||
credentials,
|
||||
}).then((data) => data.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const fetchFollowers = ({
|
||||
id,
|
||||
|
|
@ -143,16 +143,20 @@ export const fetchFollowers = ({
|
|||
withRelationships: true,
|
||||
}),
|
||||
credentials,
|
||||
}).then((data) => data.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const fetchConversation = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_STATUS_CONTEXT_URL(id),
|
||||
credentials,
|
||||
})
|
||||
.then(({ ancestors, descendants }) => ({
|
||||
ancestors: ancestors.map(parseStatus),
|
||||
descendants: descendants.map(parseStatus),
|
||||
.then((result) => ({
|
||||
...result,
|
||||
data: {
|
||||
...result.data,
|
||||
ancestors: result.data.ancestors.map(parseStatus),
|
||||
descendants: result.data.descendants.map(parseStatus),
|
||||
},
|
||||
}))
|
||||
.catch((error) => {
|
||||
throw new Error('Error fetching timeline', error)
|
||||
|
|
@ -163,7 +167,7 @@ export const fetchStatus = ({ id, credentials }) =>
|
|||
url: MASTODON_STATUS_URL(id),
|
||||
credentials,
|
||||
})
|
||||
.then((data) => parseStatus(data))
|
||||
.then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
.catch((error) => {
|
||||
throw new Error('Error fetching timeline', error)
|
||||
})
|
||||
|
|
@ -173,7 +177,7 @@ export const fetchStatusSource = ({ id, credentials }) =>
|
|||
url: MASTODON_STATUS_SOURCE_URL(id),
|
||||
credentials,
|
||||
})
|
||||
.then((data) => parseSource(data))
|
||||
.then(({ data, ...rest }) => ({ ...rest, data: parseSource(data) }))
|
||||
.catch((error) => {
|
||||
throw new Error('Error fetching timeline', error)
|
||||
})
|
||||
|
|
@ -182,9 +186,8 @@ export const fetchStatusHistory = ({ status, credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_STATUS_HISTORY_URL(status.id),
|
||||
credentials,
|
||||
}).then((data) => {
|
||||
data.reverse()
|
||||
return data.map((item) => {
|
||||
}).then(({ data }) => {
|
||||
return [...data].reverse().map((item) => {
|
||||
item.originalStatus = status
|
||||
return parseStatus(item)
|
||||
})
|
||||
|
|
@ -277,16 +280,17 @@ export const fetchTimeline = ({
|
|||
return promisedRequest({
|
||||
url: url + paramsString(params),
|
||||
credentials,
|
||||
}).then(async (data) => {
|
||||
}).then(async (result) => {
|
||||
const pagination = parseLinkHeaderPagination(
|
||||
data._response.headers.get('Link'),
|
||||
result.response.headers.get('Link'),
|
||||
{
|
||||
flakeId: timeline !== 'bookmarks' && timeline !== 'notifications',
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
data: data.map(isNotifications ? parseNotification : parseStatus),
|
||||
...result,
|
||||
data: result.data.map(isNotifications ? parseNotification : parseStatus),
|
||||
pagination,
|
||||
}
|
||||
})
|
||||
|
|
@ -301,13 +305,13 @@ export const fetchPinnedStatuses = ({ id, credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_USER_TIMELINE_URL(id) + '?pinned=true',
|
||||
credentials,
|
||||
}).then((data) => data.map(parseStatus))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseStatus) }))
|
||||
|
||||
export const verifyCredentials = ({ credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_LOGIN_URL,
|
||||
credentials,
|
||||
}).then((data) => (data.error ? data : parseUser(data)))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
|
||||
export const suggestions = ({ credentials }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -327,25 +331,26 @@ export const fetchFavoritedByUsers = ({ id, credentials }) =>
|
|||
url: MASTODON_STATUS_FAVORITEDBY_URL(id),
|
||||
method: 'GET',
|
||||
credentials,
|
||||
}).then((users) => users.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
|
||||
export const fetchRebloggedByUsers = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_STATUS_REBLOGGEDBY_URL(id),
|
||||
method: 'GET',
|
||||
credentials,
|
||||
}).then((users) => users.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
|
||||
export const fetchEmojiReactions = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: PLEROMA_EMOJI_REACTIONS_URL(id),
|
||||
credentials,
|
||||
}).then((reactions) =>
|
||||
reactions.map((r) => {
|
||||
}).then(({ data, ...rest }) => ({
|
||||
...rest,
|
||||
data: data.map((r) => {
|
||||
r.accounts = r.accounts.map(parseUser)
|
||||
return r
|
||||
}),
|
||||
)
|
||||
}))
|
||||
|
||||
export const searchUsers = ({ credentials, query }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -355,7 +360,7 @@ export const searchUsers = ({ credentials, query }) =>
|
|||
resolve: true,
|
||||
},
|
||||
credentials,
|
||||
}).then((data) => data.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const search2 = ({
|
||||
credentials,
|
||||
|
|
@ -404,10 +409,10 @@ export const search2 = ({
|
|||
url,
|
||||
credentials,
|
||||
})
|
||||
.then((data) => {
|
||||
.then(({ data, ...rest }) => {
|
||||
data.accounts = data.accounts.slice(0, limit).map((u) => parseUser(u))
|
||||
data.statuses = data.statuses.slice(0, limit).map((s) => parseStatus(s))
|
||||
return data
|
||||
return { ...rest, data }
|
||||
})
|
||||
.catch((error) => {
|
||||
throw new Error('Error fetching timeline', error)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
import { concat, each, last, map } from 'lodash'
|
||||
|
||||
import { paramsString, promisedRequest } from './helpers.js'
|
||||
import { fetchFriends, MASTODON_STATUS_URL } from './public.js'
|
||||
|
||||
import {
|
||||
parseAttachment,
|
||||
parseNotification,
|
||||
parseSource,
|
||||
parseStatus,
|
||||
parseUser,
|
||||
} from 'src/services/entity_normalizer/entity_normalizer.service.js'
|
||||
import { paramsString, promisedRequest } from './helpers.js'
|
||||
import { fetchFriends, MASTODON_STATUS_URL } from './public.js'
|
||||
|
||||
const MUTES_IMPORT_URL = '/api/pleroma/mutes_import'
|
||||
const BLOCKS_IMPORT_URL = '/api/pleroma/blocks_import'
|
||||
|
|
@ -99,42 +98,42 @@ export const favorite = ({ id, credentials }) =>
|
|||
url: MASTODON_FAVORITE_URL(id),
|
||||
method: 'POST',
|
||||
credentials,
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const unfavorite = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_UNFAVORITE_URL(id),
|
||||
method: 'POST',
|
||||
credentials,
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const retweet = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_RETWEET_URL(id),
|
||||
method: 'POST',
|
||||
credentials,
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const unretweet = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_UNRETWEET_URL(id),
|
||||
method: 'POST',
|
||||
credentials,
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const reactWithEmoji = ({ id, emoji, credentials }) =>
|
||||
promisedRequest({
|
||||
url: PLEROMA_EMOJI_REACT_URL(id, emoji),
|
||||
method: 'PUT',
|
||||
credentials,
|
||||
}).then(parseStatus)
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const unreactWithEmoji = ({ id, emoji, credentials }) =>
|
||||
promisedRequest({
|
||||
url: PLEROMA_EMOJI_UNREACT_URL(id, emoji),
|
||||
method: 'DELETE',
|
||||
credentials,
|
||||
}).then(parseStatus)
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const bookmarkStatus = ({ id, credentials, ...options }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -158,28 +157,28 @@ export const pinOwnStatus = ({ id, credentials }) =>
|
|||
url: MASTODON_PIN_OWN_STATUS(id),
|
||||
credentials,
|
||||
method: 'POST',
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const unpinOwnStatus = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_UNPIN_OWN_STATUS(id),
|
||||
credentials,
|
||||
method: 'POST',
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const muteConversation = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_MUTE_CONVERSATION(id),
|
||||
credentials,
|
||||
method: 'POST',
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const unmuteConversation = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
url: MASTODON_UNMUTE_CONVERSATION(id),
|
||||
credentials,
|
||||
method: 'POST',
|
||||
}).then((data) => parseStatus(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
|
||||
export const vote = ({ pollId, choices, credentials }) => {
|
||||
const form = new FormData()
|
||||
|
|
@ -256,7 +255,7 @@ export const postStatus = ({
|
|||
method: 'POST',
|
||||
credentials,
|
||||
headers,
|
||||
}).then((data) => (data.error ? data : parseStatus(data)))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
}
|
||||
|
||||
export const editStatus = ({
|
||||
|
|
@ -299,7 +298,7 @@ export const editStatus = ({
|
|||
formData: form,
|
||||
method: 'PUT',
|
||||
credentials,
|
||||
}).then((data) => (data.error ? data : parseStatus(data)))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
||||
}
|
||||
|
||||
export const deleteStatus = ({ id, credentials }) =>
|
||||
|
|
@ -315,7 +314,7 @@ export const uploadMedia = ({ formData, credentials }) =>
|
|||
formData,
|
||||
method: 'POST',
|
||||
credentials,
|
||||
}).then((data) => parseAttachment(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseAttachment(data) }))
|
||||
|
||||
export const setMediaDescription = ({ id, description, credentials }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -325,7 +324,7 @@ export const setMediaDescription = ({ id, description, credentials }) =>
|
|||
payload: {
|
||||
description,
|
||||
},
|
||||
}).then((data) => parseAttachment(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseAttachment(data) }))
|
||||
|
||||
// #Notifications
|
||||
export const dismissNotification = ({ credentials, id }) =>
|
||||
|
|
@ -456,12 +455,7 @@ export const updateProfileImages = ({
|
|||
credentials,
|
||||
method: 'PATCH',
|
||||
formData: form,
|
||||
}).then((data) => {
|
||||
if (data.error) {
|
||||
throw new Error(data.error)
|
||||
}
|
||||
return parseUser(data)
|
||||
})
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
}
|
||||
|
||||
export const updateProfile = ({ credentials, params }) => {
|
||||
|
|
@ -489,7 +483,7 @@ export const updateProfile = ({ credentials, params }) => {
|
|||
credentials,
|
||||
method: 'PATCH',
|
||||
formData,
|
||||
}).then((data) => parseUser(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
}
|
||||
|
||||
export const updateProfileJSON = ({ credentials, params }) =>
|
||||
|
|
@ -498,7 +492,7 @@ export const updateProfileJSON = ({ credentials, params }) =>
|
|||
credentials,
|
||||
payload: params,
|
||||
method: 'PATCH',
|
||||
}).then((data) => parseUser(data))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
||||
|
||||
export const changeEmail = ({ credentials, email, password }) => {
|
||||
const form = new FormData()
|
||||
|
|
@ -671,7 +665,7 @@ export const fetchFollowRequests = ({ credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_FOLLOW_REQUESTS_URL,
|
||||
credentials,
|
||||
}).then((data) => data.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const approveUser = ({ id, credentials }) =>
|
||||
promisedRequest({
|
||||
|
|
@ -701,7 +695,7 @@ export const fetchMutes = ({ maxId, credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_USER_MUTES_URL({ maxId, withRelationships: true }),
|
||||
credentials,
|
||||
}).then((users) => users.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const muteUser = ({ id, expiresIn, credentials }) => {
|
||||
const payload = {}
|
||||
|
|
@ -728,7 +722,7 @@ export const fetchBlocks = ({ maxId, credentials }) =>
|
|||
promisedRequest({
|
||||
url: MASTODON_USER_BLOCKS_URL({ maxId, withRelationships: true }),
|
||||
credentials,
|
||||
}).then((users) => users.map(parseUser))
|
||||
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
||||
|
||||
export const blockUser = ({ id, expiresIn, credentials }) => {
|
||||
const payload = {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue