diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java index 398838d14d24..b832cbd76b8e 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java @@ -23,6 +23,7 @@ import software.amazon.smithy.codegen.core.SymbolReference; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.knowledge.TopDownIndex; +import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.typescript.codegen.ApplicationProtocol; @@ -113,40 +114,49 @@ private void generateOperations() { // Generate a multiple overloaded methods for each command. writer.writeDocs(DocumentClientUtils.getCommandDocs(operationSymbol.getName())); - writer.write("public $L(\n" - + " args: $L,\n" - + " options?: $T,\n" - + "): Promise<$L>;", methodName, input, options, output); - writer.write("public $L(\n" - + " args: $L,\n" - + " cb: (err: any, data?: $L) => void\n" - + "): void;", methodName, input, output); - writer.write("public $L(\n" - + " args: $L,\n" - + " options: $T,\n" - + " cb: (err: any, data?: $L) => void\n" - + "): void;", methodName, input, options, output); - writer.openBlock("public $1L(\n" - + " args: $2L,\n" - + " optionsOrCb?: $3T | ((err: any, data?: $4L) => void),\n" - + " cb?: (err: any, data?: $4L) => void\n" - + "): Promise<$4L> | void { ", "}", - methodName, - input, - options, - output, - () -> { - writer.write("const command = new $L(args);\n" - + "if (typeof optionsOrCb === \"function\") {\n" - + " this.send(command, optionsOrCb)\n" - + "} else if (typeof cb === \"function\") {\n" - + " if (typeof optionsOrCb !== \"object\")\n" - + " throw new Error(`Expect http options but get $${typeof optionsOrCb}`)\n" - + " this.send(command, optionsOrCb || {}, cb)\n" - + "} else {\n" - + " return this.send(command, optionsOrCb);\n" - + "}", name); - }); + boolean inputOptional = model.getShape(operation.getInputShape()).map( + shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired) + ).orElse(true); + if (inputOptional) { + writer.write("$L(): Promise<$T>;", methodName, output); + } + writer.write(""" + public $1L( + args: $2L, + options?: $3T, + ): Promise<$4L>; + public $1L( + args: $2L, + cb: (err: any, data?: $4L) => void + ): void; + public $1L( + args: $2L, + options: $3T, + cb: (err: any, data?: $4L) => void + ): void; + public $1L( + args: $2L, + optionsOrCb?: $3T | ((err: any, data?: $4L) => void), + cb?: (err: any, data?: $4L) => void + ): Promise<$4L> | void { + const command = new $5L(args); + if (typeof optionsOrCb === "function") { + this.send(command, optionsOrCb); + } else if (typeof cb === "function") { + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get $${typeof optionsOrCb}`) + } + this.send(command, optionsOrCb || {}, cb); + } else { + return this.send(command, optionsOrCb); + } + }""", + methodName, + input, + options, + output, + name + ); writer.write(""); } } diff --git a/lib/lib-dynamodb/src/DynamoDBDocument.ts b/lib/lib-dynamodb/src/DynamoDBDocument.ts index 408e1c1d2136..7e3143875fbb 100644 --- a/lib/lib-dynamodb/src/DynamoDBDocument.ts +++ b/lib/lib-dynamodb/src/DynamoDBDocument.ts @@ -113,7 +113,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -143,7 +145,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -173,7 +177,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -203,7 +209,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -239,7 +247,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -275,7 +285,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -305,7 +317,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -335,7 +349,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -365,7 +381,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -395,7 +413,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -425,7 +445,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -461,7 +483,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb); @@ -491,7 +515,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient { if (typeof optionsOrCb === "function") { this.send(command, optionsOrCb); } else if (typeof cb === "function") { - if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + if (typeof optionsOrCb !== "object") { + throw new Error(`Expect http options but get ${typeof optionsOrCb}`); + } this.send(command, optionsOrCb || {}, cb); } else { return this.send(command, optionsOrCb);