Skip to content

Commit 857ab06

Browse files
authored
feat(sdk): add AxonListParams support and auto-pagination to AxonOps.list() (#763)
1 parent c5abc92 commit 857ab06

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

src/sdk.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
import type { BlueprintListParams } from './resources/blueprints';
2626
import type { ObjectCreateParams, ObjectListParams } from './resources/objects';
2727
import type { AgentCreateParams, AgentListParams } from './resources/agents';
28-
import type { AxonCreateParams } from './resources/axons';
28+
import type { AxonCreateParams, AxonListParams } from './resources/axons/axons';
2929
import type { ScorerCreateParams, ScorerListParams } from './resources/scenarios/scorers';
3030
import type { NetworkPolicyCreateParams, NetworkPolicyListParams } from './resources/network-policies';
3131
import type { GatewayConfigCreateParams, GatewayConfigListParams } from './resources/gateway-configs';
@@ -1565,12 +1565,24 @@ export class AxonOps {
15651565
/**
15661566
* [Beta] List all active axons.
15671567
*
1568+
* @example
1569+
* ```typescript
1570+
* const runloop = new RunloopSDK();
1571+
* const axons = await runloop.axon.list({ limit: 10 });
1572+
* console.log(axons.map((a) => a.id));
1573+
* ```
1574+
*
1575+
* @param {AxonListParams} [params] - Optional filter/pagination parameters.
15681576
* @param {Core.RequestOptions} [options] - Request options.
15691577
* @returns {Promise<Axon[]>} An array of {@link Axon} instances.
15701578
*/
1571-
async list(options?: Core.RequestOptions): Promise<Axon[]> {
1572-
const result = await this.client.axons.list(options);
1573-
return result.axons.map((axon) => Axon.fromId(this.client, axon.id));
1579+
async list(params?: AxonListParams, options?: Core.RequestOptions): Promise<Axon[]> {
1580+
const result = await this.client.axons.list(params, options);
1581+
const axons: Axon[] = [];
1582+
for await (const axon of result) {
1583+
axons.push(Axon.fromId(this.client, axon.id));
1584+
}
1585+
return axons;
15741586
}
15751587
}
15761588

tests/sdk/axon-ops.test.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxonOps } from '../../src/sdk';
22
import { Axon } from '../../src/sdk/axon';
3-
import type { AxonView, AxonListView } from '../../src/resources/axons';
3+
import type { AxonView } from '../../src/resources/axons';
44

55
jest.mock('../../src/sdk/axon');
66

@@ -77,19 +77,25 @@ describe('AxonOps', () => {
7777
});
7878

7979
describe('list', () => {
80-
it('should list axons and wrap as Axon instances', async () => {
81-
const mockListView: AxonListView = {
82-
axons: [
83-
{ id: 'axn_1', created_at_ms: Date.now(), name: 'axon-1' },
84-
{ id: 'axn_2', created_at_ms: Date.now(), name: 'axon-2' },
85-
{ id: 'axn_3', created_at_ms: Date.now() },
86-
],
80+
function mockPageResult(items: AxonView[]) {
81+
return {
82+
[Symbol.asyncIterator]: async function* () {
83+
yield* items;
84+
},
8785
};
88-
mockClient.axons.list.mockResolvedValue(mockListView);
86+
}
87+
88+
it('should list axons and wrap as Axon instances', async () => {
89+
const mockAxons: AxonView[] = [
90+
{ id: 'axn_1', created_at_ms: Date.now(), name: 'axon-1' },
91+
{ id: 'axn_2', created_at_ms: Date.now(), name: 'axon-2' },
92+
{ id: 'axn_3', created_at_ms: Date.now() },
93+
];
94+
mockClient.axons.list.mockResolvedValue(mockPageResult(mockAxons));
8995

9096
const axons = await axonOps.list();
9197

92-
expect(mockClient.axons.list).toHaveBeenCalledWith(undefined);
98+
expect(mockClient.axons.list).toHaveBeenCalledWith(undefined, undefined);
9399
expect(Axon.fromId).toHaveBeenCalledTimes(3);
94100
expect(Axon.fromId).toHaveBeenCalledWith(mockClient, 'axn_1');
95101
expect(Axon.fromId).toHaveBeenCalledWith(mockClient, 'axn_2');
@@ -98,19 +104,27 @@ describe('AxonOps', () => {
98104
});
99105

100106
it('should return empty array when no axons exist', async () => {
101-
mockClient.axons.list.mockResolvedValue({ axons: [] });
107+
mockClient.axons.list.mockResolvedValue(mockPageResult([]));
102108

103109
const axons = await axonOps.list();
104110

105111
expect(axons).toHaveLength(0);
106112
});
107113

114+
it('should pass filter params', async () => {
115+
mockClient.axons.list.mockResolvedValue(mockPageResult([]));
116+
117+
await axonOps.list({ name: 'my-axon', limit: 10 });
118+
119+
expect(mockClient.axons.list).toHaveBeenCalledWith({ name: 'my-axon', limit: 10 }, undefined);
120+
});
121+
108122
it('should pass request options', async () => {
109-
mockClient.axons.list.mockResolvedValue({ axons: [] });
123+
mockClient.axons.list.mockResolvedValue(mockPageResult([]));
110124

111-
await axonOps.list({ timeout: 3000 });
125+
await axonOps.list(undefined, { timeout: 3000 });
112126

113-
expect(mockClient.axons.list).toHaveBeenCalledWith({ timeout: 3000 });
127+
expect(mockClient.axons.list).toHaveBeenCalledWith(undefined, { timeout: 3000 });
114128
});
115129
});
116130
});

0 commit comments

Comments
 (0)