response-standardizer 1.3.1 → 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.d.ts +1 -1
- package/dist/index.js +4 -17
- package/package.json +1 -1
- package/src/index.ts +4 -18
package/dist/index.d.ts
CHANGED
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) => { }) => {
|
|
@@ -198,7 +185,7 @@ const response = (req, res, response, handler = (eq, res, data = null) => { }) =
|
|
|
198
185
|
}
|
|
199
186
|
return exports.RestResponse.exceptionError(req, res, response.message);
|
|
200
187
|
}
|
|
201
|
-
return handler(req, res, response);
|
|
188
|
+
return handler(req, res, response.entity);
|
|
202
189
|
};
|
|
203
190
|
exports.response = response;
|
|
204
191
|
const handleServiceException = (message, err) => {
|
|
@@ -239,7 +226,7 @@ class ServiceException {
|
|
|
239
226
|
}
|
|
240
227
|
exports.ServiceException = ServiceException;
|
|
241
228
|
class ServiceResponseSuccess {
|
|
242
|
-
constructor(entity) {
|
|
229
|
+
constructor(entity = null) {
|
|
243
230
|
this.entity = entity;
|
|
244
231
|
}
|
|
245
232
|
}
|
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,
|
|
@@ -247,7 +233,7 @@ export const response = (
|
|
|
247
233
|
}
|
|
248
234
|
return RestResponse.exceptionError(req, res, response.message)
|
|
249
235
|
}
|
|
250
|
-
return handler(req, res, response)
|
|
236
|
+
return handler(req, res, (response as ServiceResponseSuccess).entity)
|
|
251
237
|
}
|
|
252
238
|
export const handleServiceException = (message: string, err: any) : ServiceResponse => {
|
|
253
239
|
if(err instanceof ZodError){
|
|
@@ -282,5 +268,5 @@ export class ServiceException implements ServiceResponse {
|
|
|
282
268
|
constructor(public code: number, public message: string, public errors?: ErrorFields) {}
|
|
283
269
|
}
|
|
284
270
|
export class ServiceResponseSuccess implements ServiceResponse {
|
|
285
|
-
constructor(public entity: any) {}
|
|
271
|
+
constructor(public entity: any = null) {}
|
|
286
272
|
}
|