Skip to content

Commit 6ee54ea

Browse files
authored
Merge pull request #11 from Serverless-Devs/fix-list-all-toolsets
Fix list all toolsets
2 parents 16c03fe + de3824e commit 6ee54ea

3 files changed

Lines changed: 72 additions & 55 deletions

File tree

src/toolset/api/control.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export class ToolControlAPI {
134134

135135
const response = await client.listToolsetsWithOptions(input, headers || {}, runtime);
136136

137-
logger.debug(`API listToolsets called, Request ID: ${response.body?.requestId}`);
137+
logger.debug(
138+
`API listToolsets called, Request ID: ${response?.headers?.['x-acs-request-id']}`
139+
);
138140

139141
if (!response.body) {
140142
throw new Error('Empty response body');

src/toolset/toolset.ts

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { Config } from '../utils/config';
99
import { logger } from '../utils/log';
10-
import { updateObjectProperties } from '../utils/resource';
10+
import { listAllResourcesFunction, updateObjectProperties } from '../utils/resource';
1111

1212
import {
1313
ToolSetCreateInput,
@@ -50,6 +50,8 @@ export class ToolSet implements ToolSetData {
5050
return new ToolSetClient();
5151
}
5252

53+
uniqIdCallback = () => this.name;
54+
5355
/**
5456
* Create a new ToolSet
5557
*/
@@ -77,48 +79,70 @@ export class ToolSet implements ToolSetData {
7779
/**
7880
* List ToolSets
7981
*/
80-
static async list(input?: ToolSetListInput, config?: Config): Promise<ToolSet[]> {
81-
return await ToolSet.getClient().list({ input, config });
82-
}
8382

84-
/**
85-
* List all ToolSets with pagination
86-
*/
87-
static async listAll(
88-
options?: { prefix?: string; labels?: Record<string, string> },
89-
config?: Config
90-
): Promise<ToolSet[]> {
91-
const toolsets: ToolSet[] = [];
92-
const pageSize = 50;
93-
94-
// eslint-disable-next-line no-constant-condition
95-
while (true) {
96-
const result = await ToolSet.list(
97-
{
98-
prefix: options?.prefix,
99-
labels: options?.labels,
100-
pageSize,
101-
},
102-
config
103-
);
104-
105-
toolsets.push(...result);
83+
static list: /**
84+
* @deprecated
85+
*/
86+
| ((input?: ToolSetListInput, config?: Config) => Promise<ToolSet[]>)
87+
/**
88+
* 枚举 ToolSet 列表 / List ToolSet list
89+
*/
90+
| ((params?: { input?: ToolSetListInput; config?: Config }) => Promise<ToolSet[]>) = async (
91+
...args: any
92+
): Promise<ToolSet[]> => {
93+
let input: ToolSetListInput | undefined;
94+
let config: Config | undefined;
95+
96+
if (args.length >= 1 && 'input' in args[0]) {
97+
input = args[0].input;
98+
} else {
99+
input = args[0];
100+
}
106101

107-
if (result.length < pageSize) {
108-
break;
109-
}
102+
if (args.length >= 1 && 'config' in args[0]) {
103+
config = args[0].config;
104+
} else if (args.length > 1 && args[1] instanceof Config) {
105+
config = args[1];
110106
}
111107

112-
// Deduplicate
113-
const seen = new Set<string>();
114-
return toolsets.filter(t => {
115-
if (!t.uid || seen.has(t.uid)) {
116-
return false;
117-
}
118-
seen.add(t.uid);
119-
return true;
108+
return await this.getClient().list({
109+
input: {
110+
...input,
111+
} as ToolSetListInput,
112+
config,
120113
});
121-
}
114+
};
115+
116+
static listAll: /**
117+
* @deprecated
118+
*/
119+
| ((
120+
options?: { prefix?: string; labels?: Record<string, string> },
121+
config?: Config
122+
) => Promise<ToolSet[]>)
123+
/**
124+
* 枚举 ToolSet 列表 / List ToolSet list
125+
*/
126+
| ((params?: { input?: ToolSetListInput; config?: Config }) => Promise<ToolSet[]>) = async (
127+
...args: any
128+
) => {
129+
let input: ToolSetListInput | undefined;
130+
let config: Config | undefined;
131+
132+
if (args.length >= 1 && 'input' in args[0]) {
133+
input = args[0].input;
134+
} else {
135+
input = args[0];
136+
}
137+
138+
if (args.length >= 1 && 'config' in args[0]) {
139+
config = args[0].config;
140+
} else if (args.length > 1 && args[1] instanceof Config) {
141+
config = args[1];
142+
}
143+
144+
return await listAllResourcesFunction(this.list as any)({ ...input, config });
145+
};
122146

123147
/**
124148
* Update a ToolSet by Name

tests/unittests/toolset/toolset-resource.test.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -356,30 +356,19 @@ describe('ToolSet Module', () => {
356356
describe('listAll', () => {
357357
it('should list all toolsets with pagination and deduplication', async () => {
358358
mockToolSetClient.list.mockResolvedValue([
359-
{ name: 'toolset-1', uid: 'uid-1' },
360-
{ name: 'toolset-2', uid: 'uid-2' },
359+
new ToolSet({ name: 'toolset-1', uid: 'uid-1' }),
360+
new ToolSet({ name: 'toolset-2', uid: 'uid-2' }),
361361
]);
362362

363363
const result = await ToolSet.listAll();
364364

365365
expect(result.length).toBeGreaterThanOrEqual(1);
366366
});
367367

368-
it('should deduplicate by uid', async () => {
369-
mockToolSetClient.list.mockResolvedValue([
370-
{ name: 'toolset-1', uid: 'uid-1' },
371-
{ name: 'toolset-1-dup', uid: 'uid-1' }, // Same uid
372-
]);
373-
374-
const result = await ToolSet.listAll();
375-
376-
expect(result).toHaveLength(1);
377-
});
378-
379368
it('should filter out items without uid', async () => {
380369
mockToolSetClient.list.mockResolvedValue([
381-
{ name: 'toolset-1', uid: 'uid-1' },
382-
{ name: 'toolset-no-uid' }, // No uid
370+
new ToolSet({ name: 'toolset-1' }),
371+
new ToolSet({ name: 'toolset-1' }), // No uid
383372
]);
384373

385374
const result = await ToolSet.listAll();
@@ -389,7 +378,9 @@ describe('ToolSet Module', () => {
389378
});
390379

391380
it('should support prefix and labels options', async () => {
392-
mockToolSetClient.list.mockResolvedValue([{ name: 'my-toolset', uid: 'uid-1' }]);
381+
mockToolSetClient.list.mockResolvedValue([
382+
new ToolSet({ name: 'my-toolset', uid: 'uid-1' }),
383+
]);
393384

394385
const result = await ToolSet.listAll({
395386
prefix: 'my-',

0 commit comments

Comments
 (0)