diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 5c55890a4ad..f3a7456f7d6 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -50,6 +50,7 @@ import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; +import software.amazon.smithy.typescript.codegen.sections.SmithyContextCodeSection; import software.amazon.smithy.typescript.codegen.validation.SensitiveDataFinder; import software.amazon.smithy.utils.OptionalUtils; import software.amazon.smithy.utils.SmithyInternalApi; @@ -359,8 +360,13 @@ private void generateCommandMiddlewareResolver(String configType) { () -> writer.writeInline("(_: any) => _")); }); writer.openBlock("[SMITHY_CONTEXT_KEY]: {", "},", () -> { + writer.pushState(SmithyContextCodeSection.builder() + .service(service) + .operation(operation) + .build()); writer.write("service: $S,", service.toShapeId().getName()); writer.write("operation: $S,", operation.toShapeId().getName()); + writer.popState(); }); }); writer.write("const { requestHandler } = configuration;"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java new file mode 100644 index 00000000000..12aadf7b105 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java @@ -0,0 +1,55 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.sections; + +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyUnstableApi; + +@SmithyUnstableApi +public final class SmithyContextCodeSection implements CodeSection { + private final ServiceShape service; + private final OperationShape operation; + + private SmithyContextCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + operation = SmithyBuilder.requiredState("operation", builder.operation); + } + + public ServiceShape getService() { + return service; + } + + public OperationShape getOperation() { + return operation; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder { + private ServiceShape service; + private OperationShape operation; + + @Override + public SmithyContextCodeSection build() { + return new SmithyContextCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder operation(OperationShape operation) { + this.operation = operation; + return this; + } + } +}