Skip to content

Commit d97ab7a

Browse files
committed
refactor(sandbox): migrate client and sandbox classes to object-based API parameters
This commit refactors the SandboxClient and Sandbox classes to use object-based API parameters instead of the legacy string-based parameters. The changes include updating method signatures, parameter handling, and maintaining backward compatibility for deprecated APIs. Additionally, the template tests have been updated to use direct SandboxControlAPI mocking instead of client mocking. The parameter migration affects listTemplates, deleteSandbox, stopSandbox, and listSandboxes methods to accept object parameters for better type safety and flexibility while maintaining support for legacy string parameter usage. refactor(sandbox): 将客户端和沙箱类迁移到基于对象的 API 参数 此提交重构了 SandboxClient 和 Sandbox 类,以使用基于对象的 API 参数, 而不是传统的基于字符串的参数。更改包括更新方法签名、参数处理, 并维护对已弃用 API 的向后兼容性。此外,模板测试已更新为直接使用 SandboxControlAPI 模拟,而不是客户端模拟。 参数迁移影响了 listTemplates、deleteSandbox、stopSandbox 和 listSandboxes 方法,使其接受对象参数以实现更好的类型安全性和灵活性, 同时保持对传统字符串参数用法的支持。 Change-Id: I9d1b8be721a41b38851d1280727b892590d085c5 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 67049cc commit d97ab7a

4 files changed

Lines changed: 256 additions & 499 deletions

File tree

src/sandbox/client.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import { HTTPError } from '../utils/exception';
1111
import { logger } from '../utils/log';
1212
import { SandboxControlAPI } from './api/control';
1313

