Skip to content

Commit dcf3f68

Browse files
committed
refactor: add stronger typing
1 parent c6e3b5f commit dcf3f68

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

packages/core/src/awsService/apprunner/wizards/imageRepositoryWizard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function createEcrRole(client: IamClient): Promise<IamRole> {
5959
AssumeRolePolicyDocument: JSON.stringify(policy),
6060
})
6161
.then((resp) => {
62-
const role = resp.Role! as IamRole
62+
const role = resp.Role
6363
return client.attachRolePolicy({ RoleName: role.RoleName, PolicyArn: ecrPolicy }).then(() => role)
6464
})
6565
}

packages/core/src/shared/clients/clientWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export abstract class ClientWrapper<C extends AwsClient> implements vscode.Dispo
2828
: globals.sdkClientBuilderV3.getAwsService(args)
2929
}
3030

31-
protected async makeRequest<CommandInput extends object, Command extends AwsCommand>(
31+
protected async makeRequest<CommandInput extends object, CommandOutput extends object, Command extends AwsCommand>(
3232
command: new (o: CommandInput) => Command,
3333
commandOptions: CommandInput
34-
) {
34+
): Promise<CommandOutput> {
3535
return await this.getClient().send(new command(commandOptions))
3636
}
3737

packages/core/src/shared/clients/ec2.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
paginateDescribeIamInstanceProfileAssociations,
2323
IamInstanceProfile,
2424
GetConsoleOutputCommand,
25+
RebootInstancesCommandOutput,
26+
GetConsoleOutputCommandOutput,
2527
} from '@aws-sdk/client-ec2'
2628
import { Timeout } from '../utilities/timeoutUtils'
2729
import { showMessageWithCancel } from '../utilities/messages'
@@ -191,7 +193,7 @@ export class Ec2Client extends ClientWrapper<EC2Client> {
191193
}
192194
}
193195

194-
public async rebootInstance(instanceId: string): Promise<void> {
196+
public async rebootInstance(instanceId: string): Promise<RebootInstancesCommandOutput> {
195197
return await this.makeRequest(RebootInstancesCommand, { InstanceIds: [instanceId] })
196198
}
197199

@@ -233,7 +235,10 @@ export class Ec2Client extends ClientWrapper<EC2Client> {
233235
}
234236

235237
public async getConsoleOutput(instanceId: string, latest: boolean): Promise<SafeEc2GetConsoleOutputResult> {
236-
const response = await this.makeRequest(GetConsoleOutputCommand, { InstanceId: instanceId, Latest: latest })
238+
const response: GetConsoleOutputCommandOutput = await this.makeRequest(GetConsoleOutputCommand, {
239+
InstanceId: instanceId,
240+
Latest: latest,
241+
})
237242

238243
return {
239244
...response,

packages/core/src/shared/clients/iam.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
AttachRolePolicyCommand,
99
AttachRolePolicyRequest,
1010
CreateRoleCommand,
11+
CreateRoleCommandOutput,
1112
CreateRoleRequest,
1213
CreateRoleResponse,
1314
EvaluationResult,
@@ -18,6 +19,7 @@ import {
1819
paginateListAttachedRolePolicies,
1920
paginateListRoles,
2021
PutRolePolicyCommand,
22+
PutRolePolicyCommandOutput,
2123
Role,
2224
SimulatePolicyResponse,
2325
SimulatePrincipalPolicyCommand,
@@ -32,6 +34,10 @@ export interface IamRole extends Role {
3234
Arn: string
3335
}
3436

37+
export interface IamCreateRoleResponse extends CreateRoleResponse {
38+
Role: IamRole
39+
}
40+
3541
export class IamClient extends ClientWrapper<IAMClient> {
3642
public constructor(public override readonly regionCode: string) {
3743
super(regionCode, IAMClient)
@@ -48,8 +54,12 @@ export class IamClient extends ClientWrapper<IAMClient> {
4854
return this.getRoles(request).flatten().promise()
4955
}
5056

51-
public async createRole(request: CreateRoleRequest): Promise<CreateRoleResponse> {
52-
return await this.makeRequest(CreateRoleCommand, request)
57+
public async createRole(request: CreateRoleRequest): Promise<IamCreateRoleResponse> {
58+
const response: CreateRoleCommandOutput = await this.makeRequest(CreateRoleCommand, request)
59+
if (!response.Role || !hasRequiredFields(response.Role)) {
60+
throw new ToolkitError('Failed to create IAM role')
61+
}
62+
return response as IamCreateRoleResponse // Safe to assume by check above.
5363
}
5464

5565
public async attachRolePolicy(request: AttachRolePolicyRequest): Promise<AttachRolePolicyCommand> {
@@ -108,7 +118,11 @@ export class IamClient extends ClientWrapper<IAMClient> {
108118
return response.InstanceProfile.Roles[0]
109119
}
110120

111-
public async putRolePolicy(roleArn: string, policyName: string, policyDocument: string): Promise<void> {
121+
public async putRolePolicy(
122+
roleArn: string,
123+
policyName: string,
124+
policyDocument: string
125+
): Promise<PutRolePolicyCommandOutput> {
112126
return await this.makeRequest(PutRolePolicyCommand, {
113127
RoleName: this.getFriendlyName(roleArn),
114128
PolicyName: policyName,

0 commit comments

Comments
 (0)