Skip to content
Open
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
15 changes: 4 additions & 11 deletions site/lib/cli/library.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions site/lib/cli/pagination.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** Pull the `page[cursor]` token out of a JSON:API `links.next` value. */
export declare function extractCursor(next: unknown): string | undefined;
interface PageParams {
path?: Record<string, unknown>;
query?: Record<string, unknown>;
}
interface PaginationOptions {
/** Delay between per-page retries. Overridable (set to 0) to keep tests fast. */
retryDelayMs?: number;
}
/**
* Repeatedly GET `path`, following `links.next`, until no further page.
* Returns the concatenated `data` and `included` arrays from every page.
*/
export declare function fetchAllPages(client: any, path: string, params: PageParams, options?: PaginationOptions): Promise<{
data: any[];
included: any[];
}>;
export {};
86 changes: 86 additions & 0 deletions site/lib/cli/pagination.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 10 additions & 22 deletions site/lib/cli/playlist.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions site/scripts/copy-cli-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const files = [
'album.js', 'album.d.ts',
'playlist.js', 'playlist.d.ts',
'library.js', 'library.d.ts',
'pagination.js', 'pagination.d.ts',
'playback.js', 'playback.d.ts',
'recommend.js', 'recommend.d.ts',
'mix.js', 'mix.d.ts',
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ describe('listFavoritedPlaylists', () => {
it('lists favorited playlists', async () => {
mockClient.GET.mockResolvedValue({
data: {
data: [
{ type: 'playlists', id: 'pl-fav-1' },
{ type: 'playlists', id: 'pl-fav-2' },
],
included: [
{
id: 'pl-fav-1',
Expand All @@ -143,6 +147,7 @@ describe('listFavoritedPlaylists', () => {
it('outputs JSON', async () => {
mockClient.GET.mockResolvedValue({
data: {
data: [{ type: 'playlists', id: 'pl-1' }],
included: [
{ id: 'pl-1', type: 'playlists', attributes: { name: 'Fav PL', numberOfItems: 10 } },
],
Expand All @@ -169,6 +174,37 @@ describe('listFavoritedPlaylists', () => {

await expect(listFavoritedPlaylists(false)).rejects.toThrow('process.exit(1)');
});

it('follows cursor pagination across multiple pages', async () => {
mockClient.GET
.mockResolvedValueOnce({
data: {
data: [{ type: 'playlists', id: 'pl-1' }],
included: [
{ id: 'pl-1', type: 'playlists', attributes: { name: 'Page1', numberOfItems: 10 } },
],
links: {
next: '/userCollectionPlaylists/me/relationships/items?page%5Bcursor%5D=CURSOR2',
},
},
})
.mockResolvedValueOnce({
data: {
data: [{ type: 'playlists', id: 'pl-2' }],
included: [
{ id: 'pl-2', type: 'playlists', attributes: { name: 'Page2', numberOfItems: 20 } },
],
links: {}, // no `next` -> stop
},
});

await listFavoritedPlaylists(false);

expect(mockClient.GET).toHaveBeenCalledTimes(2);
expect(mockClient.GET.mock.calls[1][1].params.query['page[cursor]']).toBe('CURSOR2');
expect(output.some((l) => l.includes('[pl-1] Page1'))).toBe(true);
expect(output.some((l) => l.includes('[pl-2] Page2'))).toBe(true);
});
});

describe('addPlaylistToFavorites', () => {
Expand Down
Loading