Skip to content

Commit 3571b6c

Browse files
committed
refactor: switch to using service paginator
1 parent 3469164 commit 3571b6c

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
import * as vscode from 'vscode'
66
import globals from '../extensionGlobals'
77
import { AwsClient, AwsClientConstructor, AwsCommand } from '../awsClientBuilderV3'
8-
import { pageableToCollection } from '../utilities/collectionUtils'
8+
import { PaginationConfiguration, Paginator } from '@aws-sdk/types'
99

10+
type SDKPaginator<C, CommandInput extends object, CommandOutput extends object> = (
11+
config: Omit<PaginationConfiguration, 'client'> & { client: C },
12+
input: CommandInput,
13+
...rest: any[]
14+
) => Paginator<CommandOutput>
1015
export abstract class ClientWrapper<C extends AwsClient> implements vscode.Disposable {
1116
protected client?: C
1217

@@ -31,24 +36,21 @@ export abstract class ClientWrapper<C extends AwsClient> implements vscode.Dispo
3136
return await client.send(new command(commandOptions))
3237
}
3338

34-
protected makePaginatedRequest<
39+
protected async makePaginatedRequest<
3540
CommandInput extends object,
3641
CommandOutput extends object,
37-
Command extends AwsCommand,
42+
Output extends object,
3843
>(
39-
command: new (o: CommandInput) => Command,
40-
commandOptions: CommandInput,
41-
collectKey: keyof CommandOutput & string,
42-
nextTokenKey?: keyof CommandOutput & keyof CommandInput & string
43-
) {
44-
const requester = async (req: CommandInput) => await this.makeRequest(command, req)
45-
const response = pageableToCollection(
46-
requester,
47-
commandOptions,
48-
nextTokenKey ?? ('NextToken' as never),
49-
collectKey
50-
)
51-
return response
44+
paginator: SDKPaginator<C, CommandInput, CommandOutput>,
45+
input: CommandInput,
46+
extractPage: (page: CommandOutput) => Output[] | undefined
47+
): Promise<Output[]> {
48+
const p = paginator({ client: await this.getClient() }, input)
49+
const results = []
50+
for await (const page of p) {
51+
results.push(extractPage(page))
52+
}
53+
return results.flat().filter((result) => result !== undefined)
5254
}
5355

5456
public dispose() {

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
TerminateSessionCommand,
1111
TerminateSessionResponse,
1212
StartSessionCommandOutput,
13-
DescribeInstanceInformationCommand,
1413
DescribeInstanceInformationCommandInput,
1514
InstanceInformation,
1615
SendCommandCommand,
1716
SendCommandCommandOutput,
1817
waitUntilCommandExecuted,
1918
SessionState,
20-
DescribeSessionsCommand,
19+
paginateDescribeInstanceInformation,
20+
paginateDescribeSessions,
2121
} from '@aws-sdk/client-ssm'
2222
import { WaiterState } from '@smithy/util-waiter'
2323
import { ToolkitError } from '../errors'
@@ -52,20 +52,15 @@ export class SsmClient extends ClientWrapper<SSMClient> {
5252
}
5353

5454
public async describeInstance(target: string): Promise<InstanceInformation> {
55-
const response = this.makePaginatedRequest(
56-
DescribeInstanceInformationCommand,
57-
{
58-
InstanceInformationFilterList: [
59-
{
60-
key: 'InstanceIds',
61-
valueSet: [target],
62-
},
63-
],
64-
} as DescribeInstanceInformationCommandInput,
65-
'InstanceIds'
66-
)
67-
const resolvedResponse = await response.flatten().flatten().promise()
68-
return resolvedResponse[0]!
55+
return (
56+
await this.makePaginatedRequest(
57+
paginateDescribeInstanceInformation,
58+
{
59+
InstanceInformationFilterList: [{ key: 'InstanceIds', valueSet: [target] }],
60+
} satisfies DescribeInstanceInformationCommandInput,
61+
(page) => page.InstanceInformationList
62+
)
63+
)[0]!
6964
}
7065

7166
public async getTargetPlatformName(target: string): Promise<string> {
@@ -115,6 +110,6 @@ export class SsmClient extends ClientWrapper<SSMClient> {
115110
}
116111

117112
public async describeSessions(state: SessionState) {
118-
return await this.makePaginatedRequest(DescribeSessionsCommand, { State: state }, 'Sessions').promise()
113+
return await this.makePaginatedRequest(paginateDescribeSessions, { State: state }, (page) => page.Sessions)
119114
}
120115
}

0 commit comments

Comments
 (0)