Ensured response from servers.json has been parsed to a json array

This commit is contained in:
Alejandro Celaya
2019-10-21 19:38:32 +02:00
parent f74d135922
commit fc7a2e0c6d
2 changed files with 26 additions and 16 deletions

View File

@@ -32,9 +32,19 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
// If local list is empty, try to fetch it remotely and calculate IDs for every server
// It's important to parse the content to json, so that it is ignored for other formats (because it will catch)
const getDataAsJsonWithIds = pipe(prop('data'), JSON.parse, map(assocId));
const getDataAsArrayWithIds = pipe(
prop('data'),
(value) => {
if (!Array.isArray(value)) {
throw new Error('Value is not an array');
}
return value;
},
map(assocId),
);
const remoteList = await get(`${homepage}/servers.json`)
.then(getDataAsJsonWithIds)
.then(getDataAsArrayWithIds)
.catch(() => []);
createServers(remoteList);