UNPKG

anchor-sdk

Version:

TypeScript SDK for interacting with Anchor ecosystem - badge minting, payment processing, and ERC1155 token management

450 lines (449 loc) 15.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnchorApiClientV2 = void 0; const Api_1 = require("./generated/Api"); /** * Anchor API V2 客户端 * 基于生成的 API 类型和端点,提供完整的 Anchor API 功能 * * 这个客户端使用生成的 API 类型和端点,同时提供 * 自动 header 注入和配置管理功能。 */ class AnchorApiClientV2 { /** * 创建 Anchor API V2 客户端 * @param config 客户端配置 */ constructor(config) { this.network = config.network; this.authToken = config.authToken; this.projectId = config.projectId; this.chainId = config.network.id.toString(); this.onTokenExpired = config.onTokenExpired; // 初始化生成的 API this.generatedApi = new Api_1.Api({ baseUrl: config.apiBaseUrl.endsWith("/") ? config.apiBaseUrl.slice(0, -1) : config.apiBaseUrl, }); } /** * 设置认证令牌 * @param token 认证令牌 */ setAuthToken(token) { this.authToken = token; } /** * 设置 Token 过期回调 * @param callback Token 过期回调函数 */ setTokenExpiredCallback(callback) { this.onTokenExpired = callback; } /** * 设置项目 ID * @param projectId 项目 ID */ setProjectId(projectId) { this.projectId = projectId; } /** * 设置链 ID * @param chainId 链 ID */ setChainId(chainId) { this.chainId = chainId; } /** * 创建请求头 * @returns 请求头对象 */ createHeaders() { const headers = { "Content-Type": "application/json", }; // 添加标准化的请求头 if (this.authToken) { headers["Authorization"] = `Bearer ${this.authToken}`; headers["jwt_token"] = `${this.authToken}`; } if (this.projectId) { headers["X-Project-Id"] = this.projectId; headers["saas_id"] = this.projectId; } if (this.chainId) { headers["X-Chain-Id"] = this.chainId; headers["chain_id"] = this.chainId; } return headers; } /** * 获取请求参数(包含 headers) * @returns 请求参数对象 */ getRequestParams() { return { headers: this.createHeaders(), }; } /** * 处理 API 调用的错误和 token 过期 * @param apiCall API 调用函数 * @returns API 调用结果 */ async handleApiCall(apiCall) { try { const result = await apiCall(); // 检查响应体中的 token 过期信息 if (this.isTokenExpiredResponse(result)) { console.log("Token expired: JWT token is invalid or expired"); const error = new Error("Token expired: JWT token is invalid or expired"); if (this.onTokenExpired) { this.onTokenExpired(error); } throw error; } return result; } catch (error) { // 检查 HTTP 401 状态码的 token 过期 if (error?.status === 401) { if (this.onTokenExpired) { this.onTokenExpired(error); } } throw error; } } /** * 检查响应是否为 token 过期错误 * @param response API 响应 * @returns 是否为 token 过期错误 */ isTokenExpiredResponse(response) { // 检查响应体中的 token 过期标识 // 优先检查:success 为 false 且 code 为 9101(标准 token 过期错误码) if (response?.success === false && response?.code === "9101") { return true; } // 检查 msgKey 是否为特定的 JWT 错误 if (response?.success === false && response?.msgKey === "params.jwt.check.invalid") { return true; } return false; } // ==================== V2 API Methods ==================== /** * 获取徽章验证器 * @param platform 平台标识符 * @param type 条件类型 * @returns 徽章验证器响应 */ async getBadgeVerifiers(platform, type) { return this.handleApiCall(async () => { const query = { platform, type }; const response = await this.generatedApi.v2.getBadgeVerifiers(query, this.getRequestParams()); return response.data; }); } /** * 获取用户可领取徽章 * @param params 查询参数 * @param params.series 徽章系列过滤器 * @param params.limit 每页项目数 * @param params.nextToken 分页令牌 * @returns 徽章可领取列表响应 */ async getUserClaimableBadges(params) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getUserClaimableBadges(params, this.getRequestParams()); return response.data; }); } /** * 获取用户徽章资产 * @param params 查询参数 * @param params.status 徽章状态过滤器 pending/claimable/claimed * @param params.strategy 返回策略 BADGE_ALL/BADGE_MAX/BADGE_REWARD * @param params.series 徽章系列过滤器 * @param params.limit 每页项目数 * @param params.nextToken 分页令牌 * @returns 徽章资产响应 */ async getUserBadgeAssets(params) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getUserBadgeAssets(params, this.getRequestParams()); return response.data; }); } /** * 获取徽章系列详情 * @param seriesId 系列标识符 * @param params 查询参数 * @param params.status 状态过滤器 * @param params.businessType 业务类型过滤器 * @returns 徽章系列详情响应 */ async getBadgeSeriesDetail(seriesId, params) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getBadgeSeriesDetail(seriesId, params, this.getRequestParams()); return response.data; }); } /** * 获取徽章详情 * @param badgeId 徽章标识符 * @returns 徽章详情响应 */ async getBadgeDetail(badgeId) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getBadgeDetail(badgeId, this.getRequestParams()); return response.data; }); } /** * 检查单个徽章可领取状态 * @param badgeId 徽章标识符 * @param data 徽章检查请求数据 * @returns 徽章可领取响应 */ async checkSingleBadgeClaimable(badgeId, data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.checkSingleBadgeClaimable(badgeId, data, this.getRequestParams()); return response.data; }); } /** * 检查用户可领取徽章 * @param data 徽章检查请求 * @returns 徽章检查响应 */ async checkUserClaimableBadges(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.checkUserClaimableBadges(data, this.getRequestParams()); return response.data; }); } /** * 获取徽章领取签名 * @param data 徽章领取签名请求 * @returns 徽章领取签名响应 */ async getBadgeClaimSignatures(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getBadgeClaimSignatures(data, this.getRequestParams()); return response.data; }); } /** * 获取徽章系列列表 * @param params 查询参数 * @param params.includeBadges 是否包含徽章 * @param params.status 状态过滤器 * @param params.businessType 业务类型过滤器 * @param params.limit 每页数量 * @returns 徽章系列响应 */ async getBadgeSeries(params) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.getBadgeSeries(params, this.getRequestParams()); return response.data; }); } /** * Anchor 铸造 * @param type 铸造类型 * @param data 铸造请求数据 * @returns 铸造响应 */ async mint(type, data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.mint(type, data, this.getRequestParams()); return response.data; }); } /** * Anchor 批量铸造 * @param type 铸造类型 * @param data 批量铸造请求数据 * @returns 批量铸造响应 */ async batchMint(type, data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v2.batchMint(type, data, this.getRequestParams()); return response.data; }); } // ==================== V1 API Methods ==================== /** * 根据 ID 获取 NFT 系列 * @param type NFT 类型 * @param series 系列标识符 * @param customerId 客户 ID * @returns NFT 系列详情响应 */ async getNftSeriesById(type, series, customerId) { return this.handleApiCall(async () => { const query = customerId ? { customerId } : undefined; const response = await this.generatedApi.v1.getNftSeriesById(type, series, query, this.getRequestParams()); return response.data; }); } /** * 获取 NFT 系列 * @param type NFT 类型 * @returns NFT 系列响应 */ async nftSeries(type) { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.nftSeries(type, this.getRequestParams()); return response.data; }); } /** * Anchor 检查 * @param type 检查类型 * @returns 检查响应 */ async check(type) { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.check(type, this.getRequestParams()); return response.data; }); } /** * 获取检查结果 * @param type 检查类型 * @returns 检查结果响应 */ async getCheckResult(type) { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.getCheckResult(type, this.getRequestParams()); return response.data; }); } /** * 获取赛季配置列表 * @returns 赛季配置列表响应 */ async listSeasonConfig() { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.listSeasonConfig(this.getRequestParams()); return response.data; }); } /** * 查询用户可领取或已领取的徽章 * @param customerId 客户 ID * @param status 徽章状态 * @param strategy 返回策略 * @param series 徽章系列 * @param limit 每页条数 * @param nextToken 分页参数 * @returns 徽章响应 */ async badgeAssets(customerId, status, strategy, series, limit, nextToken) { return this.handleApiCall(async () => { const query = { customerId, status, strategy, series, limit, nextToken }; const response = await this.generatedApi.v1.badgeAssets(query, this.getRequestParams()); return response.data; }); } /** * 处理用户操作哈希 * @param data 用户操作哈希处理请求 * @returns 用户操作哈希处理响应 */ async userOpHashProcess(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.userOpHashProcess(data, this.getRequestParams()); return response.data; }); } /** * 处理交易哈希(加速交易处理) * @param txHash 交易哈希 * @returns 交易哈希处理响应 */ async transactionHashProcess(txHash) { return this.handleApiCall(async () => { const response = await this.generatedApi.v1.transactionHashProcess(txHash, this.getRequestParams()); return response.data; }); } // ==================== S1 API Methods ==================== /** * 授予徽章 * @param data 授予徽章请求 * @returns 授予徽章响应 */ async grantBadge(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.s1.grantBadge(data, this.getRequestParams()); return response.data; }); } /** * 购买铸造(带签名) * @param type 购买类型 * @param data 购买请求数据 * @returns 购买响应 */ async purchaseMint(type, data) { return this.handleApiCall(async () => { const response = await this.generatedApi.s1.purchaseMint(type, data, this.getRequestParams()); return response.data; }); } /** * 查询资产 * @param type 资产类型 * @param data 查询请求数据 * @returns 资产响应 */ async queryAssets(type, data) { return this.handleApiCall(async () => { const response = await this.generatedApi.s1.queryAssets(type, data, this.getRequestParams()); return response.data; }); } /** * 根据 ID 获取 NFT 系列详情 * @param type NFT 类型 * @param series 系列标识符 * @param customerId 客户 ID * @returns NFT 系列详情响应 */ async getNftSeriesDetail(type, series, customerId) { return this.handleApiCall(async () => { const query = customerId ? { customerId } : undefined; const response = await this.generatedApi.s1.getNftSeriesDetail(type, series, query, this.getRequestParams()); return response.data; }); } /** * 根据事件检查 * @param data 检查事件请求 * @returns 检查事件响应 */ async checkByEvent(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.s1.checkByEvent(data, this.getRequestParams()); return response.data; }); } /** * 徽章检查 * @param data 徽章检查请求 * @returns 徽章检查响应 */ async badgesCheck(data) { return this.handleApiCall(async () => { const response = await this.generatedApi.s1.badgesCheck(data, this.getRequestParams()); return response.data; }); } } exports.AnchorApiClientV2 = AnchorApiClientV2;