Skip to content

Commit 5ffe5d2

Browse files
committed
refactor: migrate first api call
1 parent 6c396e1 commit 5ffe5d2

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ export interface AwsCommand<InputType extends object, OutputType extends object>
6161
resolveMiddleware: (stack: any, configuration: any, options: any) => Handler<any, any>
6262
}
6363

64-
export interface AwsCommandOutput {
65-
$metadata: object
66-
}
67-
6864
export interface AwsClientOptions {
6965
credentials: AwsCredentialIdentityProvider
7066
region: string | Provider<string>

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

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ import {
2828
ListSourceRepositoriesItem,
2929
ListSourceRepositoriesItems,
3030
} from 'aws-sdk/clients/codecatalyst'
31-
import { CodeCatalystClient as CodeCatalystSDKClient } from '@aws-sdk/client-codecatalyst'
31+
import {
32+
CodeCatalystClient as CodeCatalystSDKClient,
33+
CreateAccessTokenCommand,
34+
CreateAccessTokenRequest,
35+
CreateAccessTokenResponse,
36+
} from '@aws-sdk/client-codecatalyst'
3237
import { truncateProps } from '../utilities/textUtilities'
3338
import { SsoConnection } from '../../auth/connection'
3439
import { DevSettings } from '../settings'
3540
import { getServiceEnvVarConfig } from '../vscode/env'
36-
import { AwsCommand, AwsCommandOutput } from '../awsClientBuilderV3'
41+
import { AwsCommand } from '../awsClientBuilderV3'
42+
import { ClientWrapper } from './clientWrapper'
3743

3844
export interface CodeCatalystConfig {
3945
readonly region: string
@@ -189,7 +195,7 @@ export async function createClient(
189195
authOptions: AuthOptions = {}
190196
): Promise<CodeCatalystClient> {
191197
const sdkClient = await createCodeCatalystClient(connection, regionCode, endpoint)
192-
const sdkv3Client = await createCodeCatalystClientV3(connection, regionCode, endpoint)
198+
const sdkv3Client = createCodeCatalystClientV3(connection, regionCode, endpoint)
193199
const c = new CodeCatalystClientInternal(connection, sdkClient, sdkv3Client)
194200
try {
195201
await c.verifySession()
@@ -217,7 +223,7 @@ function fixAliasInRequest<
217223
return request
218224
}
219225

220-
class CodeCatalystClientInternal {
226+
class CodeCatalystClientInternal extends ClientWrapper<CodeCatalystSDKClient> {
221227
private userDetails?: UserDetails
222228
private readonly log: logger.Logger
223229

@@ -241,11 +247,12 @@ class CodeCatalystClientInternal {
241247
private readonly sdkClient: CodeCatalyst,
242248
private readonly sdkClientV3: CodeCatalystSDKClient
243249
) {
250+
super(sdkClient.config.region!, CodeCatalystSDKClient)
244251
this.log = logger.getLogger()
245252
}
246253

247-
public get regionCode() {
248-
return this.sdkClient.config.region!
254+
protected override getClient(ignoreCache?: boolean): CodeCatalystSDKClient {
255+
return this.sdkClientV3
249256
}
250257

251258
public get identity(): CodeCatalystClient['identity'] {
@@ -257,25 +264,41 @@ class CodeCatalystClientInternal {
257264
}
258265

259266
private async callV3<
260-
Command extends AwsCommand<CommandInput, CommandOutput>,
261267
CommandInput extends object,
262-
CommandOutput extends AwsCommandOutput,
263-
>(cmd: Command, silent: true, defaultVal: CommandOutput): Promise<CommandOutput>
264-
private async callV3<
268+
CommandOutput extends object,
269+
CommandOptions extends CommandInput,
265270
Command extends AwsCommand<CommandInput, CommandOutput>,
266-
CommandInput extends object,
267-
CommandOutput extends AwsCommandOutput,
268-
>(cmd: Command, silent: false): Promise<CommandOutput>
271+
>(
272+
cmd: new (o: CommandInput) => Command,
273+
commandOptions: CommandOptions,
274+
silent: true,
275+
defaultVal: CommandOutput
276+
): Promise<CommandOutput>
269277
private async callV3<
278+
CommandInput extends object,
279+
CommandOutput extends object,
280+
CommandOptions extends CommandInput,
270281
Command extends AwsCommand<CommandInput, CommandOutput>,
282+
>(cmd: new (o: CommandInput) => Command, commandOptions: CommandOptions, silent: false): Promise<CommandOutput>
283+
private async callV3<
271284
CommandInput extends object,
272-
CommandOutput extends AwsCommandOutput,
273-
>(cmd: Command, silent: boolean, defaultVal?: CommandOutput): Promise<CommandOutput> {
285+
CommandOutput extends object,
286+
CommandOptions extends CommandInput,
287+
Command extends AwsCommand<CommandInput, CommandOutput>,
288+
>(
289+
cmd: new (o: CommandInput) => Command,
290+
commandOptions: CommandOptions,
291+
silent: boolean,
292+
defaultVal?: CommandOutput
293+
): Promise<CommandOutput> {
274294
const bearerToken = (await this.connection.getToken()).accessToken
275295

276296
return new Promise<CommandOutput>(async (resolve, reject) => {
277297
try {
278-
const data = await this.sdkClientV3.send<CommandInput, CommandOutput>(cmd)
298+
const data = await this.makeRequest<CommandInput, CommandOutput, CommandOptions, Command>(
299+
cmd,
300+
commandOptions
301+
)
279302
resolve(data)
280303
} catch (e) {
281304
const error = e as Error
@@ -286,9 +309,9 @@ class CodeCatalystClientInternal {
286309
if (defaultVal === undefined) {
287310
throw Error()
288311
}
289-
return defaultVal
312+
resolve(defaultVal)
290313
} else {
291-
throw new ToolkitError(`CodeCatalyst: request failed with error`, { cause: error })
314+
reject(new ToolkitError(`CodeCatalyst: request failed with error`, { cause: error }))
292315
}
293316
}
294317
})
@@ -390,10 +413,10 @@ class CodeCatalystClientInternal {
390413
* @returns PAT secret
391414
*/
392415
public async createAccessToken(
393-
args: CodeCatalyst.CreateAccessTokenRequest
394-
): Promise<CodeCatalyst.CreateAccessTokenResponse> {
416+
args: CreateAccessTokenRequest
417+
): Promise<RequiredProps<CreateAccessTokenResponse, 'secret'>> {
395418
try {
396-
return this.call(this.sdkClient.createAccessToken(args), false)
419+
return await this.callV3(CreateAccessTokenCommand, args, false)
397420
} catch (e) {
398421
if ((e as Error).name === 'ServiceQuotaExceededException') {
399422
throw new ToolkitError('Access token limit exceeded', { cause: e as Error })

0 commit comments

Comments
 (0)