@dec112/cap-ts

中文文档
0.3.3发布于 2025/08/11MITTypeScriptCommonJS近 30 天下载 2.2万

Common Alerting Protocol (CAP) (ITU X.1303) Library in TypeScript

原始使用文档
文档目录8 节
  1. Install
  2. Build
  3. Example
  4. Creating a CAP alert
  5. Using strings to derive corresponding enum values
  6. Parsing a CAP alert
  7. Thank You!
  8. License

Common Alerting Protocol (CAP) in TypeScript

This library should allow parsing and writing of CAP (Common Alerting Protocol) messages.

Install

@dec112/cap-ts is publicly available on the npm registry.

npm install @dec112/cap-ts

Build

# install dependencies
npm install

# build sources
npm run build

Example

Creating a CAP alert

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();

Using strings to derive corresponding enum values

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();

Parsing a CAP alert

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
}

Thank You!

A big thank you to JonathanWilbur who initiated this project and to Philgresh who added valuable tests to this project.

This project has received funding from Netidee Call 17.


License

MIT License

版本与发布记录

@dec112/cap-ts 共发布 4 个版本,首次发布于 2023/08/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.3latest2025/08/111.3 年前
0.3.22023/08/313 年前
0.3.12023/08/033 年前
0.3.02023/08/033 年前

关于 @dec112/cap-ts 的常见问题

@dec112/cap-ts 是什么?

Common Alerting Protocol (CAP) (ITU X.1303) Library in TypeScript

如何安装 @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 和提交记录判断。