@be-link/shield-for-tcb-node-sdk 1.0.3 → 1.0.5

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.
@@ -47,6 +47,10 @@ export declare class ConfigServiceV2 extends BaseService implements ServiceV2.Co
47
47
  * @returns 解析后的配置值映射
48
48
  */
49
49
  getConfigValues<T = any>(service: string, keys?: string[], env?: string): Promise<Record<string, T>>;
50
+ /**
51
+ * 解析配置值(自动检测类型)
52
+ */
53
+ private _parseValue;
50
54
  /**
51
55
  * 获取指定服务的所有配置
52
56
  * @param service 服务名称
@@ -13,7 +13,7 @@ const BaseService_1 = __importDefault(require("../BaseService"));
13
13
  class ConfigServiceV2 extends BaseService_1.default {
14
14
  constructor() {
15
15
  super(...arguments);
16
- this.prefixUrl = '/v2/shield/config';
16
+ this.prefixUrl = '/shield/v2/config';
17
17
  }
18
18
  /**
19
19
  * 获取单个配置
@@ -92,29 +92,39 @@ class ConfigServiceV2 extends BaseService_1.default {
92
92
  async getConfigValues(service, keys, env) {
93
93
  const result = await this.fetchConfigs({ service, keys, env });
94
94
  const parsed = {};
95
- for (const [key, item] of Object.entries(result)) {
96
- switch (item.value_type) {
97
- case 'number':
98
- parsed[key] = Number(item.value);
99
- break;
100
- case 'boolean':
101
- parsed[key] = (item.value === 'true' || item.value === '1');
102
- break;
103
- case 'object':
104
- case 'array':
105
- try {
106
- parsed[key] = JSON.parse(item.value);
107
- }
108
- catch {
109
- parsed[key] = item.value;
110
- }
111
- break;
112
- default:
113
- parsed[key] = item.value;
114
- }
95
+ for (const [key, value] of Object.entries(result)) {
96
+ parsed[key] = this._parseValue(value);
115
97
  }
116
98
  return parsed;
117
99
  }
100
+ /**
101
+ * 解析配置值(自动检测类型)
102
+ */
103
+ _parseValue(value) {
104
+ // 尝试解析 JSON (object/array)
105
+ if ((value.startsWith('{') && value.endsWith('}')) ||
106
+ (value.startsWith('[') && value.endsWith(']'))) {
107
+ try {
108
+ return JSON.parse(value);
109
+ }
110
+ catch {
111
+ return value;
112
+ }
113
+ }
114
+ // 尝试解析数字
115
+ if (/^-?\d+(\.\d+)?$/.test(value)) {
116
+ return Number(value);
117
+ }
118
+ // 尝试解析布尔值
119
+ if (value === 'true' || value === '1') {
120
+ return true;
121
+ }
122
+ if (value === 'false' || value === '0') {
123
+ return false;
124
+ }
125
+ // 默认返回字符串
126
+ return value;
127
+ }
118
128
  /**
119
129
  * 获取指定服务的所有配置
120
130
  * @param service 服务名称
@@ -60,13 +60,10 @@ export declare namespace ServiceV2 {
60
60
  }
61
61
  /**
62
62
  * 批量获取配置响应
63
+ * 返回 Map<config_key, parsed_value> 格式
64
+ * value 会自动解析:JSON 对象/数组会解析,其他返回原始字符串
63
65
  */
64
- interface FetchConfigsResponse {
65
- [key: string]: {
66
- value: string;
67
- value_type: 'string' | 'number' | 'boolean' | 'object' | 'array';
68
- };
69
- }
66
+ type FetchConfigsResponse = Record<string, any>;
70
67
  /**
71
68
  * 获取服务列表响应
72
69
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/shield-for-tcb-node-sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "ShieldForTCB Node.js SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",