response-standardizer 1.1.9 → 1.2.0

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/dist/index.d.ts CHANGED
@@ -24,4 +24,4 @@ export declare const warn: (message: string, meta?: any) => void;
24
24
  export declare const info: (message: string, meta?: any) => void;
25
25
  export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
26
26
  export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
27
- export declare const handleValidationException: (error: any) => void;
27
+ export declare const handleValidationException: (error: any) => never;
package/dist/index.js CHANGED
@@ -2,7 +2,6 @@ import { generateRequestId, getTimestamp } from "./utils.js";
2
2
  import axios from "axios";
3
3
  import jwt from "jsonwebtoken";
4
4
  import path from "path";
5
- import { ZodError } from "zod";
6
5
  import { ServiceException } from "./service.exception.js";
7
6
  let KEYCLOAK_PUBLIC_KEY = null;
8
7
  export const initKeycloak = async (config) => {
@@ -191,15 +190,16 @@ export const handleRestException = (req, res, err) => {
191
190
  }
192
191
  };
193
192
  export const handleValidationException = (error) => {
194
- if (error instanceof ZodError) {
193
+ const zodError = error;
194
+ if (zodError.issues) {
195
195
  const validationErrors = {};
196
- error.issues.forEach((issue) => {
197
- const field = issue.path[0];
198
- if (!validationErrors[field]) {
199
- validationErrors[field] = [];
200
- }
201
- validationErrors[field].push(issue.message);
196
+ zodError.issues.forEach((i) => {
197
+ const key = i.path[0];
198
+ if (!validationErrors[key])
199
+ validationErrors[key] = [];
200
+ validationErrors[key].push(i.message);
202
201
  });
203
202
  throw new ServiceException("Validation error", 400, validationErrors);
204
203
  }
204
+ throw new ServiceException("Internal server error", 500, error);
205
205
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -248,15 +248,15 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
248
248
  }
249
249
 
250
250
  export const handleValidationException = (error: any) => {
251
- if (error instanceof ZodError) {
252
- const validationErrors: any = {};
253
- error.issues.forEach((issue) => {
254
- const field = issue.path[0] as string;
255
- if (!validationErrors[field]) {
256
- validationErrors[field] = [];
257
- }
258
- validationErrors[field].push(issue.message);
259
- });
260
- throw new ServiceException("Validation error", 400, validationErrors);
251
+ const zodError = error as ZodError;
252
+ if (zodError.issues) {
253
+ const validationErrors: any = {};
254
+ zodError.issues.forEach((i) => {
255
+ const key = i.path[0];
256
+ if (!validationErrors[key]) validationErrors[key] = [];
257
+ validationErrors[key].push(i.message);
258
+ });
259
+ throw new ServiceException("Validation error", 400, validationErrors);
261
260
  }
261
+ throw new ServiceException("Internal server error", 500, error);
262
262
  }