npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-node-client

v1.4.3

Published

Node.js Client for Apollo

Downloads

506

Readme

apollo-node-client

Node.js Client for Apollo

install

$ npm install apollo-node-client --save

Examples

examples

Usage

实例化 ConfigService

const { ConfigService } = require('apollo-node-client');

const service = new ConfigService({
  configServerUrl: 'http://localhost:8080/',
  appId: 'SampleApp',
  clusterName: 'default',
  secret: 'cf86d564d10a46d4a5989dfdeed3a3a2'
});

获取默认 namespace 的配置(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'));    // default

获取 properties 格式 namespace 的配置

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'));    // default

获取 json 格式 namespace 的配置

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'));    // default

获取 xml/yml/yaml/txt 格式 namespace 的配置

const config = await service.getConfig('config.txt');
config.getAllConfig();                                          // txt config
console.log(config.getProperty('', 'default'));                 // txt config
console.log(config.getProperty());                              // txt config

指定灰度发布的服务 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'));    // default

监听配置变化事件

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 配置服务的地址
      • appId <string> 应用的 appId
      • [clusterName] <string> 集群名
      • [secret] <string> 服务端密钥 access key
    • Returns: ConfigService
  • configService.getAppConfig( [ ip ] )

    • [ip] <string> 应用部署的机器ip

    • Returns: Promise<PropertiesConfig> 默认的 namespaceapplication

  • configService.getConfig( namespaceName, [ ip ] )

    • namespaceName <string> Namespace的名字,以后缀名判断是什么类型格式的 Config。如果没有后缀名,默认为 properties,目前支持 .json.properties.xml.yml.yaml.txt

    • [ip] <string> 应用部署的机器ip

    • Returns: Promise<PropertiesConfig | JSONConfig | PlainConfig>


Class: PropertiesConfig

  • propertiesConfig.getAllConfig()

    • Returns: Map<string, string>
  • propertiesConfig.getProperty( key, [ defaultValue ] )

    • key <string> 要获取的配置的 key

    • [defaultValue] <string> 默认值,当传入的 key 不存在时,会返回 defaultValue

    • Returns: undefined | string

  • propertiesConfig.addChangeListener( handle )

    • handle ( changeEvent: ConfigChangeEvent<string> ) => void 监听配置变化事件的回调函数

    • Returns: void


Class: JSONConfig

  • jsonConfig.getAllConfig()

    • Returns: JSONValueType
  • jsonConfig.getProperty( key, [ defaultValue ] )

    • key <string> 要获取的配置的 key

    • [defaultValue] <string> 默认值,当传入的 key 不存在时,会返回 defaultValue

    • Returns: undefined | JSONValueType

  • jsonConfig.addChangeListener( handle )

    • handle ( changeEvent: ConfigChangeEvent<JSONValueType> ) => void 监听配置变化事件的回调函数

    • Returns: void


Class: PlainConfig

  • plainConfig.getAllConfig()

    • Returns: string
  • plainConfig.getProperty( key, [ defaultValue ] )

    • key <string> 兼容其他类型的 Config,不做校验,传入任意 key 都会返回整个配置文本内容

    • [defaultValue] <string> 默认值,当配置不存在时,会返回 defaultValue

    • Returns: 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.getOldValues()

    • 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!

License

MIT