Skip to content

uni-app Multi-platform SDK

The HealthGuard uni-app SDK (@health-guard/sdk-uniapp) covers H5, WeChat, Alipay, Douyin mini-programs, and native App runtimes with a single integration. It automatically detects the runtime and adapts the capture logic.

Installation

bash
npm install @health-guard/sdk-uniapp

Basic Usage

ts
import { createUniAppClient } from '@health-guard/sdk-uniapp';

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

Platform Auto-detection

The SDK sets the platform field automatically based on the runtime:

Runtimeplatform value
H5uniapp-h5
WeChat Mini Programuniapp-wechat
Alipay Mini Programuniapp-alipay
Douyin / Toutiao Mini Programuniapp-douyin
Appuniapp-app
Unknownuniapp

Configuration

OptionTypeRequiredDefaultDescription
appKeystringYes-Unique app identifier
endpointstringYes-Collector endpoint URL
releasestringNo-App version
environmentstringNo-Environment: development / test / production
userIdstringNo-Business user identifier
autoCaptureboolean | objectNofalseAuto-capture errors, requests, and unhandled rejections
flushIntervalMsnumberNo5000Batch flush interval in milliseconds
maxBatchSizenumberNo10Max events per batch
transportFailureRetryDelayMsnumberNo30000Backoff delay before retrying a failed transport (ms)

autoCapture Options

When autoCapture is true, all auto-capture features are enabled:

js
autoCapture: {
  errors: true,               // JS runtime errors
  unhandledRejections: true,  // Unhandled promise rejections
  fetch: true,                // H5 fetch (H5 only)
  xhr: true,                  // H5 XMLHttpRequest (H5 only)
  request: true               // uni.request (mini-program / App only)
}

Manual API

captureException(error)

Manually report an error:

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

captureHttp(input)

Manually report an HTTP request:

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

capturePerformance(input)

Manually report a performance metric:

js
client.capturePerformance({
  name: 'custom-metric',
  value: 120,
  rating: 'good'
});

addBreadcrumb(breadcrumb)

Add a breadcrumb:

js
client.addBreadcrumb({
  type: 'navigation',
  message: 'User navigated to /pages/order/detail',
  data: { from: '/pages/index/index' }
});

flush()

Immediately flush the queue and send all pending events (including events requeued after a transport failure):

js
await client.flush();

Privacy & Sanitization

  • URL query parameters such as authorization, password, token, secret, and cookie are filtered by default.
  • Request and response bodies are not collected.
  • Uses the business-provided userId or an anonymous ID; no real identity data is collected.

Released under the MIT License.