WeChat Mini Program SDK
The HealthGuard Mini Program SDK captures errors, requests, and performance data in WeChat Mini Program environments.
Features
- Zero external dependencies: No third-party npm packages, extremely small footprint
- Auto-injection: Wraps
App()andPage()lifecycles automatically viawrapAppandwrapPage - Privacy-compliant: Sanitizes sensitive data by default
Installation
Option 1: Direct Download (Recommended)
Download healthguard.miniprogram.js from the main project at packages/sdk-miniprogram/dist/ and place it in your mini program's utils/ folder:
project/
├── utils/
│ └── healthguard.miniprogram.js
├── app.js
└── pages/Option 2: npm (if supported)
bash
npm install @healthguard/sdk-miniprogramInitialization
Import and initialize in app.js:
js
import { createMiniProgramClient, wrapApp } from './utils/healthguard.miniprogram.js';
const client = createMiniProgramClient({
appKey: 'your-app-key',
endpoint: 'https://your-collector.com/api/events/batch',
autoCapture: true
});
// Wrap App lifecycle
App(wrapApp({
onLaunch() {
console.log('App Launch');
}
}, client));Wrap Pages
Use wrapPage in page JS files:
js
import { wrapPage } from './utils/healthguard.miniprogram.js';
Page(wrapPage({
data: {
motto: 'Hello World'
},
onLoad() {
// Business logic
}
}, client));Recommended: attach the
clientinstance to the globalgetApp()to avoid recreating it per page.
Configuration
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
appKey | string | Yes | - | Unique app identifier |
endpoint | string | Yes | - | Collector endpoint URL |
release | string | No | - | Mini program version |
environment | string | No | - | Environment label |
autoCapture | boolean | object | No | false | Auto-capture errors and requests |
autoCapture Options
js
autoCapture: {
errors: true, // Listen to onError
rejections: true, // unhandledrejection
requests: true // Intercept wx.request
}Manual API
Same API as the H5 SDK:
js
// Manual error report
client.captureException(new Error('something went wrong'));
// Manual HTTP report
client.captureHttp({
method: 'GET',
url: '/api/user',
status: 200,
duration: 120,
success: true
});
// Add breadcrumb
client.addBreadcrumb({
type: 'navigation',
message: 'navigated to /pages/detail'
});
// Flush immediately
client.flush();wx.request Interception
The SDK automatically wraps wx.request and records:
- Request method, URL, and status code
- Request duration
- Failure reasons (timeout / fail)
Notes:
- Request body and response body are not collected
- Sensitive query parameters in URLs are automatically sanitized
Bundle Size
- About 3KB gzipped, minimal impact on mini program package size
- No external dependencies; no extra npm build config needed in
project.config.json
Privacy & Sanitization
- Filters URL query params:
authorization,password,token,secret - Does not collect real user identity
- Uses
userId(business-provided) or anonymous ID to identify users