@carecard/common-util 3.1.3 → 3.1.5

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
@@ -5,9 +5,9 @@
5
5
  * @returns A new object with the extracted properties.
6
6
  */
7
7
  export function extractObjectWithProperties(
8
- obj: any,
8
+ obj: unknown,
9
9
  arrayOfProperties: string[]
10
- ): Record<string, any>;
10
+ ): Record<string, unknown>;
11
11
 
12
12
  /**
13
13
  * Utility functions for object manipulation.
@@ -18,69 +18,99 @@ export const util: {
18
18
  };
19
19
 
20
20
  /** Throws an Account_Suspended error. */
21
- export function throwAccountSuspendedError(params?: { userMessage?: string; details?: any }): never;
21
+ export function throwAccountSuspendedError(params?: {
22
+ userMessage?: string;
23
+ details?: unknown;
24
+ }): never;
22
25
  /** Throws an Account_Blocked error. */
23
- export function throwAccountBlockedError(params?: { userMessage?: string; details?: any }): never;
26
+ export function throwAccountBlockedError(params?: {
27
+ userMessage?: string;
28
+ details?: unknown;
29
+ }): never;
24
30
  /** Throws an Account_Inactive error. */
25
- export function throwAccountInactiveError(params?: { userMessage?: string; details?: any }): never;
31
+ export function throwAccountInactiveError(params?: {
32
+ userMessage?: string;
33
+ details?: unknown;
34
+ }): never;
26
35
  /** Middleware to handle 404 Not Found. */
27
- export function notFound404(req: any, res: any, next: any): void;
36
+ export function notFound404(req: unknown, res: unknown, next: unknown): void;
28
37
  /** Central application error handler middleware. */
29
- export function appErrorHandler(err: any, req: any, res: any, next: any): void;
38
+ export function appErrorHandler(err: unknown, req: unknown, res: unknown, next: unknown): void;
30
39
  /** Throws a Validation_Failure error. */
31
40
  export function throwValidationFailureError(params?: {
32
41
  userMessage?: string;
33
- details?: any;
42
+ details?: unknown;
34
43
  }): never;
35
44
  /** Throws a Record_Exist error. */
36
- export function throwRecordExistError(params?: { userMessage?: string; details?: any }): never;
45
+ export function throwRecordExistError(params?: { userMessage?: string; details?: unknown }): never;
37
46
  /** Throws a Wrong_Credentials error. */
38
- export function throwWrongCredentialsError(params?: { userMessage?: string; details?: any }): never;
47
+ export function throwWrongCredentialsError(params?: {
48
+ userMessage?: string;
49
+ details?: unknown;
50
+ }): never;
39
51
  /** Throws a Login_Required error. */
40
- export function throwLoginRequiredError(params?: { userMessage?: string; details?: any }): never;
52
+ export function throwLoginRequiredError(params?: {
53
+ userMessage?: string;
54
+ details?: unknown;
55
+ }): never;
41
56
  /** Throws a Record_NotFound error. */
42
- export function throwRecordNotFoundError(params?: { userMessage?: string; details?: any }): never;
57
+ export function throwRecordNotFoundError(params?: {
58
+ userMessage?: string;
59
+ details?: unknown;
60
+ }): never;
43
61
  /** Throws a Record_NotSaved error. */
44
- export function throwRecordNotSavedError(params?: { userMessage?: string; details?: any }): never;
62
+ export function throwRecordNotSavedError(params?: {
63
+ userMessage?: string;
64
+ details?: unknown;
65
+ }): never;
45
66
  /** Throws an Update_Failed error. */
46
- export function throwUpdateFailedError(params?: { userMessage?: string; details?: any }): never;
67
+ export function throwUpdateFailedError(params?: { userMessage?: string; details?: unknown }): never;
47
68
  /** Throws a Transaction_Failed error. */
48
69
  export function throwTransactionFailedError(params?: {
49
70
  userMessage?: string;
50
- details?: any;
71
+ details?: unknown;
51
72
  }): never;
52
73
  /** Throws a Used_Token error. */
53
- export function throwUsedTokenError(params?: { userMessage?: string; details?: any }): never;
74
+ export function throwUsedTokenError(params?: { userMessage?: string; details?: unknown }): never;
54
75
  /** Throws a Bad_Visitor_Token error. */
55
- export function throwBadVisitorTokenError(params?: { userMessage?: string; details?: any }): never;
76
+ export function throwBadVisitorTokenError(params?: {
77
+ userMessage?: string;
78
+ details?: unknown;
79
+ }): never;
56
80
  /** Throws a File_Format_Not_Supported error. */
