From 5b8318dd066775ba243f3d3f7dbcb7b0a04de22b Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Tue, 30 Jul 2024 12:59:58 +0100 Subject: [PATCH] fix iterator types (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: update flake.lock Flake lock file updates: • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/f4f322d1424aa547eba9cb092f905f5ceb9b639c' (2024-07-29) → 'github:NixOS/nixpkgs/3563397b2f10ffa1891e1a6ce99d13d960d73acd' (2024-07-30) * fix: types --- flake.lock | 6 +++--- src/v1/create_search_iterator_fn.ts | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/flake.lock b/flake.lock index f562e72..c55ef0f 100644 --- a/flake.lock +++ b/flake.lock @@ -57,11 +57,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1722267763, - "narHash": "sha256-E22vXGsS/NK1T0oZWDbI+E34+oGJHk9YUkszDYInUIU=", + "lastModified": 1722335482, + "narHash": "sha256-ogz81JDwIyuX67JC2dZUr3tIPqJABgSKJF9tynZLksQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "f4f322d1424aa547eba9cb092f905f5ceb9b639c", + "rev": "3563397b2f10ffa1891e1a6ce99d13d960d73acd", "type": "github" }, "original": { diff --git a/src/v1/create_search_iterator_fn.ts b/src/v1/create_search_iterator_fn.ts index 34b4eaa..5690883 100644 --- a/src/v1/create_search_iterator_fn.ts +++ b/src/v1/create_search_iterator_fn.ts @@ -1,6 +1,7 @@ import type { PagedResponse } from './paged_response.ts' import type { PagedRequest } from './paged_request.ts' +// TODO(@joscha): this is a mess, simplify types here whilst preserving the required paged request/response types export const createSearchIteratorFn = < FN extends (r: PAGED_REQUEST) => Promise, PAGED_REQUEST extends PagedRequest, @@ -12,20 +13,25 @@ export const createSearchIteratorFn = < & string, SINGLE_RESPONSE = object, >(searchFn: FN, key: PAYLOAD_KEY) => { + type Single = ReturnType extends Promise ? T : never + type Unwrapped = Single extends { [key in PAYLOAD_KEY]: infer T } ? T + : never async function* searchIterator( - params: Omit, - ): AsyncGenerator { + params: + & Omit[0], keyof PagedRequest> + & Omit, + ): AsyncGenerator { 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, + (page_token ? { ...params, page_token } : params) as Parameters< + FN + >[0], ) // TODO(@joscha): remove cast - yield response[key] as unknown as PAGED_RESPONSE[] + yield response[key] as Unwrapped if (response.next_page_token === null) { // no more pages to fetch