response-standardizer 1.2.5 → 1.2.7

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,24 +202,24 @@ 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
- exports.RestResponse.unauthorized(req, res, response?.message);
207
+ return exports.RestResponse.unauthorized(req, res, response?.message);
208
208
  }
209
209
  else if (response?.code === 403) {
210
- exports.RestResponse.accessDenied(req, res, response?.message);
210
+ return exports.RestResponse.accessDenied(req, res, response?.message);
211
211
  }
212
212
  else if (response?.code === 400) {
213
- exports.RestResponse.validationError(req, res, response?.errors);
213
+ return exports.RestResponse.validationError(req, res, response?.errors);
214
214
  }
215
215
  else if (response?.code === 500) {
216
- exports.RestResponse.exceptionError(req, res, response.message);
216
+ return exports.RestResponse.exceptionError(req, res, response.message);
217
217
  }
218
218
  else if (response?.code === 404) {
219
- exports.RestResponse.notFound(req, res, response.message);
219
+ return exports.RestResponse.notFound(req, res, response.message);
220
220
  }
221
- exports.RestResponse.exceptionError(req, res, response.message);
222
221
  }
222
+ return exports.RestResponse.exceptionError(req, res, response.message);
223
223
  };
224
224
  exports.handleRestResponse = handleRestResponse;
225
225
  const handleValidationException = (error) => {
@@ -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?: ErrorFields;
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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
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";
@@ -247,20 +247,20 @@ 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
- RestResponse.unauthorized(req, res, response?.message)
252
+ return RestResponse.unauthorized(req, res, response?.message)
253
253
  }else if(response?.code === 403){
254
- RestResponse.accessDenied(req, res, response?.message)
254
+ return RestResponse.accessDenied(req, res, response?.message)
255
255
  }else if(response?.code === 400){
256
- RestResponse.validationError(req, res, response?.errors)
256
+ return RestResponse.validationError(req, res, response?.errors)
257
257
  }else if(response?.code === 500){
258
- RestResponse.exceptionError(req, res, response.message)
258
+ return RestResponse.exceptionError(req, res, response.message)
259
259
  }else if(response?.code === 404){
260
- RestResponse.notFound(req, res, response.message)
260
+ return RestResponse.notFound(req, res, response.message)
261
261
  }
262
- RestResponse.exceptionError(req, res, response.message)
263
262
  }
263
+ return RestResponse.exceptionError(req, res, response.message)
264
264
  }
265
265
 
266
266
  export const handleValidationException = (error: any) => {
@@ -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?: ErrorFields;
27
- }
28
23
 
29
24
  export type ErrorFields = Record<string, string[]>;
30
25