first commit
This commit is contained in:
33
src/common/utils/helper/ApiError.ts
Normal file
33
src/common/utils/helper/ApiError.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
class ApiError<T = unknown> extends Error {
|
||||
statusCode: number;
|
||||
data: T | null;
|
||||
message: string;
|
||||
success: boolean;
|
||||
errors: Array<Error>;
|
||||
isOperational: boolean;
|
||||
stack?: string;
|
||||
|
||||
constructor(
|
||||
statusCode: number,
|
||||
message: string = 'Something went wrong',
|
||||
errors: Array<Error> = [],
|
||||
isOperational: boolean = true,
|
||||
stack?: string
|
||||
) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.data = null;
|
||||
this.message = message;
|
||||
this.success = false;
|
||||
this.errors = errors;
|
||||
this.isOperational = isOperational;
|
||||
|
||||
if (stack) {
|
||||
this.stack = stack;
|
||||
} else {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ApiError;
|
||||
32
src/common/utils/pagination.util.ts
Normal file
32
src/common/utils/pagination.util.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { PaginationDto, PaginationMetaDto } from '../dto/pagination.dto';
|
||||
|
||||
export function createPaginationMeta(
|
||||
paginationDto: PaginationDto,
|
||||
total: number,
|
||||
): PaginationMetaDto {
|
||||
const { page = 1, limit = 10 } = paginationDto;
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1,
|
||||
};
|
||||
}
|
||||
|
||||
export function paginate<T>(
|
||||
data: T[],
|
||||
paginationDto: PaginationDto,
|
||||
total: number,
|
||||
) {
|
||||
const meta = createPaginationMeta(paginationDto, total);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user