response-standardizer 1.1.6 → 1.1.8

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
@@ -1,5 +1,5 @@
1
1
  import { Request, Response } from "express";
2
- import { RestResponseFunctions, RestMiddlewareFunctions, AuthRequest } from "./types.js";
2
+ import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
3
3
  declare global {
4
4
  namespace Express {
5
5
  interface Response {
@@ -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 controller: (req: AuthRequest, res: Response, process: () => {}) => void;
27
+ export declare const handleValidationException: (error: Error) => void;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ 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";
5
6
  import { ServiceException } from "./service.exception.js";
6
7
  let KEYCLOAK_PUBLIC_KEY = null;
7
8
  export const initKeycloak = async (config) => {
@@ -189,11 +190,16 @@ export const handleRestException = (req, res, err) => {
189
190
  RestResponse.exceptionError(req, res, err.message);
190
191
  }
191
192
  };
192
- export const controller = (req, res, process) => {
193
- try {
194
- process();
195
- }
196
- catch (err) {
197
- handleRestException(req, res, err);
193
+ export const handleValidationException = (error) => {
194
+ if (error instanceof ZodError) {
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);
202
+ });
203
+ throw new ServiceException("Validation error", 400, validationErrors);
198
204
  }
199
205
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,7 +24,8 @@
24
24
  "dependencies": {
25
25
  "axios": "^1.13.2",
26
26
  "jsonwebtoken": "^9.0.2",
27
- "uuid": "^9.0.1"
27
+ "uuid": "^9.0.1",
28
+ "zod": "^4.1.12"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/express": "^5.0.5",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import { generateRequestId, getTimestamp } from "./utils.js";
4
4
  import axios from "axios";
5
5
  import jwt, { TokenExpiredError } from "jsonwebtoken";
6
6
  import path from "path";
7
+ import { ZodError } from "zod";
7
8
  import { ServiceException } from "./service.exception.js";
8
9
  declare global {
9
10
  namespace Express {
@@ -246,10 +247,16 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
246
247
  }
247
248
  }
248
249
 
249
- export const controller = (req: AuthRequest, res: Response, process: () => {}) => {
250
- try{
251
- process();
252
- }catch(err){
253
- handleRestException(req, res, err as Error)
250
+ export const handleValidationException = (error: Error) => {
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);
254
261
  }
255
262
  }
@@ -1,7 +1,3 @@
1
- import { Response } from "express";
2
- import { handleRestException } from ".";
3
- import { AuthRequest } from "./types";
4
-
5
1
  export class ServiceException extends Error {
6
2
  status: number;
7
3
  errors: any
@@ -11,5 +7,4 @@ export class ServiceException extends Error {
11
7
  this.status = status;
12
8
  this.errors = errors;
13
9
  }
14
- }
15
-
10
+ }