Skip to content

Commit 5528d09

Browse files
committed
generate example for using bare-bone client and command API
also makes filterSensitive internal
1 parent 6835798 commit 5528d09

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private void generateClientCommand() {
105105
writer.addImport("MiddlewareStack", "MiddlewareStack", "@aws-sdk/types");
106106

107107
String name = symbol.getName();
108-
writer.writeShapeDocs(operation);
108+
writer.writeShapeDocs(operation, shapeDoc -> shapeDoc + "\n" + getCommandExample(serviceSymbol.getName(),
109+
configType, name, inputType.getName(), outputType.getName()));
109110
writer.openBlock("export class $L extends $$Command<$T, $T, $L> {", "}", name, inputType, outputType,
110111
configType, () -> {
111112

@@ -128,6 +129,21 @@ private void generateClientCommand() {
128129
});
129130
}
130131

132+
private String getCommandExample(String serviceName, String configName, String commandName, String commandInput,
133+
String commandOutput) {
134+
return "@example\n"
135+
+ "User a bare-bone client and the command you need to make an API call.\n"
136+
+ "```javascript\n"
137+
+ String.format("const client = new %s(config);%n", serviceName)
138+
+ String.format("const command = new %s(input);%n", commandName)
139+
+ "const response = await client.send(command);\n"
140+
+ "```\n"
141+
+ "\n"
142+
+ String.format("@see {@link %s} for command's `input` shape.%n", commandInput)
143+
+ String.format("@see {@link %s} for command's `response` shape.%n", commandOutput)
144+
+ String.format("@see {@link %s | config} for command's `input` shape.%n", configName);
145+
}
146+
131147
private void generateCommandConstructor() {
132148
writer.openBlock("constructor(readonly input: $T) {", "}", inputType, () -> {
133149
// The constructor can be intercepted and changed.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.StringJoiner;
2525
import java.util.function.BiFunction;
26+
import java.util.function.UnaryOperator;
2627
import java.util.logging.Logger;
2728
import software.amazon.smithy.codegen.core.CodegenException;
2829
import software.amazon.smithy.codegen.core.Symbol;
@@ -208,15 +209,17 @@ public TypeScriptWriter writeDocs(String docs) {
208209
}
209210

210211
/**
211-
* Writes shape documentation comments if docs are present.
212+
* Modifies and writes shape documentation comments if docs are present.
212213
*
213214
* @param shape Shape to write the documentation of.
215+
* @param preprocessor UnaryOperator that takes documentation and returns modified one.
214216
* @return Returns true if docs were written.
215217
*/
216-
boolean writeShapeDocs(Shape shape) {
218+
boolean writeShapeDocs(Shape shape, UnaryOperator<String> preprocessor) {
217219
return shape.getTrait(DocumentationTrait.class)
218220
.map(DocumentationTrait::getValue)
219221
.map(docs -> {
222+
docs = preprocessor.apply(docs);
220223
if (shape.getTrait(DeprecatedTrait.class).isPresent()) {
221224
docs = "@deprecated\n\n" + docs;
222225
}
@@ -225,6 +228,16 @@ boolean writeShapeDocs(Shape shape) {
225228
}).orElse(false);
226229
}
227230

231+
/**
232+
* Writes shape documentation comments if docs are present.
233+
*
234+
* @param shape Shape to write the documentation of.
235+
* @return Returns true if docs were written.
236+
*/
237+
boolean writeShapeDocs(Shape shape) {
238+
return writeShapeDocs(shape, (docs) -> docs);
239+
}
240+
228241
/**
229242
* Writes member shape documentation comments if docs are present.
230243
*

0 commit comments

Comments
 (0)