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

@fangjinlyx/apollo-node

v0.0.13

Published

```typescript import { ApolloClient } from './client';

Downloads

2

Readme

Usage

import { ApolloClient } from './client';

interface Config {
  key1: string;
  key2: number;
  key3: boolean,
  // 此处的mongodb和redis分别对应两个namespace下的数据,部门名称会被自动去除
  mongodb: {
    url: string,
  },
  redis: {
    port: number,
    host: string,
    password: string,
  }
}

const client = new ApolloClient<Config>({
  appId: 'lock-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
  ],
  host: 'http://192.168.8.8:8080',
  secret: 'b27a01fea3bd4f23a83e3261be146036',
  configConstructor: {
    key1: String,
    key2: Number,
    key3: Boolean,
    mongodb: {
      url: String,
    },
    redis: {
      port: Number,
      host: String,
      password: String,
    },
  },
  // 以同步的方式获取配置
  sync: true,
});

console.log(client.config);

// 监听key值变动
client.changeEvent.on('key1', (notify) => {
  console.log(notify);
});

// 监听某个namespace下的key值变动
client.changeEvent.on('mongodb.url', (notify) => {
  console.log(notify);
});

// 监听namespace,其下任何值发生变化都会触发事件
client.namespaceChangeEvent.on('redis', (notify) => {
  console.log(notify);
});

Change Log

v0.0.13

  • 文档修正

v0.0.12

Bug Fixes

  • 签名时appId被写死

以下版本有致命bug,不推荐使用

v0.0.11

Bug Fixes

  • 由于给spawnSync设置env属性,所以子进程无法继承父进程的process.env,这会导致$PATH也无法被继承,一些常用命令如node无法找到会报错

v0.0.9

Bug Fixes

  • 修复文档说明错误

v0.0.8

New Feature

  • 新增监听namespa变动
interface Config {
  key1: string;
  key2: number;
}
const client = new ApolloClient<Config>({
  appId: 'mind-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
    'tx.redis',
  ],
});

client.namespaceChangeEvent.on('redis', (notify) => {
  console.log(notify);
});

v0.0.7

New Feature

  • 支持秘钥
const client = new ApolloClient<Config>({
  secret: '18b079a14e9c43ca83374f614da793b4',
  // ...
});
  • 支持同步获取配置数据,防止await/async污染
const client = new ApolloClient<Config>({
  sync: true,
  // ...
});
console.log(client.config);
  • 支持实时推送

  • 配置类型泛形

interface Config {
  key1: string;
  key2: number;
}
const client = new ApolloClient<Config>({
  appId: 'mind-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
    'tx.redis',
  ],
});
// client.config的类型为Config
配置类型格式化

由于Apollo获取到的参数值只能是string类型,使用时还需要二次解析,新版本内置了类型转换:

interface Config {
  key1: string;
  key2: number;
  key3: boolean,
  mongoDb: {
    url: string,
  },
}
const client = new ApolloClient<Config>({
  appId: 'mind-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
    'tx.redis',
  ],
  configConstructor: {
    key1: String,
    key2: Number,
    key3: Boolean,
    mongodb: {
      url: String,
    },
  },
});
console.log(client.config);
// {
//   key1: '字符串',
//   key2: 12345,
//   key3: false,
//   mongodb: {
//     url: 'mongodb://root:[email protected]:27017/auth?authSource=admin'
//   },
//   redis: {
//     port: 6379,
//     host: '127.0.0.1',
//     password: 'xxxxxxx'
//   }
// }
异步回调获取方式
const client = new ApolloClient<Config>({
  appId: 'mind-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
    'tx.redis',
  ],
  secret: '18b079a14e9c43ca83374f614da793b4',
  configConstructor: {
    key1: String,
    key2: Number,
  },
}, (config: Config) => {
  console.log(config);
});

当设置sync不为true的时候必须传入callback回调

配置更新回调
const client = new ApolloClient<Config>({
  appId: 'mind-server',
  cluster: 'nuc',
  namespace: [
    'nuc.mongodb',
    'tx.redis',
  ],
  configConstructor: {
    key1: String,
    key2: Number,
    key3: Boolean,
    mongodb: {
      url: String,
    },
    redis: {
      port: Number,
      host: String,
      password: String,
    },
  },
  sync: true,
});

client.changeEvent.on('key1', (notify) => {
  console.log(notify);
});

client.changeEvent.on('redis.port', (notify) => {
  console.log(notify);
});

client.changeEvent.on('mongodb.url', (notify) => {
  // 可以在此回调中进行重连
  console.log(notify);
});