-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@smithy/core": minor | ||
--- | ||
|
||
add paginator factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |