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.
- package/dist/index.js +17 -14
- package/package.json +1 -1
- 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
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
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
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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) {
|