response-standardizer 1.3.3 → 1.3.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.
@@ -0,0 +1,26 @@
1
+ import { Request, Response } from "express";
2
+ import { ErrorFields } from "./types";
3
+ export declare class ServiceException extends Error {
4
+ statusCode: number;
5
+ constructor(statusCode: number, message: string);
6
+ }
7
+ export declare class UnknownException extends ServiceException {
8
+ constructor(statusCode: number, message: string);
9
+ }
10
+ export declare class NotFoundException extends ServiceException {
11
+ constructor(message: string);
12
+ }
13
+ export declare class UnauthorizedException extends ServiceException {
14
+ constructor(message: string);
15
+ }
16
+ export declare class AccessDeniedException extends ServiceException {
17
+ constructor(message: string);
18
+ }
19
+ export declare class InternalServerException extends ServiceException {
20
+ constructor(message: string);
21
+ }
22
+ export declare class ValidationException extends ServiceException {
23
+ errors?: ErrorFields;
24
+ constructor(message: string, errors?: ErrorFields);
25
+ }
26
+ export declare const handleControllerException: (req: Request, res: Response, err: Error) => void;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleControllerException = exports.ValidationException = exports.InternalServerException = exports.AccessDeniedException = exports.UnauthorizedException = exports.NotFoundException = exports.UnknownException = exports.ServiceException = void 0;
4
+ const _1 = require(".");
5
+ class ServiceException extends Error {
6
+ constructor(statusCode, message) {
7
+ super(message);
8
+ this.statusCode = statusCode;
9
+ }
10
+ }
11
+ exports.ServiceException = ServiceException;
12
+ class UnknownException extends ServiceException {
13
+ constructor(statusCode, message) {
14
+ super(statusCode, message);
15
+ Object.setPrototypeOf(this, new.target.prototype);
16
+ }
17
+ }
18
+ exports.UnknownException = UnknownException;
19
+ class NotFoundException extends ServiceException {
20
+ constructor(message) {
21
+ super(404, message);
22
+ Object.setPrototypeOf(this, new.target.prototype);
23
+ }
24
+ }
25
+ exports.NotFoundException = NotFoundException;
26
+ class UnauthorizedException extends ServiceException {
27
+ constructor(message) {
28
+ super(401, message);
29
+ Object.setPrototypeOf(this, new.target.prototype);
30
+ }
31
+ }
32
+ exports.UnauthorizedException = UnauthorizedException;
33
+ class AccessDeniedException extends ServiceException {
34
+ constructor(message) {
35
+ super(403, message);
36
+ Object.setPrototypeOf(this, new.target.prototype);
37
+ }
38
+ }
39
+ exports.AccessDeniedException = AccessDeniedException;
40
+ class InternalServerException extends ServiceException {
41
+ constructor(message) {
42
+ super(500, message);
43
+ Object.setPrototypeOf(this, new.target.prototype);
44
+ }
45
+ }
46
+ exports.InternalServerException = InternalServerException;
47
+ class ValidationException extends ServiceException {
48
+ constructor(message, errors) {
49
+ super(400, message);
50
+ this.errors = errors;
51
+ Object.setPrototypeOf(this, new.target.prototype);
52
+ }
53
+ }
54
+ exports.ValidationException = ValidationException;
55
+ const handleControllerException = (req, res, err) => {
56
+ if (err instanceof UnauthorizedException) {
57
+ return _1.RestResponse.unauthorized(req, res, err.message);
58
+ }
59
+ else if (err instanceof AccessDeniedException) {
60
+ return _1.RestResponse.accessDenied(req, res, err.message);
61
+ }
62
+ else if (err instanceof ValidationException) {
63
+ return _1.RestResponse.validationError(req, res, err?.errors, err?.message);
64
+ }
65
+ else if (err instanceof InternalServerException) {
66
+ return _1.RestResponse.exceptionError(req, res, err.message);
67
+ }
68
+ else if (err instanceof NotFoundException) {
69
+ return _1.RestResponse.notFound(req, res, err.message);
70
+ }
71
+ return _1.RestResponse.exceptionError(req, res, err.message);
72
+ };
73
+ exports.handleControllerException = handleControllerException;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { Request, Response } from "express";
2
- import { ErrorFields, RestResponseFunctions, RestMiddlewareFunctions } from "./types";
1
+ import { RestResponseFunctions, RestMiddlewareFunctions } from "./types";
3
2
  declare global {
4
3
  namespace Express {
5
4
  interface Response {
@@ -23,17 +22,3 @@ export declare const error: (message: string, meta?: any) => void;
23
22
  export declare const warn: (message: string, meta?: any) => void;
24
23
  export declare const info: (message: string, meta?: any) => void;
25
24
  export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
26
- export declare const response: (req: Request, res: Response, response: ServiceResponse, handler?: (eq: Request, res: Response, data?: any) => void) => void;
27
- export declare const handleServiceException: (message: string, err: any) => ServiceResponse;
28
- export interface ServiceResponse {
29
- }
30
- export declare class ServiceException implements ServiceResponse {
31
- code: number;
32
- message: string;
33
- errors?: ErrorFields | undefined;
34
- constructor(code: number, message: string, errors?: ErrorFields | undefined);
35
- }
36
- export declare class ServiceResponseSuccess implements ServiceResponse {
37
- entity: any;
38
- constructor(entity?: any);
39
- }
package/dist/index.js CHANGED
@@ -3,11 +3,10 @@ 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.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;
6
+ 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"));
10
- const zod_1 = require("zod");
11
10
  let KEYCLOAK_PUBLIC_KEY = null;
12
11
  const initKeycloak = async (config) => {
13
12
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
@@ -166,68 +165,3 @@ const log = (level, message, meta) => {
166
165
  console.log(`${color}[${timestamp}][${level}] ${message}${metaStr}${colors.RESET}`);
167
166
  };
168
167
  exports.log = log;
169
- const response = (req, res, response, handler = (eq, res, data = null) => { }) => {
170
- if (response instanceof ServiceException) {
171
- if (response?.code === 401) {
172
- return exports.RestResponse.unauthorized(req, res, response?.message);
173
- }
174
- else if (response?.code === 403) {
175
- return exports.RestResponse.accessDenied(req, res, response?.message);
176
- }
177
- else if (response?.code === 400) {
178
- return exports.RestResponse.validationError(req, res, response?.errors);
179
- }
180
- else if (response?.code === 500) {
181
- return exports.RestResponse.exceptionError(req, res, response.message);
182
- }
183
- else if (response?.code === 404) {
184
- return exports.RestResponse.notFound(req, res, response.message);
185
- }
186
- return exports.RestResponse.exceptionError(req, res, response.message);
187
- }
188
- return handler(req, res, response.entity);
189
- };
190
- exports.response = response;
191
- const handleServiceException = (message, err) => {
192
- if (err instanceof zod_1.ZodError) {
193
- const zodError = err;
194
- if (zodError.issues) {
195
- const validationErrors = {};
196
- zodError.issues.forEach((i) => {
197
- const key = i.path[0];
198
- if (!validationErrors[key])
199
- validationErrors[key] = [];
200
- validationErrors[key].push(i.message);
201
- });
202
- return new ServiceException(400, "Validation error", validationErrors);
203
- }
204
- }
205
- else if (err?.isAxiosError && err?.response && err.response.status) {
206
- (0, exports.error)(message, {
207
- status: err.response.status,
208
- statusText: err.response.statusText,
209
- error: err.response.data
210
- });
211
- return new ServiceException(err.response.status, err.response.data?.error_description);
212
- }
213
- else if (err instanceof Error) {
214
- (0, exports.error)(message, { error: err });
215
- return new ServiceException(500, err?.message);
216
- }
217
- return new ServiceException(500, "Internal server error");
218
- };
219
- exports.handleServiceException = handleServiceException;
220
- class ServiceException {
221
- constructor(code, message, errors) {
222
- this.code = code;
223
- this.message = message;
224
- this.errors = errors;
225
- }
226
- }
227
- exports.ServiceException = ServiceException;
228
- class ServiceResponseSuccess {
229
- constructor(entity = null) {
230
- this.entity = entity;
231
- }
232
- }
233
- exports.ServiceResponseSuccess = ServiceResponseSuccess;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,65 @@
1
+ import { Request, Response } from "express";
2
+ import { RestResponse } from ".";
3
+ import { ErrorFields } from "./types";
4
+ export class ServiceException extends Error{
5
+ statusCode: number;
6
+ constructor(statusCode: number, message: string) {
7
+ super(message);
8
+ this.statusCode = statusCode;
9
+ }
10
+ }
11
+ export class UnknownException extends ServiceException {
12
+ constructor(statusCode: number, message: string) {
13
+ super(statusCode, message);
14
+ Object.setPrototypeOf(this, new.target.prototype);
15
+ }
16
+ }
17
+ export class NotFoundException extends ServiceException {
18
+ constructor(message: string) {
19
+ super(404, message);
20
+ Object.setPrototypeOf(this, new.target.prototype);
21
+ }
22
+ }
23
+ export class UnauthorizedException extends ServiceException {
24
+ constructor(message: string) {
25
+ super(401, message);
26
+ Object.setPrototypeOf(this, new.target.prototype);
27
+ }
28
+ }
29
+ export class AccessDeniedException extends ServiceException {
30
+ constructor(message: string) {
31
+ super(403, message);
32
+ Object.setPrototypeOf(this, new.target.prototype);
33
+ }
34
+ }
35
+ export class InternalServerException extends ServiceException {
36
+ constructor(message: string) {
37
+ super(500, message);
38
+ Object.setPrototypeOf(this, new.target.prototype);
39
+ }
40
+ }
41
+ export class ValidationException extends ServiceException {
42
+ errors?: ErrorFields
43
+
44
+ constructor(message: string, errors?: ErrorFields) {
45
+ super(400, message);
46
+ this.errors = errors;
47
+ Object.setPrototypeOf(this, new.target.prototype);
48
+ }
49
+ }
50
+
51
+ export const handleControllerException = (req: Request, res: Response, err: Error) => {
52
+ if(err instanceof UnauthorizedException){
53
+ return RestResponse.unauthorized(req, res, err.message);
54
+ }else if(err instanceof AccessDeniedException){
55
+ return RestResponse.accessDenied(req, res, err.message);
56
+ }else if(err instanceof ValidationException){
57
+ return RestResponse.validationError(req, res, (err as any)?.errors, (err as any)?.message);
58
+ }else if(err instanceof InternalServerException){
59
+ return RestResponse.exceptionError(req, res, err.message);
60
+ }else if(err instanceof NotFoundException){
61
+ return RestResponse.notFound(req, res, err.message);
62
+ }
63
+
64
+ return RestResponse.exceptionError(req, res, err.message);
65
+ }
package/src/index.ts CHANGED
@@ -212,61 +212,4 @@ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: an
212
212
 
213
213
  // چاپ لاگ
214
214
  console.log(`${color}[${timestamp}][${level}] ${message}${metaStr}${colors.RESET}`);
215
- };
216
- export const response = (
217
- req: Request,
218
- res: Response,
219
- response: ServiceResponse,
220
- handler = (eq: Request, res: Response, data: any = null) => {}
221
- ) => {
222
- if(response instanceof ServiceException){
223
- if(response?.code === 401){
224
- return RestResponse.unauthorized(req, res, response?.message)
225
- }else if(response?.code === 403){
226
- return RestResponse.accessDenied(req, res, response?.message)
227
- }else if(response?.code === 400){
228
- return RestResponse.validationError(req, res, response?.errors)
229
- }else if(response?.code === 500){
230
- return RestResponse.exceptionError(req, res, response.message)
231
- }else if(response?.code === 404){
232
- return RestResponse.notFound(req, res, response.message)
233
- }
234
- return RestResponse.exceptionError(req, res, response.message)
235
- }
236
- return handler(req, res, (response as ServiceResponseSuccess).entity)
237
- }
238
- export const handleServiceException = (message: string, err: any) : ServiceResponse => {
239
- if(err instanceof ZodError){
240
- const zodError = err as ZodError;
241
- if (zodError.issues) {
242
- const validationErrors: any = {};
243
- zodError.issues.forEach((i) => {
244
- const key = i.path[0];
245
- if (!validationErrors[key]) validationErrors[key] = [];
246
- validationErrors[key].push(i.message);
247
- });
248
- return new ServiceException(400, "Validation error", validationErrors)
249
- }
250
- }else if((err as any)?.isAxiosError && (err as any)?.response && (err as any).response.status){
251
- error(message, {
252
- status: (err as any).response.status,
253
- statusText: (err as any).response.statusText,
254
- error: (err as any).response.data
255
- })
256
- return new ServiceException((err as any).response.status, (err as any).response.data?.error_description);
257
- }else if(err instanceof Error){
258
- error(message, { error: err })
259
- return new ServiceException(500, err?.message);
260
- }
261
- return new ServiceException(500, "Internal server error");
262
- }
263
-
264
- export interface ServiceResponse{
265
-
266
- }
267
- export class ServiceException implements ServiceResponse {
268
- constructor(public code: number, public message: string, public errors?: ErrorFields) {}
269
- }
270
- export class ServiceResponseSuccess implements ServiceResponse {
271
- constructor(public entity: any = null) {}
272
- }
215
+ };