3939import software .amazon .smithy .model .Model ;
4040import software .amazon .smithy .model .knowledge .OperationIndex ;
4141import software .amazon .smithy .model .knowledge .TopDownIndex ;
42+ import software .amazon .smithy .model .node .ObjectNode ;
4243import software .amazon .smithy .model .shapes .MemberShape ;
4344import software .amazon .smithy .model .shapes .OperationShape ;
4445import software .amazon .smithy .model .shapes .ServiceShape ;
4849import software .amazon .smithy .model .traits .DeprecatedTrait ;
4950import software .amazon .smithy .model .traits .DocumentationTrait ;
5051import software .amazon .smithy .model .traits .ErrorTrait ;
52+ import software .amazon .smithy .model .traits .ExamplesTrait ;
5153import software .amazon .smithy .model .traits .InternalTrait ;
5254import software .amazon .smithy .rulesengine .traits .EndpointRuleSetTrait ;
55+ import software .amazon .smithy .typescript .codegen .documentation .DocumentationExampleGenerator ;
5356import software .amazon .smithy .typescript .codegen .documentation .StructureExampleGenerator ;
5457import software .amazon .smithy .typescript .codegen .endpointsV2 .RuleSetParameterFinder ;
5558import software .amazon .smithy .typescript .codegen .integration .ProtocolGenerator ;
@@ -133,11 +136,13 @@ private void generateClientCommand() {
133136 String name = symbol .getName ();
134137
135138 StringBuilder additionalDocs = new StringBuilder ()
136- .append ("\n " )
137- .append (getCommandExample (
138- serviceSymbol .getName (), configType , name , inputType .getName (), outputType .getName ()))
139- .append ("\n " )
140- .append (getThrownExceptions ());
139+ .append ("\n " )
140+ .append (getCommandExample (
141+ serviceSymbol .getName (), configType , name , inputType .getName (), outputType .getName ()))
142+ .append ("\n " )
143+ .append (getThrownExceptions ())
144+ .append ("\n " )
145+ .append (getCuratedExamples (name ));
141146
142147 boolean operationHasDocumentation = operation .hasTrait (DocumentationTrait .class );
143148
@@ -244,10 +249,12 @@ private void generateClientCommand() {
244249 writer .write ("}" ); // class close bracket.
245250 }
246251
247- private String getCommandExample (String serviceName , String configName , String commandName , String commandInput ,
248- String commandOutput ) {
252+ private String getCommandExample (
253+ String serviceName , String configName , String commandName ,
254+ String commandInput , String commandOutput
255+ ) {
249256 String packageName = settings .getPackageName ();
250- return "@example\n "
257+ String exampleDoc = "@example\n "
251258 + "Use a bare-bones client and the command you need to make an API call.\n "
252259 + "```javascript\n "
253260 + String .format ("import { %s, %s } from \" %s\" ; // ES Modules import%n" , serviceName , commandName ,
@@ -270,6 +277,46 @@ private String getCommandExample(String serviceName, String configName, String c
270277 + String .format ("@see {@link %s} for command's `input` shape.%n" , commandInput )
271278 + String .format ("@see {@link %s} for command's `response` shape.%n" , commandOutput )
272279 + String .format ("@see {@link %s | config} for %s's `config` shape.%n" , configName , serviceName );
280+
281+ return exampleDoc ;
282+ }
283+
284+ /**
285+ * Handwritten examples from the operation ExamplesTrait.
286+ */
287+ private String getCuratedExamples (String commandName ) {
288+ String exampleDoc = "" ;
289+ if (operation .getTrait (ExamplesTrait .class ).isPresent ()) {
290+ List <ExamplesTrait .Example > examples = operation .getTrait (ExamplesTrait .class ).get ().getExamples ();
291+ StringBuilder buffer = new StringBuilder ();
292+
293+ for (ExamplesTrait .Example example : examples ) {
294+ ObjectNode input = example .getInput ();
295+ Optional <ObjectNode > output = example .getOutput ();
296+ buffer
297+ .append ("\n " )
298+ .append (String .format ("@example %s%n" , example .getTitle ()))
299+ .append ("```javascript\n " )
300+ .append (String .format ("// %s%n" , example .getDocumentation ().orElse ("" )))
301+ .append ("""
302+ const input = %s;
303+ const command = new %s(input);
304+ const response = await client.send(command);
305+ /* response is
306+ %s
307+ */
308+ """ .formatted (
309+ DocumentationExampleGenerator .inputToJavaScriptObject (input ),
310+ commandName ,
311+ DocumentationExampleGenerator .outputToJavaScriptObject (output .orElse (null ))
312+ ))
313+ .append ("```" )
314+ .append ("\n " );
315+ }
316+
317+ exampleDoc += buffer .toString ();
318+ }
319+ return exampleDoc ;
273320 }
274321
275322 private String getThrownExceptions () {
0 commit comments