New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mothership-client

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mothership-client - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

71

lib/client.js

@@ -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 });
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc