Refactor user search api, better api error response handling
This commit is contained in:
parent
2f87540612
commit
532b76eb64
7 changed files with 31 additions and 79 deletions
|
@ -65,6 +65,7 @@ const MASTODON_PROFILE_UPDATE_URL = '/api/v1/accounts/update_credentials'
|
|||
const MASTODON_REPORT_USER_URL = '/api/v1/reports'
|
||||
const MASTODON_PIN_OWN_STATUS = id => `/api/v1/statuses/${id}/pin`
|
||||
const MASTODON_UNPIN_OWN_STATUS = id => `/api/v1/statuses/${id}/unpin`
|
||||
const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search'
|
||||
|
||||
const oldfetch = window.fetch
|
||||
|
||||
|
@ -76,7 +77,7 @@ let fetch = (url, options) => {
|
|||
return oldfetch(fullUrl, options)
|
||||
}
|
||||
|
||||
const promisedRequest = ({ method, url, payload, credentials, headers = {} }) => {
|
||||
const promisedRequest = ({ method, url, params, payload, credentials, headers = {} }) => {
|
||||
const options = {
|
||||
method,
|
||||
headers: {
|
||||
|
@ -85,6 +86,11 @@ const promisedRequest = ({ method, url, payload, credentials, headers = {} }) =>
|
|||
...headers
|
||||
}
|
||||
}
|
||||
if (params) {
|
||||
url += '?' + Object.entries(params)
|
||||
.map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))
|
||||
.join('&')
|
||||
}
|
||||
if (payload) {
|
||||
options.body = JSON.stringify(payload)
|
||||
}
|
||||
|
@ -837,6 +843,18 @@ const reportUser = ({ credentials, userId, statusIds, comment, forward }) => {
|
|||
})
|
||||
}
|
||||
|
||||
const searchUsers = ({ credentials, query }) => {
|
||||
return promisedRequest({
|
||||
url: MASTODON_USER_SEARCH_URL,
|
||||
params: {
|
||||
q: query,
|
||||
resolve: true
|
||||
},
|
||||
credentials
|
||||
})
|
||||
.then((data) => data.map(parseUser))
|
||||
}
|
||||
|
||||
const apiService = {
|
||||
verifyCredentials,
|
||||
fetchTimeline,
|
||||
|
@ -899,7 +917,8 @@ const apiService = {
|
|||
fetchFavoritedByUsers,
|
||||
fetchRebloggedByUsers,
|
||||
reportUser,
|
||||
updateNotificationSettings
|
||||
updateNotificationSettings,
|
||||
searchUsers
|
||||
}
|
||||
|
||||
export default apiService
|
||||
|
|
|
@ -147,6 +147,8 @@ const backendInteractorService = credentials => {
|
|||
const retweet = (id) => apiService.retweet({ id, credentials })
|
||||
const unretweet = (id) => apiService.unretweet({ id, credentials })
|
||||
|
||||
const searchUsers = (query) => apiService.searchUsers({ query, credentials })
|
||||
|
||||
const backendInteractorServiceInstance = {
|
||||
fetchStatus,
|
||||
fetchConversation,
|
||||
|
@ -205,7 +207,8 @@ const backendInteractorService = credentials => {
|
|||
unfavorite,
|
||||
retweet,
|
||||
unretweet,
|
||||
updateNotificationSettings
|
||||
updateNotificationSettings,
|
||||
searchUsers
|
||||
}
|
||||
|
||||
return backendInteractorServiceInstance
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
import utils from './utils.js'
|
||||
import { parseUser } from '../entity_normalizer/entity_normalizer.service.js'
|
||||
|
||||
const search = ({ query, store }) => {
|
||||
return utils.request({
|
||||
store,
|
||||
url: '/api/v1/accounts/search',
|
||||
params: {
|
||||
q: query,
|
||||
resolve: true
|
||||
}
|
||||
})
|
||||
.then((data) => data.json())
|
||||
.then((data) => data.map(parseUser))
|
||||
}
|
||||
const UserSearch = {
|
||||
search
|
||||
}
|
||||
|
||||
export default UserSearch
|
|
@ -1,36 +0,0 @@
|
|||
const queryParams = (params) => {
|
||||
return Object.keys(params)
|
||||
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
|
||||
.join('&')
|
||||
}
|
||||
|
||||
const headers = (store) => {
|
||||
const accessToken = store.getters.getToken()
|
||||
if (accessToken) {
|
||||
return { 'Authorization': `Bearer ${accessToken}` }
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
const request = ({ method = 'GET', url, params, store }) => {
|
||||
const instance = store.state.instance.server
|
||||
let fullUrl = `${instance}${url}`
|
||||
|
||||
if (method === 'GET' && params) {
|
||||
fullUrl = fullUrl + `?${queryParams(params)}`
|
||||
}
|
||||
|
||||
return window.fetch(fullUrl, {
|
||||
method,
|
||||
headers: headers(store),
|
||||
credentials: 'same-origin'
|
||||
})
|
||||
}
|
||||
|
||||
const utils = {
|
||||
queryParams,
|
||||
request
|
||||
}
|
||||
|
||||
export default utils
|
Loading…
Add table
Add a link
Reference in a new issue