Skip to content

Commit

Permalink
feat(pagination): add middleware support (#1480)
Browse files Browse the repository at this point in the history
* feat(pagination): add middleware support

* use command callback

* formatting

* update changeset

---------

Co-authored-by: George Fu <[email protected]>
  • Loading branch information
icholy and kuhe authored Jan 9, 2025
1 parent 292c134 commit 2aff9df
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-cups-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/types": minor
"@smithy/core": minor
---

Added middleware support to pagination
66 changes: 65 additions & 1 deletion packages/core/src/pagination/createPaginator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PaginationConfiguration } from "@smithy/types";
import { describe, expect, test as it } from "vitest";
import { afterEach, describe, expect, test as it, vi } from "vitest";

import { createPaginator } from "./createPaginator";

Expand All @@ -20,6 +20,10 @@ describe(createPaginator.name, () => {
}
}
class CommandObjectToken {
public middlewareStack = {
add: vi.fn(),
addRelativeTo: vi.fn(),
};
public constructor(public input: any) {
expect(input).toEqual({
sizeToken: 100,
Expand All @@ -33,6 +37,10 @@ describe(createPaginator.name, () => {
}

class CommandStringToken {
public middlewareStack = {
add: vi.fn(),
addRelativeTo: vi.fn(),
};
public constructor(public input: any) {
expect(input).toEqual({
sizeToken: 100,
Expand All @@ -41,6 +49,10 @@ describe(createPaginator.name, () => {
}
}

afterEach(() => {
vi.resetAllMocks();
});

it("should create a paginator", async () => {
const paginate = createPaginator<PaginationConfiguration, { inToken?: string }, { outToken: string }>(
Client,
Expand Down Expand Up @@ -112,4 +124,56 @@ describe(createPaginator.name, () => {

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

it("should allow modification of the instantiated command", async () => {
const paginate = createPaginator<PaginationConfiguration, { inToken?: string }, { outToken: string }>(
Client,
CommandObjectToken,
"inToken",
"outToken",
"sizeToken"
);

let pages = 0;
const client: any = new Client();
vi.spyOn(client, "send");
const config = {
client,
pageSize: 100,
startingToken: {
outToken2: {
outToken3: "TOKEN_VALUE",
},
},
withCommand(command) {
command.middlewareStack.add((next) => (args) => next(args));
command.middlewareStack.addRelativeTo((next: any) => (args: any) => next(args), {
toMiddleware: "",
relation: "before",
});
expect(command.middlewareStack.add).toHaveBeenCalledTimes(1);
expect(command.middlewareStack.addRelativeTo).toHaveBeenCalledTimes(1);
return command;
},
} as Parameters<typeof paginate>[0];
vi.spyOn(config, "withCommand");

for await (const page of paginate(config, {})) {
pages += 1;
if (pages === 5) {
expect(page.outToken).toBeUndefined();
} else {
expect(page.outToken).toEqual({
outToken2: {
outToken3: "TOKEN_VALUE",
},
});
}
}

expect(pages).toEqual(5);
expect(client.send).toHaveBeenCalledTimes(5);
expect(config.withCommand).toHaveBeenCalledTimes(5);
expect(config.withCommand).toHaveBeenCalledWith(expect.any(CommandObjectToken));
});
});
15 changes: 12 additions & 3 deletions packages/core/src/pagination/createPaginator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client, PaginationConfiguration, Paginator } from "@smithy/types";
import type { Client, Command, PaginationConfiguration, Paginator } from "@smithy/types";

/**
* @internal
Expand All @@ -7,9 +7,12 @@ const makePagedClientRequest = async <ClientType extends Client<any, any, any>,
CommandCtor: any,
client: ClientType,
input: InputType,
withCommand: (command: Command<any, any, any, any, any>) => typeof command = (_) => _,
...args: any[]
): Promise<OutputType> => {
return await client.send(new CommandCtor(input), ...args);
let command = new CommandCtor(input);
command = withCommand(command);
return await client.send(command, ...args);
};

/**
Expand Down Expand Up @@ -43,7 +46,13 @@ export function createPaginator<
(input as any)[pageSizeTokenName] = (input as any)[pageSizeTokenName] ?? config.pageSize;
}
if (config.client instanceof ClientCtor) {
page = await makePagedClientRequest(CommandCtor, config.client, input, ...additionalArguments);
page = await makePagedClientRequest(
CommandCtor,
config.client,
input,
config.withCommand,
...additionalArguments
);
} else {
throw new Error(`Invalid client, expected instance of ${ClientCtor.name}`);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client } from "./client";
import type { Client } from "./client";
import type { Command } from "./command";

/**
* @public
Expand All @@ -25,4 +26,10 @@ export interface PaginationConfiguration {
* instead of when it is not present.
*/
stopOnSameToken?: boolean;
/**
* @param command - reference to the instantiated command. This callback is executed
* prior to sending the command with the paginator's client.
* @returns the original command or a replacement.
*/
withCommand?: (command: Command<any, any, any, any, any>) => typeof command;
}

0 comments on commit 2aff9df

Please sign in to comment.