response-standardizer 1.2.9 → 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,18 +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;
26
+ export declare const response: (req: Request, res: Response, response: ServiceResponse, handler?: <T>(eq: Request, res: Response, data: T) => void) => void;
28
27
  export declare const handleServiceException: (message: string, err: any) => ServiceResponse;
29
- export declare const handleValidationException: (error: any) => never;
30
- export declare class ServiceException extends Error {
31
- status: number;
32
- errors: any;
33
- constructor(message: string, status?: number, errors?: any);
28
+ export interface ServiceResponse {
34
29
  }
35
- export declare class ServiceResponse {
30
+ export declare class ServiceException implements ServiceResponse {
36
31
  code: number;
37
32
  message: string;
38
33
  errors?: ErrorFields | undefined;
39
34
  constructor(code: number, message: string, errors?: ErrorFields | undefined);
40
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.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,10 +196,11 @@ 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);
201
+ return handler(req, res, response);
224
202
  };
225
- exports.handleRestResponse = handleRestResponse;
203
+ exports.response = response;
226
204
  const handleServiceException = (message, err) => {
227
205
  if (err instanceof zod_1.ZodError) {
228
206
  const zodError = err;
@@ -234,7 +212,7 @@ const handleServiceException = (message, err) => {
234
212
  validationErrors[key] = [];
235
213
  validationErrors[key].push(i.message);
236
214
  });
237
- return new ServiceResponse(400, "Validation error", validationErrors);
215
+ return new ServiceException(400, "Validation error", validationErrors);
238
216
  }
239
217
  }
240
218
  else if (err?.isAxiosError && err?.response && err.response.status) {
@@ -243,44 +221,26 @@ const handleServiceException = (message, err) => {
243
221
  statusText: err.response.statusText,
244
222
  error: err.response.data
245
223
  });
246
- return new ServiceResponse(err.response.status, err.response.data?.error_description);
224
+ return new ServiceException(err.response.status, err.response.data?.error_description);
247
225
  }
248
226
  else if (err instanceof Error) {
249
227
  (0, exports.error)(message, { error: err });
250
- return new ServiceResponse(500, err?.message);
228
+ return new ServiceException(500, err?.message);
251
229
  }
252
- return new ServiceResponse(500, "Internal server error");
230
+ return new ServiceException(500, "Internal server error");
253
231
  };
254
232
  exports.handleServiceException = handleServiceException;
255
- const handleValidationException = (error) => {
256
- const zodError = error;
257
- if (zodError.issues) {
258
- const validationErrors = {};
259
- zodError.issues.forEach((i) => {
260
- const key = i.path[0];
261
- if (!validationErrors[key])
262
- validationErrors[key] = [];
263
- validationErrors[key].push(i.message);
264
- });
265
- throw new ServiceException("Validation error", 400, validationErrors);
266
- }
267
- throw new ServiceException("Internal server error", 500, error);
268
- };
269
- exports.handleValidationException = handleValidationException;
270
- class ServiceException extends Error {
271
- constructor(message, status = 400, errors = null) {
272
- super(message);
273
- this.name = "ServiceException";
274
- this.status = status;
275
- this.errors = errors;
276
- }
277
- }
278
- exports.ServiceException = ServiceException;
279
- class ServiceResponse {
233
+ class ServiceException {
280
234
  constructor(code, message, errors) {
281
235
  this.code = code;
282
236
  this.message = message;
283
237
  this.errors = errors;
284
238
  }
285
239
  }
286
- 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.9",
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,8 +245,9 @@ 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)
250
+ return handler(req, res, response)
264
251
  }
265
252
  export const handleServiceException = (message: string, err: any) : ServiceResponse => {
266
253
  if(err instanceof ZodError){
@@ -272,7 +259,7 @@ export const handleServiceException = (message: string, err: any) : ServiceRespo
272
259
  if (!validationErrors[key]) validationErrors[key] = [];
273
260
  validationErrors[key].push(i.message);
274
261
  });
275
- return new ServiceResponse(400, "Validation error", validationErrors)
262
+ return new ServiceException(400, "Validation error", validationErrors)
276
263
  }
277
264
  }else if((err as any)?.isAxiosError && (err as any)?.response && (err as any).response.status){
278
265
  error(message, {
@@ -280,37 +267,20 @@ export const handleServiceException = (message: string, err: any) : ServiceRespo
280
267
  statusText: (err as any).response.statusText,
281
268
  error: (err as any).response.data
282
269
  })
283
- 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);
284
271
  }else if(err instanceof Error){
285
272
  error(message, { error: err })
286
- return new ServiceResponse(500, err?.message);
273
+ return new ServiceException(500, err?.message);
287
274
  }
288
- return new ServiceResponse(500, "Internal server error");
289
- }
290
- export const handleValidationException = (error: any) => {
291
- const zodError = error as ZodError;
292
- if (zodError.issues) {
293
- const validationErrors: any = {};
294
- zodError.issues.forEach((i) => {
295
- const key = i.path[0];
296
- if (!validationErrors[key]) validationErrors[key] = [];
297
- validationErrors[key].push(i.message);
298
- });
299
- throw new ServiceException("Validation error", 400, validationErrors);
300
- }
301
- throw new ServiceException("Internal server error", 500, error);
275
+ return new ServiceException(500, "Internal server error");
302
276
  }
303
277
 
304
- export class ServiceException extends Error {
305
- status: number;
306
- errors: any
307
- constructor(message: string, status: number = 400, errors: any = null) {
308
- super(message);
309
- this.name = "ServiceException";
310
- this.status = status;
311
- this.errors = errors;
312
- }
278
+ export interface ServiceResponse{
279
+
313
280
  }
314
- export class ServiceResponse {
281
+ export class ServiceException implements ServiceResponse {
315
282
  constructor(public code: number, public message: string, public errors?: ErrorFields) {}
316
- }
283
+ }
284
+ export class ServiceResponseSuccess implements ServiceResponse {
285
+ constructor(public entity: any) {}
286
+ }