Skip to content

Commit

Permalink
feat: paginator factory (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Dec 18, 2023
1 parent 3eb09aa commit 12adf84
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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;
};
}

0 comments on commit 12adf84

Please sign in to comment.