@carecard/common-util 3.0.4 → 3.0.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/index.d.ts CHANGED
@@ -1,72 +1,103 @@
1
+ /**
2
+ * Creates a new object containing only the specified properties from the source object.
3
+ * @param obj - The source object.
4
+ * @param arrayOfProperties - An array of property names to extract.
5
+ * @returns A new object with the extracted properties.
6
+ */
7
+ export function extractObjectWithProperties(obj: any, arrayOfProperties: string[]): Record<string, any>;
8
+
1
9
  /**
2
10
  * Utility functions for object manipulation.
11
+ * @deprecated Use direct imports instead.
3
12
  */
4
13
  export const util: {
5
- /**
6
- * Creates a new object containing only the specified properties from the source object.
7
- * @param obj - The source object.
8
- * @param arrayOfProperties - An array of property names to extract.
9
- * @returns A new object with the extracted properties.
10
- */
11
- extractObjectWithProperties: (obj: any, arrayOfProperties: string[]) => Record<string, any>;
14
+ extractObjectWithProperties: typeof extractObjectWithProperties;
12
15
  };
13
16
 
17
+ /** Throws an Account_Suspended error. */
18
+ export function throwAccountSuspendedError(params?: { userMessage?: string, details?: any }): never;
19
+ /** Throws an Account_Blocked error. */
20
+ export function throwAccountBlockedError(params?: { userMessage?: string, details?: any }): never;
21
+ /** Throws an Account_Inactive error. */
22
+ export function throwAccountInactiveError(params?: { userMessage?: string, details?: any }): never;
23
+ /** Middleware to handle 404 Not Found. */
24
+ export function notFound404(req: any, res: any, next: any): void;
25
+ /** Central application error handler middleware. */
26
+ export function appErrorHandler(err: any, req: any, res: any, next: any): void;
27
+ /** Throws a Validation_Failure error. */
28
+ export function throwValidationFailureError(params?: { userMessage?: string, details?: any }): never;
29
+ /** Throws a Record_Exist error. */
30
+ export function throwRecordExistError(params?: { userMessage?: string, details?: any }): never;
31
+ /** Throws a Wrong_Credentials error. */
32
+ export function throwWrongCredentialsError(params?: { userMessage?: string, details?: any }): never;
33
+ /** Throws a Login_Required error. */
34
+ export function throwLoginRequiredError(params?: { userMessage?: string, details?: any }): never;
35
+ /** Throws a Record_NotFound error. */
36
+ export function throwRecordNotFoundError(params?: { userMessage?: string, details?: any }): never;
37
+ /** Throws a Record_NotSaved error. */
38
+ export function throwRecordNotSavedError(params?: { userMessage?: string, details?: any }): never;
39
+ /** Throws an Update_Failed error. */
40
+ export function throwUpdateFailedError(params?: { userMessage?: string, details?: any }): never;
41
+ /** Throws a Transaction_Failed error. */
42
+ export function throwTransactionFailedError(params?: { userMessage?: string, details?: any }): never;
43
+ /** Throws a Used_Token error. */
44
+ export function throwUsedTokenError(params?: { userMessage?: string, details?: any }): never;
45
+ /** Throws a Bad_Visitor_Token error. */
46
+ export function throwBadVisitorTokenError(params?: { userMessage?: string, details?: any }): never;
47
+ /** Throws a File_Format_Not_Supported error. */
48
+ export function throwFileFormatNotSupportedError(params?: { userMessage?: string, details?: any }): never;
49
+ /** Throws a Not_Authorized error. */
50
+ export function throwNotAuthorizedError(params?: { userMessage?: string, details?: any }): never;
51
+ /** Throws a Bad_Input error. */
52
+ export function throwBadInputError(params?: { userMessage?: string, details?: any }): never;
53
+ /** Throws an Input_Not_Uuid error. */
54
+ export function throwInputNotUuidError(params?: { userMessage?: string, details?: any }): never;
55
+ /** Throws a File_Too_Large error. */
56
+ export function throwFileTooLargeError(params?: { userMessage?: string, details?: any }): never;
57
+ /** Throws an Invalid_Time_Value error. */
58
+ export function throwInvalidTimeValueError(params?: { userMessage?: string, details?: any }): never;
59
+
14
60
  /**
15
61
  * Application-level error handlers and throwers.
62
+ * @deprecated Use direct imports instead.
16
63
  */
