hm-tracking-sdk 0.1.5 → 0.1.8

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
@@ -26,6 +26,8 @@
26
26
  const options: SDKInitOptions = {
27
27
  // 可选:不传则使用内置默认地址
28
28
  baseURL: "https://your-api.example.com",
29
+ // 可选:回调校验用,SDK方会提供
30
+ appId: "app-xxxxxxx",
29
31
 
30
32
  // 可选:本地存储 Key 前缀,避免项目间冲突
31
33
  storageKeyPrefix: "your_app_prefix",
@@ -55,25 +57,50 @@
55
57
  export interface SDKInitOptions {
56
58
  /** 自定义接口地址(可选)。不传则使用默认正式环境 */
57
59
  baseURL?: string;
60
+ appId?: string; // 可选:回调校验用,SDK方提供
61
+ notifyUrl?: string; // 可选:SDK 初始化鉴权成功后的服务端回调地址
58
62
  /** 本地存储 Key 前缀,避免冲突 */
59
63
  storageKeyPrefix?: string;
60
64
  /** 自定义用户信息(可选) */
61
65
  customUser?: TelegramUserInfo;
66
+ /** 可选:TON Connect manifest 文件地址,规则请看5.1.0;未传则使用默认测试清单 */
67
+ tonConnectManifestUrl?: string;
62
68
  }
63
69
  ```
64
70
 
65
71
  - baseURL(可选)
66
72
  - 说明:后端服务地址;不传则使用 SDK 内置默认地址。
67
73
  - 类型:`string`
74
+ - appId (可选)
75
+ - 说明:服务器之间用来鉴权,SDK方提供
76
+ - 类型:`string`
77
+ - notifyUrl (可选)
78
+ - 说明:SDK方服务器回调参数
79
+ - 类型:`string`
68
80
  - storageKeyPrefix(可选)
69
81
  - 说明:本地缓存命名空间前缀;用于隔离不同项目的存储键。
70
82
  - 类型:`string`
71
83
  - customUser(可选)
72
84
  - 说明:自定义用户数据,仅在非 Telegram 环境可用;若不传,SDK 会自动使用“匿名浏览器用户”(基于 Cookie 生成并持久化随机 userId),从而可以直接完成鉴权与后续请求。
73
85
  - 类型:`TelegramUserInfo`
86
+ - tonConnectManifestUrl(可选)
87
+ - 说明:TON Connect manifest 文件地址。若不传,SDK 使用默认测试清单:`https://miniapp.spinfi.me/tonconnect-manifest.json`。当小程序部署后需要改为对应链接,具体见`5.1.0`说明
88
+ - 类型:`string`
74
89
  - 返回值:`Promise<void>`。
90
+
91
+ ### 2.1 初始化成功通知调用说明(服务端回调)
92
+
93
+ - 触发时机:当 SDK 调用 `init()` 完成鉴权后,服务端会读取鉴权请求体中的 `notifyUrl`,并向该地址发起一次通知,上报获取到的用户信息。
94
+ - 通知参数:可能是 JSON 对象或一个加密后的字符串。
95
+ - JSON 示例:
96
+
97
+ ```json
98
+ {"id":"7506466780","first_name":"first","last_name":"name","nickname":"nickname","username":"nickname1992","language_code":"zh","allows_write_to_pm":1,"auth_date":"2025-04-08T02:08:53.000Z","hash":"a9d2d7c1b066bebb73c170006dc474061d7601183b388b037936aa7cb6620acf","chat_instance":"-2254244616056098321","is_premium_user":0,"photo_url":"https://t.me/i/userpic/320/uLzA4KyVzKwd0vi6Uvupe6U2mdnVEQOIbUZbFTQff6B3UhjRpQObL9I8Nq81G0ln.svg"}
99
+ ```
100
+
101
+ - 若为加密字符串:请与服务端约定解密方式及签名校验流程。
75
102
 
76
- ### 2.1 环境
103
+ ### 2.2 环境
77
104
 
78
105
  - Telegram 环境
79
106
  - SDK 自动读取当前Telegram用户的信息。
@@ -84,7 +111,7 @@
84
111
  - 便于无登录状态下完成鉴权,拿到 token 后再进行广告或调试。
85
112
  - 自定义用户:也可传入 `customUser` 替代匿名用户。
86
113
 
87
- ### 2.2 环境判断
114
+ ### 2.3 环境判断
88
115
 
89
116
  - 接口: isTelegramEnv()
90
117
 
@@ -181,6 +208,46 @@
181
208
 
182
209
  ### 5.1 TON钱包相关API
183
210
 
211
+ #### 5.1.0 配置 tonconnect-manifest
212
+
213
+ - 作用
214
+ - TON Connect 要求每个 dApp(小程序) 提供 `tonconnect-manifest.json`。钱包在连接前会读取该文件以展示 dApp 名称、图标,并校验来源。
215
+ - 托管与访问要求
216
+ - 将该 JSON 文件托管在公网可访问的 HTTPS 地址(建议:`https://your-domain.com/tonconnect-manifest.json`,放在站点根路径)。
217
+ - 文件及其中引用的资源(如图标)须可在任意来源公开访问,避免 CORS 限制。
218
+ - 必须能通过 GET 直接获取,无需鉴权或特殊请求头。
219
+ - `url` 字段请避免末尾斜杠,例如使用 `https://mydapp.com` 而非 `https://mydapp.com/`。
220
+ - 最小示例
221
+
222
+ ```json
223
+ {
224
+ "url": "https://your-domain.com",
225
+ "name": "Your DApp Name",
226
+ "iconUrl": "https://your-domain.com/icons/tonconnect-icon.png"
227
+ }
228
+ ```
229
+
230
+ - 字段要求
231
+ - `url`(必填):应用基础 URL,点击钱包内图标时用于打开应用;避免末尾斜杠。
232
+ - `name`(必填):应用名称。
233
+ - `iconUrl`(必填):应用图标 URL,仅支持 PNG 或 ICO,不支持 SVG;建议 180x180 PNG。
234
+ - `termsOfUseUrl`(可选):条款页 URL。
235
+ - `privacyPolicyUrl`(可选):隐私政策 URL。
236
+ - SDK 行为与配置
237
+ - 默认使用测试清单:`https://miniapp.spinfi.me/tonconnect-manifest.json`。
238
+ - 生产环境建议通过初始化参数传入:
239
+
240
+ ```ts
241
+ const sdk = new HmTrackingSDK({
242
+ tonConnectManifestUrl: "https://your-domain.com/tonconnect-manifest.json",
243
+ // 其他可选项...
244
+ });
245
+ ```
246
+
247
+ - 自检
248
+ - 在浏览器访问你的 manifest URL,确认返回 200 且 JSON 内容正确。
249
+ - 打开连接钱包弹窗,应显示你在 manifest 中配置的名称与图标。
250
+
184
251
  #### 5.1.1 钱包是否链接
185
252
 
186
253
  - 接口: isWalletConnected()
@@ -352,3 +419,7 @@
352
419
  - **msg**: 描述信息
353
420
  - **timestamp/datetime**: 服务端时间
354
421
  - **data**: 业务扩展字段(本例为空对象)
422
+
423
+ ---
424
+
425
+
@@ -2855,10 +2855,11 @@ class HttpClient {
2855
2855
  const v = this.options.getToken();
2856
2856
  if (v) {
2857
2857
  m.headers = m.headers || {};
2858
- const g = `Bearer ${v}`;
2859
- m.headers.Authorization = g;
2858
+ const w = `Bearer ${v}`;
2859
+ m.headers.Authorization = w;
2860
2860
  }
2861
- return m;
2861
+ const g = this.options.getAppId ? this.options.getAppId() : null;
2862
+ return g && (m.headers = m.headers || {}, m.headers["hm-app-id"] = String(g)), m;
2862
2863
  }), this.instance.interceptors.response.use(
2863
2864
  (m) => m,
2864
2865
  (m) => (m.response && m.response.status === 401 && this.options.onAuthError && this.options.onAuthError(), Promise.reject(m))
@@ -35924,14 +35925,16 @@ class TonConnectUI {
35924
35925
  }
35925
35926
  }
