@pluve/logger-sdk 0.0.17 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -50,7 +50,6 @@ sdk.setStage('testing');
50
50
  ## 配置项(节选)
51
51
 
52
52
  - enableGzip:是否启用 gzip 压缩(默认 true)
53
- - gzipBatchMinSize:开启批量压缩的最小 items 数量(默认 2)
54
53
  - enableBatch:是否启用批量上报(默认需显式开启)
55
54
  - batchSize:批量阈值(默认 10)
56
55
  - batchInterval:批量定时器间隔(默认 15000ms)
@@ -63,7 +62,7 @@ sdk.setStage('testing');
63
62
 
64
63
  说明:
65
64
 
66
- - enableGzip 仅在启用批量模式时生效;当 items 数量 ≥ gzipBatchMinSize 时自动压缩,否则走未压缩编码
65
+ - enableGzip 仅在启用批量模式时生效;
67
66
  - enableStorage 默认在开启批量时启用;即使未开启批量,若 enableStorage 未显式设为 false,也会启用
68
67
  - autoCapture 默认全部开启;可按需关闭各子项,或关闭总开关
69
68
 
@@ -1,36 +1,11 @@
1
1
  // src/core/httpClient.ts
2
- import { isWeChatMiniProgram } from "../utils/environment";
3
2
  import { logDebug } from "../utils/tools";
