response-standardizer 1.1.2 → 1.1.4

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";
@@ -78,6 +79,9 @@ const unauthorized = (req, res, message = "Unauthorized") => {
78
79
  const accessDenied = (req, res, message = "Access denied") => {
79
80
  res.status(403).json({ message });
80
81
  };
82
+ const notFound = (req, res, message = "Not found") => {
83
+ res.status(404).json({ message });
84
+ };
81
85
  export const RestResponse = {
82
86
  success,
83
87
  paginate,
@@ -86,7 +90,8 @@ export const RestResponse = {
86
90
  validationError,
87
91
  exceptionError,
88
92
  unauthorized,
89
- accessDenied
93
+ accessDenied,
94
+ notFound
90
95
  };
91
96
  const responseHandlerMiddleware = (req, res, next) => {
92
97
  const oldJson = res.json.bind(res);
@@ -162,3 +167,25 @@ export const log = (level, message, meta) => {
162
167
  // چاپ لاگ
163
168
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
164
169
  };
170
+ export const handleRestException = (req, res, err) => {
171
+ if (err instanceof ServiceException) {
172
+ if (err?.status === 401) {
173
+ RestResponse.unauthorized(req, res, err.message);
174
+ }
175
+ else if (err?.status === 403) {
176
+ RestResponse.accessDenied(req, res, err.message);
177
+ }
178
+ else if (err?.status === 400) {
179
+ RestResponse.validationError(req, res, err.errors);
180
+ }
181
+ else if (err?.status === 500) {
182
+ RestResponse.exceptionError(req, res, err.message);
183
+ }
184
+ else if (err?.status === 404) {
185
+ RestResponse.notFound(req, res, err.message);
186
+ }
187
+ }
188
+ else if (err instanceof Error) {
189
+ RestResponse.exceptionError(req, res, err.message);
190
+ }
191
+ };
@@ -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/dist/types.d.ts CHANGED
@@ -29,6 +29,7 @@ export interface RestResponseFunctions {
29
29
  exceptionError: (req: Request, res: Response, errors: any, message?: string) => void;
30
30
  unauthorized: (req: Request, res: Response, message?: string) => void;
31
31
  accessDenied: (req: Request, res: Response, message?: string) => void;
32
+ notFound: (req: Request, res: Response, message?: string) => void;
32
33
  }
33
34
  export interface AuthRequest extends Request {
34
35
  user?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
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 {
@@ -120,6 +121,10 @@ const accessDenied = (req: Request, res: Response, message = "Access denied") =>
120
121
  res.status(403).json({ message });
121
122
  };
122
123
 
124
+ const notFound = (req: Request, res: Response, message = "Not found") => {
125
+ res.status(404).json({ message });
126
+ };
127
+
123
128
  export const RestResponse: RestResponseFunctions = {
124
129
  success,
125
130
  paginate,
@@ -128,7 +133,8 @@ export const RestResponse: RestResponseFunctions = {
128
133
  validationError,
129
134
  exceptionError,
130
135
  unauthorized,
131
- accessDenied
136
+ accessDenied,
137
+ notFound
132
138
  };
133
139
 
134
140
 
@@ -220,4 +226,22 @@ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: an
220
226
 
221
227
  // چاپ لاگ
222
228
  console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
223
- };
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
+ }
@@ -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/types.ts CHANGED
@@ -48,6 +48,7 @@ export interface RestResponseFunctions {
48
48
  ) => void;
49
49
  unauthorized: (req: Request, res: Response, message?: string) => void;
50
50
  accessDenied: (req: Request, res: Response, message?: string) => void;
51
+ notFound: (req: Request, res: Response, message?: string) => void;
51
52
  }
52
53
 
53
54
  export interface AuthRequest extends Request {
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
+ }