Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Command classBuilder #1118

Merged
merged 5 commits into from
Dec 22, 2023
Merged

feat: Command classBuilder #1118

merged 5 commits into from
Dec 22, 2023

Conversation

kuhe
Copy link
Contributor

@kuhe kuhe commented Dec 18, 2023

Creates a builder-style helper for concrete Command classes to reduce their boilerplate.
The builder creates Command Classes, not Command instances.

Example diff:

import { EndpointParameterInstructions, getEndpointPlugin } from "@smithy/middleware-endpoint";
import { getSerdePlugin } from "@smithy/middleware-serde";
import { HttpRequest as __HttpRequest, HttpResponse as __HttpResponse } from "@smithy/protocol-http";
import { Command as $Command } from "@smithy/smithy-client";
import {
  FinalizeHandlerArguments,
  Handler,
  HandlerExecutionContext,
  HttpHandlerOptions as __HttpHandlerOptions,
  MetadataBearer as __MetadataBearer,
  MiddlewareStack,
  SerdeContext as __SerdeContext,
  SMITHY_CONTEXT_KEY,
} from "@smithy/types";

import { ListBucketsOutput } from "../models/models_0";
import { de_ListBucketsCommand, se_ListBucketsCommand } from "../protocols/Aws_restXml";
import { S3ClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../S3Client";

export class ListBucketsCommand extends $Command<
  ListBucketsCommandInput,
  ListBucketsCommandOutput,
  S3ClientResolvedConfig
> {
  public static getEndpointParameterInstructions(): EndpointParameterInstructions {
    return {
      ForcePathStyle: { type: "clientContextParams", name: "forcePathStyle" },
      UseArnRegion: { type: "clientContextParams", name: "useArnRegion" },
      DisableMultiRegionAccessPoints: { type: "clientContextParams", name: "disableMultiregionAccessPoints" },
      Accelerate: { type: "clientContextParams", name: "useAccelerateEndpoint" },
      DisableS3ExpressSessionAuth: { type: "clientContextParams", name: "disableS3ExpressSessionAuth" },
      UseGlobalEndpoint: { type: "builtInParams", name: "useGlobalEndpoint" },
      UseFIPS: { type: "builtInParams", name: "useFipsEndpoint" },
      Endpoint: { type: "builtInParams", name: "endpoint" },
      Region: { type: "builtInParams", name: "region" },
      UseDualStack: { type: "builtInParams", name: "useDualstackEndpoint" },
    };
  }
  constructor(readonly input: ListBucketsCommandInput) {
    super();
  }
  resolveMiddleware(
    clientStack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>,
    configuration: S3ClientResolvedConfig,
    options?: __HttpHandlerOptions
  ): Handler<ListBucketsCommandInput, ListBucketsCommandOutput> {
    this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));
    this.middlewareStack.use(getEndpointPlugin(configuration, ListBucketsCommand.getEndpointParameterInstructions()));

    const stack = clientStack.concat(this.middlewareStack);

    const { logger } = configuration;
    const clientName = "S3Client";
    const commandName = "ListBucketsCommand";
    const handlerExecutionContext: HandlerExecutionContext = {
      logger,
      clientName,
      commandName,
      inputFilterSensitiveLog: (_: any) => _,
      outputFilterSensitiveLog: (_: any) => _,
      [SMITHY_CONTEXT_KEY]: {
        service: "AmazonS3",
        operation: "ListBuckets",
      },
    };
    const { requestHandler } = configuration;
    return stack.resolve(
      (request: FinalizeHandlerArguments<any>) =>
        requestHandler.handle(request.request as __HttpRequest, options || {}),
      handlerExecutionContext
    );
  }
  private serialize(input: ListBucketsCommandInput, context: __SerdeContext): Promise<__HttpRequest> {
    return se_ListBucketsCommand(input, context);
  }
  private deserialize(output: __HttpResponse, context: __SerdeContext): Promise<ListBucketsCommandOutput> {
    return de_ListBucketsCommand(output, context);
  }
}

becomes

import { getEndpointPlugin } from "@smithy/middleware-endpoint";
import { getSerdePlugin } from "@smithy/middleware-serde";
import { Command as $Command } from "@smithy/smithy-client";
import { MetadataBearer as __MetadataBearer } from "@smithy/types";

import { commonParams } from "../endpoint/commonParams";
import { ListBucketsOutput } from "../models/models_0";
import { de_ListBucketsCommand, se_ListBucketsCommand } from "../protocols/Aws_restXml";
import { S3ClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../S3Client";

export class ListBucketsCommand extends $Command
  .classBuilder<
    ListBucketsCommandInput,
    ListBucketsCommandOutput,
    S3ClientResolvedConfig,
    ServiceInputTypes,
    ServiceOutputTypes
  >()
  .ep({
    ...commonParams,
  })
  .m(function (this: any /*Command*/, Command: any /*static*/, config: S3ClientResolvedConfig) {
    return [
      getSerdePlugin(config, this.serialize, this.deserialize),
      getEndpointPlugin(config, Command.getEndpointParameterInstructions()),
    ];
  })
  .s("AmazonS3", "ListBuckets", {})
  .n("S3Client", "ListBucketsCommand")
  .f(void 0, void 0)
  .ser(se_ListBucketsCommand)
  .de(de_ListBucketsCommand)
  .build() {}

example of potential impact:
AWS SDK EC2 client dist-js unpacked size: 4.9MB to 4.2MB

@kuhe kuhe requested review from a team as code owners December 18, 2023 20:48
@kuhe kuhe requested a review from gosar December 18, 2023 20:48
@kuhe kuhe marked this pull request as draft December 18, 2023 20:59
@kuhe kuhe force-pushed the feat/commands-ts branch 2 times, most recently from 679f804 to 9edffba Compare December 19, 2023 16:35
@kuhe kuhe mentioned this pull request Dec 19, 2023
2 tasks
@kuhe kuhe marked this pull request as ready for review December 19, 2023 17:27
@kuhe kuhe force-pushed the feat/commands-ts branch 2 times, most recently from 4303b38 to a8b0d25 Compare December 21, 2023 19:07
@kuhe kuhe changed the title feat: resolve method helper for Command feat: Command classBuilder Dec 21, 2023
@kuhe kuhe merged commit 164f3bb into smithy-lang:main Dec 22, 2023
7 checks passed
@kuhe kuhe deleted the feat/commands-ts branch December 22, 2023 15:01
trivikr pushed a commit to trivikr/smithy-typescript that referenced this pull request Dec 27, 2023
* feat: add Command ClassBuilder

* fix: add dependency check script

* add changeset

* rename script file

* fix yarn lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants