mothership-client
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -1,2 +0,3 @@ | ||
const request = require('sync-request'); | ||
const _get = require('lodash/get'); | ||
const got = require('got'); | ||
const debug = require('debug')('mothership:client'); | ||
@@ -6,27 +7,65 @@ | ||
let config; | ||
const syncRequest = require('sync-request'); | ||
const asyncRequest = got.extend({ | ||
json: true | ||
}); | ||
function init(opts) { | ||
let { key, env } = opts; | ||
let configUrl = `${MOTHERHIP_BASE_URL}/api/configs/retrieve`; | ||
let config = null; | ||
// Ensure we don't have a config already | ||
function init(key, opts) { | ||
if (typeof key === 'object') { | ||
key = key.key; | ||
} | ||
opts = { | ||
sync: false, | ||
...opts | ||
}; | ||
// Ensure we don't re-use a previous config | ||
config = null; | ||
const configUrl = `${MOTHERHIP_BASE_URL}/api/configs/retrieve`; | ||
const headers = { | ||
'X-Config-Key': key | ||
}; | ||
if (opts.sync) { | ||
return config = syncClient(configUrl, headers); | ||
} | ||
// Can't use async/await here, since function can be either sync or async | ||
return asyncClient(configUrl, headers).then(body => { | ||
return config = body; | ||
}); | ||
} | ||
function syncClient(url, headers) { | ||
try { | ||
let resp = request('GET', configUrl, { | ||
headers: { | ||
'X-Config-Key': key | ||
} | ||
}); | ||
let resp = syncRequest('GET', url, { headers }); | ||
return JSON.parse(resp.getBody('utf-8')); | ||
} catch (err) { | ||
configError(err); | ||
} | ||
} | ||
return config = JSON.parse(resp.getBody('utf-8')); | ||
async function asyncClient(url, headers) { | ||
try { | ||
let resp = await asyncRequest.get(url, { headers }); | ||
return resp.body; | ||
} catch (err) { | ||
debug('Failed to load or parse config', err); | ||
throw new Error(`Failed to load or parse config: ${err.message}`); | ||
configError(err); | ||
} | ||
} | ||
function configError(err) { | ||
debug('Failed to load or parse config', err); | ||
throw new Error(`Failed to load or parse config: ${err.message}`); | ||
} | ||
function get() { | ||
function get(path, defaultVal) { | ||
if (path) { | ||
return _get(config, path, defaultVal); | ||
} | ||
return config; | ||
@@ -38,2 +77,2 @@ } | ||
get, | ||
}; | ||
}; |
{ | ||
"name": "mothership-client", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Node.js client for loading config data from Mothership", | ||
@@ -26,6 +26,8 @@ "main": "index.js", | ||
"debug": "^4.1.0", | ||
"got": "^9.6.0", | ||
"lodash": "^4.17.11", | ||
"sync-request": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"ava": "^1.4.1", | ||
"body-parser": "^1.18.3", | ||
@@ -32,0 +34,0 @@ "express": "^4.16.4", |
# mothership-client | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/spicket/mothership-client-nodejs.svg)](https://greenkeeper.io/) | ||
The official Node.js client for [Mothership](https://mothership.cloud). | ||
@@ -14,2 +16,5 @@ | ||
## Usage | ||
> If you're upgrading from v1.x, see the [upgrading](#upgrading) section at the bottom of this doc. | ||
Since most configuration values are needed during intial bootstrap of an app, this | ||
@@ -22,5 +27,3 @@ module should probably be one of the first things your code requires. | ||
const mothership = require('mothership-client'); | ||
const config = mothership.init({ | ||
key: '<config-key>' | ||
}); | ||
const config = mothership.init('<config-key>'); | ||
``` | ||
@@ -36,2 +39,21 @@ | ||
Or, if you want a specific portion of the config object, you can use `lodash` dotted-notation syntax, and even specify a default if the key isn't found: | ||
``` | ||
const subConfig = mothership.get('some.sub.key', 'sensible default'); | ||
``` | ||
For more info, see [our documentation](https://docs.mothership.cloud). | ||
## Upgrading | ||
### To v2.x | ||
The release of v2.x enables both synchronous and asynchronus modes of operation. v1.x only supported synchronous mode, which was incompatible with some platforms. | ||
Async mode is now the default, returning a promise from the `init()` method. | ||
If you want to use sync mode, pass a second argument during initialization: | ||
``` | ||
const config = mothership.init('<config-key>', { sync: true }); | ||
``` |
5348
60
56
4
+ Addedgot@^9.6.0
+ Addedlodash@^4.17.11
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addeddecompress-response@3.3.0(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedduplexer3@0.1.5(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedjson-buffer@3.0.0(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlowercase-keys@1.0.12.0.0(transitive)
+ Addedmimic-response@1.0.1(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedresponselike@1.0.2(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedurl-parse-lax@3.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)