Skip to content

Commit

Permalink
Add async iterator for query results
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Dec 18, 2024
1 parent 5b6792b commit 3309850
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions e2e/node/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
getPurposes,
} from "../../src/common/getters";
import { toBeEqual } from "../../src/gConsent/util/toBeEqual.mock";
import { paginateQuery } from "../../src/gConsent/query/query";

const { namedNode } = DataFactory;

Expand Down Expand Up @@ -1768,4 +1769,18 @@ describe(`End-to-end access grant tests for environment [${environment}] `, () =
);
});
});

it.only("can iterate through pages", async () => {

Check failure on line 1773 in e2e/node/e2e.test.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Unexpected focused test
const pages = paginateQuery(
{},
{
fetch: addUserAgent(requestorSession.fetch, TEST_USER_AGENT),
// FIXME add query endpoint discovery check.
queryEndpoint: new URL("query", vcProvider),
},
);
for await (const page of pages) {
expect(page.items).not.toHaveLength(0);
}
});
});
1 change: 1 addition & 0 deletions src/gConsent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export {
CredentialType,
DURATION,
query,
paginateQuery,
} from "./query/query";

export {
Expand Down
19 changes: 19 additions & 0 deletions src/gConsent/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,22 @@ export async function query(
}
return toCredentialResult(response);
}

export async function* paginateQuery(
filter: CredentialFilter,
options: {
fetch: typeof fetch;
queryEndpoint: URL;
},
) {
let page = await query(filter, options);
while (page.next !== undefined) {
yield page;
// This is a generator, so we don't want to go through
// all the pages at once with a Promise.all approach.
// eslint-disable-next-line no-await-in-loop
page = await query(page.next, options);
}
// Return the last page.
yield page;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export {
getAccessManagementUi,
getAccessRequestFromRedirectUrl,
issueAccessRequest,
paginateQuery,
query,
redirectToAccessManagementUi,
redirectToRequestor,
Expand Down

0 comments on commit 3309850

Please sign in to comment.