17
64
  export const error: {
18
- /** Throws an Account_Suspended error. */
19
- throwAccountSuspendedError: (params?: { userMessage?: string, details?: any }) => never;
20
- /** Throws an Account_Blocked error. */
21
- throwAccountBlockedError: (params?: { userMessage?: string, details?: any }) => never;
22
- /** Throws an Account_Inactive error. */
23
- throwAccountInactiveError: (params?: { userMessage?: string, details?: any }) => never;
24
- /** Middleware to handle 404 Not Found. */
25
- notFound404: (req: any, res: any, next: any) => void;
26
- /** Central application error handler middleware. */
27
- appErrorHandler: (err: any, req: any, res: any, next: any) => void;
28
- /** Throws a Validation_Failure error. */
29
- throwValidationFailureError: (params?: { userMessage?: string, details?: any }) => never;
30
- /** Throws a Record_Exist error. */
31
- throwRecordExistError: (params?: { userMessage?: string, details?: any }) => never;
32
- /** Throws a Wrong_Credentials error. */
33
- throwWrongCredentialsError: (params?: { userMessage?: string, details?: any }) => never;
34
- /** Throws a Login_Required error. */
35
- throwLoginRequiredError: (params?: { userMessage?: string, details?: any }) => never;
36
- /** Throws a Record_NotFound error. */
37
- throwRecordNotFoundError: (params?: { userMessage?: string, details?: any }) => never;
38
- /** Throws a Record_NotSaved error. */
39
- throwRecordNotSavedError: (params?: { userMessage?: string, details?: any }) => never;
40
- /** Throws an Update_Failed error. */
41
- throwUpdateFailedError: (params?: { userMessage?: string, details?: any }) => never;
42
- /** Throws a Transaction_Failed error. */
43
- throwTransactionFailedError: (params?: { userMessage?: string, details?: any }) => never;
44
- /** Throws a Used_Token error. */
45
- throwUsedTokenError: (params?: { userMessage?: string, details?: any }) => never;
46
- /** Throws a Bad_Visitor_Token error. */
47
- throwBadVisitorTokenError: (params?: { userMessage?: string, details?: any }) => never;
48
- /** Throws a File_Format_Not_Supported error. */
49
- throwFileFormatNotSupportedError: (params?: { userMessage?: string, details?: any }) => never;
50
- /** Throws a Not_Authorized error. */
51
- throwNotAuthorizedError: (params?: { userMessage?: string, details?: any }) => never;
52
- /** Throws a Bad_Input error. */
53
- throwBadInputError: (params?: { userMessage?: string, details?: any }) => never;
54
- /** Throws an Input_Not_Uuid error. */
55
- throwInputNotUuidError: (params?: { userMessage?: string, details?: any }) => never;
56
- /** Throws a File_Too_Large error. */
57
- throwFileTooLargeError: (params?: { userMessage?: string, details?: any }) => never;
58
- /** Throws an Invalid_Time_Value error. */
59
- throwInvalidTimeValueError: (params?: { userMessage?: string, details?: any }) => never;
65
+ throwAccountSuspendedError: typeof throwAccountSuspendedError;
66
+ throwAccountBlockedError: typeof throwAccountBlockedError;
67
+ throwAccountInactiveError: typeof throwAccountInactiveError;
68
+ notFound404: typeof notFound404;
69
+ appErrorHandler: typeof appErrorHandler;
70
+ throwValidationFailureError: typeof throwValidationFailureError;
71
+ throwRecordExistError: typeof throwRecordExistError;
72
+ throwWrongCredentialsError: typeof throwWrongCredentialsError;
73
+ throwLoginRequiredError: typeof throwLoginRequiredError;
74
+ throwRecordNotFoundError: typeof throwRecordNotFoundError;
75
+ throwRecordNotSavedError: typeof throwRecordNotSavedError;
76
+ throwUpdateFailedError: typeof throwUpdateFailedError;
77
+ throwTransactionFailedError: typeof throwTransactionFailedError;
78
+ throwUsedTokenError: typeof throwUsedTokenError;
79
+ throwBadVisitorTokenError: typeof throwBadVisitorTokenError;
80
+ throwFileFormatNotSupportedError: typeof throwFileFormatNotSupportedError;
81
+ throwNotAuthorizedError: typeof throwNotAuthorizedError;
82
+ throwBadInputError: typeof throwBadInputError;
83
+ throwInputNotUuidError: typeof throwInputNotUuidError;
84
+ throwFileTooLargeError: typeof throwFileTooLargeError;
85
+ throwInvalidTimeValueError: typeof throwInvalidTimeValueError;
60
86
  };
