Skip to content

Commit 201403f

Browse files
authored
Generate examples for API call with bare-bones client and command (#325)
* mark filterSensitiveLog as internal * generate example for using bare-bone client and command API also makes filterSensitive internal * fix typo
1 parent c19843c commit 201403f

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

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

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

109109
String name = symbol.getName();
110-
writer.writeShapeDocs(operation);
110+
writer.writeShapeDocs(operation, shapeDoc -> shapeDoc + "\n" + getCommandExample(serviceSymbol.getName(),
111+
configType, name, inputType.getName(), outputType.getName()));
111112
writer.openBlock("export class $L extends $$Command<$T, $T, $L> {", "}", name, inputType, outputType,
112113
configType, () -> {
113114

@@ -130,6 +131,26 @@ private void generateClientCommand() {
130131
});
131132
}
132133

134+
private String getCommandExample(String serviceName, String configName, String commandName, String commandInput,
135+
String commandOutput) {
136+
String packageName = settings.getPackageName();
137+
return "@example\n"
138+
+ "Use a bare-bones client and the command you need to make an API call.\n"
139+
+ "```javascript\n"
140+
+ String.format("import { %s, %s } from \"%s\"; // ES Modules import%n", serviceName, commandName,
141+
packageName)
142+
+ String.format("// const { %s, %s } = require(\"%s\"); // CommonJS import%n", serviceName, commandName,
143+
packageName)
144+
+ String.format("const client = new %s(config);%n", serviceName)
145+
+ String.format("const command = new %s(input);%n", commandName)
146+
+ "const response = await client.send(command);\n"
147+
+ "```\n"
148+
+ "\n"
149+
+ String.format("@see {@link %s} for command's `input` shape.%n", commandInput)
150+
+ String.format("@see {@link %s} for command's `response` shape.%n", commandOutput)
151+
+ String.format("@see {@link %s | config} for command's `input` shape.%n", configName);
152+
}
153+
133154
private void generateCommandConstructor() {
134155
writer.openBlock("constructor(readonly input: $T) {", "}", inputType, () -> {
135156
// The constructor can be intercepted and changed.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ private void renderStructureNamespace(StructuredMemberWriter structuredMemberWri
199199
Symbol symbol = symbolProvider.toSymbol(shape);
200200
writer.openBlock("export namespace $L {", "}", symbol.getName(), () -> {
201201
String objectParam = "obj";
202+
writer.writeDocs("@internal");
202203
writer.openBlock("export const filterSensitiveLog = ($L: $L): any => ({", "})",
203204
objectParam, symbol.getName(),
204205
() -> {

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;
@@ -210,15 +211,17 @@ public TypeScriptWriter writeDocs(String docs) {
210211
}
211212

212213
/**
213-
* Writes shape documentation comments if docs are present.
214+
* Modifies and writes shape documentation comments if docs are present.
214215
*
215216
* @param shape Shape to write the documentation of.
217+
* @param preprocessor UnaryOperator that takes documentation and returns modified one.
216218
* @return Returns true if docs were written.
217219
*/
218-
boolean writeShapeDocs(Shape shape) {
220+
boolean writeShapeDocs(Shape shape, UnaryOperator<String> preprocessor) {
219221
return shape.getTrait(DocumentationTrait.class)
220222
.map(DocumentationTrait::getValue)
221223
.map(docs -> {
224+
docs = preprocessor.apply(docs);
222225
if (shape.getTrait(DeprecatedTrait.class).isPresent()) {
223226
docs = "@deprecated\n\n" + docs;
224227
}
@@ -227,6 +230,16 @@ boolean writeShapeDocs(Shape shape) {
227230
}).orElse(false);
228231
}
229232

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ private void writeVisitorFunction() {
242242

243243
private void writeFilterSensitiveLog() {
244244
String objectParam = "obj";
245+
writer.writeDocs("@internal");
245246
writer.openBlock("export const filterSensitiveLog = ($L: $L): any => {", "}",
246247
objectParam, symbol.getName(),
247248
() -> {

0 commit comments

Comments
 (0)