UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

97 lines (96 loc) 4.67 kB
import { IResourceData, IResourcePaginationMetaData, IResourceQueryOptions, IResourceQueryOptionsOrderBy, IResourceQueryOptionsOrderByDirection } from "../types"; export declare class ResourcePaginationHelper { /** * Normalizes pagination parameters by calculating the `page` from `skip` if provided, * or calculating `skip` from `page` if only `page` is provided. * * @param {IResourceQueryOptions} options - The pagination options. * @param {number} options.limit - The number of records per page. * @param {number} optioins.page - The requested page number (optional). * @param {number} options.skip - The number of records to skip (optional). * @returns {{ page: number, skip: number,limit:number }} The normalized pagination options : "page" and "skip" are calculated from "limit" and "skip" or "page" and "limit" if provided. * * @example * ```typescript * normalizePagination({ limit: 10, skip: 20 }); // { page: 3, skip: 20 } * normalizePagination({ limit: 10 }); // { page: 1, skip: 0 } * normalizePagination({ page: 3, limit: 10 }); // { page: 3, skip: 0 } * normalizePagination({ page: 3, skip: 20 }); // { page: 3, skip: 20 } * ``` */ static normalizePagination(options?: IResourceQueryOptions): { page?: undefined; skip?: undefined; limit?: undefined; } | { page: number; skip: number; limit: number; }; /** * Normalizes the `orderBy` object from the query options. * Ensures the order direction is either 'ASC' or 'DESC' and handles multiple fields. * * @template DataType - The type of the data being queried. * * @param {IResourceQueryOptionsOrderBy<DataType>} orderBy - The orderBy property from the query options. * @returns A normalized object suitable for TypeORM's `order` option. * * @example * ```typescript * normalizeOrderBy<User>([{ name: 'asc' }, { age: 'desc' }]); * // Output: { lastName: 'ASC', firstName: 'DESC' } * * normalizeOrderBy<User>([{ age: 'asc'}, { createdAt: 'DESC' }]); * // Output: { age: 'ASC', createdAt: 'DESC' } * ``` */ static normalizeOrderBy<DataType = any>(orderBy?: IResourceQueryOptionsOrderBy<DataType>): Partial<{ [K in keyof DataType]?: IResourceQueryOptionsOrderByDirection | IResourceQueryOptionsOrderBy<DataType[K]>; }>; /*** * Determines if result can be paginated based on the provided query options. * It checks if the query options have a `limit` property of type number. * @param {IResourceQueryOptions} queryOptions - The query options. * @returns {boolean} Whether the result can be paginated. * * @example * canPaginateResult({ limit: undefined }); //false * canPaginateResult({ limit: 10, skip: 20 }); // true * canPaginateResult({ page: 3, limit: 10 }); // true * canPaginateResult({ page: 3, skip: 20 }); // true */ static canPaginateResult(queryOptions: IResourceQueryOptions): boolean; static getPaginationMetaData(count: number, queryOptions?: IResourceQueryOptions): IResourcePaginationMetaData; /*** * Paginates the result based on the provided options. * @param {DataType[]} data - The data to paginate. * @param {number} count - The total count of the data. * @param {IResourceQueryOptions} options - The pagination options. * @returns {IResourcePaginatedResult<DataType>} The paginated result. */ static paginate<DataType extends IResourceData = any>(data: DataType[], count: number, options?: IResourceQueryOptions): { data: DataType[]; total: number; meta: IResourcePaginationMetaData; }; /** * Parses query options from the provided request object. * It extracts query parameters, headers, and other relevant information to construct a query options object. * @template T The type of resource data. * @param req The request object containing the URL and headers. * @returns {IResourceQueryOptions<T> & {queryParams: Record<string, any>}} The parsed query options object. * * @example * const req = { url: '/api/resources?limit=10&skip=5', headers: { 'x-filters': { limit: 10, skip: 5 } } }; * const queryOptions = parseQueryOptions(req); */ static parseQueryOptions<T extends IResourceData = IResourceData>(req: { url: string; headers: Record<string, any>; params?: Record<string, any>; filters?: Record<string, any>; }): IResourceQueryOptions<T> & { queryParams: Record<string, any>; }; }