response-standardizer 1.2.7 → 1.2.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
@@ -25,6 +25,8 @@ 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
27
  export declare const handleRestResponse: (req: Request, res: Response, response: ServiceResponse) => void;
28
+ export declare const getValidationErrors: (error: any) => ServiceResponse | undefined;
29
+ export declare const handleServiceException: (message: string, err: any) => ServiceResponse | undefined;
28
30
  export declare const handleValidationException: (error: any) => never;
29
31
  export declare class ServiceException extends Error {
30
32
  status: number;
package/dist/index.js CHANGED
@@ -3,11 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ServiceResponse = exports.ServiceException = exports.handleValidationException = exports.handleRestResponse = exports.handleRestException = exports.log = exports.info = exports.warn = exports.error = exports.RestMiddleware = exports.RestResponse = exports.protect = exports.initKeycloak = void 0;
6
+ exports.ServiceResponse = exports.ServiceException = exports.handleValidationException = exports.handleServiceException = exports.getValidationErrors = exports.handleRestResponse = exports.handleRestException = exports.log = exports.info = exports.warn = exports.error = exports.RestMiddleware = exports.RestResponse = exports.protect = exports.initKeycloak = void 0;
7
7
  const utils_1 = require("./utils");
8
8
  const axios_1 = __importDefault(require("axios"));
9
9
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
10
10
  const path_1 = __importDefault(require("path"));
11
+ const zod_1 = require("zod");
11
12
  let KEYCLOAK_PUBLIC_KEY = null;
12
13
  const initKeycloak = async (config) => {
13
14
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
@@ -222,6 +223,39 @@ const handleRestResponse = (req, res, response) => {
222
223
  return exports.RestResponse.exceptionError(req, res, response.message);
223
224
  };
224
225
  exports.handleRestResponse = handleRestResponse;
226
+ const getValidationErrors = (error) => {
227
+ const zodError = error;
228
+ if (zodError.issues) {
229
+ const validationErrors = {};
230
+ zodError.issues.forEach((i) => {
231
+ const key = i.path[0];
232
+ if (!validationErrors[key])
233
+ validationErrors[key] = [];
234
+ validationErrors[key].push(i.message);
235
+ });
236
+ return new ServiceResponse(400, "Validation error", validationErrors);
237
+ }
238
+ };
239
+ exports.getValidationErrors = getValidationErrors;
240
+ const handleServiceException = (message, err) => {
241
+ if (err instanceof zod_1.ZodError) {
242
+ return (0, exports.getValidationErrors)(err);
243
+ }
244
+ else if (err?.isAxiosError && err?.response && err.response.status) {
245
+ (0, exports.error)(message, {
246
+ status: err.response.status,
247
+ statusText: err.response.statusText,
248
+ error: err.response.data
249
+ });
250
+ return new ServiceResponse(err.response.status, err.response.data?.error_description);
251
+ }
252
+ else if (err instanceof Error) {
253
+ (0, exports.error)(message, { error: err });
254
+ return new ServiceResponse(500, err?.message);
255
+ }
256
+ return new ServiceResponse(500, "Internal server error");
257
+ };
258
+ exports.handleServiceException = handleServiceException;
225
259
  const handleValidationException = (error) => {
226
260
  const zodError = error;
227
261
  if (zodError.issues) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
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
@@ -262,17 +262,44 @@ export const handleRestResponse = (req: Request, res: Response, response: Servic
262
262
  }
263
263
  return RestResponse.exceptionError(req, res, response.message)
264
264
  }
265
-
265
+ export const getValidationErrors = (error: any) : ServiceResponse | undefined => {
266
+ const zodError = error as ZodError;
267
+ if (zodError.issues) {
268
+ const validationErrors: any = {};
269
+ zodError.issues.forEach((i) => {
270
+ const key = i.path[0];
271
+ if (!validationErrors[key]) validationErrors[key] = [];
272
+ validationErrors[key].push(i.message);
273
+ });
274
+ return new ServiceResponse(400, "Validation error", validationErrors)
275
+ }
276
+ }
277
+ export const handleServiceException = (message: string, err: any) : ServiceResponse | undefined => {
278
+ if(err instanceof ZodError){
279
+ return getValidationErrors(err)
280
+ }else if((err as any)?.isAxiosError && (err as any)?.response && (err as any).response.status){
281
+ error(message, {
282
+ status: (err as any).response.status,
283
+ statusText: (err as any).response.statusText,
284
+ error: (err as any).response.data
285
+ })
286
+ return new ServiceResponse((err as any).response.status, (err as any).response.data?.error_description);
287
+ }else if(err instanceof Error){
288
+ error(message, { error: err })
289
+ return new ServiceResponse(500, err?.message);
290
+ }
291
+ return new ServiceResponse(500, "Internal server error");
292
+ }
266
293
  export const handleValidationException = (error: any) => {
267
294
  const zodError = error as ZodError;
268
295
  if (zodError.issues) {
269
- const validationErrors: any = {};
270
- zodError.issues.forEach((i) => {
271
- const key = i.path[0];
272
- if (!validationErrors[key]) validationErrors[key] = [];
273
- validationErrors[key].push(i.message);
274
- });
275
- throw new ServiceException("Validation error", 400, validationErrors);
296
+ const validationErrors: any = {};
297
+ zodError.issues.forEach((i) => {
298
+ const key = i.path[0];
299
+ if (!validationErrors[key]) validationErrors[key] = [];
300
+ validationErrors[key].push(i.message);
301
+ });
302
+ throw new ServiceException("Validation error", 400, validationErrors);
276
303
  }
277
304
  throw new ServiceException("Internal server error", 500, error);
278
305
  }