4
3
  var HttpClient = class {
5
4
  /**
6
- * 发送 POST 请求,需要兼容浏览器和微信小程序
5
+ * 发送 POST 请求,需要兼容浏览器
7
6
  */
8
7
  static async post(url, data, token) {
9
8
  logDebug(true, "post", url, data, token);
10
- if (isWeChatMiniProgram()) {
11
- return new Promise((resolve, reject) => {
12
- wx.request({
13
- url,
14
- method: "POST",
15
- data: JSON.stringify(data),
16
- header: {
17
- "Content-Type": "application/json",
18
- token
19
- },
20
- success: (res) => resolve({
21
- type: "wechat",
22
- response: res
23
- }),
24
- fail: (err) => (
25
- // eslint-disable-next-line prefer-promise-reject-errors
26
- reject({
27
- type: "wechat",
28
- error: err
29
- })
30
- )
31
- });
32
- });
33
- }
34
9
  if (typeof fetch === "undefined") {
35
10
  return new Promise((resolve, reject) => {
36
11
  const xhr = new XMLHttpRequest();
@@ -5,7 +5,6 @@ import { registerResourceErrorCapture } from "../capture/resourceError";
5
5
  import { getRegisterApi } from "../config";
6
6
  import { TransportAdapter } from "../transport/transportAdapter";
7
7
  import { collectEnvironmentTags, getCurrentUrl, isBrowser } from "../utils/environment";
8
- import { registerWechatErrorCapture, registerWechatUnhandledCapture } from "../capture/wechatError";
9
8
  import { getSessionId } from "../utils/session";
10
9
  import { flattenStack, hashToProb, logDebug, now, normalizeMessage } from "../utils/tools";
11
10
  import { uuid } from "../utils/uuid";
@@ -53,8 +52,6 @@ var LoggerSDK = class {
53
52
  /** 是否启用 gzip 压缩,默认 true */
54
53
  enableGzip: options.enableGzip !== false,
55
54
  // 默认启用,但仅在批量模式开启情况下有效
56
- gzipBatchMinSize: options.gzipBatchMinSize || 2,
57
- // 默认 2 条数据开启 gzip 压缩
58
55
  /** 最大像素图 URL 长度,默认 1900 */
59
56
  maxPixelUrlLen: options.maxPixelUrlLen || 8192,
60
57
  // 批量上报配置(默认关闭,需显式开启)
@@ -98,8 +95,7 @@ var LoggerSDK = class {
98
95
  autoCapture: options.autoCapture || {
99
96
  js: true,
100
97
  promise: true,
101
- resource: true,
102
- wechat: true
98
+ resource: true
103
99
  }
104
100
  };
105
101
  if (this.opts.enableBatch) {
@@ -122,7 +118,7 @@ var LoggerSDK = class {
122
118
  this.sessionId = getSessionId();
123
119
  this.envTags = collectEnvironmentTags();
124
120
  this.initSDK(this.opts, (data) => {
125
- var _a, _b, _c, _d, _e, _f, _g, _h;
121
+ var _a, _b, _c, _d, _e, _f;
126
122
  this.collectSwitch = data.collectSwitch;
127
123
  this.collectLogLevel = data.collectLogLevel;
128
124
  this.opts.sampleRate = data.samplingRate;
@@ -142,10 +138,6 @@ var LoggerSDK = class {
142
138
  if (enableAuto && ac.resource !== false) {
143
139
  this.offResource = registerResourceErrorCapture(!!((_f = this.opts) == null ? void 0 : _f.debug), this.trackInner.bind(this));
144
140
  }
145
- if (enableAuto && ac.wechat !== false) {
146
- this.offWxError = registerWechatErrorCapture(!!((_g = this.opts) == null ? void 0 : _g.debug), this.trackInner.bind(this));
147
- this.offWxUnhandled = registerWechatUnhandledCapture(!!((_h = this.opts) == null ? void 0 : _h.debug), this.trackInner.bind(this));
148
- }
149
141
  });
150
142
  }
151
143
  /**
@@ -233,7 +225,7 @@ var LoggerSDK = class {
233
225
  /** 可选的结构化额外信息 */
234
226
  // tags: this.envTags,
235
227
  };
236
- this.doTrack(logEvent);
228
+ await this.doTrack(logEvent);
237
229
  }
238
230
  trackInner(error) {
239
231
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -415,14 +407,6 @@ var LoggerSDK = class {
415
407
  this.offResource();
416
408
  this.offResource = void 0;
417
409
  }
418
- if (this.offWxError) {
419
- this.offWxError();
420
- this.offWxError = void 0;
421
- }
422
- if (this.offWxUnhandled) {
423
- this.offWxUnhandled();
424
- this.offWxUnhandled = void 0;
425
- }
426
410
  this.initialized = false;
427
411
  this.sessionId = void 0;
428
412
  LoggerSDK.instance = void 0;
@@ -596,10 +580,7 @@ var LoggerSDK = class {
596
580
  const { type, response: res, error } = response;
597
581
  let isSuccess = false;
598
582
  let data;
599
- if (type === "wechat" && (res == null ? void 0 : res.statusCode) === 200) {
600
- isSuccess = true;
601
- data = res.data.data;
602
- } else if (type === "fetch") {
583
+ if (type === "fetch") {
603
584
  const responseData = await ((_b = res == null ? void 0 : res.json) == null ? void 0 : _b.call(res));
604
585
  logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Register fetch response", responseData);
605
586
  if ((responseData == null ? void 0 : responseData.code) === 0) {
@@ -1,5 +1,5 @@
1
1
  // src/core/queueManager.ts
2
- import { isBrowser, isWeChatMiniProgram } from "../utils/environment";
2
+ import { isBrowser } from "../utils/environment";
3
3
  import { logDebug, safeStringify } from "../utils/tools";
4
4
  var QueueManager = class {
5
5
  constructor(options) {
@@ -90,10 +90,7 @@ var QueueManager = class {
90
90
  */
91
91
  async loadFromStorage() {
92
92
  try {
93
- let stored = null;
94
- if (isWeChatMiniProgram()) {
95
- stored = wx.getStorageSync(this.storageKey);
96
- } else if (isBrowser()) {
93
+ if (isBrowser()) {
97
94
  if (!this.localforage) {
98
95
  await this.loadLocalForage();
99
96
  }
@@ -122,13 +119,6 @@ var QueueManager = class {
122
119
  }
123
120
  return;
124
121
  }
125
- if (stored) {
126
- const parsed = JSON.parse(stored);
127
- if (Array.isArray(parsed)) {
128
- this.queue = parsed.slice(0, this.opts.maxSize);
129
- logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from storage`);
130
- }
131
- }
132
122
  } catch (error) {
133
123
  logDebug(!!this.opts.debug, "Failed to load queue from storage", error);
134
124
  }
@@ -139,9 +129,7 @@ var QueueManager = class {
139
129
  saveToStorage() {
140
130
  try {
141
131
  const data = safeStringify(this.queue);
142
- if (isWeChatMiniProgram()) {
143
- wx.setStorageSync(this.storageKey, data);
144
- } else if (isBrowser()) {
132
+ if (isBrowser()) {
145
133
  const saveToLocalStorage = () => {
146
134
  if (typeof localStorage !== "undefined") {
147
135
  localStorage.setItem(this.storageKey, data);
@@ -182,9 +170,7 @@ var QueueManager = class {
182
170
  */
183
171
  removeFromStorage() {
184
172
  try {
185
- if (isWeChatMiniProgram()) {
186
- wx.removeStorageSync(this.storageKey);
187
- } else if (isBrowser()) {
173
+ if (isBrowser()) {
188
174
  const removeFromLocalStorage = () => {
189
175
  if (typeof localStorage !== "undefined") {
190
176
  localStorage.removeItem(this.storageKey);
@@ -14,23 +14,24 @@ var BeaconTransport = class {
14
14
  return isBrowser() && typeof navigator !== "undefined" && navigator.sendBeacon && typeof navigator.sendBeacon === "function";
15
15
  }
16
16
  async send(token, payload) {
17
- var _a, _b, _c, _d, _e;
17
+ var _a, _b, _c, _d;
18
18
  let body = typeof payload === "string" ? payload : safeStringify(payload);
19
19
  const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
20
20
  let contentType = "application/json";
21
- if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
21
+ if ((_b = this.opts) == null ? void 0 : _b.enableGzip) {
22
22
  const compressedItems = await gzipCompress(safeStringify(payload.items));
23
23
  body = safeStringify({
24
24
  ...payload,
25
25
  items: compressedItems
26
26
  });
27
27
  contentType = "application/json; charset=utf-8";
28
+ } else {
28
29
  }
29
30
  const blob = new Blob([body], { type: contentType });
30
31
  const success = navigator.sendBeacon(endpoint, blob);
31
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
32
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "sendBeacon result", success);
32
33
  if (!success) {
33
- logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "sendBeacon failed (queue full or other error)");
34
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon failed (queue full or other error)");
34
35
  }
35
36
  return Promise.resolve();
36
37
  }
@@ -14,27 +14,27 @@ var PixelImageTransport = class {
14
14
  return isBrowser() && typeof Image !== "undefined";
15
15
  }
16
16
  async send(token, payload) {
17
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
18
18
  const body = safeStringify(payload.items);
19
19
  const endpoint = `${getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
20
20
  const param = "items";
21
21
  const maxLen = ((_b = this.opts) == null ? void 0 : _b.maxPixelUrlLen) || 8192;
22
22
  let compressedBody;
23
- if (((_c = this.opts) == null ? void 0 : _c.enableGzip) && payload.items.length >= ((_d = this.opts) == null ? void 0 : _d.gzipBatchMinSize)) {
23
+ if ((_c = this.opts) == null ? void 0 : _c.enableGzip) {
24
24
  const t = now();
25
- logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress body: ", body);
25
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "PixelImage request gzip compress body: ", body);
26
26
  compressedBody = await gzipCompress(body);
27
- logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), "PixelImage request gzip compress cost: ", now() - t);
28
- logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
29
- logDebug(!!((_h = this.opts) == null ? void 0 : _h.debug), "PixelImage request gzip compress body: ", compressedBody);
27
+ logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress cost: ", now() - t);
28
+ logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
29
+ logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), "PixelImage request gzip compress body: ", compressedBody);
30
30
  } else {
31
31
  compressedBody = await convert2Base64(body);
32
32
  }
33
33
  const cacheBuster = `_=${Date.now()}`;
34
- const qs = `appId=${((_i = this.opts) == null ? void 0 : _i.appId) || ""}&appStage=${((_j = this.opts) == null ? void 0 : _j.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_k = this.opts) == null ? void 0 : _k.enableGzip) ? 1 : 0}&${cacheBuster}`;
34
+ const qs = `appId=${((_h = this.opts) == null ? void 0 : _h.appId) || ""}&appStage=${((_i = this.opts) == null ? void 0 : _i.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_j = this.opts) == null ? void 0 : _j.enableGzip) ? 1 : 0}&${cacheBuster}`;
35
35
  const url = endpoint.includes("?") ? `${endpoint}&${qs}` : `${endpoint}?${qs}`;
36
36
  if (url.length > maxLen) {
37
- logDebug(!!((_l = this.opts) == null ? void 0 : _l.debug), `URL too long (${url.length} > ${maxLen})`);
37
+ logDebug(!!((_k = this.opts) == null ? void 0 : _k.debug), `URL too long (${url.length} > ${maxLen})`);
38
38
  }
39
39
  return new Promise((resolve, reject) => {
40
40
  const img = new Image();
@@ -1,5 +1,4 @@
1
1
  // src/transport/transportAdapter.ts
2
- import { isWeChatMiniProgram } from "../utils/environment";
3
2
  var TransportAdapter = class {
4
3
  constructor(opts) {
5
4
  this.opts = opts;
@@ -11,10 +10,6 @@ var TransportAdapter = class {
11
10
  return TransportAdapter.instance;
12
11
  }
13
12
  async getTransporter() {
14
- if (isWeChatMiniProgram()) {
15
- const mod = await import("./wechatTransport");
16
- return new mod.WechatTransport(this.opts);
17
- }
18
13
  const beaconMod = await import("./beaconTransport");
19
14
  const pixelMod = await import("./pixelImageTransport");
20
15
  const beacon = new beaconMod.BeaconTransport(this.opts);
@@ -6,25 +6,7 @@ function isBrowser() {
6
6
  return false;
7
7
  }
8
8
  }
9
- function isWeChatMiniProgram() {
10
- try {
11
- return typeof wx !== "undefined" && typeof wx.getSystemInfo === "function";
12
- } catch (e) {
13
- return false;
14
- }
15
- }
16
9
  function getCurrentUrl() {
17
- if (isWeChatMiniProgram()) {
18
- try {
19
- const pages = getCurrentPages();
20
- if (pages && pages.length > 0) {
21
- const currentPage = pages[pages.length - 1];
22
- return currentPage.route || "";
23
- }
24
- } catch (e) {
25
- return "";
26
- }
27
- }
28
10
  if (isBrowser()) {
29
11
  return window.location.href;
30
12
  }
@@ -34,24 +16,7 @@ function getEnvironmentInfo() {
34
16
  const envInfo = {
35
17
  platform: "unknown"
36
18
  };
37
- if (isWeChatMiniProgram()) {
38
- envInfo.platform = "wechat";
39
- try {
40
- const systemInfo = wx.getSystemInfoSync();
41
- envInfo.systemInfo = {
42
- brand: systemInfo.brand,
43
- model: systemInfo.model,
44
- system: systemInfo.system,
45
- platform: systemInfo.platform,
46
- version: systemInfo.version,
47
- SDKVersion: systemInfo.SDKVersion
48
- };
49
- envInfo.screenWidth = systemInfo.screenWidth;
50
- envInfo.screenHeight = systemInfo.screenHeight;
51
- envInfo.language = systemInfo.language;
52
- } catch (e) {
53
- }
54
- } else if (isBrowser()) {
19
+ if (isBrowser()) {
55
20
  envInfo.platform = "browser";
56
21
  envInfo.userAgent = navigator.userAgent;
57
22
  envInfo.screenWidth = window.screen.width;
@@ -132,15 +97,6 @@ function collectEnvironmentTags() {
132
97
  tags.screenWidth = envInfo.screenWidth;
133
98
  tags.screenHeight = envInfo.screenHeight;
134
99
  tags.language = envInfo.language;
135
- } else if (envInfo.platform === "wechat" && envInfo.systemInfo) {
136
- tags.brand = envInfo.systemInfo.brand;
137
- tags.model = envInfo.systemInfo.model;
138
- tags.system = envInfo.systemInfo.system;
139
- tags.wechatVersion = envInfo.systemInfo.version;
140
- tags.SDKVersion = envInfo.systemInfo.SDKVersion;
141
- tags.screenWidth = envInfo.screenWidth;
142
- tags.screenHeight = envInfo.screenHeight;
143
- tags.language = envInfo.language;
144
100
  }
145
101
  return tags;
146
102
  }
@@ -149,6 +105,5 @@ export {
149
105
  getCurrentUrl,
150
106
  getEnvironmentInfo,
151
107
  isBrowser,
152
- isWeChatMiniProgram,
153
108
  parseBrowserInfo
154
109
  };
@@ -1,11 +1,11 @@
1
1
  export type HttpClientResponse = {
2
- type: 'xhr' | 'fetch' | 'wechat';
2
+ type: 'xhr' | 'fetch';
3
3
  response?: any;
4
4
  error?: any;
5
5
  };
6
6
  export declare class HttpClient {
7
7
  /**
8
- * 发送 POST 请求,需要兼容浏览器和微信小程序
8
+ * 发送 POST 请求,需要兼容浏览器
9
9
  */
10
10
  static post(url: string, data: any, token: string): Promise<HttpClientResponse>;
11
11
  }
@@ -29,8 +29,6 @@ export declare class LoggerSDK {
29
29
  private offJs?;
30
30
  private offPromise?;
31
31
  private offResource?;
32
- private offWxError?;
33
- private offWxUnhandled?;
34
32
  private constructor();
35
33
  static getInstance(): LoggerSDK;
36
34
  /**
@@ -2,7 +2,7 @@
2
2
  export type Env = 'develop' | 'testing' | 'product';
3
3
  export type Stage = 'develop' | 'testing' | 'sit' | 'yace' | 'product';
4
4
  /** 平台类型 */
5
- export type PlatformType = 'browser' | 'wechat' | 'unknown';
5
+ export type PlatformType = 'browser' | 'unknown';
6
6
  /** 环境信息 */
7
7
  export interface EnvironmentInfo {
8
8
  platform: PlatformType;
@@ -17,8 +17,6 @@ export interface SDKOptions {
17
17
  batchSize?: number;
18
18
  /** 是否启用 gzip 压缩,默认 true (仅在批量模式下有效) */
19
19
  enableGzip?: boolean;
20
- /** 批量模式下启用 gzip 压缩的最小 items 数量,默认 2 */
21
- gzipBatchMinSize?: number;
22
20
  /** 最大像素图 URL 长度,默认 1900 */
23
21
  maxPixelUrlLen?: number;
24
22
  /** 批量上报时间间隔(毫秒),默认 5000 */
@@ -55,7 +53,6 @@ export interface SDKOptions {
55
53
  js?: boolean;
56
54
  promise?: boolean;
57
55
  resource?: boolean;
58
- wechat?: boolean;
59
56
  };
60
57
  enableAutoCapture?: boolean;
61
58
  }
@@ -1,6 +1,5 @@
1
1
  import { EnvironmentInfo } from '../types/env';
2
2
  export declare function isBrowser(): boolean;
3
- export declare function isWeChatMiniProgram(): boolean;
4
3
  export declare function getCurrentUrl(): string;
5
4
  /**
6
5
  * 获取环境信息
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluve/logger-sdk",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "logger sdk",
5
5
  "keywords": [
6
6
  "logger"
@@ -12,14 +12,6 @@
12
12
  "module": "dist/esm/index.js",
13
13
  "browser": "dist/esm/index.js",
14
14
  "types": "dist/types/index.d.ts",
15
- "exports": {
16
- ".": {
17
- "types": "./dist/types/index.d.ts",
18
- "import": "./dist/esm/index.js",
19
- "require": "./dist/cjs/index.js",
20
- "default": "./dist/esm/index.js"
21
- }
22
- },
23
15
  "typesVersions": {
24
16
  "*": {
25
17
  "index": ["dist/types/index.d.ts"],
@@ -1,70 +0,0 @@
1
- // src/capture/wechatError.ts
2
- import { parseStack } from "../stack/stacktrace";
3
- import { isWeChatMiniProgram } from "../utils/environment";
4
- import { logDebug } from "../utils/tools";
5
- function registerWechatErrorCapture(debug, callback) {
6
- if (!isWeChatMiniProgram())
7
- return void 0;
8
- try {
9
- const wxAny = globalThis.wx;
10
- if (wxAny && typeof wxAny.onError === "function") {
11
- const handler = async (error) => {
12
- logDebug(debug, "registerWechatErrorCapture onError", error);
13
- const msg = String((error == null ? void 0 : error.message) ?? error);
14
- const err = error instanceof Error ? error : new Error(msg);
15
- const stack = await parseStack(err);
16
- callback({
17
- type: "js",
18
- message: err.message,
19
- stack,
20
- throwable: err.stack || ""
21
- });
22
- };
23
- wxAny.onError(handler);
24
- return () => {
25
- try {
26
- wxAny.offError && wxAny.offError(handler);
27
- } catch {
28
- }
29
- };
30
- }
31
- } catch (e) {
32
- logDebug(debug, "registerWechatErrorCapture attach failed", e);
33
- }
34
- return void 0;
35
- }
36
- function registerWechatUnhandledCapture(debug, callback) {
37
- if (!isWeChatMiniProgram())
38
- return void 0;
39
- try {
40
- const wxAny = globalThis.wx;
41
- if (wxAny && typeof wxAny.onUnhandledRejection === "function") {
42
- const handler = async (res) => {
43
- const reason = res == null ? void 0 : res.reason;
44
- logDebug(debug, "registerWechatUnhandledCapture onUnhandledRejection", reason);
45
- const err = reason instanceof Error ? reason : new Error(String(reason));
46
- const stack = await parseStack(err);
47
- callback({
48
- type: "promise",
49
- message: err.message,
50
- stack,
51
- throwable: err.stack || ""
52
- });
53
- };
54
- wxAny.onUnhandledRejection(handler);
55
- return () => {
56
- try {
57
- wxAny.offUnhandledRejection && wxAny.offUnhandledRejection(handler);
58
- } catch {
59
- }
60
- };
61
- }
62
- } catch (e) {
63
- logDebug(debug, "registerWechatUnhandledCapture attach failed", e);
64
- }
65
- return void 0;
66
- }
67
- export {
68
- registerWechatErrorCapture,
69
- registerWechatUnhandledCapture
70
- };
@@ -1,79 +0,0 @@
1
- // src/transport/wechatTransport.ts
2
- import { gzipCompress } from "../compress/compression";
3
- import { getReportApi } from "../config";
4
- import { isWeChatMiniProgram } from "../utils/environment";
5
- import { logDebug, now, safeStringify } from "../utils/tools";
6
- var WechatTransport = class {
7
- constructor(opts) {
8
- /** 传输器名称 */
9
- this.name = "wechat";
10
- this.opts = opts;
11
- }
12
- // eslint-disable-next-line class-methods-use-this
13
- isSupported() {
14
- return isWeChatMiniProgram();
15
- }
16
- async send(token, payload) {
17
- var _a, _b, _c, _d, _e;
18
- let body = typeof payload === "string" ? payload : safeStringify(payload);
19
- const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
20
- const timeout = 1e4;
21
- let contentType = "application/json";
22
- if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
23
- const t = now();
24
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "WeChat request enable gzip compress: ", t);
25
- const compressedItems = await gzipCompress(safeStringify(payload.items));
26
- body = safeStringify({
27
- ...payload,
28
- items: compressedItems
29
- });
30
- logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "WeChat request gzip compress cost: ", now() - t);
31
- contentType = "application/json; charset=utf-8";
32
- }
33
- return new Promise((resolve, reject) => {
34
- var _a2;
35
- let timeoutId = null;
36
- let settled = false;
37
- timeoutId = setTimeout(() => {
38
- if (!settled) {
39
- settled = true;
40
- reject(new Error(`WeChat request timeout after ${timeout}ms`));
41
- }
42
- }, timeout);
43
- wx.request({
44
- url: endpoint,
45
- method: "POST",
46
- data: body,
47
- header: {
48
- "Content-Type": contentType,
49
- token: ((_a2 = this.opts) == null ? void 0 : _a2.token) || ""
50
- },
51
- success(res) {
52
- if (timeoutId)
53
- clearTimeout(timeoutId);
54
- if (!settled) {
55
- settled = true;
56
- if (res.statusCode >= 200 && res.statusCode < 300) {
57
- resolve();
58
- } else {
59
- reject(new Error(`HTTP ${res.statusCode}`));
60
- }
61
- }
62
- },
63
- fail(err) {
64
- var _a3;
65
- if (timeoutId)
66
- clearTimeout(timeoutId);
67
- if (!settled) {
68
- settled = true;
69
- logDebug(!!((_a3 = this.opts) == null ? void 0 : _a3.debug), "WeChat request failed", err);
70
- reject(new Error(`WeChat request failed: ${err.errMsg || "unknown error"}`));
71
- }
72
- }
73
- });
74
- });
75
- }
76
- };
77
- export {
78
- WechatTransport
79
- };