# @creditkarma/consul-client 中文文档

规范地址：https://npmcn.com/package/%40creditkarma/consul-client/

版本：1\.0\.0
npm 元数据更新时间：2022-06-23T19:17:04.356Z
中文译文更新时间：2026-11-14T08:20:39.499Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40creditkarma/consul-client
原文对照：https://npmcn.com/package/%40creditkarma/consul-client/?lang=original
项目主页：https://github.com/creditkarma/consul-client#readme
源码仓库：https://github.com/creditkarma/consul-client

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：77.5 kB，50 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2017-12-06T00:02:23.033Z
- 最新版本发布：2022-06-23T19:17:04.216Z
- 近 30 天下载量：2106（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：43（近 12 个月 0 个）

## 中文简介

用 TypeScript 写的 Hashircorp 领事客户端

## 安装

```sh
npm install @creditkarma/consul-client
```

也可以使用：`pnpm add @creditkarma/consul-client`、`yarn add @creditkarma/consul-client`、`bun add @creditkarma/consul-client`

## README

# 领事客户端

一个用TypeScript写的Hashicorp领事的客户端.

此库目前有客户端支持 [KV API领事](https://www.consul.io/api/kv.html) 编辑 [Catalog API 领事](https://www.consul.io/api/catalog.html)。 。 。 .

## 安装

```sh
$ npm install --save @creditkarma/consul-client
```

## K/V 存储

K/V商店提供了简单的JS API,用于获取领事内外的价值. 此API主要通过克夫斯托尔级被曝光.

```typescript
import { KvStore, IKey, Observer } from '@creditkarma/consul-client'

/**
 * Instantiate KvStore with location of Consul, default is localhost:8500.
 *
 * The argument is actually an array of locations in order to support fail over.
 */
const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ])
```

### 键

KvStore 根据 `IKey` 对象。 这些物品有1个财产和1个可选财产。 所需财产为: `path`。路径是向上看的关键名称。 可选财产是: `dc`。dc是读取的数据中心。 根据领事文件,这将默认为被询问代理的数据中心,由主机地址指定.

```typescript
// Set value for key
kvStore.set({ path: 'key', dc: 'dc1' }, 'test').then((success: boolean) => {
  if (success) {
    // write was successful
  }
})

// Get current value of key
kvStore.get({ path: 'key', dc: 'dc1' }).then((val: string) => {
  // val === 'test'
})

// Watch a key for value changes
kvStore.watch({ path: 'key', dc: 'dc1' }).onValue((val: string) => {
  // runs anytime the value changes, initially fires with current value
})

// Stop watching a value
kvStore.unwatch({ path: 'key', dc: 'dc1' })

// Delete key from consul
kvStore.delete({ path: 'key', dc: 'dc1' }).then((success: boolean) => {
  if (success) {
    // delete was successful
  }
})
```

### 观察员

一个 `Observer` 是从呼叫中返回的对象 `watch`。 。 。 .

它有五种公开的方法:

```typescript
const observer: Observer<string> = kvStore.watch({ path: 'key', dc: 'dc1' })

observer.onValue((val: string) => {
    // runs anytime the value changes, initially fires with current value
})

observer.onError((err: Error) => {
    // runs anytime there is an error updating the value.
    // depending on the error the library may continue watching the key.
})

observer.current() // Returns the current value

observer.previous() // Returns the previous value

observer.destroy() // Nulls out all internal state

// Get the current value, returns null if no value
const currentVal: string | null = observer.current()

// Get the previous value, returns null in no previous value
const previousVal: string | null = observer.previous()
```

### 请求选项

我们用 [请求](https://github.com/request/request) 作为我们的HTTP客户端。 因此,您可以通过选项请求自定义您的环境 HTTP 请求 。 无论是在即时处理新 KvStore 时,还是在提出请求时,都可以做到这一点.

```typescript
const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ], { headers: { ... } })

kvStore.get({ path: 'key' }, { headers: { ... } })
```

给 KvStore 构造器的选项在每次请求中都会被使用. 为某种方法提供的选项只用于该请求。 传递到请求方法的选项在满足请求之前被与实例选项深度合并.

可用 [选项](https://github.com/request/request#requestoptions-callback)

## 目录

Catalog API 允许您发现您领事注册的其他资产 。 这对服务发现有用.

```typescript
import { Catalog } from '@creditkarma/consul-client'

/**
 * Instantiate Catalog with location of Consul, default is localhost:8500
 *
 * Like KvStore the Catalog supports fail over by providing a list of addresses.
 */
const catalog: Catalog = new Catalog([ 'http://localhost:8500' ])
```

### API 概览

```typescript
// List all nodes registered with Consul
catalog.listNodes().then((res: Array<INodeDescription>) => {
    // Do something
})

// List all services registered with Consul
catalog.listServices().then((res: IServiceMap) => {
    // Do something
})

// List all nodes registered with a given service
catalog.listNodesForService('service-name').then((res: IServiceMap) => {
    // Do something
})

// Get the registered address for a given service
catalog.resolveAddress('service-name').then((res: string) => {
    // Do something
})

// Watch for runtime changes to a service address
catalog.watchAddress('service-name').onValue((res: string) => {
    // Do something
})
```

## 捐款

欲了解更多关于贡献新功能和错误修正的信息,请参见我们 [贡献准则](https://github.com/creditkarma/CONTRIBUTING.md)。 。 。 . 外部捐助者必须签署捐助者许可证协定

## 许可证

这个项目的许可证根据 [Apache 许可证版本 2.0](https://npmcn.com/package/%40creditkarma/consul-client/LICENSE)

## 常见问题

### @creditkarma/consul-client 是什么？

用 TypeScript 写的 Hashircorp 领事客户端

### 如何安装 @creditkarma/consul-client？

在项目目录运行 npm install @creditkarma/consul-client，或使用 pnpm add / yarn add / bun add。

### @creditkarma/consul-client 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @creditkarma/consul-client 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @creditkarma/consul-client 可以商用吗？

@creditkarma/consul-client 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @creditkarma/consul-client 的下载量有多少？

根据 npm 官方下载统计，@creditkarma/consul-client 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,106 次，日均约 70 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @creditkarma/consul-client 还在维护吗？

最新版本 1\.0\.0 发布于 2022/06/24（4 年前）；至今共发布 43 个版本，其中过去 12 个月发布了 0 个；首次发布于 2017/12/06。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

