39
39
import software .amazon .smithy .model .Model ;
40
40
import software .amazon .smithy .model .knowledge .OperationIndex ;
41
41
import software .amazon .smithy .model .knowledge .TopDownIndex ;
42
+ import software .amazon .smithy .model .node .ObjectNode ;
42
43
import software .amazon .smithy .model .shapes .MemberShape ;
43
44
import software .amazon .smithy .model .shapes .OperationShape ;
44
45
import software .amazon .smithy .model .shapes .ServiceShape ;
48
49
import software .amazon .smithy .model .traits .DeprecatedTrait ;
49
50
import software .amazon .smithy .model .traits .DocumentationTrait ;
50
51
import software .amazon .smithy .model .traits .ErrorTrait ;
52
+ import software .amazon .smithy .model .traits .ExamplesTrait ;
51
53
import software .amazon .smithy .model .traits .InternalTrait ;
52
54
import software .amazon .smithy .rulesengine .traits .EndpointRuleSetTrait ;
55
+ import software .amazon .smithy .typescript .codegen .documentation .DocumentationExampleGenerator ;
53
56
import software .amazon .smithy .typescript .codegen .documentation .StructureExampleGenerator ;
54
57
import software .amazon .smithy .typescript .codegen .endpointsV2 .RuleSetParameterFinder ;
55
58
import software .amazon .smithy .typescript .codegen .integration .ProtocolGenerator ;
@@ -133,11 +136,13 @@ private void generateClientCommand() {
133
136
String name = symbol .getName ();
134
137
135
138
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 ));
141
146
142
147
boolean operationHasDocumentation = operation .hasTrait (DocumentationTrait .class );
143
148
@@ -244,10 +249,12 @@ private void generateClientCommand() {
244
249
writer .write ("}" ); // class close bracket.
245
250
}
246
251
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
+ ) {
249
256
String packageName = settings .getPackageName ();
250
- return "@example\n "
257
+ String exampleDoc = "@example\n "
251
258
+ "Use a bare-bones client and the command you need to make an API call.\n "
252
259
+ "```javascript\n "
253
260
+ 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
270
277
+ String .format ("@see {@link %s} for command's `input` shape.%n" , commandInput )
271
278
+ String .format ("@see {@link %s} for command's `response` shape.%n" , commandOutput )
272
279
+ 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 ;
273
320
}
274
321
275
322
private String getThrownExceptions () {
0 commit comments