-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
80 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,39 @@ | ||
import type { PagedResponse } from './paged_response.ts' | ||
import type { PagedRequest } from './paged_request.ts' | ||
|
||
export const createSearchIteratorFn = < | ||
FN extends (r: PAGED_REQUEST) => Promise<PAGED_RESPONSE>, | ||
PAGED_REQUEST extends PagedRequest, | ||
PAGED_RESPONSE extends | ||
& PagedResponse | ||
& Record<PAYLOAD_KEY, SINGLE_RESPONSE[]>, | ||
PAYLOAD_KEY extends string = | ||
& keyof Omit<PAGED_RESPONSE, keyof PagedResponse> | ||
& string, | ||
SINGLE_RESPONSE = object, | ||
>(searchFn: FN, key: PAYLOAD_KEY) => { | ||
async function* searchIterator( | ||
params: Omit<PAGED_REQUEST, 'page_token'>, | ||
): AsyncGenerator<PAGED_RESPONSE[]> { | ||
let page_token: string | undefined = undefined | ||
while (true) { | ||
const response: PAGED_RESPONSE = await searchFn( | ||
// TODO(@joscha): remove cast | ||
(page_token | ||
? { ...params, page_token } | ||
: params) as PAGED_REQUEST, | ||
) | ||
|
||
// TODO(@joscha): remove cast | ||
yield response[key] as unknown as PAGED_RESPONSE[] | ||
|
||
if (response.next_page_token === null) { | ||
// no more pages to fetch | ||
return | ||
} else { | ||
page_token = response.next_page_token | ||
} | ||
} | ||
} | ||
return searchIterator | ||
} |
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
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,16 @@ | ||
// TODO(@joscha): see if we need to unify some of this with the `PagingParameters`. | ||
|
||
export type PagedRequest = { | ||
/** | ||
* The number of items to return per page. | ||
* | ||
* Default is the maximum value of 500. | ||
*/ | ||
page_size?: number | ||
|
||
/** | ||
* The page token to retrieve the next page of items. | ||
* if you do not pass the `page_size` parameter, the next page will have the default page size of 500. | ||
*/ | ||
page_token?: string | ||
} |
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,3 @@ | ||
export type PagedResponse = { | ||
next_page_token: string | null | ||
} |
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