import type { RequireAllOrNone } from "type-fest"; /** Represents the expected value of the "limit" parameter. */ export type Limit = number; /** Represents the expected value of the "offset" parameter. */ export type Offset = number; /** * Represents the expected value of the "offset" parameter for infinite * pagination queries. */ export type PageParam = Offset | undefined; /** * Represents the value that contains the total number of items that can be * paginated. */ export type Total = number; export interface DirectionalInterval { /** Contains the start of the current page's interval. */ from: number; /** Contains the end of the current page's interval. */ to: number; } export interface DirectionalStats { /** Indicates whether the previous page is available. */ prev: boolean; /** Indicates whether the next page is available. */ next: boolean; } type PaginationCallback = (response: T, limit: Limit, offset: Offset) => R; interface IntervalConfig { /** * Allows to determine the current page's interval's start. * * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * * @returns The current page's interval's start. */ getIntervalFrom: PaginationCallback; /** * Allows to determine the current page's interval's end. * * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * * @returns The current page's interval's end. */ getIntervalTo: PaginationCallback; } export type DirectionalConfig = RequireAllOrNone, keyof IntervalConfig> & { /** * Allows to determine the previous page's offset. * * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * * @returns The previous page's offset, if available. */ getPreviousOffset: PaginationCallback; /** * Allows to determine the next page's offset. * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * * @returns The next page's offset, if available. */ getNextOffset: PaginationCallback; /** * Determines whether backwards pagination is possible. * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * @returns Whether a previous page is available. */ hasPreviousPage: PaginationCallback; /** * Determines whether forwards pagination is possible. * @param response The current response. * @param limit The current "limit"'s parameter's value. * @param offset The current "offset"'s parameter's value. * @returns Whether a subsequent page is available. */ hasNextPage: PaginationCallback; }; export interface DirectionalResult { /** * Contains the current page's interval. * * The values are available only if the proper configuration is provided. */ interval?: DirectionalInterval; /** Whether the previous page is available. */ hasPreviousPage: boolean; /** Whether the next page is available. */ hasNextPage: boolean; /** * Fetches the previous page, if available. * * The function will be executed only if the query has data in the cache, * it's not in fetching state and the previous page is available. */ fetchPreviousPage: () => void; /** * Fetches the next page, if available. * * The function will be executed only if the query has data in the cache, * it's not in fetching state and the next page is available. */ fetchNextPage: () => void; /** * Forces the current "limit" parameter's value. * * If no value is specified, the function will use the default value provided * to the concierge. * * @param limit The desired value. */ setLimit: (limit?: Limit) => void; /** * Forces the current "offset" parameter's value. * * If no value is specified, the function will use the default value provided * to the concierge. * * @param offset The desired value. */ setOffset: (offset?: Offset) => void; } export interface InfiniteConfig { /** * Accesses the list of results present in the response. * * @param response The current response. * * @returns The array containing the response's results. */ getResults: (response: T) => unknown[] | readonly unknown[]; /** * Retrieves the field that contains the total number of results. * * @param response The current response. * * @returns The number containing the amount of total results. */ getTotal: (response: T) => Total; } /** Represents the output of an infinite pagination hook. */ export interface InfiniteResult { /** * Forces the current "limit" parameter's value. * * If no value is specified, the function will use the default value provided * to the concierge. * * @param limit The desired value. */ setLimit: (limit?: Limit) => void; /** * Contans the number of total records. * * This value is only available if the query has data in the cache. */ totalRecords: Total | null; /** * Contans the number of records fetched so far. * * This value is only available if the query has data in the cache. */ totalFetched: Total | null; } export {};