Skip to content

Commit c00618f

Browse files
committed
refactor(sandbox): migrate API methods to object-based parameters with backward compatibility
This commit refactors the sandbox client and template classes to use object-based API parameters instead of positional arguments. The changes maintain backward compatibility by supporting both the new object-based API and legacy parameter formats, while adding deprecation warnings for the old usage patterns. The client methods now accept parameters as objects ({ input, config }) rather than positional arguments (input, config), improving type safety and parameter clarity. The template class numeric field normalization was also improved to properly handle invalid numeric values by setting them to undefined. Additionally, the test suite was updated to mock the SandboxClient directly instead of the lower-level control API, providing better test isolation and more accurate testing of the actual API usage patterns. refactor(sandbox): 将API方法迁移到基于对象的参数并保持向后兼容性 此提交重构了sandbox客户端和模板类,使用基于对象的API参数而不是位置参数。这些更改通过支持新的基于对象的API和旧的参数格式来保持向后兼容性,同时为旧的使用模式添加弃用警告。 客户端方法现在接受参数作为对象({ input, config})而不是位置参数(input, config),提高了类型安全性和参数清晰度。模板类数值字段规范化也得到了改进,通过将无效的数值设置为undefined来正确处理它们。 此外,测试套件已更新为直接模拟SandboxClient而不是底层控制API,提供更好的测试隔离和更准确的实际API使用模式测试。 Change-Id: I8fd2bad5dac5c898328fe64d6d7f40b82ee00fe4 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent d97ab7a commit c00618f

5 files changed

Lines changed: 468 additions & 125 deletions

File tree

