|
23 | 23 | import software.amazon.smithy.codegen.core.SymbolReference;
|
24 | 24 | import software.amazon.smithy.model.Model;
|
25 | 25 | import software.amazon.smithy.model.knowledge.TopDownIndex;
|
| 26 | +import software.amazon.smithy.model.shapes.MemberShape; |
26 | 27 | import software.amazon.smithy.model.shapes.OperationShape;
|
27 | 28 | import software.amazon.smithy.model.shapes.ServiceShape;
|
28 | 29 | import software.amazon.smithy.typescript.codegen.ApplicationProtocol;
|
@@ -113,40 +114,49 @@ private void generateOperations() {
|
113 | 114 |
|
114 | 115 | // Generate a multiple overloaded methods for each command.
|
115 | 116 | writer.writeDocs(DocumentClientUtils.getCommandDocs(operationSymbol.getName()));
|
116 |
| - writer.write("public $L(\n" |
117 |
| - + " args: $L,\n" |
118 |
| - + " options?: $T,\n" |
119 |
| - + "): Promise<$L>;", methodName, input, options, output); |
120 |
| - writer.write("public $L(\n" |
121 |
| - + " args: $L,\n" |
122 |
| - + " cb: (err: any, data?: $L) => void\n" |
123 |
| - + "): void;", methodName, input, output); |
124 |
| - writer.write("public $L(\n" |
125 |
| - + " args: $L,\n" |
126 |
| - + " options: $T,\n" |
127 |
| - + " cb: (err: any, data?: $L) => void\n" |
128 |
| - + "): void;", methodName, input, options, output); |
129 |
| - writer.openBlock("public $1L(\n" |
130 |
| - + " args: $2L,\n" |
131 |
| - + " optionsOrCb?: $3T | ((err: any, data?: $4L) => void),\n" |
132 |
| - + " cb?: (err: any, data?: $4L) => void\n" |
133 |
| - + "): Promise<$4L> | void { ", "}", |
134 |
| - methodName, |
135 |
| - input, |
136 |
| - options, |
137 |
| - output, |
138 |
| - () -> { |
139 |
| - writer.write("const command = new $L(args);\n" |
140 |
| - + "if (typeof optionsOrCb === \"function\") {\n" |
141 |
| - + " this.send(command, optionsOrCb)\n" |
142 |
| - + "} else if (typeof cb === \"function\") {\n" |
143 |
| - + " if (typeof optionsOrCb !== \"object\")\n" |
144 |
| - + " throw new Error(`Expect http options but get $${typeof optionsOrCb}`)\n" |
145 |
| - + " this.send(command, optionsOrCb || {}, cb)\n" |
146 |
| - + "} else {\n" |
147 |
| - + " return this.send(command, optionsOrCb);\n" |
148 |
| - + "}", name); |
149 |
| - }); |
| 117 | + boolean inputOptional = model.getShape(operation.getInputShape()).map( |
| 118 | + shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired) |
| 119 | + ).orElse(true); |
| 120 | + if (inputOptional) { |
| 121 | + writer.write("$L(): Promise<$T>;", methodName, output); |
| 122 | + } |
| 123 | + writer.write(""" |
| 124 | + public $1L( |
| 125 | + args: $2L, |
| 126 | + options?: $3T, |
| 127 | + ): Promise<$4L>; |
| 128 | + public $1L( |
| 129 | + args: $2L, |
| 130 | + cb: (err: any, data?: $4L) => void |
| 131 | + ): void; |
| 132 | + public $1L( |
| 133 | + args: $2L, |
| 134 | + options: $3T, |
| 135 | + cb: (err: any, data?: $4L) => void |
| 136 | + ): void; |
| 137 | + public $1L( |
| 138 | + args: $2L, |
| 139 | + optionsOrCb?: $3T | ((err: any, data?: $4L) => void), |
| 140 | + cb?: (err: any, data?: $4L) => void |
| 141 | + ): Promise<$4L> | void { |
| 142 | + const command = new $5L(args); |
| 143 | + if (typeof optionsOrCb === "function") { |
| 144 | + this.send(command, optionsOrCb); |
| 145 | + } else if (typeof cb === "function") { |
| 146 | + if (typeof optionsOrCb !== "object") { |
| 147 | + throw new Error(`Expect http options but get $${typeof optionsOrCb}`) |
| 148 | + } |
| 149 | + this.send(command, optionsOrCb || {}, cb); |
| 150 | + } else { |
| 151 | + return this.send(command, optionsOrCb); |
| 152 | + } |
| 153 | + }""", |
| 154 | + methodName, |
| 155 | + input, |
| 156 | + options, |
| 157 | + output, |
| 158 | + name |
| 159 | + ); |
150 | 160 | writer.write("");
|
151 | 161 | }
|
152 | 162 | }
|
|
0 commit comments