Spring Cloud Config Client for NodeJS with OAuth for Spring Cloud Services on PCF
npm install @crdant/cloud-config-client
Requires: NodeJS 4+
Feature requests are welcome.
npm install cloud-config-client --save
``js
const client = require("cloud-config-client");
client.load({
application: "invoices"
}).then((config) => {
// Look for a key
const value1 = config.get("this.is.a.key");
// Using a prefix, this is equivalent to .get("this.is.another.key");
const value2 = config.get("this.is", "another.key");
});
`
#### Parameters
* options - Object, mandatory:string
* endpoint , optional, default=http://localhost:8888: Config server URL.boolean
* rejectUnauthorized - , optional, default = true: if false accepts self-signed certificatesstring
* application - , deprecated, use name: Load configuration for this application.string
* name - , mandatory: Load the configuration with this name.string|string[]
* profiles , optional, default="default": Load profiles.string
* label - , optional: Load environment.Object
* auth - , optional: Basic Authentication for access config server (e.g.: { user: "username", pass: "password"}).http://user:pass@localhost:8888
_endpoint_ accepts also basic auth (e.g. ).string
* user - , mandatorystring
* pass - , mandatoryhttp.Agent|https.Agent
* agent - , optional: Agent for the request. (e.g. use a proxy) (Since version 1.2.0)object
* context - , optional: Context for substitution (see context section below) (Since version 1.4.0)function(error: Error, config: Config)
* callback - , optional: node style callback. If missing, the method will return a promise.
`js
client.load(options)
.then((config) => { ... })
.catch((error) => { ... })
// or
client.load(options, (error, config) => { ... })
// or
async function foo () {
const config = await client.load(options)
//...
}
`
Strings could contain context references, if a contexts is provided those references will be replaced by the actual values in the Config object reference.
YAML example:
`yaml`
key01: Hello ${NAME:World}!!!
This is not related to spEl and those references are not an expression language.
Reference structure: ${CONTEXT_KEY:DEFAULT_VALUE?}:
* CONTEXT_KEY: Context key, if the value for the key is missing it will be use the default value or return the original stringDEFAULT_VALUE
* : Optional default value
Environment variables as context:
`js`
Client.load({
endpoint: 'http://server:8888',
name: 'application',
context: process.env })
.then(config => {
// config loaded
}).catch(console.error)
#### Properties
* raw: Spring raw response data.properties
* : computed properties as per Spring specification:
> Property keys in more specifically named files override those in application.properties or application.yml.
#### Methods
* get(...parts): Retrieve a value at a given path or undefined. Multiple parameters can be used to calculate the key.string
* parts - , variable, mandatory:forEach(callback, includeOverridden)
* : Iterates over every key/value in the config.function(key: string, value: string)
* callback - , mandatory: iteration callback.boolean
* includeOverridden - , optional, default=false: if true, include overridden keys.toString(spaces): string
* : Returns a string representation of raw property.number
* spaces - , optional: spaces to use in format.toObject(): object
* : Returns the whole configuration as an object. (Since version 1.3.0)
`js
config.get("this.is.a.key");
config.get("this.is", "a.key");
config.get("this", "is", "a", "key");
config.forEach((key, value) => console.log(key + ":" + value));
`
Example (adapted from https-proxy-agent site):
`js
const HttpsProxyAgent = require('https-proxy-agent')
const client = require('cloud-config-client')
const proxy = process.env.http_proxy || 'http://168.63.76.32:3128'
console.log('using proxy server %j', proxy)
const agent = new HttpsProxyAgent(proxy)
const options = {
application: 'demo',
profiles: ['test', 'timeout'],
agent
}
client.load(options).then((cfg) => {
console.log(cfg.get('test.users', 'multi.uid'))
console.log(cfg.toString(2))
}).catch((error) => console.error(error))
``