mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 01:20:24 +03:00
Ensured response from servers.json has been parsed to a json array
This commit is contained in:
parent
f74d135922
commit
fc7a2e0c6d
2 changed files with 26 additions and 16 deletions
|
@ -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);
|
||||
|
|
|
@ -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: {
|
||||
|
|
Loading…
Reference in a new issue