@open-dy/ai_coding_sdk 1.0.18 → 1.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.
@@ -4,6 +4,8 @@ export interface UserHashMobileParams {
4
4
  open_id: string;
5
5
  }
6
6
  export interface UserHashMobileResponse {
7
+ /** 单向哈希加密后的手机号(实际接口返回字段) */
8
+ hash_mobile?: string;
7
9
  /** 单向哈希加密后的手机号 */
8
10
  mobile_hash?: string;
9
11
  }
@@ -13,11 +13,20 @@ const core_1 = require("../core");
13
13
  * @remarks 返回的是经过单向哈希处理的手机号,无法还原为明文。
14
14
  */
15
15
  async function getUserHashMobile(params, options) {
16
- return (0, core_1.callOpenAPI)({
16
+ const response = await (0, core_1.callOpenAPI)({
17
17
  path: '/api/douyin/v1/user/get_user_hash_mobile/',
18
18
  method: 'GET',
19
19
  queryParams: params,
20
20
  domain: 'open.douyin.com',
21
21
  requiresUserAuth: true,
22
22
  }, options);
23
+ const hashMobile = response.data?.hash_mobile ?? response.data?.mobile_hash;
24
+ if (hashMobile) {
25
+ response.data = {
26
+ ...response.data,
27
+ hash_mobile: hashMobile,
28
+ mobile_hash: hashMobile,
29
+ };
30
+ }
31
+ return response;
23
32
  }
@@ -61,6 +61,37 @@ function maskHeaders(headers) {
61
61
  ...(headers['access-token'] ? { 'access-token': '***' } : {}),
62
62
  };
63
63
  }
64
+ function isRecord(value) {
65
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
66
+ }
67
+ function getNumericValue(value) {
68
+ if (typeof value === 'number' && Number.isFinite(value)) {
69
+ return value;
70
+ }
71
+ if (typeof value === 'string' && value.trim() !== '') {
72
+ const parsed = Number(value);
73
+ if (Number.isFinite(parsed)) {
74
+ return parsed;
75
+ }
76
+ }
77
+ return undefined;
78
+ }
79
+ function getStringValue(value) {
80
+ return typeof value === 'string' ? value : undefined;
81
+ }
82
+ function stripLegacyDataMeta(value) {
83
+ if (!isRecord(value)) {
84
+ return value;
85
+ }
86
+ const { error_code: _errorCode, description: _description, ...rest } = value;
87
+ return rest;
88
+ }
89
+ function buildOpenAPIErrorMessage(errMsg, errNo, logId, subErrMsg, subErrNo) {
90
+ const subErrorSuffix = subErrNo && subErrNo !== 0
91
+ ? `, sub_error: ${subErrMsg || 'N/A'} (${subErrNo})`
92
+ : '';
93
+ return `OpenAPI Error: ${errMsg || 'Unknown error'} (${errNo})${subErrorSuffix}, x-tt-logid: ${logId}`;
94
+ }
64
95
  async function callOpenAPI(params, options) {
65
96
  const { path, method = 'GET', queryParams, body, domain = 'open.douyin.com', requiresUserAuth, } = params;
66
97
  const accessToken = options?.accessToken ?? options?.delegateToken;
@@ -91,19 +122,67 @@ async function callOpenAPI(params, options) {
91
122
  if (!response.ok) {
92
123
  throw new Error(`HTTP Error: ${response.status} ${response.statusText}, x-tt-logid: ${response.headers.get('x-tt-logid') || 'N/A'}`);
93
124
  }
94
- if (!rawData ||
95
- typeof rawData !== 'object' ||
96
- !("err_no" in rawData) ||
97
- !("err_msg" in rawData)) {
125
+ if (!isRecord(rawData)) {
98
126
  throw new Error(`Invalid OpenAPI response, x-tt-logid: ${response.headers.get('x-tt-logid') || 'N/A'}`);
99
127
  }
100
128
  const dataAny = rawData;
101
- if (typeof dataAny.err_no === 'number' && dataAny.err_no !== 0) {
102
- throw new Error(`OpenAPI Error: ${String(dataAny.err_msg)} (${String(dataAny.err_no)}), x-tt-logid: ${response.headers.get('x-tt-logid') || 'N/A'}`);
129
+ const responseLogId = response.headers.get('x-tt-logid') || 'N/A';
130
+ const topLevelLogId = getStringValue(dataAny.log_id);
131
+ const dataPayload = dataAny.data;
132
+ const payloadRecord = isRecord(dataPayload) ? dataPayload : undefined;
133
+ const topErrNo = getNumericValue(dataAny.err_no);
134
+ const topErrMsg = getStringValue(dataAny.err_msg);
135
+ const payloadErrNo = getNumericValue(payloadRecord?.error_code);
136
+ const payloadErrMsg = getStringValue(payloadRecord?.description);
137
+ const extraRecord = isRecord(dataAny.extra) ? dataAny.extra : undefined;
138
+ const extraErrNo = getNumericValue(extraRecord?.error_code);
139
+ const extraErrMsg = getStringValue(extraRecord?.description);
140
+ const payloadExtraRecord = payloadRecord && isRecord(payloadRecord.extra) ? payloadRecord.extra : undefined;
141
+ const payloadExtraErrNo = getNumericValue(payloadExtraRecord?.error_code);
142
+ const payloadExtraErrMsg = getStringValue(payloadExtraRecord?.description);
143
+ const payloadExtraSubErrNo = getNumericValue(payloadExtraRecord?.sub_error_code);
144
+ const payloadExtraSubErrMsg = getStringValue(payloadExtraRecord?.sub_description);
145
+ const logId = response.headers.get('x-tt-logid')
146
+ || getStringValue(extraRecord?.logid)
147
+ || getStringValue(payloadRecord?.log_id)
148
+ || topLevelLogId
149
+ || responseLogId;
150
+ // 兼容两类返回协议:
151
+ // 1. 标准协议:顶层 err_no / err_msg / data
152
+ // 2. 授权类老协议:data.error_code / description + extra
153
+ if (topErrNo === undefined &&
154
+ topErrMsg === undefined &&
155
+ payloadErrNo === undefined &&
156
+ extraErrNo === undefined) {
157
+ throw new Error(`Invalid OpenAPI response, x-tt-logid: ${logId}`);
158
+ }
159
+ if (topErrNo !== undefined && topErrNo !== 0) {
160
+ throw new Error(buildOpenAPIErrorMessage(topErrMsg, topErrNo, logId));
161
+ }
162
+ if (payloadExtraErrNo !== undefined && payloadExtraErrNo !== 0) {
163
+ throw new Error(buildOpenAPIErrorMessage(payloadExtraErrMsg, payloadExtraErrNo, logId, payloadExtraSubErrMsg, payloadExtraSubErrNo));
164
+ }
165
+ if (topErrNo === undefined) {
166
+ const normalizedErrNo = payloadErrNo ?? extraErrNo ?? 0;
167
+ const normalizedErrMsg = payloadErrMsg ?? extraErrMsg ?? '';
168
+ if (normalizedErrNo !== 0) {
169
+ throw new Error(buildOpenAPIErrorMessage(normalizedErrMsg, normalizedErrNo, logId));
170
+ }
171
+ const normalized = {
172
+ err_no: normalizedErrNo,
173
+ err_msg: normalizedErrMsg,
174
+ data: stripLegacyDataMeta((dataPayload ?? {})),
175
+ };
176
+ console.log('[OpenAPI] Response:', JSON.stringify(normalized).slice(0, 500));
177
+ return normalized;
103
178
  }
104
179
  const normalized = (() => {
105
180
  if (dataAny.data !== undefined) {
106
- return dataAny;
181
+ return {
182
+ err_no: topErrNo,
183
+ err_msg: topErrMsg ?? '',
184
+ data: stripLegacyDataMeta(dataAny.data),
185
+ };
107
186
  }
108
187
  const { err_no, err_msg, ...rest } = dataAny;
109
188
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-dy/ai_coding_sdk",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "AI Coding SDK — 抖音开放平台 OpenAPI 客户端与工具集",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",