Skip to content

Commit 00fe434

Browse files
committed
docs: WIP add doc examples to operations
1 parent 38da900 commit 00fe434

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import software.amazon.smithy.model.traits.DocumentationTrait;
4949
import software.amazon.smithy.model.traits.ErrorTrait;
5050
import software.amazon.smithy.model.traits.InternalTrait;
51+
import software.amazon.smithy.model.traits.ExamplesTrait;
5152
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
5253
import software.amazon.smithy.typescript.codegen.documentation.StructureExampleGenerator;
5354
import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder;
@@ -199,10 +200,12 @@ private void generateClientCommand() {
199200
writer.write("}"); // class close bracket.
200201
}
201202

202-
private String getCommandExample(String serviceName, String configName, String commandName, String commandInput,
203-
String commandOutput) {
203+
private String getCommandExample(
204+
String serviceName, String configName, String commandName,
205+
String commandInput, String commandOutput
206+
) {
204207
String packageName = settings.getPackageName();
205-
return "@example\n"
208+
String exampleDoc = "@example\n"
206209
+ "Use a bare-bones client and the command you need to make an API call.\n"
207210
+ "```javascript\n"
208211
+ String.format("import { %s, %s } from \"%s\"; // ES Modules import%n", serviceName, commandName,
@@ -225,6 +228,13 @@ private String getCommandExample(String serviceName, String configName, String c
225228
+ String.format("@see {@link %s} for command's `input` shape.%n", commandInput)
226229
+ String.format("@see {@link %s} for command's `response` shape.%n", commandOutput)
227230
+ String.format("@see {@link %s | config} for %s's `config` shape.%n", configName, serviceName);
231+
232+
if (operation.getTrait(ExamplesTrait.class).isPresent()) {
233+
List<ExamplesTrait.Example> examples = operation.getTrait(ExamplesTrait.class).get().getExamples();
234+
exampleDoc += getOperationExample(examples);
235+
}
236+
237+
return exampleDoc;
228238
}
229239

230240
private String getThrownExceptions() {
@@ -252,6 +262,21 @@ private String getThrownExceptions() {
252262
return buffer.toString();
253263
}
254264

265+
private String getOperationExample(List<ExamplesTrait.Example> examples) {
266+
StringBuilder buffer = new StringBuilder();
267+
for (ExamplesTrait.Example example : examples) {
268+
buffer
269+
.append("\n")
270+
.append(String.format("@example %s", example.getTitle()))
271+
.append("```javascript")
272+
.append(String.format("/* %s */", example.getDocumentation()))
273+
.append("some code here")
274+
.append("```")
275+
.append("\n");
276+
}
277+
return buffer.toString();
278+
}
279+
255280
private void generateEndpointParameterInstructionProvider() {
256281
if (!service.hasTrait(EndpointRuleSetTrait.class)) {
257282
return;

0 commit comments

Comments
 (0)