apaas-oapi-client 0.1.32 → 0.1.34
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/dist/index.js +93 -41
- package/dist/src/index.d.ts +11 -3
- package/package.json +1 -1
- package/src/index.ts +104 -42
package/dist/index.js
CHANGED
|
@@ -120,10 +120,10 @@ class Client {
|
|
|
120
120
|
/**
|
|
121
121
|
* 列出所有对象(数据表)
|
|
122
122
|
* @param params 请求参数 { offset?, filter?, limit? }
|
|
123
|
-
* @returns
|
|
123
|
+
* @returns 接口返回结果 { code, items, total, msg, has_more }
|
|
124
124
|
*/
|
|
125
125
|
list: async (params) => {
|
|
126
|
-
var _a, _b;
|
|
126
|
+
var _a, _b, _c, _d, _e, _f;
|
|
127
127
|
const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
|
|
128
128
|
const limit = (_b = params === null || params === void 0 ? void 0 : params.limit) !== null && _b !== void 0 ? _b : 50;
|
|
129
129
|
const filter = params === null || params === void 0 ? void 0 : params.filter;
|
|
@@ -139,23 +139,28 @@ class Client {
|
|
|
139
139
|
});
|
|
140
140
|
this.log(LoggerLevel.debug, `[object.list] Objects list fetched successfully: code=${res.data.code}`);
|
|
141
141
|
this.log(LoggerLevel.trace, `[object.list] Response: ${JSON.stringify(res.data)}`);
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
142
|
+
// 扁平化返回结构并添加 has_more 字段
|
|
143
|
+
const items = ((_d = (_c = res.data) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.items) || [];
|
|
144
|
+
const total = ((_f = (_e = res.data) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.total) || 0;
|
|
145
|
+
const currentEnd = offset + limit;
|
|
146
|
+
const has_more = currentEnd < total;
|
|
147
|
+
this.log(LoggerLevel.debug, `[object.list] has_more=${has_more}, total=${total}, currentEnd=${currentEnd}`);
|
|
148
|
+
return {
|
|
149
|
+
code: res.data.code,
|
|
150
|
+
items,
|
|
151
|
+
total,
|
|
152
|
+
msg: res.data.msg,
|
|
153
|
+
has_more
|
|
154
|
+
};
|
|
150
155
|
},
|
|
151
156
|
/**
|
|
152
157
|
* 列出所有对象(数据表)- 支持自动分页查询
|
|
153
158
|
* @description 该方法会自动处理分页,直到没有更多数据为止
|
|
154
159
|
* @param params 请求参数 { filter?, limit? }
|
|
155
|
-
* @returns {
|
|
160
|
+
* @returns { code, msg, items, total, failed? } - code 表示失败的分页数量
|
|
156
161
|
*/
|
|
157
162
|
listWithIterator: async (params) => {
|
|
158
|
-
var _a, _b
|
|
163
|
+
var _a, _b;
|
|
159
164
|
const filter = params === null || params === void 0 ? void 0 : params.filter;
|
|
160
165
|
const limit = (_a = params === null || params === void 0 ? void 0 : params.limit) !== null && _a !== void 0 ? _a : 50;
|
|
161
166
|
let results = [];
|
|
@@ -164,40 +169,87 @@ class Client {
|
|
|
164
169
|
let hasMore = true;
|
|
165
170
|
let page = 0;
|
|
166
171
|
let totalPages = 0;
|
|
172
|
+
let failed = [];
|
|
173
|
+
let allSuccess = true;
|
|
167
174
|
this.log(LoggerLevel.info, `[object.listWithIterator] Starting paginated query with limit=${limit}`);
|
|
168
175
|
while (hasMore) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
176
|
+
try {
|
|
177
|
+
const res = await this.object.list({
|
|
178
|
+
offset,
|
|
179
|
+
limit,
|
|
180
|
+
filter
|
|
181
|
+
});
|
|
182
|
+
if (res.code !== '0') {
|
|
183
|
+
this.log(LoggerLevel.error, `[object.listWithIterator] Error querying objects: code=${res.code}, msg=${res.msg}, offset=${offset}`);
|
|
184
|
+
allSuccess = false;
|
|
185
|
+
failed.push({
|
|
186
|
+
offset,
|
|
187
|
+
limit,
|
|
188
|
+
code: res.code,
|
|
189
|
+
msg: res.msg || `Query failed with code ${res.code}`
|
|
190
|
+
});
|
|
191
|
+
// 继续尝试下一页,而不是直接退出
|
|
192
|
+
offset += limit;
|
|
193
|
+
page += 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
page += 1;
|
|
197
|
+
if (Array.isArray(res.items)) {
|
|
198
|
+
results = results.concat(res.items);
|
|
199
|
+
}
|
|
200
|
+
if (res.total !== undefined && res.total !== null) {
|
|
201
|
+
total = res.total;
|
|
202
|
+
}
|
|
203
|
+
if (page === 1) {
|
|
204
|
+
totalPages = Math.ceil(total / limit);
|
|
205
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Total objects: ${total}, pages: ${totalPages}`);
|
|
206
|
+
}
|
|
207
|
+
// 判断是否还有更多数据
|
|
208
|
+
hasMore = res.has_more === true;
|
|
209
|
+
offset += limit;
|
|
210
|
+
const padLength = totalPages.toString().length;
|
|
211
|
+
const pageStr = page.toString().padStart(padLength, '0');
|
|
212
|
+
const totalPagesStr = totalPages.toString().padStart(padLength, '0');
|
|
213
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Page completed: [${pageStr}/${totalPagesStr}]`);
|
|
214
|
+
this.log(LoggerLevel.debug, `[object.listWithIterator] Page ${page} details: items=${(_b = res.items) === null || _b === void 0 ? void 0 : _b.length}, hasMore=${hasMore}`);
|
|
215
|
+
this.log(LoggerLevel.trace, `[object.listWithIterator] Page ${page} data: ${JSON.stringify(res.items)}`);
|
|
184
216
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
217
|
+
catch (error) {
|
|
218
|
+
this.log(LoggerLevel.error, `[object.listWithIterator] Exception occurred: ${error}, offset=${offset}`);
|
|
219
|
+
allSuccess = false;
|
|
220
|
+
failed.push({
|
|
221
|
+
offset,
|
|
222
|
+
limit,
|
|
223
|
+
code: '1',
|
|
224
|
+
msg: error instanceof Error ? error.message : String(error)
|
|
225
|
+
});
|
|
226
|
+
// 继续尝试下一页
|
|
227
|
+
offset += limit;
|
|
228
|
+
page += 1;
|
|
229
|
+
// 如果没有获取到 total,可能需要退出循环
|
|
230
|
+
if (total === 0) {
|
|
231
|
+
hasMore = false;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
hasMore = offset < total;
|
|
235
|
+
}
|
|
188
236
|
}
|
|
189
|
-
// 判断是否还有更多数据
|
|
190
|
-
hasMore = res.has_more === true;
|
|
191
|
-
offset += limit;
|
|
192
|
-
const padLength = totalPages.toString().length;
|
|
193
|
-
const pageStr = page.toString().padStart(padLength, '0');
|
|
194
|
-
const totalPagesStr = totalPages.toString().padStart(padLength, '0');
|
|
195
|
-
this.log(LoggerLevel.info, `[object.listWithIterator] Page completed: [${pageStr}/${totalPagesStr}]`);
|
|
196
|
-
this.log(LoggerLevel.debug, `[object.listWithIterator] Page ${page} details: items=${(_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b.items) === null || _c === void 0 ? void 0 : _c.length}, hasMore=${hasMore}`);
|
|
197
|
-
this.log(LoggerLevel.trace, `[object.listWithIterator] Page ${page} data: ${JSON.stringify((_d = res.data) === null || _d === void 0 ? void 0 : _d.items)}`);
|
|
198
237
|
}
|
|
199
|
-
|
|
200
|
-
|
|
238
|
+
const resultCode = failed.length.toString();
|
|
239
|
+
const resultMsg = failed.length === 0
|
|
240
|
+
? 'Success'
|
|
241
|
+
: `Completed with ${failed.length} failed page(s)`;
|
|
242
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Completed: code=${resultCode}, total=${total}, fetched=${results.length}, failed=${failed.length}`);
|
|
243
|
+
const result = {
|
|
244
|
+
code: resultCode,
|
|
245
|
+
msg: resultMsg,
|
|
246
|
+
items: results,
|
|
247
|
+
total
|
|
248
|
+
};
|
|
249
|
+
if (failed.length > 0) {
|
|
250
|
+
result.failed = failed;
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
201
253
|
},
|
|
202
254
|
metadata: {
|
|
203
255
|
/**
|
package/dist/src/index.d.ts
CHANGED
|
@@ -103,7 +103,7 @@ declare class Client {
|
|
|
103
103
|
/**
|
|
104
104
|
* 列出所有对象(数据表)
|
|
105
105
|
* @param params 请求参数 { offset?, filter?, limit? }
|
|
106
|
-
* @returns
|
|
106
|
+
* @returns 接口返回结果 { code, items, total, msg, has_more }
|
|
107
107
|
*/
|
|
108
108
|
list: (params?: {
|
|
109
109
|
offset?: number;
|
|
@@ -117,7 +117,7 @@ declare class Client {
|
|
|
117
117
|
* 列出所有对象(数据表)- 支持自动分页查询
|
|
118
118
|
* @description 该方法会自动处理分页,直到没有更多数据为止
|
|
119
119
|
* @param params 请求参数 { filter?, limit? }
|
|
120
|
-
* @returns {
|
|
120
|
+
* @returns { code, msg, items, total, failed? } - code 表示失败的分页数量
|
|
121
121
|
*/
|
|
122
122
|
listWithIterator: (params?: {
|
|
123
123
|
filter?: {
|
|
@@ -126,8 +126,16 @@ declare class Client {
|
|
|
126
126
|
};
|
|
127
127
|
limit?: number;
|
|
128
128
|
}) => Promise<{
|
|
129
|
-
|
|
129
|
+
code: string;
|
|
130
|
+
msg: string;
|
|
130
131
|
items: any[];
|
|
132
|
+
total: number;
|
|
133
|
+
failed?: Array<{
|
|
134
|
+
offset: number;
|
|
135
|
+
limit: number;
|
|
136
|
+
code: string;
|
|
137
|
+
msg: string;
|
|
138
|
+
}>;
|
|
131
139
|
}>;
|
|
132
140
|
metadata: {
|
|
133
141
|
/**
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -291,7 +291,7 @@ class Client {
|
|
|
291
291
|
/**
|
|
292
292
|
* 列出所有对象(数据表)
|
|
293
293
|
* @param params 请求参数 { offset?, filter?, limit? }
|
|
294
|
-
* @returns
|
|
294
|
+
* @returns 接口返回结果 { code, items, total, msg, has_more }
|
|
295
295
|
*/
|
|
296
296
|
list: async (params?: { offset?: number; filter?: { type?: string; quickQuery?: string }; limit?: number }): Promise<any> => {
|
|
297
297
|
const offset = params?.offset ?? 0;
|
|
@@ -314,24 +314,36 @@ class Client {
|
|
|
314
314
|
this.log(LoggerLevel.debug, `[object.list] Objects list fetched successfully: code=${res.data.code}`);
|
|
315
315
|
this.log(LoggerLevel.trace, `[object.list] Response: ${JSON.stringify(res.data)}`);
|
|
316
316
|
|
|
317
|
-
//
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
this.log(LoggerLevel.debug, `[object.list] has_more=${res.data.has_more}, total=${total}, currentEnd=${currentEnd}`);
|
|
323
|
-
}
|
|
317
|
+
// 扁平化返回结构并添加 has_more 字段
|
|
318
|
+
const items = res.data?.data?.items || [];
|
|
319
|
+
const total = res.data?.data?.total || 0;
|
|
320
|
+
const currentEnd = offset + limit;
|
|
321
|
+
const has_more = currentEnd < total;
|
|
324
322
|
|
|
325
|
-
|
|
323
|
+
this.log(LoggerLevel.debug, `[object.list] has_more=${has_more}, total=${total}, currentEnd=${currentEnd}`);
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
code: res.data.code,
|
|
327
|
+
items,
|
|
328
|
+
total,
|
|
329
|
+
msg: res.data.msg,
|
|
330
|
+
has_more
|
|
331
|
+
};
|
|
326
332
|
},
|
|
327
333
|
|
|
328
334
|
/**
|
|
329
335
|
* 列出所有对象(数据表)- 支持自动分页查询
|
|
330
336
|
* @description 该方法会自动处理分页,直到没有更多数据为止
|
|
331
337
|
* @param params 请求参数 { filter?, limit? }
|
|
332
|
-
* @returns {
|
|
338
|
+
* @returns { code, msg, items, total, failed? } - code 表示失败的分页数量
|
|
333
339
|
*/
|
|
334
|
-
listWithIterator: async (params?: { filter?: { type?: string; quickQuery?: string }; limit?: number }): Promise<{
|
|
340
|
+
listWithIterator: async (params?: { filter?: { type?: string; quickQuery?: string }; limit?: number }): Promise<{
|
|
341
|
+
code: string;
|
|
342
|
+
msg: string;
|
|
343
|
+
items: any[];
|
|
344
|
+
total: number;
|
|
345
|
+
failed?: Array<{ offset: number; limit: number; code: string; msg: string }>
|
|
346
|
+
}> => {
|
|
335
347
|
const filter = params?.filter;
|
|
336
348
|
const limit = params?.limit ?? 50;
|
|
337
349
|
|
|
@@ -341,51 +353,101 @@ class Client {
|
|
|
341
353
|
let hasMore = true;
|
|
342
354
|
let page = 0;
|
|
343
355
|
let totalPages = 0;
|
|
356
|
+
let failed: Array<{ offset: number; limit: number; code: string; msg: string }> = [];
|
|
357
|
+
let allSuccess = true;
|
|
344
358
|
|
|
345
359
|
this.log(LoggerLevel.info, `[object.listWithIterator] Starting paginated query with limit=${limit}`);
|
|
346
360
|
|
|
347
361
|
while (hasMore) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
362
|
+
try {
|
|
363
|
+
const res = await this.object.list({
|
|
364
|
+
offset,
|
|
365
|
+
limit,
|
|
366
|
+
filter
|
|
367
|
+
});
|
|
353
368
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
369
|
+
if (res.code !== '0') {
|
|
370
|
+
this.log(LoggerLevel.error, `[object.listWithIterator] Error querying objects: code=${res.code}, msg=${res.msg}, offset=${offset}`);
|
|
371
|
+
allSuccess = false;
|
|
372
|
+
failed.push({
|
|
373
|
+
offset,
|
|
374
|
+
limit,
|
|
375
|
+
code: res.code,
|
|
376
|
+
msg: res.msg || `Query failed with code ${res.code}`
|
|
377
|
+
});
|
|
378
|
+
// 继续尝试下一页,而不是直接退出
|
|
379
|
+
offset += limit;
|
|
380
|
+
page += 1;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
358
383
|
|
|
359
|
-
|
|
384
|
+
page += 1;
|
|
360
385
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
386
|
+
if (Array.isArray(res.items)) {
|
|
387
|
+
results = results.concat(res.items);
|
|
388
|
+
}
|
|
364
389
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
390
|
+
if (res.total !== undefined && res.total !== null) {
|
|
391
|
+
total = res.total;
|
|
392
|
+
}
|
|
368
393
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
394
|
+
if (page === 1) {
|
|
395
|
+
totalPages = Math.ceil(total / limit);
|
|
396
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Total objects: ${total}, pages: ${totalPages}`);
|
|
397
|
+
}
|
|
373
398
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
399
|
+
// 判断是否还有更多数据
|
|
400
|
+
hasMore = res.has_more === true;
|
|
401
|
+
offset += limit;
|
|
377
402
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
403
|
+
const padLength = totalPages.toString().length;
|
|
404
|
+
const pageStr = page.toString().padStart(padLength, '0');
|
|
405
|
+
const totalPagesStr = totalPages.toString().padStart(padLength, '0');
|
|
381
406
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
407
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Page completed: [${pageStr}/${totalPagesStr}]`);
|
|
408
|
+
this.log(LoggerLevel.debug, `[object.listWithIterator] Page ${page} details: items=${res.items?.length}, hasMore=${hasMore}`);
|
|
409
|
+
this.log(LoggerLevel.trace, `[object.listWithIterator] Page ${page} data: ${JSON.stringify(res.items)}`);
|
|
410
|
+
} catch (error) {
|
|
411
|
+
this.log(LoggerLevel.error, `[object.listWithIterator] Exception occurred: ${error}, offset=${offset}`);
|
|
412
|
+
allSuccess = false;
|
|
413
|
+
failed.push({
|
|
414
|
+
offset,
|
|
415
|
+
limit,
|
|
416
|
+
code: '1',
|
|
417
|
+
msg: error instanceof Error ? error.message : String(error)
|
|
418
|
+
});
|
|
419
|
+
// 继续尝试下一页
|
|
420
|
+
offset += limit;
|
|
421
|
+
page += 1;
|
|
422
|
+
|
|
423
|
+
// 如果没有获取到 total,可能需要退出循环
|
|
424
|
+
if (total === 0) {
|
|
425
|
+
hasMore = false;
|
|
426
|
+
} else {
|
|
427
|
+
hasMore = offset < total;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
385
430
|
}
|
|
386
431
|
|
|
387
|
-
|
|
388
|
-
|
|
432
|
+
const resultCode = failed.length.toString();
|
|
433
|
+
const resultMsg = failed.length === 0
|
|
434
|
+
? 'Success'
|
|
435
|
+
: `Completed with ${failed.length} failed page(s)`;
|
|
436
|
+
|
|
437
|
+
this.log(LoggerLevel.info, `[object.listWithIterator] Completed: code=${resultCode}, total=${total}, fetched=${results.length}, failed=${failed.length}`);
|
|
438
|
+
|
|
439
|
+
const result: any = {
|
|
440
|
+
code: resultCode,
|
|
441
|
+
msg: resultMsg,
|
|
442
|
+
items: results,
|
|
443
|
+
total
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
if (failed.length > 0) {
|
|
447
|
+
result.failed = failed;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return result;
|
|
389
451
|
},
|
|
390
452
|
|
|
391
453
|
metadata: {
|