lieko-express 1.0.0 → 1.0.1

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/lib/cors.js CHANGED
@@ -56,7 +56,6 @@ module.exports = function cors(userOptions = {}) {
56
56
  if (!allowed) {
57
57
  res.statusCode = 403;
58
58
  return res.end(JSON.stringify({
59
- success: false,
60
59
  error: "Origin Forbidden",
61
60
  message: `Origin "${requestOrigin}" is not allowed`
62
61
  }));
package/lib/schema.js CHANGED
@@ -371,15 +371,11 @@ function validate(schema) {
371
371
  try {
372
372
  schema.validate(req.body);
373
373
  next();
374
- } catch (error) {
375
- if (error instanceof ValidationError) {
376
- return res.status(400).json({
377
- success: false,
378
- message: 'Validation failed',
379
- errors: error.errors
380
- });
374
+ } catch (err) {
375
+ if (err instanceof ValidationError) {
376
+ return res.status(400).json({ error: { status: 400, type: "VALIDATION_ERROR", message: 'Validation failed', details: err.errors } });
381
377
  }
382
- throw error;
378
+ throw err;
383
379
  }
384
380
  };
385
381
  }
package/lieko-express.js CHANGED
@@ -781,7 +781,8 @@ ${cyan} (req, res, next) => {
781
781
  return res.status(500).json({
782
782
  error: {
783
783
  message: "Internal Server Error",
784
- code: 500
784
+ status: 500,
785
+ type: "InternalServerError"
785
786
  }
786
787
  });
787
788
  }
@@ -812,7 +813,7 @@ ${cyan} (req, res, next) => {
812
813
  res.status(500).json({
813
814
  error: {
814
815
  message: "Internal Server Error",
815
- code: 500
816
+ status: 500
816
817
  }
817
818
  });
818
819
  }
@@ -827,13 +828,12 @@ ${cyan} (req, res, next) => {
827
828
  return res.status(500).json({
828
829
  error: {
829
830
  message: "Invalid error format passed to res.error()",
830
- code: 500
831
+ status: 500
831
832
  }
832
833
  });
833
834
  }
834
835
 
835
836
  const HTTP_STATUS = {
836
- // 4xx – CLIENT ERRORS
837
837
  INVALID_REQUEST: 400,
838
838
  VALIDATION_FAILED: 400,
839
839
  NO_TOKEN_PROVIDED: 401,
@@ -844,18 +844,21 @@ ${cyan} (req, res, next) => {
844
844
  CONFLICT: 409,
845
845
  RECORD_EXISTS: 409,
846
846
  TOO_MANY_REQUESTS: 429,
847
-
848
- // 5xx – SERVER ERRORS
849
847
  SERVER_ERROR: 500,
850
848
  SERVICE_UNAVAILABLE: 503
851
849
  };
852
850
 
853
- const status = errorObj.status || HTTP_STATUS[errorObj.code] || 500;
851
+ let currentStatus = res.statusCode || 200;
852
+ let desiredStatus = errorObj.status || HTTP_STATUS[errorObj.status];
853
+
854
+ const finalStatus = (currentStatus >= 400 && currentStatus < 600)
855
+ ? currentStatus
856
+ : (desiredStatus || 500);
854
857
 
855
- return res.status(status).json({
858
+ return res.status(finalStatus).json({
856
859
  error: {
857
860
  message: errorObj.message || 'An error occurred',
858
- code: errorObj.code || 500,
861
+ status: errorObj.status || finalStatus,
859
862
  ...errorObj
860
863
  }
861
864
  });
@@ -982,7 +985,7 @@ ${cyan} (req, res, next) => {
982
985
  return res.status(413).json({
983
986
  error: {
984
987
  message: 'Payload Too Large',
985
- code: 413
988
+ status: 413
986
989
  }
987
990
  });
988
991
  }
@@ -1032,7 +1035,7 @@ ${cyan} (req, res, next) => {
1032
1035
 
1033
1036
  if (!route) {
1034
1037
  if (this.notFoundHandler) return this.notFoundHandler(req, res);
1035
- return res.status(404).json({ error: { message: 'Route not found', code: 404 } });
1038
+ return res.status(404).error('Not Found');
1036
1039
  }
1037
1040
 
1038
1041
  req.params = route.params;
@@ -1058,7 +1061,11 @@ ${cyan} (req, res, next) => {
1058
1061
 
1059
1062
  if (res.headersSent) return;
1060
1063
 
1061
- await route.handler(req, res);
1064
+ await route.handler(req, res, (err) => {
1065
+ if (err) {
1066
+ return this._runErrorHandlers(err, req, res);
1067
+ }
1068
+ });
1062
1069
 
1063
1070
  } catch (error) {
1064
1071
  if (!res.headersSent) {
@@ -1138,16 +1145,21 @@ ${cyan} (req, res, next) => {
1138
1145
  .map(item => item.type);
1139
1146
  };
1140
1147
 
1141
- const accepts = (types) => {
1148
+ const acceptedTypes = parseAccept(req.headers['accept']);
1149
+
1150
+ req.accepts = function (types) {
1151
+ if (arguments.length === 0) {
1152
+ return acceptedTypes;
1153
+ }
1154
+
1142
1155
  if (!Array.isArray(types)) types = [types];
1143
- const accepted = parseAccept(req.headers['accept']);
1144
1156
 
1145
1157
  for (const type of types) {
1146
1158
  const t = type.toLowerCase();
1147
1159
 
1148
- if (accepted.includes(t)) return type;
1160
+ if (acceptedTypes.includes(t)) return type;
1149
1161
 
1150
- if (accepted.some(a => {
1162
+ if (acceptedTypes.some(a => {
1151
1163
  if (a === '*/*') return true;
1152
1164
  if (a.endsWith('/*')) {
1153
1165
  const prefix = a.slice(0, -1);
@@ -1162,8 +1174,24 @@ ${cyan} (req, res, next) => {
1162
1174
  return false;
1163
1175
  };
1164
1176
 
1165
- req.accepts = function (types) {
1166
- return accepts(types);
1177
+ req.responseType = function () {
1178
+ const accepted = this.accepts();
1179
+
1180
+ if (accepted.length > 0) {
1181
+ for (const type of accepted) {
1182
+ if (type === 'text/html') return 'html';
1183
+ if (type === 'application/json') return 'json';
1184
+ if (type === '*/*') break;
1185
+ }
1186
+ }
1187
+
1188
+ const ua = (this.headers['user-agent'] || '').toLowerCase();
1189
+ const isApi = ua.includes('curl') ||
1190
+ ua.includes('postman') ||
1191
+ this.xhr ||
1192
+ this.headers['content-type']?.includes('application/json');
1193
+
1194
+ return isApi ? 'json' : 'html';
1167
1195
  };
1168
1196
 
1169
1197
  req.acceptsLanguages = function (langs) {
@@ -1538,6 +1566,7 @@ ${cyan} (req, res, next) => {
1538
1566
  status = 404;
1539
1567
  message = 'File Not Found';
1540
1568
  details = `The file "${filePath}" does not exist.\nFull path tried: ${file}`;
1569
+ console.error(details, err);
1541
1570
  } else if (err.code === 'FORBIDDEN') {
1542
1571
  status = 403;
1543
1572
  message = 'Forbidden';
@@ -1576,11 +1605,9 @@ ${cyan} (req, res, next) => {
1576
1605
  if (message !== undefined) payload.message = message;
1577
1606
  return res.json(payload);
1578
1607
  };
1579
- //res.success = res.ok;
1580
1608
 
1581
1609
  res.created = (data, message = 'Resource created successfully') => {
1582
- const payload = { success: true, data, message };
1583
- return res.status(201).json(payload);
1610
+ return res.status(201).json({ data, message });
1584
1611
  };
1585
1612
 
1586
1613
  res.noContent = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lieko-express",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/eiwSrvt/lieko-express"