From fc7a2e0c6dba501735bc3db4600782beba806495 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 21 Oct 2019 19:38:32 +0200 Subject: [PATCH] Ensured response from servers.json has been parsed to a json array --- src/servers/reducers/server.js | 14 ++++++++++++-- test/servers/reducers/server.test.js | 28 ++++++++++++++-------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/servers/reducers/server.js b/src/servers/reducers/server.js index b14cd713..e28e050a 100644 --- a/src/servers/reducers/server.js +++ b/src/servers/reducers/server.js @@ -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); diff --git a/test/servers/reducers/server.test.js b/test/servers/reducers/server.test.js index e47c57b7..278e3e92 100644 --- a/test/servers/reducers/server.test.js +++ b/test/servers/reducers/server.test.js @@ -50,20 +50,20 @@ describe('serverReducer', () => { each([ [ Promise.resolve({ - data: `[ - { - "id": "111", - "name": "acel.me from servers.json", - "url": "https://acel.me", - "apiKey": "07fb8a96-8059-4094-a24c-80a7d5e7e9b0" - }, - { - "id": "222", - "name": "Local from servers.json", - "url": "http://localhost:8000", - "apiKey": "7a531c75-134e-4d5c-86e0-a71b7167b57a" - } -]`, + data: [ + { + id: '111', + name: 'acel.me from servers.json', + url: 'https://acel.me', + apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0', + }, + { + id: '222', + name: 'Local from servers.json', + url: 'http://localhost:8000', + apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a', + }, + ], }), { 111: {