-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from saleor/fix-cloud-apl-limit
Add pagination and option to configure limit for CloudAPL
- Loading branch information
Showing
8 changed files
with
230 additions
and
10 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 @@ | ||
--- | ||
"@saleor/app-sdk": minor | ||
--- | ||
|
||
Added pagination for CloudAPL. Previously if there were more than 100 results it would return only first 100. This change adds an option to configure the page size and automatically paginates through the responses until `next` property is set to null on the response |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,64 @@ | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { Paginator } from "./paginator"; | ||
|
||
const fetchMock = vi.fn(); | ||
|
||
describe("Paginator", () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("Returns single page when there is no `next` property", async () => { | ||
fetchMock.mockResolvedValue({ | ||
status: 200, | ||
json: async () => ({ count: 1, next: null, previous: null, results: [{ ok: "yes" }] }), | ||
ok: true, | ||
}); | ||
const paginator = new Paginator("https://test.com", {}, fetchMock); | ||
const result = await paginator.fetchAll(); | ||
|
||
expect(fetchMock).toHaveBeenCalledOnce(); | ||
expect(result).toStrictEqual({ | ||
count: 1, | ||
next: null, | ||
previous: null, | ||
results: [{ ok: "yes" }], | ||
}); | ||
}); | ||
|
||
it("Returns all pages when there is `next` property", async () => { | ||
fetchMock | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
json: async () => ({ | ||
next: "https://test.com?page=2", | ||
previous: null, | ||
count: 2, | ||
results: [{ ok: "1" }], | ||
}), | ||
ok: true, | ||
}) | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
json: async () => ({ | ||
next: null, | ||
previous: "https://test.com?page=1", | ||
count: 2, | ||
results: [{ ok: "2" }], | ||
}), | ||
ok: true, | ||
}); | ||
const paginator = new Paginator("https://test.com", {}, fetchMock); | ||
const result = await paginator.fetchAll(); | ||
|
||
expect(fetchMock).toHaveBeenLastCalledWith("https://test.com?page=2", {}); | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(result).toStrictEqual({ | ||
count: 2, | ||
next: null, | ||
previous: null, | ||
results: [{ ok: "1" }, { ok: "2" }], | ||
}); | ||
}); | ||
}); |
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,54 @@ | ||
import { createAPLDebug } from "../apl-debug"; | ||
|
||
const debug = createAPLDebug("Paginator"); | ||
|
||
interface PaginationResponse<ResultType> { | ||
next: string | null; | ||
previous: string | null; | ||
results: ResultType[]; | ||
} | ||
|
||
export class Paginator<ResultType> { | ||
constructor( | ||
private readonly url: string, | ||
private readonly fetchOptions: RequestInit, | ||
private readonly fetchFn = fetch | ||
) {} | ||
|
||
public async fetchAll() { | ||
debug("Fetching all pages for url", this.url); | ||
const response = await this.fetchFn(this.url, this.fetchOptions); | ||
debug("%0", response); | ||
|
||
const json = (await response.json()) as PaginationResponse<ResultType>; | ||
|
||
if (json.next) { | ||
const remainingPages = await this.fetchNext(json.next); | ||
const allResults = [...json.results, ...remainingPages.flatMap((page) => page.results)]; | ||
debug("Fetched all pages, total length: %d", allResults.length); | ||
|
||
return { | ||
next: null, | ||
previous: null, | ||
count: allResults.length, | ||
results: allResults, | ||
}; | ||
} | ||
|
||
debug("No more pages to fetch, returning first page"); | ||
return json; | ||
} | ||
|
||
private async fetchNext(nextUrl: string): Promise<Array<PaginationResponse<ResultType>>> { | ||
debug("Fetching next page with url %s", nextUrl); | ||
const response = await this.fetchFn(nextUrl, this.fetchOptions); | ||
debug("%0", response); | ||
|
||
const json = (await response.json()) as PaginationResponse<ResultType>; | ||
|
||
if (json.next) { | ||
return [json, ...(await this.fetchNext(json.next))]; | ||
} | ||
return [json]; | ||
} | ||
} |
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