anchor-sdk 0.1.41-internal.6 → 0.1.42-beta.0

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.
@@ -1,210 +0,0 @@
1
- import { WebResultBadgeConditionConfigResponse, WebResultBadgeClaimableListResponse, WebResultBadgeAssetsResponse, WebResultBadgeSeriesDetailResponse, WebResultAssetsBadgeResponse, WebResultBadgeClaimableResponse, WebResultBadgeCheckResponse, WebResultBadgeDetailResponse, WebResultBadgeClaimSignatureResponse } from "../generated/Api";
2
- import { BatchMintRequestVO, MintResponse, NFTMintRequestVO, UserOpHashProcessRequest, WebResultCheckResponse, WebResultCheckResultResponse, WebResultGrantBadgeRequestVO, WebResultGrantBadgeResponseVO, WebResultNftSeriesDetailResponse, WebResultNftSeriesResponse, WebResultTransactionHashProcess, WebResultUserOpHashProcess } from "./types";
3
- /**
4
- * Anchor API HTTP 客户端配置
5
- */
6
- export interface AnchorApiHttpClientConfig {
7
- /** API 基础 URL */
8
- baseUrl: string;
9
- /** 认证令牌 */
10
- authToken?: string;
11
- /** 项目 ID */
12
- projectId?: string;
13
- /** 链 ID */
14
- chainId?: string;
15
- /** Token 过期回调函数 */
16
- onTokenExpired?: (error: Error) => void;
17
- }
18
- /**
19
- * Anchor API HTTP 客户端
20
- * 用于与 Anchor 后端服务进行通信
21
- *
22
- * 这个客户端集成了生成的 API 类型和端点,同时保持了
23
- * 自动 header 注入和配置管理功能。
24
- */
25
- export declare class AnchorApiHttpClient {
26
- private baseUrl;
27
- private authToken?;
28
- private projectId?;
29
- private chainId?;
30
- private onTokenExpired?;
31
- private generatedApi;
32
- /**
33
- * 创建 Anchor API 客户端
34
- * @param config 客户端配置
35
- */
36
- constructor(config: AnchorApiHttpClientConfig);
37
- /**
38
- * 设置认证令牌
39
- * @param token 认证令牌
40
- */
41
- setAuthToken(token: string): void;
42
- /**
43
- * 设置 Token 过期回调
44
- * @param callback Token 过期回调函数
45
- */
46
- setTokenExpiredCallback(callback: (error: Error) => void): void;
47
- /**
48
- * 设置项目 ID
49
- * @param projectId 项目 ID
50
- */
51
- setProjectId(projectId: string): void;
52
- /**
53
- * 设置链 ID
54
- * @param chainId 链 ID
55
- */
56
- setChainId(chainId: string): void;
57
- /**
58
- * 创建请求头
59
- * @returns 请求头对象
60
- */
61
- private createHeaders;
62
- /**
63
- * 获取请求参数(包含 headers)
64
- * @returns 请求参数对象
65
- */
66
- private getRequestParams;
67
- /**
68
- * 处理 API 调用的错误和 token 过期
69
- * @param apiCall API 调用函数
70
- * @returns API 调用结果
71
- */
72
- private handleApiCall;
73
- /**
74
- * 发送 HTTP 请求(用于遗留 API)
75
- * @param method HTTP 方法
76
- * @param path 请求路径
77
- * @param data 请求数据
78
- * @returns 响应数据
79
- */
80
- private request;
81
- /**
82
- * 获取徽章条件配置
83
- * @param platform 平台标识符
84
- * @param type 条件类型
85
- * @returns 徽章条件配置响应
86
- */
87
- getBadgeConditionConfig(platform: string, type: string): Promise<WebResultBadgeConditionConfigResponse>;
88
- /**
89
- * 获取用户可领取徽章
90
- * @param series 徽章系列过滤器
91
- * @param limit 每页项目数
92
- * @param nextToken 分页令牌
93
- * @returns 徽章可领取列表响应
94
- */
95
- getUserClaimableBadges(series?: string, limit?: number, nextToken?: string): Promise<WebResultBadgeClaimableListResponse>;
96
- /**
97
- * 获取用户徽章资产
98
- * @param status 徽章状态过滤器
99
- * @param strategy 返回策略
100
- * @param series 徽章系列过滤器
101
- * @param limit 每页项目数
102
- * @param nextToken 分页令牌
103
- * @returns 徽章资产响应
104
- */
105
- getUserBadgeAssets(status?: string, strategy?: string, series?: string, limit?: number, nextToken?: string): Promise<WebResultBadgeAssetsResponse>;
106
- /**
107
- * 获取徽章系列详情
108
- * @param seriesId 系列标识符
109
- * @returns 徽章系列详情响应
110
- */
111
- getBadgeSeriesDetail(seriesId: string): Promise<WebResultBadgeSeriesDetailResponse>;
112
- /**
113
- * 获取用户资产
114
- * @param customerIds 用户 ID 列表
115
- * @param status 资产状态
116
- * @param strategy 返回策略
117
- * @param limit 返回数量限制
118
- * @returns 资产徽章响应
119
- */
120
- getUserAssets(customerIds?: string[], status?: string, strategy?: string, limit?: number): Promise<WebResultAssetsBadgeResponse>;
121
- /**
122
- * 获取徽章详情
123
- * @param badgeId 徽章标识符
124
- * @returns 徽章详情响应
125
- */
126
- getBadgeDetail(badgeId: string): Promise<WebResultBadgeDetailResponse>;
127
- /**
128
- * 检查单个徽章可领取状态
129
- * @param badgeId 徽章标识符
130
- * @returns 徽章可领取响应
131
- */
132
- checkSingleBadgeClaimable(badgeId: string): Promise<WebResultBadgeClaimableResponse>;
133
- /**
134
- * 检查用户可领取徽章
135
- * @param data 徽章检查请求
136
- * @returns 徽章检查响应
137
- */
138
- checkUserClaimableBadges(data: any): Promise<WebResultBadgeCheckResponse>;
139
- /**
140
- * 获取徽章领取签名
141
- * @param customerAddress 客户地址
142
- * @param claimableIds 可领取 ID 列表
143
- * @returns 徽章领取签名响应
144
- */
145
- getBadgeClaimSignatures(customerAddress: string, claimableIds: string[]): Promise<WebResultBadgeClaimSignatureResponse>;
146
- /**
147
- * 授予徽章 (Legacy)
148
- * @param data 授予徽章请求
149
- * @returns 授予徽章响应
150
- */
151
- grantBadge(data: WebResultGrantBadgeRequestVO): Promise<WebResultGrantBadgeResponseVO>;
152
- /**
153
- * 获取徽章资产 (Legacy)
154
- * @param customerId 客户 ID
155
- * @param status 徽章状态
156
- * @param strategy 返回策略
157
- * @returns 徽章资产响应
158
- */
159
- badgeAssets(customerId?: string, status?: string, strategy?: string): Promise<WebResultBadgeAssetsResponse>;
160
- /**
161
- * 获取 NFT 系列 (Legacy)
162
- * @param type NFT 类型
163
- * @returns NFT 系列响应
164
- */
165
- nftSeries(type: string): Promise<WebResultNftSeriesResponse>;
166
- /**
167
- * 根据 ID 获取 NFT 系列 (Legacy)
168
- * @param type NFT 类型
169
- * @param series 系列标识符
170
- * @param customerId 客户 ID
171
- * @returns NFT 系列详情响应
172
- */
173
- getNftSeriesById(type: string, series: string, customerId?: string): Promise<WebResultNftSeriesDetailResponse>;
174
- /**
175
- * 检查徽章 (Legacy)
176
- * @param type NFT 类型
177
- * @returns 检查响应
178
- */
179
- check(type: string): Promise<WebResultCheckResponse>;
180
- /**
181
- * 获取检查结果 (Legacy)
182
- * @param type NFT 类型
183
- * @returns 检查结果响应
184
- */
185
- getCheckResult(type: string): Promise<WebResultCheckResultResponse>;
186
- /**
187
- * 处理用户操作哈希 (Legacy)
188
- * @param data 用户操作哈希处理请求
189
- * @returns 用户操作哈希处理响应
190
- */
191
- processUserOpHash(data: UserOpHashProcessRequest): Promise<WebResultUserOpHashProcess>;
192
- /**
193
- * 处理交易哈希 (Legacy)
194
- * @param txHash 交易哈希
195
- * @returns 交易哈希处理响应
196
- */
197
- processTransactionHash(txHash: string): Promise<WebResultTransactionHashProcess>;
198
- /**
199
- * @deprecated 使用 getBadgeClaimSignatures 替代
200
- */
201
- mint(type: string, data: NFTMintRequestVO): Promise<MintResponse>;
202
- /**
203
- * @deprecated 使用 getBadgeClaimSignatures 替代
204
- */
205
- mintV2(type: string, data: NFTMintRequestVO): Promise<MintResponse>;
206
- /**
207
- * @deprecated 使用 getBadgeClaimSignatures 替代
208
- */
209
- batchMint(data: BatchMintRequestVO): Promise<MintResponse>;
210
- }
@@ -1,411 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnchorApiHttpClient = void 0;
4
- const Api_1 = require("../generated/Api");
5
- /**
6
- * Anchor API HTTP 客户端
7
- * 用于与 Anchor 后端服务进行通信
8
- *
9
- * 这个客户端集成了生成的 API 类型和端点,同时保持了
10
- * 自动 header 注入和配置管理功能。
11
- */
12
- class AnchorApiHttpClient {
13
- /**
14
- * 创建 Anchor API 客户端
15
- * @param config 客户端配置
16
- */
17
- constructor(config) {
18
- this.baseUrl = config.baseUrl.endsWith("/")
19
- ? config.baseUrl
20
- : `${config.baseUrl}/`;
21
- this.authToken = config.authToken;
22
- this.projectId = config.projectId;
23
- this.chainId = config.chainId;
24
- this.onTokenExpired = config.onTokenExpired;
25
- // 初始化生成的 API
26
- this.generatedApi = new Api_1.Api({
27
- baseUrl: this.baseUrl,
28
- });
29
- }
30
- /**
31
- * 设置认证令牌
32
- * @param token 认证令牌
33
- */
34
- setAuthToken(token) {
35
- this.authToken = token;
36
- }
37
- /**
38
- * 设置 Token 过期回调
39
- * @param callback Token 过期回调函数
40
- */
41
- setTokenExpiredCallback(callback) {
42
- this.onTokenExpired = callback;
43
- }
44
- /**
45
- * 设置项目 ID
46
- * @param projectId 项目 ID
47
- */
48
- setProjectId(projectId) {
49
- this.projectId = projectId;
50
- }
51
- /**
52
- * 设置链 ID
53
- * @param chainId 链 ID
54
- */
55
- setChainId(chainId) {
56
- this.chainId = chainId;
57
- }
58
- /**
59
- * 创建请求头
60
- * @returns 请求头对象
61
- */
62
- createHeaders() {
63
- const headers = {
64
- "Content-Type": "application/json",
65
- };
66
- // 添加标准化的请求头
67
- if (this.authToken) {
68
- headers["Authorization"] = `Bearer ${this.authToken}`;
69
- }
70
- if (this.projectId) {
71
- headers["X-Project-Id"] = this.projectId;
72
- }
73
- if (this.chainId) {
74
- headers["X-Chain-Id"] = this.chainId;
75
- }
76
- return headers;
77
- }
78
- /**
79
- * 获取请求参数(包含 headers)
80
- * @returns 请求参数对象
81
- */
82
- getRequestParams() {
83
- return {
84
- headers: this.createHeaders(),
85
- };
86
- }
87
- /**
88
- * 处理 API 调用的错误和 token 过期
89
- * @param apiCall API 调用函数
90
- * @returns API 调用结果
91
- */
92
- async handleApiCall(apiCall) {
93
- try {
94
- return await apiCall();
95
- }
96
- catch (error) {
97
- // 检查 token 过期
98
- if (error instanceof Error &&
99
- (error.message.includes("JWT") ||
100
- error.message.includes("401") ||
101
- error.message.includes("unauthorized"))) {
102
- if (this.onTokenExpired) {
103
- this.onTokenExpired(error);
104
- }
105
- }
106
- throw error;
107
- }
108
- }
109
- /**
110
- * 发送 HTTP 请求(用于遗留 API)
111
- * @param method HTTP 方法
112
- * @param path 请求路径
113
- * @param data 请求数据
114
- * @returns 响应数据
115
- */
116
- async request(method, path, data) {
117
- const url = `${this.baseUrl}${path}`;
118
- const headers = this.createHeaders();
119
- const options = {
120
- method,
121
- headers,
122
- };
123
- if (data && (method === "POST" || method === "PUT")) {
124
- options.body = JSON.stringify(data);
125
- }
126
- // 打印请求信息
127
- console.log("===== API Request Information =====");
128
- console.log(`Request URL: ${url}`);
129
- console.log("Request Method:", method);
130
- console.log("Request Headers:", JSON.stringify(headers, null, 2));
131
- if (options.body) {
132
- console.log("Request Body:", options.body);
133
- }
134
- console.log("=====================");
135
- const response = await fetch(url, options);
136
- if (!response.ok) {
137
- const errorText = await response.text();
138
- const error = new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`);
139
- throw error;
140
- }
141
- const responseData = (await response.json());
142
- console.log("responseData", responseData);
143
- // 检查响应体中的错误码(即使HTTP状态码是200)
144
- if (responseData &&
145
- typeof responseData === "object" &&
146
- "msgKey" in responseData) {
147
- const msgKey = responseData.msgKey;
148
- console.log("msgKey", msgKey);
149
- if (msgKey === "params.jwt.check.invalid") {
150
- const error = new Error(`JWT token is invalid: ${msgKey}`);
151
- // 调用token过期回调
152
- if (this.onTokenExpired) {
153
- this.onTokenExpired(error);
154
- }
155
- throw error;
156
- }
157
- }
158
- return responseData;
159
- }
160
- // ==================== V2 API Methods (使用生成的 API) ====================
161
- /**
162
- * 获取徽章条件配置
163
- * @param platform 平台标识符
164
- * @param type 条件类型
165
- * @returns 徽章条件配置响应
166
- */
167
- async getBadgeConditionConfig(platform, type) {
168
- let path = "v2/badges/condition/config";
169
- const params = new URLSearchParams();
170
- params.append("platform", platform);
171
- params.append("type", type);
172
- path = `${path}?${params.toString()}`;
173
- return this.request("GET", path);
174
- }
175
- /**
176
- * 获取用户可领取徽章
177
- * @param series 徽章系列过滤器
178
- * @param limit 每页项目数
179
- * @param nextToken 分页令牌
180
- * @returns 徽章可领取列表响应
181
- */
182
- async getUserClaimableBadges(series, limit, nextToken) {
183
- let path = "v2/badges/claimable";
184
- const params = new URLSearchParams();
185
- if (series) {
186
- params.append("series", series);
187
- }
188
- if (limit) {
189
- params.append("limit", limit.toString());
190
- }
191
- if (nextToken) {
192
- params.append("nextToken", nextToken);
193
- }
194
- const queryString = params.toString();
195
- if (queryString) {
196
- path = `${path}?${queryString}`;
197
- }
198
- return this.request("GET", path);
199
- }
200
- /**
201
- * 获取用户徽章资产
202
- * @param status 徽章状态过滤器
203
- * @param strategy 返回策略
204
- * @param series 徽章系列过滤器
205
- * @param limit 每页项目数
206
- * @param nextToken 分页令牌
207
- * @returns 徽章资产响应
208
- */
209
- async getUserBadgeAssets(status, strategy, series, limit, nextToken) {
210
- let path = "v2/badges/assets";
211
- const params = new URLSearchParams();
212
- if (status) {
213
- params.append("status", status);
214
- }
215
- if (strategy) {
216
- params.append("strategy", strategy);
217
- }
218
- if (series) {
219
- params.append("series", series);
220
- }
221
- if (limit) {
222
- params.append("limit", limit.toString());
223
- }
224
- if (nextToken) {
225
- params.append("nextToken", nextToken);
226
- }
227
- const queryString = params.toString();
228
- if (queryString) {
229
- path = `${path}?${queryString}`;
230
- }
231
- return this.request("GET", path);
232
- }
233
- /**
234
- * 获取徽章系列详情
235
- * @param seriesId 系列标识符
236
- * @returns 徽章系列详情响应
237
- */
238
- async getBadgeSeriesDetail(seriesId) {
239
- return this.request("GET", `v2/badges/series/${seriesId}`);
240
- }
241
- /**
242
- * 获取用户资产
243
- * @param customerIds 用户 ID 列表
244
- * @param status 资产状态
245
- * @param strategy 返回策略
246
- * @param limit 返回数量限制
247
- * @returns 资产徽章响应
248
- */
249
- async getUserAssets(customerIds, status, strategy, limit) {
250
- let path = "v2/assets";
251
- const params = new URLSearchParams();
252
- if (customerIds && customerIds.length > 0) {
253
- customerIds.forEach((id) => params.append("customerIds", id));
254
- }
255
- if (status) {
256
- params.append("status", status);
257
- }
258
- if (strategy) {
259
- params.append("strategy", strategy);
260
- }
261
- if (limit) {
262
- params.append("limit", limit.toString());
263
- }
264
- const queryString = params.toString();
265
- if (queryString) {
266
- path = `${path}?${queryString}`;
267
- }
268
- return this.request("GET", path);
269
- }
270
- /**
271
- * 获取徽章详情
272
- * @param badgeId 徽章标识符
273
- * @returns 徽章详情响应
274
- */
275
- async getBadgeDetail(badgeId) {
276
- return this.request("GET", `v2/badges/${badgeId}`);
277
- }
278
- /**
279
- * 检查单个徽章可领取状态
280
- * @param badgeId 徽章标识符
281
- * @returns 徽章可领取响应
282
- */
283
- async checkSingleBadgeClaimable(badgeId) {
284
- return this.request("POST", `v2/badges/${badgeId}`);
285
- }
286
- /**
287
- * 检查用户可领取徽章
288
- * @param data 徽章检查请求
289
- * @returns 徽章检查响应
290
- */
291
- async checkUserClaimableBadges(data) {
292
- return this.request("POST", "v2/badges", data);
293
- }
294
- /**
295
- * 获取徽章领取签名
296
- * @param customerAddress 客户地址
297
- * @param claimableIds 可领取 ID 列表
298
- * @returns 徽章领取签名响应
299
- */
300
- async getBadgeClaimSignatures(customerAddress, claimableIds) {
301
- return this.request("POST", "v2/badges/signatures", { customerAddress, claimableIds });
302
- }
303
- // ==================== Legacy V1 API Methods (保持现有实现) ====================
304
- /**
305
- * 授予徽章 (Legacy)
306
- * @param data 授予徽章请求
307
- * @returns 授予徽章响应
308
- */
309
- async grantBadge(data) {
310
- return this.request("POST", "s1/badges", data);
311
- }
312
- /**
313
- * 获取徽章资产 (Legacy)
314
- * @param customerId 客户 ID
315
- * @param status 徽章状态
316
- * @param strategy 返回策略
317
- * @returns 徽章资产响应
318
- */
319
- async badgeAssets(customerId, status, strategy) {
320
- let path = "s1/badges/assets";
321
- const params = new URLSearchParams();
322
- if (customerId)
323
- params.append("customerId", customerId);
324
- if (status)
325
- params.append("status", status);
326
- if (strategy)
327
- params.append("strategy", strategy);
328
- const queryString = params.toString();
329
- if (queryString) {
330
- path = `${path}?${queryString}`;
331
- }
332
- return this.request("GET", path);
333
- }
334
- /**
335
- * 获取 NFT 系列 (Legacy)
336
- * @param type NFT 类型
337
- * @returns NFT 系列响应
338
- */
339
- async nftSeries(type) {
340
- return this.request("GET", `s1/series/${type}`);
341
- }
342
- /**
343
- * 根据 ID 获取 NFT 系列 (Legacy)
344
- * @param type NFT 类型
345
- * @param series 系列标识符
346
- * @param customerId 客户 ID
347
- * @returns NFT 系列详情响应
348
- */
349
- async getNftSeriesById(type, series, customerId) {
350
- let path = `s1/series/${type}/${series}`;
351
- if (customerId) {
352
- path = `${path}?customerId=${customerId}`;
353
- }
354
- return this.request("GET", path);
355
- }
356
- /**
357
- * 检查徽章 (Legacy)
358
- * @param type NFT 类型
359
- * @returns 检查响应
360
- */
361
- async check(type) {
362
- return this.request("POST", `s1/check/${type}`);
363
- }
364
- /**
365
- * 获取检查结果 (Legacy)
366
- * @param type NFT 类型
367
- * @returns 检查结果响应
368
- */
369
- async getCheckResult(type) {
370
- return this.request("GET", `s1/check/${type}/result`);
371
- }
372
- /**
373
- * 处理用户操作哈希 (Legacy)
374
- * @param data 用户操作哈希处理请求
375
- * @returns 用户操作哈希处理响应
376
- */
377
- async processUserOpHash(data) {
378
- return this.request("POST", "s1/userOpHash/process", data);
379
- }
380
- /**
381
- * 处理交易哈希 (Legacy)
382
- * @param txHash 交易哈希
383
- * @returns 交易哈希处理响应
384
- */
385
- async processTransactionHash(txHash) {
386
- return this.request("POST", "s1/transactionHash/process", { txHash });
387
- }
388
- // ==================== Deprecated Methods (将被移除) ====================
389
- /**
390
- * @deprecated 使用 getBadgeClaimSignatures 替代
391
- */
392
- async mint(type, data) {
393
- console.warn("mint method is deprecated, use getBadgeClaimSignatures instead");
394
- return this.request("POST", `v2/mint/${type}`, data);
395
- }
396
- /**
397
- * @deprecated 使用 getBadgeClaimSignatures 替代
398
- */
399
- async mintV2(type, data) {
400
- console.warn("mintV2 method is deprecated, use getBadgeClaimSignatures instead");
401
- return this.request("POST", `v2/mint/${type}`, data);
402
- }
403
- /**
404
- * @deprecated 使用 getBadgeClaimSignatures 替代
405
- */
406
- async batchMint(data) {
407
- console.warn("batchMint method is deprecated, use getBadgeClaimSignatures instead");
408
- return this.request("POST", "v2/mint/batch", data);
409
- }
410
- }
411
- exports.AnchorApiHttpClient = AnchorApiHttpClient;