response-standardizer 1.1.9 → 1.2.1

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,9 @@ 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;
28
+ export declare class ServiceException extends Error {
29
+ status: number;
30
+ errors: any;
31
+ constructor(message: string, status?: number, errors?: any);
32
+ }
package/dist/index.js CHANGED
@@ -2,8 +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
- import { ServiceException } from "./service.exception.js";
7
5
  let KEYCLOAK_PUBLIC_KEY = null;
8
6
  export const initKeycloak = async (config) => {
9
7
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
@@ -191,15 +189,24 @@ export const handleRestException = (req, res, err) => {
191
189
  }
192
190
  };
193
191
  export const handleValidationException = (error) => {
194
- if (error instanceof ZodError) {
192
+ const zodError = error;
193
+ if (zodError.issues) {
195
194
  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);
195
+ zodError.issues.forEach((i) => {
196
+ const key = i.path[0];
197
+ if (!validationErrors[key])
198
+ validationErrors[key] = [];
199
+ validationErrors[key].push(i.message);
202
200
  });
203
201
  throw new ServiceException("Validation error", 400, validationErrors);
204
202
  }
205
- };
203
+ throw new ServiceException("Internal server error", 500, error);
204
+ };
205
+ export class ServiceException extends Error {
206
+ constructor(message, status = 400, errors = null) {
207
+ super(message);
208
+ this.name = "ServiceException";
209
+ this.status = status;
210
+ this.errors = errors;
211
+ }
212
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.9",
3
+ "version": "1.2.1",
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
@@ -5,7 +5,6 @@ import axios from "axios";
5
5
  import jwt, { TokenExpiredError } from "jsonwebtoken";
6
6
  import path from "path";
7
7
  import { ZodError } from "zod";
8
- import { ServiceException } from "./service.exception.js";
9
8
  declare global {
10
9
  namespace Express {
11
10
  interface Response {
@@ -248,15 +247,26 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
248
247
  }
249
248
 
250
249
  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);
250
+ const zodError = error as ZodError;
251
+ if (zodError.issues) {
252
+ const validationErrors: any = {};
253
+ zodError.issues.forEach((i) => {
254
+ const key = i.path[0];
255
+ if (!validationErrors[key]) validationErrors[key] = [];
256
+ validationErrors[key].push(i.message);
257
+ });
258
+ throw new ServiceException("Validation error", 400, validationErrors);
259
+ }
260
+ throw new ServiceException("Internal server error", 500, error);
261
+ }
262
+
263
+ export class ServiceException extends Error {
264
+ status: number;
265
+ errors: any
266
+ constructor(message: string, status: number = 400, errors: any = null) {
267
+ super(message);
268
+ this.name = "ServiceException";
269
+ this.status = status;
270
+ this.errors = errors;
261
271
  }
262
272
  }
@@ -1,5 +0,0 @@
1
- export declare class ServiceException extends Error {
2
- status: number;
3
- errors: any;
4
- constructor(message: string, status?: number, errors?: any);
5
- }
@@ -1,8 +0,0 @@
1
- export class ServiceException extends Error {
2
- constructor(message, status = 400, errors = null) {
3
- super(message);
4
- this.name = "ServiceException";
5
- this.status = status;
6
- this.errors = errors;
7
- }
8
- }
@@ -1,10 +0,0 @@
1
- export class ServiceException extends Error {
2
- status: number;
3
- errors: any
4
- constructor(message: string, status: number = 400, errors: any = null) {
5
- super(message);
6
- this.name = "ServiceException";
7
- this.status = status;
8
- this.errors = errors;
9
- }
10
- }