35926
35927
  const DEFAULT_MANIFEST_URL = "https://miniapp.spinfi.me/tonconnect-manifest.json";
35927
- let tonConnectUIInstance = null;
35928
+ let tonConnectUIInstance = null, currentManifestUrl = null;
35928
35929
  class DefaultTonWalletProvider {
35929
- constructor(f) {
35930
- if (this.bindWalletCallback = f, !tonConnectUIInstance)
35930
+ constructor(f, d) {
35931
+ this.bindWalletCallback = d;
35932
+ const m = f || DEFAULT_MANIFEST_URL;
35933
+ if (!tonConnectUIInstance || currentManifestUrl !== m)
35931
35934
  try {
35932
35935
  tonConnectUIInstance = new TonConnectUI({
35933
- manifestUrl: DEFAULT_MANIFEST_URL
35934
- });
35936
+ manifestUrl: m
35937
+ }), currentManifestUrl = m;
35935
35938
  } catch {
35936
35939
  throw new Error("创建TonConnectUI实例失败");
35937
35940
  }
@@ -36026,8 +36029,12 @@ class HmTrackingSDK {
36026
36029
  this.http = new HttpClient({
36027
36030
  getToken: () => this.token,
36028
36031
  onAuthError: () => this.clearAuth(),
36029
- baseURL: m
36030
- }), this.api = new APIService(this.http), this.hmAdManager = new HmAdManager({ autoInjectScript: !0 }), this.adMonitor = new AdMonitor((v) => this.api.adMonitor(v)), this.tonWallet = new DefaultTonWalletProvider(this.bindWallet.bind(this));
36032
+ baseURL: m,
36033
+ getAppId: () => this.options && this.options.appId || null
36034
+ }), this.api = new APIService(this.http), this.hmAdManager = new HmAdManager({ autoInjectScript: !0 }), this.adMonitor = new AdMonitor((v) => this.api.adMonitor(v)), this.tonWallet = new DefaultTonWalletProvider(
36035
+ d.tonConnectManifestUrl,
36036
+ this.bindWallet.bind(this)
36037
+ );
36031
36038
  }
