Skip to content

Commit e12befe

Browse files
committed
chore(codegen): use pagination factory in ddb document client
1 parent 6bf32b1 commit e12befe

File tree

1 file changed

+13
-57
lines changed

1 file changed

+13
-57
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentClientPaginationGenerator.java

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void run() {
9090

9191
// Import Pagination types
9292
writer.addImport("Paginator", "Paginator", TypeScriptDependency.SMITHY_TYPES);
93+
writer.addImport("createPaginator", "createPaginator", TypeScriptDependency.SMITHY_CORE);
9394
writer.addRelativeImport(paginationType, paginationType,
9495
Paths.get(".", getInterfaceFilelocation().replace(".ts", "")));
9596

@@ -134,56 +135,19 @@ static void generateServicePaginationInterfaces(TypeScriptWriter writer) {
134135
});
135136
}
136137

137-
private String destructurePath(String path) {
138-
return "." + path.replace(".", "!.");
139-
}
140-
141138
private void writePager() {
142-
String inputTokenName = paginatedInfo.getPaginatedTrait().getInputToken().get();
143-
String outputTokenName = paginatedInfo.getPaginatedTrait().getOutputToken().get();
144-
145-
writer.writeDocs("@public\n\n"
146-
+ String.format("@param %s - {@link %s}%n", inputTypeName, inputTypeName)
147-
+ String.format("@returns {@link %s}%n", outputTypeName)
148-
);
149-
writer.openBlock(
150-
"export async function* paginate$L(config: $L, input: $L, ...additionalArguments: any): Paginator<$L>{",
151-
"}", operationName, paginationType, inputTypeName, outputTypeName, () -> {
152-
String destructuredInputTokenName = destructurePath(inputTokenName);
153-
writer.write("// ToDo: replace with actual type instead of typeof input$L", destructuredInputTokenName);
154-
writer.write("let token: typeof input$L | undefined = config.startingToken || undefined;",
155-
destructuredInputTokenName);
156-
157-
writer.write("let hasNext = true;");
158-
writer.write("let page: $L;", outputTypeName);
159-
writer.openBlock("while (hasNext) {", "}", () -> {
160-
writer.write("input$L = token;", destructuredInputTokenName);
161-
162-
if (paginatedInfo.getPageSizeMember().isPresent()) {
163-
String pageSize = paginatedInfo.getPageSizeMember().get().getMemberName();
164-
writer.write("input[$S] = config.pageSize;", pageSize);
165-
}
166-
167-
writer.openBlock("if (config.client instanceof $L) {", "}", DocumentClientUtils.CLIENT_NAME,
168-
() -> {
169-
writer.write(
170-
"page = await makePagedClientRequest(config.client, input, ...additionalArguments);");
171-
}
172-
);
173-
writer.openBlock("else {", "}", () -> {
174-
writer.write("throw new Error(\"Invalid client, expected $L | $L\");",
175-
DocumentClientUtils.CLIENT_FULL_NAME, DocumentClientUtils.CLIENT_NAME);
176-
});
177-
178-
writer.write("yield page;");
179-
writer.write("token = page$L;", destructurePath(outputTokenName));
180-
181-
writer.write("hasNext = !!(token);");
182-
});
183-
184-
writer.write("// @ts-ignore");
185-
writer.write("return undefined;");
186-
});
139+
writer.writeDocs("@public");
140+
writer.write("""
141+
export const paginate$1L: (
142+
config: $2L,
143+
input: $3L,
144+
...additionalArguments: any
145+
) => Paginator<$4L> = createPaginator<
146+
$2L,
147+
$3L,
148+
$4L
149+
>(DynamoDBDocumentClient, $1LCommand, "ExclusiveStartKey", "LastEvaluatedKey", "Limit");
150+
""", operationName, paginationType, inputTypeName, outputTypeName);
187151
}
188152

189153

@@ -192,13 +156,5 @@ private void writePager() {
192156
* environments and does not generally expose the entire service.
193157
*/
194158
private void writeCommandRequest() {
195-
writer.writeDocs("@internal");
196-
writer.openBlock(
197-
"const makePagedClientRequest = async (client: $L, input: $L, ...args: any): Promise<$L> => {",
198-
"}", DocumentClientUtils.CLIENT_NAME, inputTypeName,
199-
outputTypeName, () -> {
200-
writer.write("// @ts-ignore");
201-
writer.write("return await client.send(new $L(input), ...args);", operationTypeName);
202-
});
203159
}
204160
}

0 commit comments

Comments
 (0)