elseware-nodejs 1.8.5 → 1.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -49,12 +49,17 @@ declare class APIResponse {
49
49
  success?: boolean;
50
50
  message?: string;
51
51
  data?: unknown;
52
- meta?: unknown;
52
+ meta?: Record<string, unknown>;
53
53
  }): Response<any, Record<string, any>>;
54
- static ok(res: Response, data: unknown, message?: string, meta?: unknown): Response<any, Record<string, any>>;
55
- static created(res: Response, data: unknown, message?: string): Response<any, Record<string, any>>;
54
+ static ok(res: Response, data?: unknown, message?: string, meta?: Record<string, unknown>): Response<any, Record<string, any>>;
55
+ static created(res: Response, data?: unknown, message?: string): Response<any, Record<string, any>>;
56
56
  static noContent(res: Response): Response<any, Record<string, any>>;
57
- static error(res: Response, message?: string, statusCode?: number): Response<any, Record<string, any>>;
57
+ static error(res: Response, message?: string, statusCode?: number, meta?: Record<string, unknown>): Response<any, Record<string, any>>;
58
+ static paginated(res: Response, data: unknown, options: {
59
+ total: number;
60
+ page: number;
61
+ limit: number;
62
+ }, message?: string): Response<any, Record<string, any>>;
58
63
  }
59
64
 
60
65
  declare const asyncHandler: (fn: (req: Request, res: Response, next: NextFunction) => Promise<void>) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
@@ -1804,10 +1809,10 @@ declare class AppError extends Error {
1804
1809
  status: string;
1805
1810
  isOperational: boolean;
1806
1811
  code?: string;
1807
- details?: unknown;
1812
+ details?: Record<string, unknown>;
1808
1813
  constructor(message: string, statusCode?: number, options?: {
1809
1814
  code?: string;
1810
- details?: unknown;
1815
+ details?: Record<string, unknown>;
1811
1816
  });
1812
1817
  }
1813
1818
 
package/dist/index.d.ts CHANGED
@@ -49,12 +49,17 @@ declare class APIResponse {
49
49
  success?: boolean;
50
50
  message?: string;
51
51
  data?: unknown;
52
- meta?: unknown;
52
+ meta?: Record<string, unknown>;
53
53
  }): Response<any, Record<string, any>>;
54
- static ok(res: Response, data: unknown, message?: string, meta?: unknown): Response<any, Record<string, any>>;
55
- static created(res: Response, data: unknown, message?: string): Response<any, Record<string, any>>;
54
+ static ok(res: Response, data?: unknown, message?: string, meta?: Record<string, unknown>): Response<any, Record<string, any>>;
55
+ static created(res: Response, data?: unknown, message?: string): Response<any, Record<string, any>>;
56
56
  static noContent(res: Response): Response<any, Record<string, any>>;
57
- static error(res: Response, message?: string, statusCode?: number): Response<any, Record<string, any>>;
57
+ static error(res: Response, message?: string, statusCode?: number, meta?: Record<string, unknown>): Response<any, Record<string, any>>;
58
+ static paginated(res: Response, data: unknown, options: {
59
+ total: number;
60
+ page: number;
61
+ limit: number;
62
+ }, message?: string): Response<any, Record<string, any>>;
58
63
  }
59
64
 
60
65
  declare const asyncHandler: (fn: (req: Request, res: Response, next: NextFunction) => Promise<void>) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
@@ -1804,10 +1809,10 @@ declare class AppError extends Error {
1804
1809
  status: string;
1805
1810
  isOperational: boolean;
1806
1811
  code?: string;
1807
- details?: unknown;
1812
+ details?: Record<string, unknown>;
1808
1813
  constructor(message: string, statusCode?: number, options?: {
1809
1814
  code?: string;
1810
- details?: unknown;
1815
+ details?: Record<string, unknown>;
1811
1816
  });
1812
1817
  }
1813
1818
 