61
87
 
88
+ /** Sets 200 OK status and optionally an ETag header. */
89
+ export function setOk200(res: any, ETag?: string): any;
90
+ /** Sets 201 Created status. */
91
+ export function setCreated201(res: any): any;
92
+ /** Sets 400 Bad Request status. */
93
+ export function setBadRequest400ClientError(res: any): any;
94
+
62
95
  /**
63
96
  * Utility functions for setting HTTP response status codes and headers.
97
+ * @deprecated Use direct imports instead.
64
98
  */
65
99
  export const resCode: {
66
- /** Sets 200 OK status and optionally an ETag header. */
67
- setOk200: (res: any, ETag?: string) => any;
68
- /** Sets 201 Created status. */
69
- setCreated201: (res: any) => any;
70
- /** Sets 400 Bad Request status. */
71
- setBadRequest400ClientError: (res: any) => any;
100
+ setOk200: typeof setOk200;
101
+ setCreated201: typeof setCreated201;
102
+ setBadRequest400ClientError: typeof setBadRequest400ClientError;
72
103
  };
package/index.js CHANGED
@@ -1,5 +1,12 @@
1
+ const util = require( './lib/untilityFunctions' );
2
+ const error = require( './lib/appErrorHandlers' );
3
+ const resCode = require( './lib/responseStatus' );
4
+
1
5
  module.exports = {
2
- util: require( './lib/untilityFunctions' ),
3
- error: require( './lib/appErrorHandlers' ),
4
- resCode: require( './lib/responseStatus' )
6
+ util,
7
+ error,
8
+ resCode,
9
+ ...util,
10
+ ...error,
11
+ ...resCode
5
12
  }
@@ -29,6 +29,7 @@ module.exports = {
29
29
 
30
30
 
31
31
  function notFound404(req, res, next) {
32
+ console.log('404 Not Found:', req.method, req.originalUrl);
32
33
  res.status(404);
33
34
  const response = {
34
35
  error:
@@ -43,6 +44,10 @@ function notFound404(req, res, next) {
43
44
  }
44
45
 
45
46
  function appErrorHandler(err, req, res, next) {
47
+ if (res.headersSent) {
48
+ return next(err)
49
+ }
50
+
46
51
  const response =
47
52
  {
48
53
  error:
@@ -114,7 +119,7 @@ function appErrorHandler(err, req, res, next) {
114
119
  break;
115
120
 
116
121
  case "Not_Authorized":
117
- res.status(401);
122
+ res.status(403);
118
123
  res.send(response)
119
124
  break;
120
125
 
@@ -139,6 +144,7 @@ function appErrorHandler(err, req, res, next) {
139
144
  break;
140
145
 
141
146
  default:
147
+ console.error('Unhandled Error:', err);
142
148
  res.status(500);
143
149
  res.send(response)
144
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/common-util",
3
- "version": "3.0.4",
3
+ "version": "3.0.7",
4
4
  "repository": "https://github.com/CareCard-ca/pkg-common-util",
5
5
  "description": "Common utility for MVC framework",
6
6
  "main": "index.js",