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 38968e2fdd4..345b7022179 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 @@ -49,6 +49,7 @@ import software.amazon.smithy.model.traits.DocumentationTrait; import software.amazon.smithy.model.traits.ErrorTrait; import software.amazon.smithy.model.traits.InternalTrait; +import software.amazon.smithy.model.traits.StreamingTrait; import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.documentation.StructureExampleGenerator; import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder; @@ -260,6 +261,7 @@ private String getCommandExample(String serviceName, String configName, String c model.getShape(operation.getInputShape()).get(), model, false, true)) + String.format("const command = new %s(input);%n", commandName) + "const response = await client.send(command);\n" + + getReadStreamExample() + String.format("%s%n", StructureExampleGenerator.generateStructuralHintDocumentation( model.getShape(operation.getOutputShape()).get(), model, true, false)) @@ -272,6 +274,30 @@ private String getCommandExample(String serviceName, String configName, String c + String.format("@see {@link %s | config} for %s's `config` shape.%n", configName, serviceName); } + private String getReadStreamExample() { + String streamingBlobOutputMemberName = model.expectShape(operation.getOutputShape()) + .asStructureShape().get() + .getAllMembers() + .values() + .stream() + .filter(ms -> { + Shape target = model.expectShape(ms.getTarget()); + return target.isBlobShape() + && (ms.hasTrait(StreamingTrait.class) || target.hasTrait(StreamingTrait.class)); + }) + .findFirst() + .map(MemberShape::getMemberName) + .orElse(""); + + if (!streamingBlobOutputMemberName.isEmpty()) { + return """ + // Read the stream or discard it to free the socket. + const bytes = await response[`%s`].transformToByteArray();\n""" + .formatted(streamingBlobOutputMemberName); + } + return ""; + } + private String getThrownExceptions() { List errors = operation.getErrors(); StringBuilder buffer = new StringBuilder();