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

fix: handle multi-part token paths in paginator #1160

Merged
merged 1 commit into from
Feb 7, 2024
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/four-steaks-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": patch
---

handle multi-part input token in paginator
114 changes: 114 additions & 0 deletions packages/core/src/pagination/createPaginator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { PaginationConfiguration } from "@smithy/types";

import { createPaginator } from "./createPaginator";

describe(createPaginator.name, () => {
class Client {
private pages = 5;
async send(command: CommandObjectToken | CommandStringToken) {

Check warning on line 8 in packages/core/src/pagination/createPaginator.spec.ts

View workflow job for this annotation

GitHub Actions / TypeScript Lint

'command' is defined but never used
if (--this.pages > 0) {
return {
outToken: {
outToken2: {
outToken3: "TOKEN_VALUE",
},
},
};
}
return {};
}
}
class CommandObjectToken {
public constructor(public input: any) {
expect(input).toEqual({
sizeToken: 100,
inToken: {
outToken2: {
outToken3: "TOKEN_VALUE",
},
},
});
}
}

class CommandStringToken {
public constructor(public input: any) {
expect(input).toEqual({
sizeToken: 100,
inToken: "TOKEN_VALUE",
});
}
}

it("should create a paginator", async () => {
const paginate = createPaginator<PaginationConfiguration, { inToken?: string }, { outToken: string }>(
Client,
CommandObjectToken,
"inToken",
"outToken",
"sizeToken"
);

let pages = 0;

for await (const page of paginate(
{
client: new Client() as any,
pageSize: 100,
startingToken: {
outToken2: {
outToken3: "TOKEN_VALUE",
},
},
},
{}
)) {
pages += 1;
if (pages === 5) {
expect(page.outToken).toBeUndefined();
} else {
expect(page.outToken).toEqual({
outToken2: {
outToken3: "TOKEN_VALUE",
},
});
}
}

expect(pages).toEqual(5);
});

it("should handle deep paths", async () => {
const paginate = createPaginator<
PaginationConfiguration,
{ inToken?: string },
{
outToken: {
outToken2: {
outToken3: string;
};
};
}
>(Client, CommandStringToken, "inToken", "outToken.outToken2.outToken3", "sizeToken");

let pages = 0;

for await (const page of paginate(
{
client: new Client() as any,
pageSize: 100,
startingToken: "TOKEN_VALUE",
},
{}
)) {
pages += 1;
if (pages === 5) {
expect(page.outToken).toBeUndefined();
} else {
expect(page.outToken.outToken2.outToken3).toEqual("TOKEN_VALUE");
}
}

expect(pages).toEqual(5);
});
});
17 changes: 16 additions & 1 deletion packages/core/src/pagination/createPaginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,25 @@ export function createPaginator<
}
yield page;
const prevToken = token;
token = (page as any)[outputTokenName];
token = get(page, outputTokenName);
hasNext = !!(token && (!config.stopOnSameToken || token !== prevToken));
}
// @ts-ignore
return undefined;
};
}

/**
* @internal
*/
const get = (fromObject: any, path: string): any => {
let cursor = fromObject;
const pathComponents = path.split(".");
for (const step of pathComponents) {
if (!cursor || typeof cursor !== "object") {
return undefined;
}
cursor = cursor[step];
}
return cursor;
};
Loading