17
17
18
18
import java .nio .file .Path ;
19
19
import java .nio .file .Paths ;
20
- import java .util .Optional ;
21
- import software .amazon .smithy .codegen .core .CodegenException ;
22
20
import software .amazon .smithy .codegen .core .Symbol ;
23
21
import software .amazon .smithy .codegen .core .SymbolProvider ;
24
22
import software .amazon .smithy .model .Model ;
25
- import software .amazon .smithy .model .knowledge .PaginatedIndex ;
26
- import software .amazon .smithy .model .knowledge .PaginationInfo ;
27
23
import software .amazon .smithy .model .shapes .OperationShape ;
28
24
import software .amazon .smithy .model .shapes .ServiceShape ;
29
25
import software .amazon .smithy .typescript .codegen .TypeScriptDependency ;
@@ -36,14 +32,13 @@ final class DocumentClientPaginationGenerator implements Runnable {
36
32
static final String PAGINATION_FOLDER = "pagination" ;
37
33
38
34
private final TypeScriptWriter writer ;
39
- private final PaginationInfo paginatedInfo ;
40
35
41
36
private final String operationTypeName ;
42
37
private final String inputTypeName ;
43
38
private final String outputTypeName ;
44
39
45
40
private final String operationName ;
46
- private final String methodName ;
41
+
47
42
private final String paginationType ;
48
43
49
44
DocumentClientPaginationGenerator (
@@ -65,14 +60,7 @@ final class DocumentClientPaginationGenerator implements Runnable {
65
60
66
61
// e.g. listObjects
67
62
this .operationName = operationTypeName .replace ("Command" , "" );
68
- this .methodName = Character .toLowerCase (operationName .charAt (0 )) + operationName .substring (1 );
69
63
this .paginationType = DocumentClientUtils .CLIENT_FULL_NAME + "PaginationConfiguration" ;
70
-
71
- PaginatedIndex paginatedIndex = PaginatedIndex .of (model );
72
- Optional <PaginationInfo > paginationInfo = paginatedIndex .getPaginationInfo (service , operation );
73
- this .paginatedInfo = paginationInfo .orElseThrow (() -> {
74
- return new CodegenException ("Expected Paginator to have pagination information." );
75
- });
76
64
}
77
65
78
66
@ Override
@@ -90,13 +78,13 @@ public void run() {
90
78
91
79
// Import Pagination types
92
80
writer .addImport ("Paginator" , "Paginator" , TypeScriptDependency .SMITHY_TYPES );
81
+ writer .addImport ("createPaginator" , "createPaginator" , TypeScriptDependency .SMITHY_CORE );
93
82
writer .addRelativeImport (paginationType , paginationType ,
94
83
Paths .get ("." , getInterfaceFilelocation ().replace (".ts" , "" )));
95
84
96
85
writer .writeDocs ("@public" );
97
86
writer .write ("export { Paginator }" );
98
87
99
- writeCommandRequest ();
100
88
writePager ();
101
89
}
102
90
@@ -134,71 +122,18 @@ static void generateServicePaginationInterfaces(TypeScriptWriter writer) {
134
122
});
135
123
}
136
124
137
- private String destructurePath (String path ) {
138
- return "." + path .replace ("." , "!." );
139
- }
140
-
141
125
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
- });
187
- }
188
-
189
-
190
- /**
191
- * Paginated command that calls CommandClient().send({...}) under the hood. This is meant for client side (browser)
192
- * environments and does not generally expose the entire service.
193
- */
194
- 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
- });
126
+ writer .writeDocs ("@public" );
127
+ writer .write ("""
128
+ export const paginate$1L: (
129
+ config: $2L,
130
+ input: $3L,
131
+ ...additionalArguments: any
132
+ ) => Paginator<$4L> = createPaginator<
133
+ $2L,
134
+ $3L,
135
+ $4L
136
+ >(DynamoDBDocumentClient, $1LCommand, "ExclusiveStartKey", "LastEvaluatedKey", "Limit");
137
+ """ , operationName , paginationType , inputTypeName , outputTypeName );
203
138
}
204
139
}
0 commit comments