36032
36039
  async init() {
36033
36040
  const f = isTelegramEnv();
@@ -36050,15 +36057,19 @@ class HmTrackingSDK {
36050
36057
  * 调用 /user/auth 并持久化 token
36051
36058
  */
36052
36059
  async auth() {
36053
- var f;
36060
+ var f, d;
36054
36061
  try {
36055
- const d = await this.api.auth(this.user);
36056
- if (d.code !== 1) throw new Error(d.msg || "鉴权失败");
36057
- const m = (f = d.data) == null ? void 0 : f.token;
36058
- if (!m) throw new Error("鉴权未返回 token");
36059
- this.token = m, this.storage.set("token", m);
36060
- } catch (d) {
36061
- throw d;
36062
+ const m = {
36063
+ ...this.user
36064
+ }, v = (f = this.options) == null ? void 0 : f.notifyUrl;
36065
+ v && (m.notifyUrl = v);
36066
+ const g = await this.api.auth(m);
36067
+ if (g.code !== 1) throw new Error(g.msg || "鉴权失败");
36068
+ const w = (d = g.data) == null ? void 0 : d.token;
36069
+ if (!w) throw new Error("鉴权未返回 token");
36070
+ this.token = w, this.storage.set("token", w);
36071
+ } catch (m) {
36072
+ throw m;
36062
36073
  }
36063
36074
  }
36064
36075
  /**