# @dec112/cap-ts 中文文档

规范地址：https://npmcn.com/package/%40dec112/cap-ts/

版本：0\.3\.3
npm 元数据更新时间：2025-08-11T06:22:37.741Z
中文译文更新时间：2026-11-11T18:52:42.125Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40dec112/cap-ts
原文对照：https://npmcn.com/package/%40dec112/cap-ts/?lang=original

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

- 包体积（解压后）：381 kB，31 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2023-08-03T11:22:51.979Z
- 最新版本发布：2025-08-11T06:22:37.553Z
- 近 30 天下载量：22205（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

共同警示协议(CAP)(ITU X.1303) 类型脚本库

## 安装

```sh
npm install @dec112/cap-ts
```

也可以使用：`pnpm add @dec112/cap-ts`、`yarn add @dec112/cap-ts`、`bun add @dec112/cap-ts`

## README

# TypeScript 中的共同警示协议

该库应允许解析和写入 CAP( 共同提醒协议) 消息 .

## 安装

@dec112/cap-ts在npm注册处公开发布.

```sh
npm install @dec112/cap-ts
```

## 构建

```sh
# install dependencies
npm install

# build sources
npm run build
```

## 示例

### 创建 CAP 提醒

```typescript
import {
  AlertBuilder,
  Category,
  Certainty,
  InfoBuilder,
  MsgType,
  Scope,
  Severity,
  Status,
  Urgency,
} from '@dec112/cap-ts/builder';

const info = new InfoBuilder(
  // event
  'Fire in shopping mall',
  Urgency.immediate,
  Severity.extreme,
  Certainty.observed,
  [Category.fire],
);

const alert = new AlertBuilder(
  // alert ID
  'ca15f4ac-7ee9-4391-a9da-a75f1c3374dd',
  // sender ID
  'http://shopping.com/sensors/fire/386',
  new Date().toISOString(),
  Status.actual,
  MsgType.alert,
  Scope.restricted,
);

// add info to alert
alert.info_list = [info];

const xmlString = alert.build().toXML();
```

### 使用字符串得出相应的 enum 值

```typescript
import {
  AlertBuilder,
  Category,
  Certainty,
  FromString,
  InfoBuilder,
  MsgType,
  Scope,
  Severity,
  Status,
  Urgency,
} from '@dec112/cap-ts/builder';

// ############################################
// Of course these examples do not make much sense
// as they use string literals and could therefore
// simply replaced by the enums itself
// However this is handy if the enum string
// is available in a variable and the corresponding
// internal enum value needs to be retreived from it
// ############################################
const info = new InfoBuilder(
  // event
  'Fire in shopping mall',
  FromString.getUrgency('immediate') ?? Urgency.unknown,
  FromString.getSeverity('extreme') ?? Severity.unknown,
  FromString.getCertainty('observed') ?? Certainty.unknown,
  [FromString.getCategory('fire') ?? Category.other],
);

const alert = new AlertBuilder(
  // alert ID
  'ca15f4ac-7ee9-4391-a9da-a75f1c3374dd',
  // sender ID
  'http://shopping.com/sensors/fire/386',
  new Date().toISOString(),
  FromString.getStatus('actual') ?? Status.actual,
  FromString.getMsgType('alert') ?? MsgType.alert,
  FromString.getScope('restricted') ?? Scope.restricted,
);

// add info to alert
alert.info_list = [info];

const xmlString = alert.build().toXML();
```

### 分析 CAP 提醒

```typescript
import { CAP_1_2 } from "@dec112/cap-ts";

try {
  const alert = await CAP_1_2.Alert.fromXML('<?xml version="1.0" ... >')
} catch {
  // parsing went wrong
}
```

## 谢谢!

非常感谢 [乔纳森·维尔伯](https://github.com/JonathanWilbur) 发起该项目的 [菲尔格雷什](https://github.com/philgresh) 他们为这个项目增加了有价值的测试.

![](https://raw.githubusercontent.com/dec112/cap-ts/master/assets/netidee.jpeg)这个项目从下列来源获得了资金: [网络电话17](https://netidee.at/)。 。 。 .

  

## 许可证

[MIT 许可证](https://mit-license.org/)

## 常见问题

### @dec112/cap-ts 是什么？

共同警示协议\(CAP\)\(ITU X\.1303\) 类型脚本库

### 如何安装 @dec112/cap-ts？

在项目目录运行 npm install @dec112/cap-ts，或使用 pnpm add / yarn add / bun add。

### @dec112/cap-ts 支持 TypeScript 吗？

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

### @dec112/cap-ts 支持 ESM 还是 CommonJS？

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

### @dec112/cap-ts 可以商用吗？

@dec112/cap-ts 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @dec112/cap-ts 的下载量有多少？

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

### @dec112/cap-ts 还在维护吗？

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

