Skip to content

Commit d3caadd

Browse files
alexforsythtrivikr
andauthored
feat: codegen for waitUntil[State] (#318)
* feat: creating codegen for WaitUntilState * fix: spacing * Update smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java Co-authored-by: Trivikram Kamat <[email protected]> * feat: throw a new error object * fix: minor updates * fix: param annotation in doc generator * fix: capitalization * fix: remove old documentation * fix: documentation spacing Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 201403f commit d3caadd

File tree

1 file changed

+22
-5
lines changed
  • smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen

1 file changed

+22
-5
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,44 @@ private void generateWaiter() {
7272
writer.addImport("createWaiter", "createWaiter", WAITABLE_UTIL_PACKAGE);
7373
writer.addImport("WaiterResult", "WaiterResult", WAITABLE_UTIL_PACKAGE);
7474
writer.addImport("WaiterState", "WaiterState", WAITABLE_UTIL_PACKAGE);
75+
writer.addImport("checkExceptions", "checkExceptions", WAITABLE_UTIL_PACKAGE);
7576
writer.addImport("WaiterConfiguration", "WaiterConfiguration", WAITABLE_UTIL_PACKAGE);
7677

78+
// generates (deprecated) WaitFor....
7779
writer.writeDocs(waiter.getDocumentation().orElse("") + " \n"
78-
+ " @param params : Waiter configuration options.\n"
79-
+ " @param input : the input to " + operationSymbol.getName() + " for polling.");
80+
+ " @deprecated Use waitUntil" + waiterName + " instead. "
81+
+ "waitFor" + waiterName + " does not throw error in non-success cases.");
8082
writer.openBlock("export const waitFor$L = async (params: WaiterConfiguration<$T>, input: $T): "
8183
+ "Promise<WaiterResult> => {", "}", waiterName, serviceSymbol, inputSymbol, () -> {
8284
writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(),
8385
waiter.getMaxDelay());
8486
writer.write("return createWaiter({...serviceDefaults, ...params}, input, checkState);");
8587
});
88+
89+
// generates WaitUtil....
90+
writer.writeDocs(waiter.getDocumentation().orElse("") + " \n"
91+
+ " @param params - Waiter configuration options.\n"
92+
+ " @param input - The input to " + operationSymbol.getName() + " for polling.");
93+
writer.openBlock("export const waitUntil$L = async (params: WaiterConfiguration<$T>, input: $T): "
94+
+ "Promise<WaiterResult> => {", "}", waiterName, serviceSymbol, inputSymbol, () -> {
95+
writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(),
96+
waiter.getMaxDelay());
97+
writer.write("const result = await createWaiter({...serviceDefaults, ...params}, input, checkState);");
98+
writer.write("return checkExceptions(result);");
99+
});
86100
}
87101

88102
private void generateAcceptors() {
89103
writer.openBlock("const checkState = async (client: $T, input: $T): Promise<WaiterResult> => {", "}",
90104
serviceSymbol, inputSymbol, () -> {
105+
writer.write("let reason;");
91106
writer.openBlock("try {", "}", () -> {
92107
writer.write("let result: any = await client.send(new $T(input))", operationSymbol);
108+
writer.write("reason = result;");
93109
writeAcceptors("result", false);
94110
});
95111
writer.openBlock("catch (exception) {", "}", () -> {
112+
writer.write("reason = exception;");
96113
writeAcceptors("exception", true);
97114
});
98115
writer.write("return $L;", makeWaiterResult(AcceptorState.RETRY));
@@ -167,11 +184,11 @@ private void generatePathMatcher(String accessor, PathMatcher pathMatcher, Accep
167184

168185
private String makeWaiterResult(AcceptorState resultantState) {
169186
if (resultantState == AcceptorState.SUCCESS) {
170-
return "{ state: WaiterState.SUCCESS }";
187+
return "{ state: WaiterState.SUCCESS, reason }";
171188
} else if (resultantState == AcceptorState.FAILURE) {
172-
return "{ state: WaiterState.FAILURE }";
189+
return "{ state: WaiterState.FAILURE, reason }";
173190
} else if (resultantState == AcceptorState.RETRY) {
174-
return "{ state: WaiterState.RETRY }";
191+
return "{ state: WaiterState.RETRY, reason }";
175192
}
176193
throw new CodegenException("Hit an invalid acceptor state to codegen " + resultantState.toString());
177194
}

0 commit comments

Comments
 (0)