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 // 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) // 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`) const remoteList = await get(`${homepage}/servers.json`)
.then(getDataAsJsonWithIds) .then(getDataAsArrayWithIds)
.catch(() => []); .catch(() => []);
createServers(remoteList); createServers(remoteList);

View file

@ -50,20 +50,20 @@ describe('serverReducer', () => {
each([ each([
[ [
Promise.resolve({ Promise.resolve({
data: `[ data: [
{ {
"id": "111", id: '111',
"name": "acel.me from servers.json", name: 'acel.me from servers.json',
"url": "https://acel.me", url: 'https://acel.me',
"apiKey": "07fb8a96-8059-4094-a24c-80a7d5e7e9b0" apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
}, },
{ {
"id": "222", id: '222',
"name": "Local from servers.json", name: 'Local from servers.json',
"url": "http://localhost:8000", url: 'http://localhost:8000',
"apiKey": "7a531c75-134e-4d5c-86e0-a71b7167b57a" apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
} },
]`, ],
}), }),
{ {
111: { 111: {