57
81
  export function throwFileFormatNotSupportedError(params?: {
58
82
  userMessage?: string;
59
- details?: any;
83
+ details?: unknown;
60
84
  }): never;
61
85
  /** Throws a Not_Authorized error. */
62
- export function throwNotAuthorizedError(params?: { userMessage?: string; details?: any }): never;
86
+ export function throwNotAuthorizedError(params?: {
87
+ userMessage?: string;
88
+ details?: unknown;
89
+ }): never;
63
90
  /** Throws a Bad_Input error. */
64
- export function throwBadInputError(params?: { userMessage?: string; details?: any }): never;
91
+ export function throwBadInputError(params?: { userMessage?: string; details?: unknown }): never;
65
92
  /** Throws an Input_Not_Uuid error. */
66
- export function throwInputNotUuidError(params?: { userMessage?: string; details?: any }): never;
93
+ export function throwInputNotUuidError(params?: { userMessage?: string; details?: unknown }): never;
67
94
  /** Throws a File_Too_Large error. */
68
- export function throwFileTooLargeError(params?: { userMessage?: string; details?: any }): never;
95
+ export function throwFileTooLargeError(params?: { userMessage?: string; details?: unknown }): never;
69
96
  /** Throws an Invalid_Time_Value error. */
70
- export function throwInvalidTimeValueError(params?: { userMessage?: string; details?: any }): never;
97
+ export function throwInvalidTimeValueError(params?: {
98
+ userMessage?: string;
99
+ details?: unknown;
100
+ }): never;
71
101
  /** Throws a Not_Found error. */
72
- export function throwNotFoundError(params?: { userMessage?: string; details?: any }): never;
102
+ export function throwNotFoundError(params?: { userMessage?: string; details?: unknown }): never;
73
103
  /** Throws a Record_Save_Failure error. */
74
104
  export function throwRecordSaveFailureError(params?: {
75
105
  userMessage?: string;
76
- details?: any;
106
+ details?: unknown;
77
107
  }): never;
78
108
  /** Throws an Application_Error error. */
79
- export function throwApplicationError(params?: { userMessage?: string; details?: any }): never;
109
+ export function throwApplicationError(params?: { userMessage?: string; details?: unknown }): never;
80
110
  /** Throws a Network_Error error. */
81
- export function throwNetworkError(params?: { userMessage?: string; details?: any }): never;
111
+ export function throwNetworkError(params?: { userMessage?: string; details?: unknown }): never;
82
112
  /** Throws an Unexpected_Error error. */
83
- export function throwUnexpectedError(params?: { userMessage?: string; details?: any }): never;
113
+ export function throwUnexpectedError(params?: { userMessage?: string; details?: unknown }): never;
84
114
 
85
115
  /**
86
116
  * Application-level error handlers and throwers.
@@ -116,11 +146,11 @@ export const error: {
116
146
  };
117
147
 
118
148
  /** Sets 200 OK status and optionally an ETag header. */
119
- export function setOk200(res: any, ETag?: string): any;
149
+ export function setOk200(res: unknown, ETag?: string): unknown;
120
150
  /** Sets 201 Created status. */
121
- export function setCreated201(res: any): any;
151
+ export function setCreated201(res: unknown): unknown;
122
152
  /** Sets 400 Bad Request status. */
123
- export function setBadRequest400ClientError(res: any): any;
153
+ export function setBadRequest400ClientError(res: unknown): unknown;
124
154
 
125
155
  /**
126
156
  * Utility functions for setting HTTP response status codes and headers.
@@ -153,7 +183,7 @@ export interface ApiResponseMeta {
153
183
  limit: number;
154
184
  total: number;
155
185
  };
156
- [key: string]: any;
186
+ [key: string]: unknown;
157
187
  }
158
188
 
159
189
  /**
@@ -169,7 +199,7 @@ export interface ApiError {
169
199
  /**
170
200
  * Standard API response body.
171
201
  */
