Skip to content

Commit adb932a

Browse files
[DECO-330] Fix repo list pagination (#229)
Co-authored-by: Fabian Jakobs <fabian.jakobs@databricks.com>
1 parent 64242e9 commit adb932a

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

packages/databricks-sdk-js/src/decorators.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import assert from "node:assert";
44
import {ListRequest, ListReposResponse} from "./apis/repos";
55
import {paginated} from "./decorators";
66
import {CancellationToken} from "./types";
7+
import {Context} from "./context";
78

89
describe(__filename, () => {
910
it("should paginate", async () => {
@@ -60,7 +61,7 @@ describe(__filename, () => {
6061
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6162
_req: ListRequest,
6263
// eslint-disable-next-line @typescript-eslint/no-unused-vars
63-
_token: CancellationToken
64+
_context: Context
6465
): Promise<ListReposResponse> {
6566
this.count += 1;
6667
if (this.count === 3) {
@@ -79,7 +80,7 @@ describe(__filename, () => {
7980
}
8081

8182
const t = new Test();
82-
await t.getRepos({}, token);
83+
await t.getRepos({}, new Context({cancellationToken: token}));
8384
assert.equal(t.count, 3);
8485
});
8586
});

packages/databricks-sdk-js/src/decorators.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CancellationToken} from "./types";
1+
import {Context} from "./context";
22

33
/**
44
* Wraps an API client function that uses pagination and calles it iteratively to
@@ -15,12 +15,12 @@ export function paginated<REQ, RES>(
1515
): PropertyDescriptor {
1616
const childFunction = descriptor.value as (
1717
req: REQ,
18-
token?: CancellationToken
18+
context?: Context
1919
) => Promise<RES>;
2020

2121
descriptor.value = async function (
2222
req: REQ,
23-
token?: CancellationToken
23+
context?: Context
2424
): Promise<RES> {
2525
const results = [];
2626
let response: RES;
@@ -31,9 +31,12 @@ export function paginated<REQ, RES>(
3131
} else {
3232
delete req[paginationKey];
3333
}
34-
response = await childFunction.call(this, req, token);
34+
response = await childFunction.call(this, req, context);
3535

36-
if (token && token.isCancellationRequested) {
36+
if (
37+
context?.cancellationToken &&
38+
context?.cancellationToken.isCancellationRequested
39+
) {
3740
return response;
3841
}
3942

@@ -43,8 +46,9 @@ export function paginated<REQ, RES>(
4346
paginationToken = response[paginationKey];
4447
} while (paginationToken);
4548

46-
(response[itemsKey] as any) = results;
47-
return response;
49+
const anyResponse = response as any;
50+
anyResponse[itemsKey] = results;
51+
return anyResponse;
4852
};
4953
return descriptor;
5054
};

packages/databricks-sdk-js/src/services/Repos.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export class Repos {
2323
@context context?: Context
2424
): Promise<RepoList> {
2525
const reposApi = new ReposService(this.client);
26+
const response = await reposApi.list(req, context);
2627
return {
2728
repos:
28-
(await reposApi.list(req, context)).repos?.map(
29+
response.repos?.map(
2930
(details) => new Repo(this.client, details)
3031
) ?? [],
31-
next_page_token: req["next_page_token"],
32+
next_page_token: response["next_page_token"],
3233
};
3334
}
3435
}

0 commit comments

Comments
 (0)