38
38
import software .amazon .smithy .model .Model ;
39
39
import software .amazon .smithy .model .knowledge .OperationIndex ;
40
40
import software .amazon .smithy .model .knowledge .TopDownIndex ;
41
+ import software .amazon .smithy .model .node .ObjectNode ;
41
42
import software .amazon .smithy .model .shapes .MemberShape ;
42
43
import software .amazon .smithy .model .shapes .OperationShape ;
43
44
import software .amazon .smithy .model .shapes .ServiceShape ;
47
48
import software .amazon .smithy .model .traits .DeprecatedTrait ;
48
49
import software .amazon .smithy .model .traits .DocumentationTrait ;
49
50
import software .amazon .smithy .model .traits .ErrorTrait ;
51
+ import software .amazon .smithy .model .traits .ExamplesTrait ;
50
52
import software .amazon .smithy .model .traits .InternalTrait ;
51
53
import software .amazon .smithy .rulesengine .traits .EndpointRuleSetTrait ;
54
+ import software .amazon .smithy .typescript .codegen .documentation .DocumentationExampleGenerator ;
52
55
import software .amazon .smithy .typescript .codegen .documentation .StructureExampleGenerator ;
53
56
import software .amazon .smithy .typescript .codegen .endpointsV2 .RuleSetParameterFinder ;
54
57
import software .amazon .smithy .typescript .codegen .integration .ProtocolGenerator ;
@@ -130,11 +133,13 @@ private void generateClientCommand() {
130
133
String name = symbol .getName ();
131
134
132
135
StringBuilder additionalDocs = new StringBuilder ()
133
- .append ("\n " )
134
- .append (getCommandExample (
135
- serviceSymbol .getName (), configType , name , inputType .getName (), outputType .getName ()))
136
- .append ("\n " )
137
- .append (getThrownExceptions ());
136
+ .append ("\n " )
137
+ .append (getCommandExample (
138
+ serviceSymbol .getName (), configType , name , inputType .getName (), outputType .getName ()))
139
+ .append ("\n " )
140
+ .append (getThrownExceptions ())
141
+ .append ("\n " )
142
+ .append (getCuratedExamples (name ));
138
143
139
144
boolean operationHasDocumentation = operation .hasTrait (DocumentationTrait .class );
140
145
@@ -199,10 +204,12 @@ private void generateClientCommand() {
199
204
writer .write ("}" ); // class close bracket.
200
205
}
201
206
202
- private String getCommandExample (String serviceName , String configName , String commandName , String commandInput ,
203
- String commandOutput ) {
207
+ private String getCommandExample (
208
+ String serviceName , String configName , String commandName ,
209
+ String commandInput , String commandOutput
210
+ ) {
204
211
String packageName = settings .getPackageName ();
205
- return "@example\n "
212
+ String exampleDoc = "@example\n "
206
213
+ "Use a bare-bones client and the command you need to make an API call.\n "
207
214
+ "```javascript\n "
208
215
+ String .format ("import { %s, %s } from \" %s\" ; // ES Modules import%n" , serviceName , commandName ,
@@ -225,6 +232,46 @@ private String getCommandExample(String serviceName, String configName, String c
225
232
+ String .format ("@see {@link %s} for command's `input` shape.%n" , commandInput )
226
233
+ String .format ("@see {@link %s} for command's `response` shape.%n" , commandOutput )
227
234
+ String .format ("@see {@link %s | config} for %s's `config` shape.%n" , configName , serviceName );
235
+
236
+ return exampleDoc ;
237
+ }
238
+
239
+ /**
240
+ * Handwritten examples from the operation ExamplesTrait.
241
+ */
242
+ private String getCuratedExamples (String commandName ) {
243
+ String exampleDoc = "" ;
244
+ if (operation .getTrait (ExamplesTrait .class ).isPresent ()) {
245
+ List <ExamplesTrait .Example > examples = operation .getTrait (ExamplesTrait .class ).get ().getExamples ();
246
+ StringBuilder buffer = new StringBuilder ();
247
+
248
+ for (ExamplesTrait .Example example : examples ) {
249
+ ObjectNode input = example .getInput ();
250
+ Optional <ObjectNode > output = example .getOutput ();
251
+ buffer
252
+ .append ("\n " )
253
+ .append (String .format ("@example %s%n" , example .getTitle ()))
254
+ .append ("```javascript\n " )
255
+ .append (String .format ("/* %s */%n" , example .getDocumentation ().orElse ("" )))
256
+ .append ("""
257
+ const input = %s;
258
+ const command = new %s(input);
259
+ const response = await client.send(command);
260
+ /* response is
261
+ %s
262
+ */
263
+ """ .formatted (
264
+ DocumentationExampleGenerator .inputToJavaScriptObject (input ),
265
+ commandName ,
266
+ DocumentationExampleGenerator .outputToJavaScriptObject (output .orElse (null ))
267
+ ))
268
+ .append ("```" )
269
+ .append ("\n " );
270
+ }
271
+
272
+ exampleDoc += buffer .toString ();
273
+ }
274
+ return exampleDoc ;
228
275
}
229
276
230
277
private String getThrownExceptions () {
0 commit comments