yunohost-admin/app/src/helpers/api.js

163 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-09-10 19:10:37 +02:00
/**
* api module.
* @module api
*/
import store from '@/store'
2020-09-10 19:10:37 +02:00
/**
* Converts an object literal into an `URLSearchParams` that can be turned into a
* query string or used as a body in a `fetch` call.
*
* @param {Object} object - An object literal to convert.
* @param {Object} options
* @param {boolean} [options.addLocale=false] - Option to append the locale to the query string.
* @return {URLSearchParams}
*/
export function objectToParams (object, { addLocale = false } = {}) {
const urlParams = new URLSearchParams()
2020-07-15 16:39:24 +02:00
for (const [key, value] of Object.entries(object)) {
2020-09-25 09:20:41 +02:00
if (Array.isArray(value)) {
value.forEach(v => urlParams.append(key, v))
} else {
urlParams.append(key, value)
}
2020-07-15 16:39:24 +02:00
}
2020-09-10 19:10:37 +02:00
if (addLocale) {
urlParams.append('locale', store.getters.locale)
}
2020-07-15 16:39:24 +02:00
return urlParams
}
2020-09-10 19:10:37 +02:00
/**
* Handler for api responses.
*
* @param {Response} response - A fetch `Response` object.
* @return {DigestedResponse} Parsed response's json, response's text or an error.
*/
export async function handleResponse (response) {
if (!response.ok) return handleErrors(response)
// FIXME the api should always return json objects
const responseText = await response.text()
try {
return JSON.parse(responseText)
} catch {
return responseText
}
}
2020-09-10 19:10:37 +02:00
/**
* Handler for API errors.
*
* @param {Response} response - A fetch `Response` object.
* @throws Will throw an error with the API response text or custom message.
*/
export async function handleErrors (response) {
2020-07-15 16:39:24 +02:00
if (response.status === 401) {
throw new Error('Unauthorized')
} else if (response.status === 400) {
const message = await response.text()
throw new Error(message)
2020-07-15 16:39:24 +02:00
}
}
2020-09-10 19:10:37 +02:00
/**
* A digested fetch response as an object, a string or an error.
* @typedef {(Object|string|Error)} DigestedResponse
*/
/**
* Actual api module.
* @module api/default
*/
export default {
2020-07-15 16:39:24 +02:00
options: {
credentials: 'include',
mode: 'cors',
headers: {
// FIXME is it important to keep this previous `Accept` header ?
// 'Accept': 'application/json, text/javascript, */*; q=0.01',
// Auto header is :
// "Accept": "*/*",
// Also is this still important ? (needed by back-end)
'X-Requested-With': 'XMLHttpRequest'
}
2020-07-15 16:39:24 +02:00
},
2020-09-10 19:10:37 +02:00
/**
* Generic method to fetch the api without automatic response handling.
*
* @param {string} method - a method between 'GET', 'POST', 'PUT' and 'DELETE'.
* @param {string} uri
* @param {string} [data={}] - data to send as body for 'POST', 'PUT' and 'DELETE' methods.
* @return {Promise<Response>} Promise that resolve a fetch `Response`.
*/
fetch (method, uri, data = {}) {
if (method === 'GET') {
const localeQs = `${uri.includes('?') ? '&' : '?'}locale=${store.getters.locale}`
return fetch('/api/' + uri + localeQs, this.options)
}
return fetch('/api/' + uri, {
...this.options,
method,
body: objectToParams(data, { addLocale: true })
})
},
/**
* Api get helper function.
*
* @param {string} uri - the uri to call.
* @return {Promise<module:api~DigestedResponse>} Promise that resolve the api response as an object, a string or as an error.
*/
get (uri) {
return this.fetch('GET', uri).then(handleResponse)
2020-07-15 16:39:24 +02:00
},
2020-09-10 19:10:37 +02:00
/**
* Api get helper function for multiple queries.
*
* @param {string} uri - the uri to call.
* @return {Promise<module:api~DigestedResponse[]>} Promise that resolve the api responses as an array.
*/
getAll (uris) {
2020-09-10 19:10:37 +02:00
return Promise.all(uris.map(uri => this.get(uri)))
},
2020-09-10 19:10:37 +02:00
/**
* Api post helper function.
*
* @param {string} uri - the uri to call.
* @param {string} [data={}] - data to send as body.
* @return {Promise<module:api~DigestedResponse>} Promise that resolve the api responses as an array.
*/
post (uri, data = {}) {
2020-09-10 19:10:37 +02:00
return this.fetch('POST', uri, data).then(handleResponse)
},
2020-09-10 19:10:37 +02:00
/**
* Api put helper function.
*
* @param {string} uri - the uri to call.
* @param {string} [data={}] - data to send as body.
* @return {Promise<module:api~DigestedResponse>} Promise that resolve the api responses as an array.
*/
put (uri, data = {}) {
2020-09-10 19:10:37 +02:00
return this.fetch('PUT', uri, data).then(handleResponse)
2020-07-27 20:46:27 +02:00
},
2020-09-10 19:10:37 +02:00
/**
* Api delete helper function.
*
* @param {string} uri - the uri to call.
* @param {string} [data={}] - data to send as body.
* @return {Promise<('ok'|Error)>} Promise that resolve the api responses as an array.
*/
delete (uri, data = {}) {
2020-09-10 19:10:37 +02:00
return this.fetch('DELETE', uri, data).then(response => {
return response.ok ? 'ok' : handleErrors(response)
})
2020-07-15 16:39:24 +02:00
}
}