Skip to content

Commit

Permalink
Add support for fetching client configuration from a remote JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarhane committed May 24, 2017
1 parent f0d3877 commit 480c329
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/shared/modules/commands/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
*/

import { getSettings, update } from 'shared/modules/settings/settingsDuck'
import { extractCommandParameters } from 'services/commandUtils'
import { extractCommandParameters, splitStringOnFirst } from 'services/commandUtils'
import { getJSON } from 'services/remote'
import { isValidURL } from 'shared/modules/commands/helpers/http'

export function handleGetConfigCommand (action, cmdchar, store) {
const settingsState = getSettings(store.getState())
Expand All @@ -29,6 +31,23 @@ export function handleGetConfigCommand (action, cmdchar, store) {

export function handleUpdateConfigCommand (action, cmdchar, put, store) {
const strippedCmd = action.cmd.substr(cmdchar.length)
const toBeSet = extractCommandParameters(`config`, strippedCmd)
put(update(toBeSet))
const parts = splitStringOnFirst(strippedCmd, ' ')
const p = new Promise((resolve, reject) => {
if (parts[1] === undefined) return resolve(true) // Nothing to do
if (!isValidURL(parts[1].trim())) { // Not an URL. Parse as command line params
const params = extractCommandParameters(`config`, strippedCmd)
if (!params) return reject(new Error('Could not parse input. Usage: `:config x: 2`.'))
put(update(params))
return resolve(params)
}
getJSON(parts[1].trim()) // It's an URL. Fetch it.
.then((res) => {
put(update(res))
resolve(res)
})
.catch((e) => {
reject(new Error(e))
})
})
return p
}
7 changes: 6 additions & 1 deletion src/shared/services/commandInterpreterHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ const availableCommands = [{
match: (cmd) => /^config(\s|$)/.test(cmd),
exec: function (action, cmdchar, put, store) {
handleUpdateConfigCommand(action, cmdchar, put, store)
put(frames.add({...action, ...handleGetConfigCommand(action, cmdchar, store)}))
.then((res) => {
put(frames.add({...action, ...handleGetConfigCommand(action, cmdchar, store)}))
})
.catch((e) => {
put(showErrorMessage(e.message))
})
}
}, {
name: 'set-param',
Expand Down
4 changes: 2 additions & 2 deletions src/shared/services/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function get (url) {
})
}

function getJSON (url) {
export function getJSON (url) {
return fetch(url, {
method: 'get',
headers: {
Expand All @@ -49,7 +49,7 @@ function getJSON (url) {
}).then((response) => {
return response.json()
}).catch((e) => {
return e
throw new Error(e)
})
}

Expand Down

0 comments on commit 480c329

Please sign in to comment.