Skip to content

WeChat Mini Program SDK

The HealthGuard Mini Program SDK (@health-guard/sdk-miniprogram) captures errors, requests, and page lifecycle events in WeChat/Alipay mini-program runtimes.

Installation

bash
npm install @health-guard/sdk-miniprogram

Basic Usage

ts
import { createMiniProgramClient } from '@health-guard/sdk-miniprogram';

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

Domain Allowlist

Mini-program runtimes require an HTTPS endpoint and the Collector domain must be added to the platform's request allowlist.

Configuration

OptionTypeRequiredDefaultDescription
appKeystringYes-Unique app identifier
endpointstringYes-Collector endpoint URL
wxMiniProgramWxLikeYes-The global wx / my runtime object
platformstringNowechat-miniprogramPlatform identifier, e.g. alipay-miniprogram
releasestringNo-App version
environmentstringNo-Environment: development / test / production
userIdstringNo-Business user identifier
autoCaptureboolean | objectNofalseAuto-capture errors and requests
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: {
  request: true   // Intercept wx.request
}

Manual API

captureException(error, errorType?, context?)

Manually report an error:

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

You can also specify the error type and context:

js
client.captureException('custom error', 'native', { page: 'pages/order/detail' });

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'
});

addBreadcrumb(breadcrumb)

Add a breadcrumb:

js
client.addBreadcrumb({
  type: 'click',
  message: 'User tapped submit button',
  data: { formId: 'checkout' }
});

wrapPage(route, definition)

Automatically add breadcrumbs for page lifecycle events:

js
Page(client.wrapPage('pages/index/index', {
  data: { ... },
  onLoad() { ... },
  onShow() { ... }
}));

wrapApp(definition)

Automatically add breadcrumbs for App lifecycle events:

js
App(client.wrapApp({
  onLaunch() { ... },
  onShow() { ... }
}));

flush()

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

js
await client.flush();

Request Annotations

When using wx.request, you can attach context via the healthGuard field to avoid sending sensitive data:

js
wx.request({
  url: '/api/order',
  healthGuard: {
    page: 'pages/order/detail',
    scene: 'checkout',
    context: { orderId: '123' },
    requestData: { sku: 'abc' }   // Only sent if explicitly provided
  }
});

Privacy & Sanitization

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

Released under the MIT License.