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.cjs CHANGED
@@ -56,26 +56,37 @@ var APIResponse = class _APIResponse {
56
56
  if (statusCode === 204) {
57
57
  return res.status(204).send();
58
58
  }
59
- const body = { success, message };
60
- if (data !== void 0) body.data = data;
61
- if (meta !== void 0) body.meta = meta;
59
+ const body = {
60
+ success,
61
+ message,
62
+ ...data !== void 0 && { data },
63
+ ...meta !== void 0 && { meta }
64
+ };
62
65
  return res.status(statusCode).json(body);
63
66
  }
64
- static ok(res, data, message, meta) {
67
+ static ok(res, data, message = "Success", meta) {
65
68
  return _APIResponse.send(res, { statusCode: 200, data, message, meta });
66
69
  }
67
- static created(res, data, message) {
70
+ static created(res, data, message = "Resource created successfully") {
68
71
  return _APIResponse.send(res, { statusCode: 201, data, message });
69
72
  }
70
73
  static noContent(res) {
71
74
  return _APIResponse.send(res, { statusCode: 204 });
72
75
  }
73
- // Error response (optional use in controllers)
74
- static error(res, message, statusCode = 500) {
76
+ static error(res, message = "Internal Server Error", statusCode = 500, meta) {
75
77
  return _APIResponse.send(res, {
76
78
  success: false,
77
79
  statusCode,
78
- message
80
+ message,
81
+ meta
82
+ });
83
+ }
84
+ static paginated(res, data, options, message = "Data fetched successfully") {
85
+ return _APIResponse.ok(res, data, message, {
86
+ total: options.total,
87
+ page: options.page,
88
+ limit: options.limit,
89
+ totalPages: Math.ceil(options.total / options.limit)
79
90
  });
80
91
  }
81
92
  };
@@ -5101,16 +5112,23 @@ var handleCastErrorDB = (err) => new AppError(`Invalid ${err.path}: ${err.value}
5101
5112
  code: "INVALID_ID"
5102
5113
  });
5103
5114
  var handleDuplicateFieldsDB = (err) => {
5104
- const value = err.keyValue ? JSON.stringify(err.keyValue) : "duplicate value";
5105
- return new AppError(`Duplicate field value: ${value}`, 400, {
5115
+ const field = err.keyValue ? Object.keys(err.keyValue)[0] : "field";
5116
+ return new AppError(`${field} already exists`, 400, {
5106
5117
  code: "DUPLICATE_FIELD"
5107
5118
  });
5108
5119
  };
5109
5120
  var handleValidationErrorDB = (err) => {
5110
- const errors = Object.values(err.errors).map((el) => el.message);
5111
- return new AppError("Invalid input data", 400, {
5121
+ const errors = Object.entries(err.errors).reduce(
5122
+ (acc, [key, value]) => {
5123
+ acc[key] = value.message;
5124
+ return acc;
5125
+ },
5126
+ {}
5127
+ );
5128
+ return new AppError("Validation failed", 400, {
5112
5129
  code: "VALIDATION_ERROR",
5113
5130
  details: errors
5131
+ // structured field errors
5114
5132
  });
5115
5133
  };
5116
5134
  var handleJWTError = () => new AppError("Invalid token. Please log in again.", 401);
@@ -5148,21 +5166,27 @@ var GlobalErrorHandler = (isProd = false) => (err, _req, res, _next) => {
5148
5166
  } else {
5149
5167
  logger.danger("Operational error", { message: error.message });
5150
5168
  }
5169
+ const baseResponse = {
5170
+ success: false,
5171
+ message: error.isOperational ? error.message : "Something went wrong",
5172
+ ...error.details && {
5173
+ meta: {
5174
+ errors: error.details
5175
+ }
5176
+ }
5177
+ };
5151
5178
  if (!isProd) {
5152
5179
  return APIResponse.send(res, {
5153
- success: false,
5154
5180
  statusCode: error.statusCode,
5155
- message: error.message,
5181
+ ...baseResponse,
5156
5182
  data: {
5157
- stack: err?.stack,
5158
- details: error.details
5183
+ stack: err?.stack
5159
5184
  }
5160
5185
  });
5161
5186
  }
5162
5187
  return APIResponse.send(res, {
5163
- success: false,
5164
5188
  statusCode: error.statusCode,
5165
- message: error.isOperational ? error.message : "Something went wrong"
5189
+ ...baseResponse
5166
5190
  });
5167
5191
  };
5168
5192