response-standardizer 1.1.2 → 1.1.3

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,3 +1,4 @@
1
+ import { Request, Response } from "express";
1
2
  import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
2
3
  declare global {
3
4
  namespace Express {
@@ -22,3 +23,4 @@ export declare const error: (message: string, meta?: any) => void;
22
23
  export declare const warn: (message: string, meta?: any) => void;
23
24
  export declare const info: (message: string, meta?: any) => void;
24
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;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { generateRequestId, getTimestamp } from "./utils.js";
2
2
  import axios from "axios";
3
3
  import jwt from "jsonwebtoken";
4
4
  import path from "path";
5
+ import { ServiceException } from "./service.exception.js";
5
6
  let KEYCLOAK_PUBLIC_KEY = null;
6
7
  export const initKeycloak = async (config) => {
7
8
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
@@ -162,3 +163,22 @@ export const log = (level, message, meta) => {
162
163
  // چاپ لاگ
163
164
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
164
165
  };
166
+ export const handleRestException = (req, res, err) => {
167
+ if (err instanceof ServiceException) {
168
+ if (err?.status === 401) {
169
+ RestResponse.unauthorized(req, res, err.message);
170
+ }
171
+ else if (err?.status === 403) {
172
+ RestResponse.accessDenied(req, res, err.message);
173
+ }
174
+ else if (err?.status === 400) {
175
+ RestResponse.validationError(req, res, err.errors);
176
+ }
177
+ else if (err?.status === 500) {
178
+ RestResponse.exceptionError(req, res, err.message);
179
+ }
180
+ }
181
+ else if (err instanceof Error) {
182
+ RestResponse.exceptionError(req, res, err.message);
183
+ }
184
+ };
@@ -0,0 +1,5 @@
1
+ export declare class ServiceException extends Error {
2
+ status: number;
3
+ errors: any;
4
+ constructor(message: string, status?: number, errors?: any);
5
+ }
@@ -0,0 +1,8 @@
1
+ export class ServiceException extends Error {
2
+ constructor(message, status = 400, errors = null) {
3
+ super(message);
4
+ this.name = "ServiceException";
5
+ this.status = status;
6
+ this.errors = errors;
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
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
@@ -4,6 +4,7 @@ import { generateRequestId, getTimestamp } from "./utils.js";
4
4
  import axios from "axios";
5
5
  import jwt, { TokenExpiredError } from "jsonwebtoken";
6
6
  import path from "path";
7
+ import { ServiceException } from "./service.exception.js";
7
8
  declare global {
8
9
  namespace Express {
9
10
  interface Response {
@@ -220,4 +221,20 @@ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: an
220
221
 
221
222
  // چاپ لاگ
222
223
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
223
- };
224
+ };
225
+
226
+ export const handleRestException = (req: Request, res: Response, err: Error) => {
227
+ if(err instanceof ServiceException){
228
+ if(err?.status === 401){
229
+ RestResponse.unauthorized(req, res, err.message)
230
+ }else if(err?.status === 403){
231
+ RestResponse.accessDenied(req, res, err.message)
232
+ }else if(err?.status === 400){
233
+ RestResponse.validationError(req, res, err.errors)
234
+ }else if(err?.status === 500){
235
+ RestResponse.exceptionError(req, res, err.message)
236
+ }
237
+ }else if(err instanceof Error){
238
+ RestResponse.exceptionError(req, res, err.message)
239
+ }
240
+ }
@@ -0,0 +1,10 @@
1
+ export class ServiceException extends Error {
2
+ status: number;
3
+ errors: any
4
+ constructor(message: string, status: number = 400, errors: any = null) {
5
+ super(message);
6
+ this.name = "ServiceException";
7
+ this.status = status;
8
+ this.errors = errors;
9
+ }
10
+ }
package/src/utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { v4 as uuidv4 } from "uuid";
2
+ import { RestResponse } from ".";
2
3
 
3
4
  export const generateRequestId = () => {
4
5
  return uuidv4();
@@ -6,4 +7,4 @@ export const generateRequestId = () => {
6
7
 
7
8
  export const getTimestamp = () => {
8
9
  return new Date().toISOString();
9
- }
10
+ }