src/sandbox/client.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,8 @@ export class SandboxClient {
340340
let templateType: TemplateType | undefined;
341341
let config: Config | undefined;
342342

343-
if ('templateName' in arg1) {
344-
logger.warn(
345-
'Deprecated: createSandbox(input, config) is deprecated. Use createSandbox({ input, config }) instead.',
346-
);
347-
input = arg1 as SandboxCreateInput;
348-
config = arg2;
349-
} else {
343+
if ('input' in arg1) {
344+
// New API: createSandbox({ input, templateType?, config? })
350345
const params = arg1 as {
351346
input: SandboxCreateInput;
352347
templateType?: TemplateType;
@@ -355,6 +350,13 @@ export class SandboxClient {
355350
input = params.input;
356351
templateType = params.templateType;
357352
config = params.config;
353+
} else {
354+
// Legacy API: createSandbox(input, config?)
355+
logger.warn(
356+
'Deprecated: createSandbox(input, config) is deprecated. Use createSandbox({ input, config }) instead.',
357+
);
358+
input = arg1 as SandboxCreateInput;
359+
config = arg2;
358360
}
359361

360362
const cfg = Config.withConfigs(this.config, config);
@@ -483,19 +485,24 @@ export class SandboxClient {
483485
(params: { id: string; config?: Config }): Promise<Sandbox>;
484486
/** @deprecated Use stopSandbox({ id, config }) instead. */
485487
(id: string, config?: Config): Promise<Sandbox>;
486-
} = async (...args: any[]): Promise<Sandbox> => {
487-
let params: { id: string; config?: Config } = args?.[0];
488+
} = async (
489+
arg1: { id: string; config?: Config } | string,
490+
arg2?: Config,
491+
): Promise<Sandbox> => {
492+
let id: string;
493+
let config: Config | undefined;
488494

489-
if (typeof args[0] === 'string') {
495+
if (typeof arg1 === 'string') {
490496
logger.warn(
491497
'Deprecated: stopSandbox(id, config) is deprecated. Use stopSandbox({ id, config }) instead.',
492498
);
493-
494-
params = { id: args[0], config: args[1] };
499+
id = arg1;
500+
config = arg2;
501+
} else {
502+
id = arg1.id;
503+
config = arg1.config;
495504
}
496505

497-
const { id, config } = params;
498-
499506
const cfg = Config.withConfigs(this.config, config);
500507

501508
try {

src/sandbox/sandbox.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ export class Sandbox extends ResourceBase implements SandboxData {
175175
| SandboxCreateInput,
176176
arg2?: Config,
177177
): Promise<Sandbox> {
178-
return await Sandbox.getClient().createSandbox(arg1 as any, arg2);
178+
if (typeof arg1 === 'object' && arg1 !== null && 'input' in arg1) {
179+
// New API: create({ input, templateType?, config? })
180+
return await Sandbox.getClient().createSandbox(arg1);
181+
}
182+
// Legacy API: create(input, config?)
183+
logger.warn(
184+
'Deprecated: Sandbox.create(input, config) is deprecated. Use Sandbox.create({ input, config }) instead.',
185+
);
186+
return await Sandbox.getClient().createSandbox(arg1 as SandboxCreateInput, arg2);
179187
}
180188

181189
/**
@@ -193,6 +201,9 @@ export class Sandbox extends ResourceBase implements SandboxData {
193201
): Promise<Sandbox> {
194202
if (typeof arg1 === 'string') {
195203
// Legacy API: delete(id, config?)
204+
logger.warn(
205+
'Sandbox.delete(id, config) is deprecated. Use Sandbox.delete({ id, config }) instead.',
206+
);
196207
return await Sandbox.getClient().deleteSandbox(arg1, arg2);
197208
}
198209
// New API: delete({ id, config })
@@ -211,6 +222,9 @@ export class Sandbox extends ResourceBase implements SandboxData {
211222
): Promise<Sandbox> {
212223
if (typeof arg1 === 'string') {
213224
// Legacy API: stop(id, config?)
225+
logger.warn(
226+
'Sandbox.stop(id, config) is deprecated. Use Sandbox.stop({ id, config }) instead.',
227+
);
214228
return await Sandbox.getClient().stopSandbox(arg1, arg2);
215229
}
216230
// New API: stop({ id, config })
@@ -347,9 +361,13 @@ export class Sandbox extends ResourceBase implements SandboxData {
347361
('maxResults' in arg1 ||
348362
'nextToken' in arg1 ||
349363
'status' in arg1 ||
350-
'templateName' in arg1))
364+
'templateName' in arg1 ||
365+
'templateType' in arg1))
351366
) {
352367
// Legacy API: list(input, config?)
368+
logger.warn(
369+
'Deprecated: Sandbox.list(input, config) is deprecated. Use Sandbox.list({ input, config }) instead.',
370+
);
353371
return await Sandbox.getClient().listSandboxes(
354372
arg1 as SandboxListInput,
355373
arg2,

src/sandbox/template.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,15 @@ export class Template extends ResourceBase implements TemplateData {
251251
return undefined;
252252
};
253253

254-
this.sandboxIdleTimeoutInSeconds =
255-
toNumber(this.sandboxIdleTimeoutInSeconds) ??
256-
this.sandboxIdleTimeoutInSeconds;
257-
this.sandboxTtlInSeconds =
258-
toNumber(this.sandboxTtlInSeconds) ?? this.sandboxTtlInSeconds;
259-
this.shareConcurrencyLimitPerSandbox =
260-
toNumber(this.shareConcurrencyLimitPerSandbox) ??
261-
this.shareConcurrencyLimitPerSandbox;
262-
this.cpu = toNumber(this.cpu) ?? this.cpu;
263-
this.memory = toNumber(this.memory) ?? this.memory;
264-
this.diskSize = toNumber(this.diskSize) ?? this.diskSize;
254+
this.sandboxIdleTimeoutInSeconds = toNumber(
255+
this.sandboxIdleTimeoutInSeconds,
256+
);
257+
this.sandboxTtlInSeconds = toNumber(this.sandboxTtlInSeconds);
258+
this.shareConcurrencyLimitPerSandbox = toNumber(
259+
this.shareConcurrencyLimitPerSandbox,
260+
);
261+
this.cpu = toNumber(this.cpu);
262+
this.memory = toNumber(this.memory);
263+
this.diskSize = toNumber(this.diskSize);
265264
}
266265
}

0 commit comments

Comments
 (0)