@carecard/common-util 3.1.3 → 3.1.4

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,69 @@ 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?: { userMessage?: string; details?: unknown }): never;
22
22
  /** Throws an Account_Blocked error. */
23
- export function throwAccountBlockedError(params?: { userMessage?: string; details?: any }): never;
23
+ export function throwAccountBlockedError(params?: { userMessage?: string; details?: unknown }): never;
24
24
  /** Throws an Account_Inactive error. */
25
- export function throwAccountInactiveError(params?: { userMessage?: string; details?: any }): never;
25
+ export function throwAccountInactiveError(params?: { userMessage?: string; details?: unknown }): never;
26
26
  /** Middleware to handle 404 Not Found. */
27
- export function notFound404(req: any, res: any, next: any): void;
27
+ export function notFound404(req: unknown, res: unknown, next: unknown): void;
28
28
  /** Central application error handler middleware. */
29
- export function appErrorHandler(err: any, req: any, res: any, next: any): void;
29
+ export function appErrorHandler(err: unknown, req: unknown, res: unknown, next: unknown): void;
30
30
  /** Throws a Validation_Failure error. */
31
31
  export function throwValidationFailureError(params?: {
32
32
  userMessage?: string;
33
- details?: any;
33
+ details?: unknown;
34
34
  }): never;
35
35
  /** Throws a Record_Exist error. */
36
- export function throwRecordExistError(params?: { userMessage?: string; details?: any }): never;
36
+ export function throwRecordExistError(params?: { userMessage?: string; details?: unknown }): never;
37
37
  /** Throws a Wrong_Credentials error. */
38
- export function throwWrongCredentialsError(params?: { userMessage?: string; details?: any }): never;
38
+ export function throwWrongCredentialsError(params?: { userMessage?: string; details?: unknown }): never;
39
39
  /** Throws a Login_Required error. */
40
- export function throwLoginRequiredError(params?: { userMessage?: string; details?: any }): never;
40
+ export function throwLoginRequiredError(params?: { userMessage?: string; details?: unknown }): never;
41
41
  /** Throws a Record_NotFound error. */
42
- export function throwRecordNotFoundError(params?: { userMessage?: string; details?: any }): never;
42
+ export function throwRecordNotFoundError(params?: { userMessage?: string; details?: unknown }): never;
43
43
  /** Throws a Record_NotSaved error. */
44
- export function throwRecordNotSavedError(params?: { userMessage?: string; details?: any }): never;
44
+ export function throwRecordNotSavedError(params?: { userMessage?: string; details?: unknown }): never;
45
45
  /** Throws an Update_Failed error. */
46
- export function throwUpdateFailedError(params?: { userMessage?: string; details?: any }): never;
46
+ export function throwUpdateFailedError(params?: { userMessage?: string; details?: unknown }): never;
47
47
  /** Throws a Transaction_Failed error. */
48
48
  export function throwTransactionFailedError(params?: {
49
49
  userMessage?: string;
50
- details?: any;
50
+ details?: unknown;
51
51
  }): never;
52
52
  /** Throws a Used_Token error. */
53
- export function throwUsedTokenError(params?: { userMessage?: string; details?: any }): never;
53
+ export function throwUsedTokenError(params?: { userMessage?: string; details?: unknown }): never;
54
54
  /** Throws a Bad_Visitor_Token error. */
55
- export function throwBadVisitorTokenError(params?: { userMessage?: string; details?: any }): never;
55
+ export function throwBadVisitorTokenError(params?: { userMessage?: string; details?: unknown }): never;
56
56
  /** Throws a File_Format_Not_Supported error. */
57
57
  export function throwFileFormatNotSupportedError(params?: {
58
58
  userMessage?: string;
59
- details?: any;
59
+ details?: unknown;
60
60
  }): never;
61
61
  /** Throws a Not_Authorized error. */
62
- export function throwNotAuthorizedError(params?: { userMessage?: string; details?: any }): never;
62
+ export function throwNotAuthorizedError(params?: { userMessage?: string; details?: unknown }): never;
63
63
  /** Throws a Bad_Input error. */
64
- export function throwBadInputError(params?: { userMessage?: string; details?: any }): never;
64
+ export function throwBadInputError(params?: { userMessage?: string; details?: unknown }): never;
65
65
  /** Throws an Input_Not_Uuid error. */
66
- export function throwInputNotUuidError(params?: { userMessage?: string; details?: any }): never;
66
+ export function throwInputNotUuidError(params?: { userMessage?: string; details?: unknown }): never;
67
67
  /** Throws a File_Too_Large error. */