package/dist/index.js CHANGED
@@ -41,26 +41,37 @@ var APIResponse = class _APIResponse {
41
41
  if (statusCode === 204) {
42
42
  return res.status(204).send();
43
43
  }
44
- const body = { success, message };
45
- if (data !== void 0) body.data = data;
46
- if (meta !== void 0) body.meta = meta;
44
+ const body = {
45
+ success,
46
+ message,
47
+ ...data !== void 0 && { data },
48
+ ...meta !== void 0 && { meta }
49
+ };
47
50
  return res.status(statusCode).json(body);
48
51
  }
49
- static ok(res, data, message, meta) {
52
+ static ok(res, data, message = "Success", meta) {
50
53
  return _APIResponse.send(res, { statusCode: 200, data, message, meta });
51
54
  }
52
- static created(res, data, message) {
55
+ static created(res, data, message = "Resource created successfully") {
53
56
  return _APIResponse.send(res, { statusCode: 201, data, message });
54
57
  }
55
58
  static noContent(res) {
56
59
  return _APIResponse.send(res, { statusCode: 204 });
57
60
  }
58
- // Error response (optional use in controllers)
59
- static error(res, message, statusCode = 500) {
61
+ static error(res, message = "Internal Server Error", statusCode = 500, meta) {
60
62
  return _APIResponse.send(res, {
61
63
  success: false,
62
64
  statusCode,
63
- message
65
+ message,
66
+ meta
67
+ });
68
+ }
69
+ static paginated(res, data, options, message = "Data fetched successfully") {
70
+ return _APIResponse.ok(res, data, message, {
71
+ total: options.total,
72
+ page: options.page,
73
+ limit: options.limit,
74
+ totalPages: Math.ceil(options.total / options.limit)
64
75
  });
65
76
  }
66
77
  };
@@ -5086,16 +5097,23 @@ var handleCastErrorDB = (err) => new AppError(`Invalid ${err.path}: ${err.value}
5086
5097
  code: "INVALID_ID"
5087
5098
  });
5088
5099
  var handleDuplicateFieldsDB = (err) => {
5089
- const value = err.keyValue ? JSON.stringify(err.keyValue) : "duplicate value";
5090
- return new AppError(`Duplicate field value: ${value}`, 400, {
5100
+ const field = err.keyValue ? Object.keys(err.keyValue)[0] : "field";
5101
+ return new AppError(`${field} already exists`, 400, {
5091
5102
  code: "DUPLICATE_FIELD"
5092
5103
  });
5093
5104
  };
5094
5105
  var handleValidationErrorDB = (err) => {
5095
- const errors = Object.values(err.errors).map((el) => el.message);
5096
- return new AppError("Invalid input data", 400, {
5106
+ const errors = Object.entries(err.errors).reduce(
5107
+ (acc, [key, value]) => {
5108
+ acc[key] = value.message;
5109
+ return acc;
5110
+ },
5111
+ {}
5112
+ );
5113
+ return new AppError("Validation failed", 400, {
5097
5114
  code: "VALIDATION_ERROR",
5098
5115
  details: errors
5116
+ // structured field errors
5099
5117
  });
5100
5118
  };
5101
5119
  var handleJWTError = () => new AppError("Invalid token. Please log in again.", 401);
@@ -5133,21 +5151,27 @@ var GlobalErrorHandler = (isProd = false) => (err, _req, res, _next) => {
5133
5151
  } else {
5134
5152
  logger.danger("Operational error", { message: error.message });
5135
5153
  }
5154
+ const baseResponse = {
5155
+ success: false,
5156
+ message: error.isOperational ? error.message : "Something went wrong",
5157
+ ...error.details && {
5158
+ meta: {
5159
+ errors: error.details
5160
+ }
5161
+ }
5162
+ };
5136
5163
  if (!isProd) {
5137
5164
  return APIResponse.send(res, {
5138
- success: false,
5139
5165
  statusCode: error.statusCode,
5140
- message: error.message,
5166
+ ...baseResponse,
5141
5167
  data: {
5142
- stack: err?.stack,
5143
- details: error.details
5168
+ stack: err?.stack
5144
5169
  }
5145
5170
  });
5146
5171
  }
5147
5172
  return APIResponse.send(res, {
5148
- success: false,
5149
5173
  statusCode: error.statusCode,
5150
- message: error.isOperational ? error.message : "Something went wrong"
5174
+ ...baseResponse
5151
5175
  });
5152
5176
  };
5153
5177