Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: paginator factory #1115

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-kings-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": minor
---

add paginator factory
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./util-identity-and-auth";
export * from "./getSmithyContext";
export * from "./normalizeProvider";
export * from "./protocols/requestBuilder";
export { createPaginator } from "./pagination/createPaginator";
58 changes: 58 additions & 0 deletions packages/core/src/pagination/createPaginator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Client, PaginationConfiguration, Paginator } from "@smithy/types";

/**
* @internal
*/
const makePagedClientRequest = async <ClientType extends Client<any, any, any>, InputType, OutputType>(
CommandCtor: any,
client: ClientType,
input: InputType,
...args: any[]
): Promise<OutputType> => {
return await client.send(new CommandCtor(input), ...args);
};

/**
* @internal
*
* Creates a paginator.
*/
export function createPaginator<
PaginationConfigType extends PaginationConfiguration,
InputType extends object,
OutputType extends object
>(
ClientCtor: any,
CommandCtor: any,
inputTokenName: string,
outputTokenName: string,
pageSizeTokenName?: string
): (config: PaginationConfigType, input: InputType, ...additionalArguments: any[]) => Paginator<OutputType> {
return async function* paginateOperation(
config: PaginationConfigType,
input: InputType,
...additionalArguments: any[]
): Paginator<OutputType> {
let token: any = config.startingToken || undefined;
let hasNext = true;
let page: OutputType;

while (hasNext) {
(input as any)[inputTokenName] = token;
if (pageSizeTokenName) {
(input as any)[pageSizeTokenName] = (input as any)[pageSizeTokenName] ?? config.pageSize;
}
if (config.client instanceof ClientCtor) {
page = await makePagedClientRequest(CommandCtor, config.client, input, ...additionalArguments);
} else {
throw new Error(`Invalid client, expected instance of ${ClientCtor.name}`);
}
yield page;
const prevToken = token;
token = (page as any)[outputTokenName];
hasNext = !!(token && (!config.stopOnSameToken || token !== prevToken));
}
// @ts-ignore
return undefined;
};
}
Loading