response-standardizer 1.2.4 → 1.2.6

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, ServiceResponse } from "./types";
2
+ import { ErrorFields, RestResponseFunctions, RestMiddlewareFunctions } from "./types";
3
3
  declare global {
4
4
  namespace Express {
5
5
  interface Response {
@@ -31,3 +31,9 @@ export declare class ServiceException extends Error {
31
31
  errors: any;
32
32
  constructor(message: string, status?: number, errors?: any);
33
33
  }
34
+ export declare class ServiceResponse {
35
+ code: number;
36
+ message: string;
37
+ errors?: ErrorFields | undefined;
38
+ constructor(code: number, message: string, errors?: ErrorFields | undefined);
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.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.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"));
@@ -202,15 +202,15 @@ const handleRestException = (req, res, err) => {
202
202
  };
203
203
  exports.handleRestException = handleRestException;
204
204
  const handleRestResponse = (req, res, response) => {
205
- if (response && response?.code) {
205
+ if (response instanceof ServiceResponse) {
206
206
  if (response?.code === 401) {
207
207
  exports.RestResponse.unauthorized(req, res, response?.message);
208
208
  }
209
209
  else if (response?.code === 403) {
210
210
  exports.RestResponse.accessDenied(req, res, response?.message);
211
211
  }
212
- else if (response?.code === 400 && response.errors) {
213
- exports.RestResponse.validationError(req, res, response.errors);
212
+ else if (response?.code === 400) {
213
+ exports.RestResponse.validationError(req, res, response?.errors);
214
214
  }
215
215
  else if (response?.code === 500) {
216
216
  exports.RestResponse.exceptionError(req, res, response.message);
@@ -246,3 +246,11 @@ class ServiceException extends Error {
246
246
  }
247
247
  }
248
248
  exports.ServiceException = ServiceException;
249
+ class ServiceResponse {
250
+ constructor(code, message, errors) {
251
+ this.code = code;
252
+ this.message = message;
253
+ this.errors = errors;
254
+ }
255
+ }
256
+ exports.ServiceResponse = ServiceResponse;
package/dist/types.d.ts CHANGED
@@ -18,11 +18,6 @@ export interface Pagination<T> {
18
18
  data: T[];
19
19
  meta: PaginationMeta;
20
20
  }
21
- export interface ServiceResponse {
22
- code: number;
23
- message: string;
24
- errors: Record<string, string[]> | null;
25
- }
26
21
  export type ErrorFields = Record<string, string[]>;
27
22
  export type Middleware = (req: Request, res: Response, next: NextFunction) => void;
28
23
  export interface RestMiddlewareFunctions {
@@ -33,7 +28,7 @@ export interface RestResponseFunctions {
33
28
  paginate: <T>(req: Request, res: Response, data: Pagination<T>) => void;
34
29
  created: <T>(req: Request, res: Response, data: T) => void;
35
30
  deleted: (req: Request, res: Response) => void;
36
- validationError: (req: Request, res: Response, errors: ErrorFields, message?: string) => void;
31
+ validationError: (req: Request, res: Response, errors?: ErrorFields, message?: string) => void;
37
32
  exceptionError: (req: Request, res: Response, errors: any, message?: string) => void;
38
33
  unauthorized: (req: Request, res: Response, message?: string) => void;
39
34
  accessDenied: (req: Request, res: Response, message?: string) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
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
@@ -1,5 +1,5 @@
1
1
  import { Request, Response, NextFunction } from "express";
2
- import { ErrorFields, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions, AuthRequest, Pagination, ServiceResponse } from "./types";
2
+ import { ErrorFields, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions, AuthRequest, Pagination } from "./types";
3
3
  import { generateRequestId, getTimestamp } from "./utils";
4
4
  import axios from "axios";
5
5
  import jwt, { TokenExpiredError } from "jsonwebtoken";
@@ -95,7 +95,7 @@ const deleted = (req: Request, res: Response) => {
95
95
  const validationError = (
96
96
  req: Request,
97
97
  res: Response,
98
- errors: ErrorFields,
98
+ errors?: ErrorFields,
99
99
  message = "Validation error"
100
100
  ) => {
101
101
  res.status(400).json({ errors, message });
@@ -247,13 +247,13 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
247
247
  }
248
248
 
249
249
  export const handleRestResponse = (req: Request, res: Response, response: ServiceResponse) => {
250
- if(response && response?.code){
250
+ if(response instanceof ServiceResponse){
251
251
  if(response?.code === 401){
252
252
  RestResponse.unauthorized(req, res, response?.message)
253
253
  }else if(response?.code === 403){
254
254
  RestResponse.accessDenied(req, res, response?.message)
255
- }else if(response?.code === 400 && response.errors){
256
- RestResponse.validationError(req, res, response.errors)
255
+ }else if(response?.code === 400){
256
+ RestResponse.validationError(req, res, response?.errors)
257
257
  }else if(response?.code === 500){
258
258
  RestResponse.exceptionError(req, res, response.message)
259
259
  }else if(response?.code === 404){
@@ -286,4 +286,7 @@ export class ServiceException extends Error {
286
286
  this.status = status;
287
287
  this.errors = errors;
288
288
  }
289
+ }
290
+ export class ServiceResponse {
291
+ constructor(public code: number, public message: string, public errors?: ErrorFields) {}
289
292
  }
package/src/types.ts CHANGED
@@ -20,11 +20,6 @@ export interface Pagination<T>{
20
20
  meta: PaginationMeta
21
21
  }
22
22
 
23
- export interface ServiceResponse {
24
- code: number;
25
- message: string;
26
- errors: Record<string, string[]> | null;
27
- }
28
23
 
29
24
  export type ErrorFields = Record<string, string[]>;
30
25
 
@@ -45,7 +40,7 @@ export interface RestResponseFunctions {
45
40
  validationError: (
46
41
  req: Request,
47
42
  res: Response,
48
- errors: ErrorFields,
43
+ errors?: ErrorFields,
49
44
  message?: string
50
45
  ) => void;
51
46
  exceptionError: (