apollo-node-client
v2.0.3
Published
Node.js Client for Apollo
Maintainers
Readme
apollo-node-client
Node.js Client for Apollo
v2.0+ supports both CommonJS and ESM. 1.x only supports CommonJS.
Install
$ npm install apollo-node-client --saveExamples
Usage
Create a ConfigService
CommonJS:
const { ConfigService } = require('apollo-node-client');ESM:
import { ConfigService } from 'apollo-node-client';Then create an instance:
const service = new ConfigService({
configServerUrl: 'http://localhost:8080/',
appId: 'SampleApp',
clusterName: 'default',
secret: 'cf86d564d10a46d4a5989dfdeed3a3a2'
});Get the default namespace config (application)
const config = await service.getAppConfig();
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // defaultGet properties format namespace config
const config = await service.getConfig('application');
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // defaultGet json format namespace config
const config = await service.getConfig('config.json');
config.getAllConfig(); // { mysql: { user: 'root' } }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // defaultGet xml/yml/yaml/txt format namespace config
const config = await service.getConfig('config.txt');
config.getAllConfig(); // txt config
console.log(config.getProperty('', 'default')); // txt config
console.log(config.getProperty()); // txt configSpecify a canary release server ip
const config = await service.getConfig('application', '192.168.3.4');
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // defaultListen for config change events
config.addChangeListener((changeEvent) => {
for (const key of changeEvent.changedKeys()) {
const change = changeEvent.getChange(key);
if (change) {
console.log(`namespace: ${change.getNamespace()},
changeType: ${change.getChangeType()},
propertyName: ${change.getPropertyName()},
oldValue: ${change.getOldValue()},
newValue: ${change.getNewValue()}`);
}
}
});API
Class: ConfigService
new ConfigService( options )
options<Object>configServerUrl<string> Apollo config server URLappId<string> Application ID[clusterName]<string> Cluster name[secret]<string> Access key secret
- Returns: ConfigService
configService.getAppConfig( [ ip ] )
[ip]<string> Server IP for canary releaseReturns: Promise<PropertiesConfig> Default
namespaceisapplication
configService.getConfig( namespaceName, [ ip ] )
namespaceName<string> Namespace name. The config format is determined by the file extension. Defaults topropertiesif no extension. Supports.json,.properties,.xml,.yml,.yaml,.txt[ip]<string> Server IP for canary releaseReturns: Promise<PropertiesConfig | JSONConfig | PlainConfig>
Class: PropertiesConfig
propertiesConfig.getAllConfig()
- Returns: Map<string, string>
propertiesConfig.getProperty( key, [ defaultValue ] )
key<string> Config key to retrieve[defaultValue]<string> Default value returned when the key does not existReturns: undefined | string
propertiesConfig.addChangeListener( handle )
handle( changeEvent: ConfigChangeEvent<string> ) => void Callback for config change eventsReturns: void
Class: JSONConfig
jsonConfig.getAllConfig()
- Returns: JSONValueType
jsonConfig.getProperty( key, [ defaultValue ] )
key<string> Config key to retrieve[defaultValue]<string> Default value returned when the key does not existReturns: undefined | JSONValueType
jsonConfig.addChangeListener( handle )
handle( changeEvent: ConfigChangeEvent<JSONValueType> ) => void Callback for config change eventsReturns: void
Class: PlainConfig
plainConfig.getAllConfig()
- Returns: string
plainConfig.getProperty( key, [ defaultValue ] )
key<string> Compatible with other config types. Any key returns the entire config text[defaultValue]<string> Default value returned when the config does not existReturns: undefined | string
Class: ConfigChangeEvent
configChangeEvent.getNamespace()
- Returns: string
configChangeEvent.changedKeys()
- Returns: string[]
configChangeEvent.getChange()
- Returns: undefined | ConfigChange<T>
Class: ConfigChange<T>
configChange.getNamespace()
- Returns: string
configChange.getPropertyName()
- Returns: string
configChange.getOldValue()
- Returns: undefined | T
configChange.getNewValue()
- Returns: undefined | T
configChange.getChangeType()
- Returns: PropertyChangeType
Enum: PropertyChangeType
propertyChangeType.ADDED
propertyChangeType.MODIFIED
propertyChangeType.DELETED
Contributing
Contributions are always welcome!
