@tangany/waas
Version:
node.js SDK for Tangany Wallet as a Service API
48 lines (47 loc) • 3.12 kB
TypeScript
import { ISearchRequestConfig, ISearchResponse } from "../../interfaces/common";
import { Waas } from "../../waas";
/**
* Base class to provide the results of API search requests as an asynchronous iterable that returns a single object per iteration rather than the complete results page.
* Nevertheless, each search result page is not fetched for each iteration, but only once.
* As soon as all items of the result list of the currently stored page have been returned, the next result page is fetched automatically.
* This way, the iterable hides the API-side pagination from the user such that it looks like an ordinary list of single objects that are actually located across pages.
* @template TApiResponse - Type of an entire search result page returned by the API
* @template TIteratorValue - Type of the item returned per iteration, usually created by converting single API search result items
*/
export declare abstract class ResourceIterable<TApiResponse extends ISearchResponse, TIteratorValue> implements AsyncIterable<TIteratorValue> {
protected readonly waas: Waas;
private readonly initialRequest;
/**
* Returns statistics on how many resources met the search criteria.
* This getter returns a Promise because the initial API search request is performed if that has not yet been done and the first page is therefore not in memory.
* If the first search result page is already stored, e.g. because the iterator has been used, the value is read from memory without any further HTTP request.
*/
get hits(): Promise<TApiResponse["hits"]>;
/**
* Explicitly store the first search result page because it contains common information (just like all other pages).
* It also serves as a kind of flag that the initial request has already been executed and the pagination links are followed from now on.
*/
private firstPage?;
constructor(waas: Waas, initialRequest: ISearchRequestConfig);
/**
* Returns the asynchronous iterator to traverse the list of items.
*/
[Symbol.asyncIterator](): AsyncGenerator<Awaited<TIteratorValue>, void, unknown>;
/**
* Converts an item returned by the API to conform to `TIteratorValue`.
* This method must be implemented by the respective child classes because each API resource may require a different transformation.
* @param item - Single search result item returned by the API
*/
protected abstract convertResponseItem(item: TApiResponse["list"][0]): TIteratorValue;
/**
* Reads the first search result page from memory if it has previously been fetched, or performs the initial HTTP request otherwise.
* Note that in the latter case the result is stored in the corresponding private property.
*/
protected getFirstPage(): Promise<TApiResponse>;
/**
* Queries another search results page based on the passed arguments.
* @param url - URL to be used for the HTTP GET request
* @param [params] - Query parameters to set the search criteria (usually only necessary to fetch the first page)
*/
private fetchPage;
}