172
- export interface ApiResponse<T = any> {
202
+ export interface ApiResponse<T = unknown> {
173
203
  success: boolean;
174
204
  statusCode: number;
175
205
  message: string;
@@ -181,21 +211,21 @@ export interface ApiResponse<T = any> {
181
211
  /**
182
212
  * Express middleware to generate and attach request context.
183
213
  */
184
- export function requestContext(req: any, res: any, next: any): void;
214
+ export function requestContext(req: unknown, res: unknown, next: unknown): void;
185
215
 
186
216
  /**
187
217
  * Standardized API response utility.
188
218
  */
189
- export function sendResponse<T = any>(params: {
190
- req: any;
191
- res: any;
219
+ export function sendResponse<T = unknown>(params: {
220
+ req: unknown;
221
+ res: unknown;
192
222
  statusCode?: number;
193
223
  success?: boolean;
194
224
  message?: string;
195
225
  data?: T | null;
196
226
  error?: ApiError | null;
197
227
  meta?: Partial<ApiResponseMeta>;
198
- }): any;
228
+ }): unknown;
199
229
 
200
230
  /**
201
231
  * Helper for standardized errors.
@@ -234,10 +264,12 @@ export enum ApiErrorType {
234
264
  BAD_INPUT = 'BAD_INPUT',
235
265
  INPUT_NOT_UUID = 'INPUT_NOT_UUID',
236
266
  FILE_TOO_LARGE = 'FILE_TOO_LARGE',
237
- INVALID_TIME_VALUE = 'INVALID_TIME_VALUE'
267
+ INVALID_TIME_VALUE = 'INVALID_TIME_VALUE',
268
+ UNKNOWN_ERROR = 'UNKNOWN_ERROR',
269
+ PARSE_ERROR = 'PARSE_ERROR'
238
270
  }
239
271
 
240
272
  /**
241
273
  * Extracts error message from standardized error data.
242
274
  */
243
- export function getApiErrorMessage(errorData: any, t: (key: string) => string): string;
275
+ export function getApiErrorMessage(errorData: unknown, t: (key: string) => string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/common-util",
3
- "version": "3.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "Standardized API response and utility functions for Express.js and Next.js microservices",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -31,7 +31,9 @@ const ApiErrorType = {
31
31
  BAD_INPUT: 'BAD_INPUT',
32
32
  INPUT_NOT_UUID: 'INPUT_NOT_UUID',
33
33
  FILE_TOO_LARGE: 'FILE_TOO_LARGE',
34
- INVALID_TIME_VALUE: 'INVALID_TIME_VALUE'
34
+ INVALID_TIME_VALUE: 'INVALID_TIME_VALUE',
35
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
36
+ PARSE_ERROR: 'PARSE_ERROR'
35
37
  };
36
38
 
37
39
  module.exports = {
@@ -27,10 +27,6 @@ function getApiErrorMessage(errorData, t) {
27
27
  message = errorData.message;
28
28
  }
29
29
 
30
- if (message) {
31
- return message;
32
- }
33
-
34
30
  // Try to get error code
35
31
  let errorCode;
36
32
  if (errorData.error) {
@@ -41,10 +37,37 @@ function getApiErrorMessage(errorData, t) {
41
37
  }
42
38
  }
43
39
 
40
+ // If we have a recognized error code, try to translate it first
41
+ // Unless we have a message that doesn't look like an error code
42
+ if (errorCode) {
43
+ const translated = translateCode(errorCode, t);
44
+ if (translated && translated !== errorCode && translated !== t('errors.unexpected_error')) {
45
+ // If we have a translation, and it's not the code itself, return it
46
+ // But wait, if the message is specific, we might still want it.
47
+ // For now, let's just return the translated message if it's found.
48
+ return translated;
49
+ }
50
+ }
51
+
52
+ if (message) {
53
+ return message;
54
+ }
55
+
44
56
  if (!errorCode) {
45
57
  return t('errors.unexpected_error');
46
58
  }
47
59
 
60
+ return translateCode(errorCode, t) || t('errors.unexpected_error');
61
+ }
62
+
63
+ /**
64
+ * Helper to translate error code.
65
+ * @param {string} errorCode
66
+ * @param {Function} t
67
+ * @returns {string|null}
68
+ */
69
+ function translateCode(errorCode, t) {
70
+ if (!errorCode) return null;
48
71
  const code = errorCode.toLowerCase();
49
72
 
50
73
  switch (code) {
@@ -117,9 +140,15 @@ function getApiErrorMessage(errorData, t) {
117
140
  case ApiErrorType.INVALID_TIME_VALUE.toLowerCase():
118
141
  return t('errors.invalid_time_value');
119
142
 
143
+ case ApiErrorType.UNKNOWN_ERROR.toLowerCase():
144
+ return t('errors.unknown_error');
145
+
146
+ case ApiErrorType.PARSE_ERROR.toLowerCase():
147
+ return t('errors.parse_error');
148
+
120
149
  case ApiErrorType.UNEXPECTED_ERROR.toLowerCase():
121
150
  default:
122
- return t('errors.unexpected_error');
151
+ return null;
123
152
  }
124
153
  }
125
154