14+
import { AioSandbox } from './aio-sandbox';
1415
import { BrowserSandbox } from './browser-sandbox';
1516
import { CodeInterpreterSandbox } from './code-interpreter-sandbox';
17+
import { CustomSandbox } from './custom-sandbox';
1618
import {
1719
NASConfig,
1820
OSSMountConfig,
@@ -28,8 +30,6 @@ import {
2830
} from './model';
2931
import { Sandbox } from './sandbox';
3032
import { Template } from './template';
31-
import { AioSandbox } from './aio-sandbox';
32-
import { CustomSandbox } from './custom-sandbox';
3333

3434
/**
3535
* Sandbox Client
@@ -268,7 +268,7 @@ export class SandboxClient {
268268
if (
269269
arg2 ||
270270
(arg1 &&
271-
('maxResults' in arg1 || 'nextToken' in arg1 || 'templateType' in arg1))
271+
('pageNumber' in arg1 || 'pageSize' in arg1 || 'templateType' in arg1))
272272
) {
273273
logger.warn(
274274
'Deprecated: listTemplates(input, config) is deprecated. Use listTemplates({ input, config }) instead.',
@@ -483,24 +483,19 @@ export class SandboxClient {
483483
(params: { id: string; config?: Config }): Promise<Sandbox>;
484484
/** @deprecated Use stopSandbox({ id, config }) instead. */
485485
(id: string, config?: Config): Promise<Sandbox>;
486-
} = async (
487-
arg1: { id: string; config?: Config } | string,
488-
arg2?: Config,
489-
): Promise<Sandbox> => {
490-
let id: string;
491-
let config: Config | undefined;
486+
} = async (...args: any[]): Promise<Sandbox> => {
487+
let params: { id: string; config?: Config } = args?.[0];
492488

493-
if (typeof arg1 === 'string') {
489+
if (typeof args[0] === 'string') {
494490
logger.warn(
495491
'Deprecated: stopSandbox(id, config) is deprecated. Use stopSandbox({ id, config }) instead.',
496492
);
497-
id = arg1;
498-
config = arg2;
499-
} else {
500-
id = arg1.id;
501-
config = arg1.config;
493+
494+
params = { id: args[0], config: args[1] };
502495
}
503496

497+
const { id, config } = params;
498+
504499
const cfg = Config.withConfigs(this.config, config);
505500

506501
try {

src/sandbox/sandbox.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { ClientError, HTTPError } from '@/utils';
99
import { logger } from '../utils/log';
1010
import { Config } from '../utils/config';
1111
import { ResourceBase, updateObjectProperties } from '../utils/resource';
12-
import type { SandboxClient } from './client';
1312

1413
import {
1514
SandboxCreateInput,
@@ -23,6 +22,7 @@ import type { AioSandbox } from './aio-sandbox';
2322
import type { BrowserSandbox } from './browser-sandbox';
2423
import type { CodeInterpreterSandbox } from './code-interpreter-sandbox';
2524
import type { CustomSandbox } from './custom-sandbox';
25+
import type { SandboxClient } from './client';
2626

2727
/**
2828
* Base Sandbox resource class
@@ -122,12 +122,12 @@ export class Sandbox extends ResourceBase implements SandboxData {
122122
);
123123
}
124124

125-
private static getClient() {
125+
private static getClient(): SandboxClient {
126126
// lazy-require to avoid circular runtime import between sandbox <-> client
127127
// keep this dynamic require so module initialization order doesn't break
128128
// eslint-disable-next-line @typescript-eslint/no-var-requires
129-
const { SandboxClient: SandboxClientCtor } = require('./client');
130-
return new SandboxClientCtor();
129+
const { SandboxClient } = require('./client') as { SandboxClient: new () => SandboxClient };
130+
return new SandboxClient();
131131
}
132132

133133
/**
@@ -191,7 +191,12 @@ export class Sandbox extends ResourceBase implements SandboxData {
191191
arg1: { id: string; config?: Config } | string,
192192
arg2?: Config,
193193
): Promise<Sandbox> {
194-
return await Sandbox.getClient().deleteSandbox(arg1 as any, arg2);
194+
if (typeof arg1 === 'string') {
195+
// Legacy API: delete(id, config?)
196+
return await Sandbox.getClient().deleteSandbox(arg1, arg2);
197+
}
198+
// New API: delete({ id, config })
199+
return await Sandbox.getClient().deleteSandbox(arg1);
195200
}
196201

197202
/**
@@ -204,7 +209,12 @@ export class Sandbox extends ResourceBase implements SandboxData {
204209
arg1: { id: string; config?: Config } | string,
205210
arg2?: Config,
206211
): Promise<Sandbox> {
207-
return await Sandbox.getClient().stopSandbox(arg1 as any, arg2);
212+
if (typeof arg1 === 'string') {
213+
// Legacy API: stop(id, config?)
214+
return await Sandbox.getClient().stopSandbox(arg1, arg2);
215+
}
216+
// New API: stop({ id, config })
217+
return await Sandbox.getClient().stopSandbox(arg1);
208218
}
209219

210220
/**
@@ -330,7 +340,25 @@ export class Sandbox extends ResourceBase implements SandboxData {
330340
arg1?: SandboxListInput | { input?: SandboxListInput; config?: Config },
331341
arg2?: Config,
332342
): Promise<Sandbox[]> {
333-
return await Sandbox.getClient().listSandboxes(arg1 as any, arg2);
343+
// Check if using legacy API (arg1 is input object with list params)
344+
if (
345+
arg2 !== undefined ||
346+
(arg1 &&
347+
('maxResults' in arg1 ||
348+
'nextToken' in arg1 ||
349+
'status' in arg1 ||
350+
'templateName' in arg1))
351+
) {
352+
// Legacy API: list(input, config?)
353+
return await Sandbox.getClient().listSandboxes(
354+
arg1 as SandboxListInput,
355+
arg2,
356+
);
357+
}
358+
// New API: list({ input, config }) or list()
359+
return await Sandbox.getClient().listSandboxes(
360+
arg1 as { input?: SandboxListInput; config?: Config },
361+
);
334362
}
335363

336364
get = async (params?: { config?: Config }) => {

tests/unittests/sandbox/sandbox.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ describe('Sandbox', () => {
188188
expect(result.sandboxId).toBe('sandbox-123');
189189
expect(mockClientDeleteSandbox).toHaveBeenCalledWith({
190190
id: 'sandbox-123',
191-
config: undefined,
192-
}, undefined);
191+
});
193192
});
194193

195194
it('should throw ClientError when deletion fails', async () => {
@@ -216,8 +215,7 @@ describe('Sandbox', () => {
216215
expect(result.sandboxId).toBe('sandbox-123');
217216
expect(mockClientStopSandbox).toHaveBeenCalledWith({
218217
id: 'sandbox-123',
219-
config: undefined,
220-
}, undefined);
218+
});
221219
});
222220

223221
it('should throw ClientError when stop fails', async () => {

0 commit comments

Comments
 (0)