@hastehaul/common 2.0.32 → 2.0.34

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ import { CustomError } from "./custom-error";
8
8
  */
9
9
  export declare class BadRequestError extends CustomError {
10
10
  message: string;
11
+ private code;
11
12
  /**
12
13
  * The HTTP status code associated with the "Bad Request" error (400).
13
14
  */
@@ -17,7 +18,7 @@ export declare class BadRequestError extends CustomError {
17
18
  *
18
19
  * @param {string} message - The error message providing details about the "Bad Request" error.
19
20
  */
20
- constructor(message: string);
21
+ constructor(message: string, code?: string);
21
22
  /**
22
23
  * Method to serialize the "Bad Request" error details into an array of error objects.
23
24
  *
@@ -28,5 +29,6 @@ export declare class BadRequestError extends CustomError {
28
29
  */
29
30
  serializeErrors(): Array<{
30
31
  message: string;
32
+ code: string;
31
33
  }>;
32
34
  }
@@ -15,9 +15,10 @@ class BadRequestError extends custom_error_1.CustomError {
15
15
  *
16
16
  * @param {string} message - The error message providing details about the "Bad Request" error.
17
17
  */
18
- constructor(message) {
18
+ constructor(message, code = "2301") {
19
19
  super(message);
20
20
  this.message = message;
21
+ this.code = code;
21
22
  /**
22
23
  * The HTTP status code associated with the "Bad Request" error (400).
23
24
  */
@@ -33,7 +34,7 @@ class BadRequestError extends custom_error_1.CustomError {
33
34
  * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
34
35
  */
35
36
  serializeErrors() {
36
- return [{ message: this.message }];
37
+ return [{ message: this.message, code: this.code }];
37
38
  }
38
39
  }
39
40
  exports.BadRequestError = BadRequestError;
@@ -23,10 +23,11 @@ export declare abstract class CustomError extends Error {
23
23
  * This method returns an array of error objects containing error messages and optional field information.
24
24
  * The specific implementation of this method depends on the type of custom error and how errors are serialized for responses.
25
25
  *
26
- * @returns {Array<{ message: string; field?: string }>} - An array of error objects containing error messages and optional field information.
26
+ * @returns {Array<{ message: string; code: string; field?: string }>} - An array of error objects containing error messages and optional field information.
27
27
  */
28
28
  abstract serializeErrors(): {
29
29
  message: string;
30
+ code: string;
30
31
  field?: string;
31
32
  }[];
32
33
  }
@@ -7,6 +7,7 @@ import { CustomError } from './custom-error';
7
7
  * Instances of this class are thrown when there are errors related to connecting to or interacting with the database.
8
8
  */
9
9
  export declare class DatabaseError extends CustomError {
10
+ private code;
10
11
  /**
11
12
  * The HTTP status code associated with the database-related error (500 - Internal Server Error).
12
13
  */
@@ -16,7 +17,7 @@ export declare class DatabaseError extends CustomError {
16
17
  *
17
18
  * @param {string} message - The error message providing details about the database-related error.
18
19
  */
19
- constructor(message?: string);
20
+ constructor(message?: string, code?: string);
20
21
  /**
21
22
  * Method to serialize the database-related error details into an array of error objects.
22
23
  *
@@ -27,5 +28,6 @@ export declare class DatabaseError extends CustomError {
27
28
  */
28
29
  serializeErrors: () => Array<{
29
30
  message: string;
31
+ code: string;
30
32
  }>;
31
33
  }
@@ -15,8 +15,9 @@ class DatabaseError extends custom_error_1.CustomError {
15
15
  *
16
16
  * @param {string} message - The error message providing details about the database-related error.
17
17
  */
18
- constructor(message = "Error connecting to database") {
18
+ constructor(message = "Error connecting to database", code = "2303") {
19
19
  super(message);
20
+ this.code = code;
20
21
  /**
21
22
  * The HTTP status code associated with the database-related error (500 - Internal Server Error).
22
23
  */
@@ -29,7 +30,7 @@ class DatabaseError extends custom_error_1.CustomError {
29
30
  *
30
31
  * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
31
32
  */
32
- this.serializeErrors = () => [{ message: this.message }];
33
+ this.serializeErrors = () => [{ message: this.message, code: this.code }];
33
34
  Object.setPrototypeOf(this, DatabaseError.prototype);
34
35
  }
35
36
  }
@@ -26,5 +26,6 @@ export declare class NotAuthorizedError extends CustomError {
26
26
  */
27
27
  serializeErrors(): {
28
28
  message: string;
29
+ code: string;
29
30
  }[];
30
31
  }
@@ -32,7 +32,7 @@ class NotAuthorizedError extends custom_error_1.CustomError {
32
32
  * @returns An array containing an object with the error message for the "Not Authorized" error.
33
33
  */
34
34
  serializeErrors() {
35
- return [{ message: 'Not authorized' }];
35
+ return [{ message: 'Not authorized', code: "2304" }];
36
36
  }
37
37
  }
38
38
  exports.NotAuthorizedError = NotAuthorizedError;
@@ -7,6 +7,7 @@ import { CustomError } from "./custom-error";
7
7
  * Instances of this class are thrown when a requested resource is not found.
8
8
  */
9
9
  export declare class NotFoundError extends CustomError {
10
+ private code;
10
11
  /**
11
12
  * The HTTP status code associated with the "Not Found" error (404).
12
13
  */
@@ -16,16 +17,17 @@ export declare class NotFoundError extends CustomError {
16
17
  *
17
18
  * @param {string} message - The error message providing details about the "Not Found" error.
18
19
  */
19
- constructor(message: string);
20
+ constructor(message: string, code?: string);
20
21
  /**
21
22
  * Method to serialize the "Not Found" error details into an array of error objects.
22
23
  *
23
24
  * This method returns an array containing a single error object with the error message provided during construction.
24
25
  * The array format is consistent with the `CustomError` class's expectation for error serialization.
25
26
  *
26
- * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
27
+ * @returns {Array<{ message: string; code: string }>} - An array containing a single error object with the error message.
27
28
  */
28
29
  serializeErrors: () => Array<{
29
30
  message: string;
31
+ code: string;
30
32
  }>;
31
33
  }
@@ -15,8 +15,9 @@ class NotFoundError extends custom_error_1.CustomError {
15
15
  *
16
16
  * @param {string} message - The error message providing details about the "Not Found" error.
17
17
  */
18
- constructor(message) {
18
+ constructor(message, code = "2305") {
19
19
  super(message);
20
+ this.code = code;
20
21
  /**
21
22
  * The HTTP status code associated with the "Not Found" error (404).
22
23
  */
@@ -27,9 +28,9 @@ class NotFoundError extends custom_error_1.CustomError {
27
28
  * This method returns an array containing a single error object with the error message provided during construction.
28
29
  * The array format is consistent with the `CustomError` class's expectation for error serialization.
29
30
  *
30
- * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
31
+ * @returns {Array<{ message: string; code: string }>} - An array containing a single error object with the error message.
31
32
  */
32
- this.serializeErrors = () => [{ message: this.message }];
33
+ this.serializeErrors = () => [{ message: this.message, code: this.code }];
33
34
  Object.setPrototypeOf(this, NotFoundError.prototype);
34
35
  }
35
36
  }
@@ -26,5 +26,6 @@ export declare class ResourceNotFound extends CustomError {
26
26
  */
27
27
  serializeErrors(): {
28
28
  message: string;
29
+ code: string;
29
30
  }[];
30
31
  }
@@ -31,7 +31,7 @@ class ResourceNotFound extends custom_error_1.CustomError {
31
31
  * @returns An array containing an object with the error message for the "Resource Not Found" error.
32
32
  */
33
33
  serializeErrors() {
34
- return [{ message: 'Resource Not Found' }];
34
+ return [{ message: 'Resource Not Found', code: "2306" }];
35
35
  }
36
36
  }
37
37
  exports.ResourceNotFound = ResourceNotFound;
@@ -7,6 +7,7 @@ import { CustomError } from "./custom-error";
7
7
  * Instances of this class are thrown when there are unexpected issues in the system.
8
8
  */
9
9
  export declare class SystemError extends CustomError {
10
+ private code;
10
11
  /**
11
12
  * The HTTP status code associated with the "System Error" (500 - Internal Server Error).
12
13
  */
@@ -17,16 +18,17 @@ export declare class SystemError extends CustomError {
17
18
  * @param {string} message - The error message providing details about the "System Error".
18
19
  * If no message is provided, the default message "Something went wrong" is used.
19
20
  */
20
- constructor(message?: string);
21
+ constructor(message?: string, code?: string);
21
22
  /**
22
23
  * Method to serialize the "System Error" details into an array of error objects.
23
24
  *
24
25
  * This method returns an array containing a single error object with the error message provided during construction.
25
26
  * The array format is consistent with the `CustomError` class's expectation for error serialization.
26
27
  *
27
- * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
28
+ * @returns {Array<{ message: string; code: string }>} - An array containing a single error object with the error message.
28
29
  */
29
30
  serializeErrors: () => Array<{
30
31
  message: string;
32
+ code: string;
31
33
  }>;
32
34
  }
@@ -16,8 +16,9 @@ class SystemError extends custom_error_1.CustomError {
16
16
  * @param {string} message - The error message providing details about the "System Error".
17
17
  * If no message is provided, the default message "Something went wrong" is used.
18
18
  */
19
- constructor(message = "Something went wrong") {
19
+ constructor(message = "Something went wrong", code = "2307") {
20
20
  super(message);
21
+ this.code = code;
21
22
  /**
22
23
  * The HTTP status code associated with the "System Error" (500 - Internal Server Error).
23
24
  */
@@ -28,9 +29,9 @@ class SystemError extends custom_error_1.CustomError {
28
29
  * This method returns an array containing a single error object with the error message provided during construction.
29
30
  * The array format is consistent with the `CustomError` class's expectation for error serialization.
30
31
  *
31
- * @returns {Array<{ message: string }>} - An array containing a single error object with the error message.
32
+ * @returns {Array<{ message: string; code: string }>} - An array containing a single error object with the error message.
32
33
  */
33
- this.serializeErrors = () => [{ message: this.message }];
34
+ this.serializeErrors = () => [{ message: this.message, code: this.code }];
34
35
  Object.setPrototypeOf(this, SystemError.prototype);
35
36
  }
36
37
  }
@@ -29,5 +29,6 @@ export declare class TooManyRequestError extends CustomError {
29
29
  */
30
30
  serializeErrors(): {
31
31
  message: string;
32
+ code: string;
32
33
  }[];
33
34
  }
@@ -36,7 +36,7 @@ class TooManyRequestError extends custom_error_1.CustomError {
36
36
  */
37
37
  serializeErrors() {
38
38
  return [
39
- { message: `Too Many Requests. Try again later` }
39
+ { message: `Too Many Requests. Try again later`, code: "2308" }
40
40
  ];
41
41
  }
42
42
  }
@@ -44,33 +44,28 @@ const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
44
44
  * @returns {Promise<void>} - A Promise that resolves once the user extraction is complete, but it is not used directly.
45
45
  */
46
46
  const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
47
- let result = null;
47
+ req.currentUser = null;
48
+ const bearerToken = req.headers['authorization'];
49
+ if (!bearerToken) {
50
+ return next();
51
+ }
48
52
  try {
49
- // Extract the Bearer token from the Authorization header.
50
- const bearerToken = req.headers["authorization"];
51
- if (typeof bearerToken !== "undefined" && bearerToken) {
52
- const token = bearerToken.split(" ")[1];
53
- if (token) {
54
- // Verify the JWT token and decode the user payload.
55
- result = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY.split("-").join(""));
56
- }
53
+ const token = bearerToken.split(' ')[1];
54
+ if (token) {
55
+ const result = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY.split('-').join(''));
56
+ req.currentUser = result;
57
57
  }
58
- // Attach the user payload to the request object for later use in the application.
59
- req.currentUser = result;
60
58
  }
61
59
  catch (err) {
62
- // Handle errors related to token verification or expiration.
63
60
  if (err instanceof jsonwebtoken_1.TokenExpiredError) {
64
61
  // If the token is expired, you may consider refreshing the token or taking appropriate actions.
65
62
  // Uncomment the following line if you want to handle token expiration using the AuthController.
66
63
  // await AuthController.refreshToken(req, res);
67
64
  }
68
65
  else {
69
- // If any other error occurs during token verification, set currentUser to null.
70
- req.currentUser = null;
66
+ console.error('Error verifying token:', err);
71
67
  }
72
68
  }
73
- // Call the next middleware in the chain.
74
69
  next();
75
70
  });
76
71
  exports.currentUser = currentUser;
@@ -53,7 +53,7 @@ const isAllowed = (req, res, next, hasVersion = false) => {
53
53
  // Determine the action based on the HTTP method
54
54
  const action = methodToAction[req.method.toUpperCase()];
55
55
  if (!action) {
56
- throw new bad_request_error_1.BadRequestError("Session expired. Please relogin.");
56
+ throw new bad_request_error_1.BadRequestError("Session expired. Please relogin.", "2201");
57
57
  }
58
58
  if (!resource) {
59
59
  throw new resource_not_found_1.ResourceNotFound();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.0.32",
3
+ "version": "2.0.34",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",