@kitstack/nest-powertools
Version:
A comprehensive collection of NestJS powertools, decorators, and utilities to supercharge your backend development
92 lines (91 loc) • 3.49 kB
TypeScript
import type { ApiResponse, PaginatedResponse, ErrorResponse, EnhancedPaginationQuery } from '../types/generics';
import { ResponseStatus, ResponseCodes } from '../types/enums';
export declare class ResponseFormatter {
private static generateRequestId;
private static getTimestamp;
static success<T>(data: T, message?: string, metadata?: Record<string, any>): ApiResponse<T>;
static error(message: string, code?: ResponseCodes, details?: any, field?: string): ErrorResponse;
static warning<T>(data: T, message: string, code?: ResponseCodes, metadata?: Record<string, any>): ApiResponse<T>;
static paginated<T>(data: T[], total: number, page: number, limit: number, message?: string): PaginatedResponse<T>;
static validationError(errors: Array<{
field: string;
message: string;
value?: any;
}>): ErrorResponse;
static authenticationError(message?: string): ErrorResponse;
static authorizationError(message?: string): ErrorResponse;
static notFound<T = any>(resource?: string): ErrorResponse;
static conflict(message?: string): ErrorResponse;
static rateLimitExceeded(message?: string): ErrorResponse;
static custom<T>(success: boolean, status: ResponseStatus, code: ResponseCodes, data?: T, message?: string, metadata?: Record<string, any>): ApiResponse<T>;
static transform<T>(data: T, options?: {
message?: string;
status?: ResponseStatus;
code?: ResponseCodes;
metadata?: Record<string, any>;
}): ApiResponse<T>;
}
export declare class PaginationHelper {
static paginate<T>(data: T[], total: number, pagination: EnhancedPaginationQuery, message?: string): PaginatedResponse<T>;
static getSkipTake(pagination: EnhancedPaginationQuery): {
skip: number;
take: number;
};
static getOffsetLimit(pagination: EnhancedPaginationQuery): {
offset: number;
limit: number;
};
static createLinks(pagination: EnhancedPaginationQuery, total: number, baseUrl: string): {
first: string;
last: string;
prev?: string;
next?: string;
self: string;
};
static validate(pagination: EnhancedPaginationQuery, options?: {
maxLimit?: number;
allowedSortFields?: string[];
requireSearch?: boolean;
}): {
valid: boolean;
errors: string[];
};
static forPrisma(pagination: EnhancedPaginationQuery): {
skip: number;
take: number;
orderBy: {
[x: string]: string;
};
};
static forTypeORM(pagination: EnhancedPaginationQuery): {
skip: number;
take: number;
order: {
[x: string]: import("../types/enums").SortOrder;
};
};
static forMongoose(pagination: EnhancedPaginationQuery): {
skip: number;
limit: number;
sort: {
[x: string]: number;
};
};
static createCursor<T>(data: T[], cursorField: keyof T, pagination: EnhancedPaginationQuery): {
data: T[];
hasNext: boolean;
hasPrevious: boolean;
nextCursor?: string;
previousCursor?: string;
};
static getStats(pagination: EnhancedPaginationQuery, total: number): {
totalItems: number;
totalPages: number;
currentPage: number;
itemsPerPage: number;
startIndex: number;
endIndex: number;
isFirstPage: boolean;
isLastPage: boolean;
};
}