68
- export function throwFileTooLargeError(params?: { userMessage?: string; details?: any }): never;
68
+ export function throwFileTooLargeError(params?: { userMessage?: string; details?: unknown }): never;
69
69
  /** Throws an Invalid_Time_Value error. */
70
- export function throwInvalidTimeValueError(params?: { userMessage?: string; details?: any }): never;
70
+ export function throwInvalidTimeValueError(params?: { userMessage?: string; details?: unknown }): never;
71
71
  /** Throws a Not_Found error. */
72
- export function throwNotFoundError(params?: { userMessage?: string; details?: any }): never;
72
+ export function throwNotFoundError(params?: { userMessage?: string; details?: unknown }): never;
73
73
  /** Throws a Record_Save_Failure error. */
74
74
  export function throwRecordSaveFailureError(params?: {
75
75
  userMessage?: string;
76
- details?: any;
76
+ details?: unknown;
77
77
  }): never;
78
78
  /** Throws an Application_Error error. */
79
- export function throwApplicationError(params?: { userMessage?: string; details?: any }): never;
79
+ export function throwApplicationError(params?: { userMessage?: string; details?: unknown }): never;
80
80
  /** Throws a Network_Error error. */
81
- export function throwNetworkError(params?: { userMessage?: string; details?: any }): never;
81
+ export function throwNetworkError(params?: { userMessage?: string; details?: unknown }): never;
82
82
  /** Throws an Unexpected_Error error. */
83
- export function throwUnexpectedError(params?: { userMessage?: string; details?: any }): never;
83
+ export function throwUnexpectedError(params?: { userMessage?: string; details?: unknown }): never;
84
84
 
85
85
  /**
86
86
  * Application-level error handlers and throwers.
@@ -116,11 +116,11 @@ export const error: {
116
116
  };
117
117
 
118
118
  /** Sets 200 OK status and optionally an ETag header. */
119
- export function setOk200(res: any, ETag?: string): any;
119
+ export function setOk200(res: unknown, ETag?: string): unknown;
120
120
  /** Sets 201 Created status. */
121
- export function setCreated201(res: any): any;
121
+ export function setCreated201(res: unknown): unknown;
122
122
  /** Sets 400 Bad Request status. */
123
- export function setBadRequest400ClientError(res: any): any;
123
+ export function setBadRequest400ClientError(res: unknown): unknown;
124
124
 
125
125
  /**
126
126
  * Utility functions for setting HTTP response status codes and headers.
@@ -153,7 +153,7 @@ export interface ApiResponseMeta {
153
153
  limit: number;
154
154
  total: number;
155
155
  };
156
- [key: string]: any;
156
+ [key: string]: unknown;
157
157
  }
158
158
 
159
159
  /**
@@ -169,7 +169,7 @@ export interface ApiError {
169
169
  /**
170
170
  * Standard API response body.
171
171
  */
172
- export interface ApiResponse<T = any> {
172
+ export interface ApiResponse<T = unknown> {
173
173
  success: boolean;
174
174
  statusCode: number;
175
175
  message: string;
@@ -181,21 +181,21 @@ export interface ApiResponse<T = any> {
181
181
  /**
182
182
  * Express middleware to generate and attach request context.
183
183
  */
184
- export function requestContext(req: any, res: any, next: any): void;
184
+ export function requestContext(req: unknown, res: unknown, next: unknown): void;
185
185
 
186
186
  /**
187
187
  * Standardized API response utility.
188
188
  */
189
- export function sendResponse<T = any>(params: {
190
- req: any;
191
- res: any;
189
+ export function sendResponse<T = unknown>(params: {
190
+ req: unknown;
191
+ res: unknown;
192
192
  statusCode?: number;
193
193
  success?: boolean;
194
194
  message?: string;
195
195
  data?: T | null;
196
196
  error?: ApiError | null;
197
197
  meta?: Partial<ApiResponseMeta>;
198
- }): any;
198
+ }): unknown;
199
199
 
200
200
  /**
201
201
  * Helper for standardized errors.
@@ -240,4 +240,4 @@ export enum ApiErrorType {
240
240
  /**
241
241
  * Extracts error message from standardized error data.
242
242
  */
243
- export function getApiErrorMessage(errorData: any, t: (key: string) => string): string;
243
+ 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.4",
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",
@@ -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) {
@@ -119,7 +142,7 @@ function getApiErrorMessage(errorData, t) {
119
142
 
120
143
  case ApiErrorType.UNEXPECTED_ERROR.toLowerCase():
121
144
  default:
122
- return t('errors.unexpected_error');
145
+ return null;
123
146
  }
124
147
  }
125
148