@pluve/logger-sdk 0.0.18 → 0.0.19

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.
@@ -12,6 +12,7 @@ import { HttpClient } from "./httpClient";
12
12
  import { QueueManager } from "./queueManager";
13
13
  import { RetryManager } from "./retryManager";
14
14
  import { Md5 } from "../utils/innerMD5";
15
+ import { SecurityType } from "../types/securityType";
15
16
  var LoggerSDK = class {
16
17
  constructor() {
17
18
  /** 事件序列编号,用于事件去重 */
@@ -151,11 +152,12 @@ var LoggerSDK = class {
151
152
  }
152
153
  }
153
154
  /** 设置店铺编码 */
154
- setStoreCode(storeCode) {
155
+ setStoreCode(storeCode, securityType) {
155
156
  var _a;
156
- logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode);
157
+ logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode, "securityType", securityType);
157
158
  if (this.opts) {
158
159
  this.opts.storeCode = storeCode;
160
+ this.opts.securityType = securityType;
159
161
  }
160
162
  }
161
163
  /** 设置日志 stage(env) */
@@ -421,11 +423,17 @@ var LoggerSDK = class {
421
423
  */
422
424
  async sendEvent(event) {
423
425
  const sendFn = async () => {
426
+ var _a, _b, _c, _d;
424
427
  const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
425
428
  this.transporter = transporter;
426
429
  await transporter.send(this.generateToken(), {
427
430
  appId: event.appId,
428
431
  appStage: event.stage,
432
+ token: this.generateToken(),
433
+ securityType: ((_a = this.opts) == null ? void 0 : _a.securityType) || SecurityType.BASE,
434
+ gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
435
+ userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
436
+ storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
429
437
  items: [
430
438
  {
431
439
  level: event.level === "FATAL" ? "ERROR" : event.level,
@@ -472,11 +480,17 @@ var LoggerSDK = class {
472
480
  if (chunk.length > 0) {
473
481
  const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
474
482
  const sendFn = async () => {
483
+ var _a2, _b, _c, _d;
475
484
  const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
476
485
  this.transporter = transporter;
477
486
  await transporter.send(this.generateToken(), {
478
487
  appId: chunk[0].appId,
479
488
  appStage: chunk[0].stage,
489
+ token: this.generateToken(),
490
+ securityType: ((_a2 = this.opts) == null ? void 0 : _a2.securityType) || SecurityType.BASE,
491
+ gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
492
+ userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
493
+ storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
480
494
  items: chunk.map((event) => ({
481
495
  level: event.level === "FATAL" ? "ERROR" : event.level,
482
496
  traceId: event.traceId,
@@ -1,5 +1,5 @@
1
1
  // src/transport/beaconTransport.ts
2
- import { gzipCompress } from "../compress/compression";
2
+ import { convert2Base64, gzipCompress } from "../compress/compression";
3
3
  import { getReportApi } from "../config";
4
4
  import { isBrowser } from "../utils/environment";
5
5
  import { logDebug, safeStringify } from "../utils/tools";
@@ -14,24 +14,30 @@ 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;
18
- let body = typeof payload === "string" ? payload : safeStringify(payload);
19
- const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
17
+ var _a, _b, _c, _d, _e;
18
+ let sendBody = "";
19
+ const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}`;
20
20
  let contentType = "application/json";
21
21
  if ((_b = this.opts) == null ? void 0 : _b.enableGzip) {
22
22
  const compressedItems = await gzipCompress(safeStringify(payload.items));
23
- body = safeStringify({
23
+ sendBody = safeStringify({
24
24
  ...payload,
25
25
  items: compressedItems
26
26
  });
27
27
  contentType = "application/json; charset=utf-8";
28
28
  } else {
29
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "WeChat request no gzip mode: ");
30
+ const compressedItems = convert2Base64(safeStringify(payload.items));
31
+ sendBody = safeStringify({
32
+ ...payload,
33
+ items: compressedItems
34
+ });
29
35
  }
30
- const blob = new Blob([body], { type: contentType });
36
+ const blob = new Blob([sendBody], { type: contentType });
31
37
  const success = navigator.sendBeacon(endpoint, blob);
32
- logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "sendBeacon result", success);
38
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
33
39
  if (!success) {
34
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon failed (queue full or other error)");
40
+ logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "sendBeacon failed (queue full or other error)");
35
41
  }
36
42
  return Promise.resolve();
37
43
  }
@@ -0,0 +1,11 @@
1
+ // src/types/securityType.ts
2
+ var SecurityType = /* @__PURE__ */ ((SecurityType2) => {
3
+ SecurityType2["ACCOUNT_CENTER"] = "2B";
4
+ SecurityType2["EHP"] = "EHP";
5
+ SecurityType2["CUSTOMER"] = "CUSTOMER";
6
+ SecurityType2["BASE"] = "BASE";
7
+ return SecurityType2;
8
+ })(SecurityType || {});
9
+ export {
10
+ SecurityType
11
+ };
@@ -1,6 +1,7 @@
1
1
  import { Env } from '../types/env';
2
2
  import { SDKOptions } from '../types/sdkOptions';
3
3
  import { TrackOptions } from '../types/trackOptions';
4
+ import { SecurityType } from '../types/securityType';
4
5
  export declare class LoggerSDK {
5
6
  private static instance;
6
7
  private opts?;
@@ -40,7 +41,7 @@ export declare class LoggerSDK {
40
41
  */
41
42
  identify(userId: string): void;
42
43
  /** 设置店铺编码 */
43
- setStoreCode(storeCode: string): void;
44
+ setStoreCode(storeCode: string, securityType: SecurityType): void;
44
45
  /** 设置日志 stage(env) */
45
46
  setStage(stage: Env): void;
46
47
  private flattenEnvTags;
@@ -1,5 +1,6 @@
1
1
  import { Stage } from './env';
2
2
  import { LogEventLevel } from './logEventLevel';
3
+ import { SecurityType } from './securityType';
3
4
  /** 标准化日志上报格式 */
4
5
  export interface LogEvent {
5
6
  /** 日志 ID */
@@ -45,6 +46,11 @@ export type BaseErrorInfo = {
45
46
  export type ReportData = {
46
47
  appId: string;
47
48
  appStage: Stage;
49
+ token: string;
50
+ securityType: SecurityType;
51
+ gzip: number;
52
+ userId: string;
53
+ storeCode: string;
48
54
  items: ReportItem[];
49
55
  };
50
56
  export type ReportItem = {
@@ -1,5 +1,6 @@
1
1
  import { Env, Stage } from './env';
2
2
  import { LogEventLevel } from './logEventLevel';
3
+ import { SecurityType } from './securityType';
3
4
  /** SDK 配置选项 */
4
5
  export interface SDKOptions {
5
6
  /** 上报端点 URL */
@@ -39,6 +40,8 @@ export interface SDKOptions {
39
40
  userId?: string;
40
41
  /** 可选:店铺编码 */
41
42
  storeCode?: string;
43
+ /** 安全类型 */
44
+ securityType?: SecurityType;
42
45
  /** token */
43
46
  token: string;
44
47
  /** 全局采样率 0.0-1.0,默认 1.0(全量) */
@@ -0,0 +1,6 @@
1
+ export declare const enum SecurityType {
2
+ ACCOUNT_CENTER = "2B",// 2B(账号中心)
3
+ EHP = "EHP",// EHP(医疗)
4
+ CUSTOMER = "CUSTOMER",// 2C(会员,这期不涉及先预留)
5
+ BASE = "BASE"
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluve/logger-sdk",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "logger sdk",
5
5
  "keywords": [
6
6
  "logger"