response-standardizer 1.3.2 → 1.3.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.js +2 -15
- package/package.json +1 -1
- package/src/index.ts +2 -16
package/dist/index.js
CHANGED
|
@@ -7,7 +7,6 @@ exports.ServiceResponseSuccess = exports.ServiceException = exports.handleServic
|
|
|
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 path_1 = __importDefault(require("path"));
|
|
11
10
|
const zod_1 = require("zod");
|
|
12
11
|
let KEYCLOAK_PUBLIC_KEY = null;
|
|
13
12
|
const initKeycloak = async (config) => {
|
|
@@ -52,7 +51,7 @@ const role = (req, res, next, allowedRoles) => {
|
|
|
52
51
|
const clientRoles = Object.values(user?.resource_access ?? {})
|
|
53
52
|
.flatMap((r) => r.roles ?? [])
|
|
54
53
|
.map((r) => r.toUpperCase());
|
|
55
|
-
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
54
|
+
const allowedUpper = allowedRoles.map(r => r === "super-admin" ? "ADMIN" : `DEZH-${r.toUpperCase()}`);
|
|
56
55
|
const allRoles = [...realmRoles, ...clientRoles];
|
|
57
56
|
const hasAccess = allowedUpper.some((role) => allRoles.includes(role));
|
|
58
57
|
if (!hasAccess) {
|
|
@@ -162,21 +161,9 @@ const log = (level, message, meta) => {
|
|
|
162
161
|
if (level === "ERROR")
|
|
163
162
|
color = colors.RED;
|
|
164
163
|
}
|
|
165
|
-
// گرفتن stack trace
|
|
166
|
-
const stack = new Error().stack?.split("\n")[2]; // خط Caller
|
|
167
|
-
let location = "";
|
|
168
|
-
if (stack) {
|
|
169
|
-
const match = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
170
|
-
if (match) {
|
|
171
|
-
const fileName = path_1.default.basename(match[1]);
|
|
172
|
-
const line = match[2];
|
|
173
|
-
const column = match[3];
|
|
174
|
-
location = `${fileName}:${line}:${column}`;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
164
|
const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
|
|
178
165
|
// چاپ لاگ
|
|
179
|
-
console.log(`${color}[${timestamp}][${
|
|
166
|
+
console.log(`${color}[${timestamp}][${level}] ${message}${metaStr}${colors.RESET}`);
|
|
180
167
|
};
|
|
181
168
|
exports.log = log;
|
|
182
169
|
const response = (req, res, response, handler = (eq, res, data = null) => { }) => {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -62,7 +62,7 @@ const role = (req: any, res: any, next: any, allowedRoles: string[]) => {
|
|
|
62
62
|
const clientRoles: string[] = Object.values(user?.resource_access ?? {})
|
|
63
63
|
.flatMap((r: any) => r.roles ?? [])
|
|
64
64
|
.map((r: string) => r.toUpperCase());
|
|
65
|
-
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
65
|
+
const allowedUpper = allowedRoles.map(r => r === "super-admin"?"ADMIN":`DEZH-${r.toUpperCase()}`);
|
|
66
66
|
const allRoles = [...realmRoles, ...clientRoles];
|
|
67
67
|
const hasAccess: boolean = allowedUpper.some((role: string) =>
|
|
68
68
|
allRoles.includes(role)
|
|
@@ -208,24 +208,10 @@ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: an
|
|
|
208
208
|
if (level === "WARN") color = colors.YELLOW;
|
|
209
209
|
if (level === "ERROR") color = colors.RED;
|
|
210
210
|
}
|
|
211
|
-
|
|
212
|
-
// گرفتن stack trace
|
|
213
|
-
const stack = new Error().stack?.split("\n")[2]; // خط Caller
|
|
214
|
-
let location = "";
|
|
215
|
-
if (stack) {
|
|
216
|
-
const match = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
217
|
-
if (match) {
|
|
218
|
-
const fileName = path.basename(match[1]);
|
|
219
|
-
const line = match[2];
|
|
220
|
-
const column = match[3];
|
|
221
|
-
location = `${fileName}:${line}:${column}`;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
211
|
const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
|
|
226
212
|
|
|
227
213
|
// چاپ لاگ
|
|
228
|
-
console.log(`${color}[${timestamp}][${
|
|
214
|
+
console.log(`${color}[${timestamp}][${level}] ${message}${metaStr}${colors.RESET}`);
|
|
229
215
|
};
|
|
230
216
|
export const response = (
|
|
231
217
|
req: Request,
|