import { type CachedGetter, type Maybe, type Page, type PromiseOrValue } from '@dereekb/util'; import { FetchRequestFactoryError } from './error'; export declare class FetchPageNoNextPageError extends FetchRequestFactoryError { readonly page: FetchPage; constructor(page: FetchPage); } export declare class FetchPageLimitReachedError extends FetchRequestFactoryError { readonly page: FetchPage; readonly limit: number; constructor(page: FetchPage, limit: number); } /** * Query/Search cursor string. * * Used by some APIs for pagination. */ export type FetchPageCursor = string; export interface FetchPage { /** * Input for this page. */ readonly input: I; /** * Fetches the next page of results. * * If the page of results has been returned, returns the same reference and does not perform the fetch request again. * * If there are no pages to return, calling this will throw a FetchPageNoNextPageError error. */ readonly fetchNext: CachedGetter>>; } export interface FetchPageResultInfo extends Page { /** * Cursor for this page, if applicable. */ readonly cursor?: Maybe; /** * Cursor for the next page, if applicable. */ readonly nextPageCursor?: Maybe; /** * Whether or not there are more results to fetch. * * If not defined, is assumed this info is not available and will default to true. * * Defaults to true. */ readonly hasNext?: Maybe; } export interface FetchPageResult extends FetchPageResultInfo { /** * Result for this page. */ readonly result: O; /** * Whether or not the max page has been reached. */ readonly isAtMaxPage?: boolean; } export interface FetchPageResultWithInput extends FetchPageResult { /** * Input for this page. */ readonly input: I; } export interface FetchNextPage extends FetchPageResult, FetchPage { /** * Previous page. * * Undefined if this is the first page. */ readonly previous?: Maybe>; } export interface FetchPageFactoryInputOptions { /** * The max number of pages to load. * * Pass null to disable the max page amount. */ readonly maxPage?: Maybe; /** * The maximum number of items to load per page. */ readonly maxItemsPerPage?: Maybe; } export type ReadFetchPageResultInfo = Omit; export interface FetchPageFactoryConfigDefaults { /** * The default max page to load up to. * * Defaults to 100. Pass null to disable the max page amount. */ readonly defaultMaxPage?: Maybe; /** * The default maximum number of items to load per page. */ readonly defaultMaxItemsPerPage?: Maybe; } export interface FetchPageFactoryConfig extends FetchPageFactoryConfigDefaults { /** * The configured Fetch function that takes in the input and returns a results. */ readonly fetch: (input: I) => Promise; /** * Returns the page results from the result. * * The page number is ignored as it is inferred from the previous page. * * @param result * @returns */ readonly readFetchPageResultInfo: (result: O) => PromiseOrValue; /** * Creates new input for the next page that is merged with the previous input. * * Returns undefined if the next page should not be loaded. * * @param result * @returns */ readonly buildInputForNextPage: (pageResult: Partial>, input: I, options: FetchPageFactoryInputOptions) => PromiseOrValue>>; } export type FetchPageFactoryOptions = FetchPageFactoryInputOptions; /** * Creates a new FetchPage instance. */ export type FetchPageFactory = (input: I, options?: Maybe>) => FetchPage; /** * Default max page for a FetchPageFactory. */ export declare const FETCH_PAGE_FACTORY_DEFAULT_MAX_PAGE = 100; /** * Creates a new FetchPageFactory from the input. * * @param config * @returns */ export declare function fetchPageFactory(config: FetchPageFactoryConfig): FetchPageFactory;