Skip to content

H5 SDK

HealthGuard Web SDK 用于在浏览器(H5)环境中采集错误、请求和性能数据。

安装

bash
npm install @healthguard/sdk-web

基础用法

js
import { createHealthGuardClient } from '@healthguard/sdk-web';

const client = createHealthGuardClient({
  appKey: 'your-app-key',
  endpoint: 'https://your-collector.com/api/events/batch',
  autoCapture: true
});

配置选项

选项类型必填默认值说明
appKeystring-应用唯一标识
endpointstring-Collector 上报地址
releasestring-应用版本号
environmentstring-环境:development / test / production
userIdstring-业务用户标识
autoCaptureboolean | objectfalse是否自动捕获错误和请求
flushIntervalMsnumber5000批量上报间隔(毫秒)
maxBatchSizenumber10单次上报最大事件数

autoCapture 选项

autoCapturetrue 时,开启全部自动采集:

js
autoCapture: {
  errors: true,           // window.onerror
  unhandledRejections: true,  // unhandledrejection
  fetch: true,            // 拦截 fetch
  xhr: true               // 拦截 XMLHttpRequest
}

手动上报 API

captureException(error)

手动上报一个错误:

js
try {
  riskyOperation();
} catch (err) {
  client.captureException(err);
}

captureHttp(input)

手动上报一次 HTTP 请求:

js
client.captureHttp({
  method: 'POST',
  url: '/api/users',
  status: 500,
  duration: 240,
  success: false,
  errorMessage: 'Internal Server Error'
});

addBreadcrumb(breadcrumb)

添加一条面包屑:

js
client.addBreadcrumb({
  type: 'navigation',
  message: 'User navigated to /checkout',
  data: { from: '/cart' }
});

flush()

立即刷新队列,上报所有未发送事件:

js
await client.flush();

隐私与脱敏

SDK 默认对以下字段进行脱敏处理:

  • URL Query 中的 authorization, password, token, secret, cookie
  • 不上报 request body 和 response body
  • 使用业务方传入的 userId 或匿名 ID,不采集真实身份信息

Released under the MIT License.