response-standardizer 1.0.9 → 1.1.1

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.
Files changed (3) hide show
  1. package/dist/index.js +17 -14
  2. package/package.json +1 -1
  3. package/src/index.ts +19 -16
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ export const protect = (allowedRoles) => {
29
29
  next();
30
30
  }
31
31
  catch (err) {
32
- console.error(err);
32
+ error(err?.message);
33
33
  return RestResponse.unauthorized(req, res, "Token is not valid");
34
34
  }
35
35
  };
@@ -93,19 +93,22 @@ const responseHandlerMiddleware = (req, res, next) => {
93
93
  const oldJson = res.json.bind(res);
94
94
  const oldSend = res.send.bind(res);
95
95
  res.json = function (data) {
96
- const response = {
97
- status: res.statusCode >= 200 && res.statusCode < 300 ? "success" : "error",
98
- code: res.statusCode,
99
- message: data?.message || res.locals.message || (res.statusCode < 300 ? "OK" : "Error"),
100
- data: data?.data ?? null,
101
- errors: data?.errors ?? null,
102
- meta: {
103
- requestId: res.locals.requestId || generateRequestId(),
104
- timestamp: getTimestamp(),
105
- pagination: data?.pagination ?? null
106
- }
107
- };
108
- return oldJson(response);
96
+ if (res.statusCode > 204) {
97
+ const response = {
98
+ status: res.statusCode >= 200 && res.statusCode < 300 ? "success" : "error",
99
+ code: res.statusCode,
100
+ message: data?.message || res.locals.message || (res.statusCode < 300 ? "OK" : "Error"),
101
+ data: data?.data ?? null,
102
+ errors: data?.errors ?? null,
103
+ meta: {
104
+ requestId: res.locals.requestId || generateRequestId(),
105
+ timestamp: getTimestamp(),
106
+ pagination: data?.pagination ?? null
107
+ }
108
+ };
109
+ return oldJson(response);
110
+ }
111
+ return oldJson(data?.data);
109
112
  };
110
113
  res.send = function (data) {
111
114
  if (res.statusCode === 204) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
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
@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from "express";
2
2
  import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions, AuthRequest } from "./types.js";
3
3
  import { generateRequestId, getTimestamp } from "./utils.js";
4
4
  import axios from "axios";
5
- import jwt from "jsonwebtoken";
5
+ import jwt, { TokenExpiredError } from "jsonwebtoken";
6
6
  import path from "path";
7
7
  declare global {
8
8
  namespace Express {
@@ -46,7 +46,7 @@ export const protect = (allowedRoles?: string[]) => {
46
46
 
47
47
  next();
48
48
  } catch (err) {
49
- console.error(err)
49
+ error((err as any)?.message)
50
50
  return RestResponse.unauthorized(req, res, "Token is not valid")
51
51
  }
52
52
  }
@@ -145,20 +145,23 @@ const responseHandlerMiddleware = (
145
145
  const oldSend = res.send.bind(res);
146
146
 
147
147
  res.json = function (data: any) {
148
- const response: StandardResponse = {
149
- status: res.statusCode >= 200 && res.statusCode < 300 ? "success" : "error",
150
- code: res.statusCode,
151
- message: data?.message || res.locals.message || (res.statusCode < 300 ? "OK" : "Error"),
152
- data: data?.data ?? null,
153
- errors: data?.errors ?? null,
154
- meta: {
155
- requestId: res.locals.requestId || generateRequestId(),
156
- timestamp: getTimestamp(),
157
- pagination: data?.pagination ?? null
158
- }
159
- };
160
-
161
- return oldJson(response);
148
+ if(res.statusCode > 204){
149
+ const response: StandardResponse = {
150
+ status: res.statusCode >= 200 && res.statusCode < 300 ? "success" : "error",
151
+ code: res.statusCode,
152
+ message: data?.message || res.locals.message || (res.statusCode < 300 ? "OK" : "Error"),
153
+ data: data?.data ?? null,
154
+ errors: data?.errors ?? null,
155
+ meta: {
156
+ requestId: res.locals.requestId || generateRequestId(),
157
+ timestamp: getTimestamp(),
158
+ pagination: data?.pagination ?? null
159
+ }
160
+ };
161
+
162
+ return oldJson(response);
163
+ }
164
+ return oldJson(data?.data);
162
165
  };
163
166
 
164
167
  res.send = function (data: any) {