response-standardizer 1.2.8 → 1.3.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
@@ -23,19 +23,17 @@ export declare const error: (message: string, meta?: any) => void;
23
23
  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
- export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
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;
30
- export declare const handleValidationException: (error: any) => never;
31
- export declare class ServiceException extends Error {
32
- status: number;
33
- errors: any;
34
- constructor(message: string, status?: number, errors?: any);
26
+ export declare const response: (req: Request, res: Response, response: ServiceResponse, handler?: <T>(eq: Request, res: Response, data: T) => void) => void;
27
+ export declare const handleServiceException: (message: string, err: any) => ServiceResponse;
28
+ export interface ServiceResponse {
35
29
  }
36
- export declare class ServiceResponse {
30
+ export declare class ServiceException implements ServiceResponse {
37
31
  code: number;
38
32
  message: string;
39
33
  errors?: ErrorFields | undefined;
40
34
  constructor(code: number, message: string, errors?: ErrorFields | undefined);
41
35
  }
36
+ export declare class ServiceResponseSuccess implements ServiceResponse {
37
+ entity: any;
38
+ constructor(entity: any);
39
+ }
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ 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.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;
6
+ exports.ServiceResponseSuccess = exports.ServiceException = exports.handleServiceException = exports.response = 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"));
@@ -179,31 +179,8 @@ const log = (level, message, meta) => {
179
179
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
180
180
  };
181
181
  exports.log = log;
182
- const handleRestException = (req, res, err) => {
183
- if (err instanceof ServiceException) {
184
- if (err?.status === 401) {
185
- exports.RestResponse.unauthorized(req, res, err.message);
186
- }
187
- else if (err?.status === 403) {
188
- exports.RestResponse.accessDenied(req, res, err.message);
189
- }
190
- else if (err?.status === 400) {
191
- exports.RestResponse.validationError(req, res, err.errors);
192
- }
193
- else if (err?.status === 500) {
194
- exports.RestResponse.exceptionError(req, res, err.message);
195
- }
196
- else if (err?.status === 404) {
197
- exports.RestResponse.notFound(req, res, err.message);
198
- }
199
- }
200
- else if (err instanceof Error) {
201
- exports.RestResponse.exceptionError(req, res, err.message);
202
- }
203
- };
204
- exports.handleRestException = handleRestException;
205
- const handleRestResponse = (req, res, response) => {
206
- if (response instanceof ServiceResponse) {
182
+ const response = (req, res, response, handler = (eq, res, data) => { }) => {
183
+ if (response instanceof ServiceException) {
207
184
  if (response?.code === 401) {
208
185
  return exports.RestResponse.unauthorized(req, res, response?.message);
209
186
  }
@@ -219,27 +196,24 @@ const handleRestResponse = (req, res, response) => {
219
196
  else if (response?.code === 404) {
220
197
  return exports.RestResponse.notFound(req, res, response.message);
221
198
  }
199
+ return exports.RestResponse.exceptionError(req, res, response.message);
222
200
  }
223
- return exports.RestResponse.exceptionError(req, res, response.message);
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
- }
201
+ return handler(req, res, response);
238
202
  };
239
- exports.getValidationErrors = getValidationErrors;
203
+ exports.response = response;
240
204
  const handleServiceException = (message, err) => {
241
205
  if (err instanceof zod_1.ZodError) {
242
- return (0, exports.getValidationErrors)(err);
206
+ const zodError = err;
207
+ if (zodError.issues) {
208
+ const validationErrors = {};
209
+ zodError.issues.forEach((i) => {
210
+ const key = i.path[0];
211
+ if (!validationErrors[key])
212
+ validationErrors[key] = [];
213
+ validationErrors[key].push(i.message);
214
+ });
215
+ return new ServiceException(400, "Validation error", validationErrors);
216
+ }
243
217
  }
244
218
  else if (err?.isAxiosError && err?.response && err.response.status) {
245
219
  (0, exports.error)(message, {
@@ -247,44 +221,26 @@ const handleServiceException = (message, err) => {
247
221
  statusText: err.response.statusText,
248
222
  error: err.response.data
249
223
  });
250
- return new ServiceResponse(err.response.status, err.response.data?.error_description);
224
+ return new ServiceException(err.response.status, err.response.data?.error_description);
251
225
  }
252
226
  else if (err instanceof Error) {
253
227
  (0, exports.error)(message, { error: err });
254
- return new ServiceResponse(500, err?.message);
228
+ return new ServiceException(500, err?.message);
255
229
  }
256
- return new ServiceResponse(500, "Internal server error");
230
+ return new ServiceException(500, "Internal server error");
257
231
  };
258
232
  exports.handleServiceException = handleServiceException;
259
- const handleValidationException = (error) => {
260
- const zodError = error;
261
- if (zodError.issues) {
262
- const validationErrors = {};
263
- zodError.issues.forEach((i) => {
264
- const key = i.path[0];
265
- if (!validationErrors[key])
266
- validationErrors[key] = [];
267
- validationErrors[key].push(i.message);
268
- });
269
- throw new ServiceException("Validation error", 400, validationErrors);
270
- }
271
- throw new ServiceException("Internal server error", 500, error);
272
- };
273
- exports.handleValidationException = handleValidationException;
274
- class ServiceException extends Error {
275
- constructor(message, status = 400, errors = null) {
276
- super(message);
277
- this.name = "ServiceException";
278
- this.status = status;
279
- this.errors = errors;
280
- }
281
- }
282
- exports.ServiceException = ServiceException;
283
- class ServiceResponse {
233
+ class ServiceException {
284
234
  constructor(code, message, errors) {
285
235
  this.code = code;
286
236
  this.message = message;
287
237
  this.errors = errors;
288
238
  }
289
239
  }
290
- exports.ServiceResponse = ServiceResponse;
240
+ exports.ServiceException = ServiceException;
241
+ class ServiceResponseSuccess {
242
+ constructor(entity) {
243
+ this.entity = entity;
244
+ }
245
+ }
246
+ exports.ServiceResponseSuccess = ServiceResponseSuccess;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.2.8",
3
+ "version": "1.3.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
@@ -227,27 +227,13 @@ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: an
227
227
  // چاپ لاگ
228
228
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
229
229
  };
230
-
231
- export const handleRestException = (req: Request, res: Response, err: Error) => {
232
- if(err instanceof ServiceException){
233
- if(err?.status === 401){
234
- RestResponse.unauthorized(req, res, err.message)
235
- }else if(err?.status === 403){
236
- RestResponse.accessDenied(req, res, err.message)
237
- }else if(err?.status === 400){
238
- RestResponse.validationError(req, res, err.errors)
239
- }else if(err?.status === 500){
240
- RestResponse.exceptionError(req, res, err.message)
241
- }else if(err?.status === 404){
242
- RestResponse.notFound(req, res, err.message)
243
- }
244
- }else if(err instanceof Error){
245
- RestResponse.exceptionError(req, res, err.message)
246
- }
247
- }
248
-
249
- export const handleRestResponse = (req: Request, res: Response, response: ServiceResponse) => {
250
- if(response instanceof ServiceResponse){
230
+ export const response = (
231
+ req: Request,
232
+ res: Response,
233
+ response: ServiceResponse,
234
+ handler = <T>(eq: Request, res: Response, data: T) => {}
235
+ ) => {
236
+ if(response instanceof ServiceException){
251
237
  if(response?.code === 401){
252
238
  return RestResponse.unauthorized(req, res, response?.message)
253
239
  }else if(response?.code === 403){
@@ -259,61 +245,42 @@ export const handleRestResponse = (req: Request, res: Response, response: Servic
259
245
  }else if(response?.code === 404){
260
246
  return RestResponse.notFound(req, res, response.message)
261
247
  }
248
+ return RestResponse.exceptionError(req, res, response.message)
262
249
  }
263
- return RestResponse.exceptionError(req, res, response.message)
264
- }
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
- }
250
+ return handler(req, res, response)
276
251
  }
277
- export const handleServiceException = (message: string, err: any) : ServiceResponse | undefined => {
252
+ export const handleServiceException = (message: string, err: any) : ServiceResponse => {
278
253
  if(err instanceof ZodError){
279
- return getValidationErrors(err)
254
+ const zodError = err as ZodError;
255
+ if (zodError.issues) {
256
+ const validationErrors: any = {};
257
+ zodError.issues.forEach((i) => {
258
+ const key = i.path[0];
259
+ if (!validationErrors[key]) validationErrors[key] = [];
260
+ validationErrors[key].push(i.message);
261
+ });
262
+ return new ServiceException(400, "Validation error", validationErrors)
263
+ }
280
264
  }else if((err as any)?.isAxiosError && (err as any)?.response && (err as any).response.status){
281
265
  error(message, {
282
266
  status: (err as any).response.status,
283
267
  statusText: (err as any).response.statusText,
284
268
  error: (err as any).response.data
285
269
  })
286
- return new ServiceResponse((err as any).response.status, (err as any).response.data?.error_description);
270
+ return new ServiceException((err as any).response.status, (err as any).response.data?.error_description);
287
271
  }else if(err instanceof Error){
288
272
  error(message, { error: err })
289
- return new ServiceResponse(500, err?.message);
290
- }
291
- return new ServiceResponse(500, "Internal server error");
292
- }
293
- export const handleValidationException = (error: any) => {
294
- const zodError = error as ZodError;
295
- if (zodError.issues) {
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);
273
+ return new ServiceException(500, err?.message);
303
274
  }
304
- throw new ServiceException("Internal server error", 500, error);
275
+ return new ServiceException(500, "Internal server error");
305
276
  }
306
277
 
307
- export class ServiceException extends Error {
308
- status: number;
309
- errors: any
310
- constructor(message: string, status: number = 400, errors: any = null) {
311
- super(message);
312
- this.name = "ServiceException";
313
- this.status = status;
314
- this.errors = errors;
315
- }
278
+ export interface ServiceResponse{
279
+
316
280
  }
317
- export class ServiceResponse {
281
+ export class ServiceException implements ServiceResponse {
318
282
  constructor(public code: number, public message: string, public errors?: ErrorFields) {}
319
- }
283
+ }
284
+ export class ServiceResponseSuccess implements ServiceResponse {
285
+ constructor(public entity: any) {}
286
+ }