response-standardizer 1.0.5 → 1.0.6
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 -0
- package/dist/index.js +36 -0
- package/package.json +1 -1
- package/src/index.ts +42 -1
package/dist/index.d.ts
CHANGED
|
@@ -18,3 +18,4 @@ export declare const initKeycloak: (config: {
|
|
|
18
18
|
export declare const protect: (allowedRoles?: string[]) => (req: any, res: any, next: any) => void;
|
|
19
19
|
export declare const RestResponse: RestResponseFunctions;
|
|
20
20
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
|
21
|
+
export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
import jwt from "jsonwebtoken";
|
|
4
|
+
import path from "path";
|
|
4
5
|
let KEYCLOAK_PUBLIC_KEY = null;
|
|
5
6
|
export const initKeycloak = async (config) => {
|
|
6
7
|
const KEYCLOAK_SERVICE = config.service ?? "localhost";
|
|
@@ -117,3 +118,38 @@ const responseHandlerMiddleware = (req, res, next) => {
|
|
|
117
118
|
export const RestMiddleware = {
|
|
118
119
|
responseHandlerMiddleware
|
|
119
120
|
};
|
|
121
|
+
const colors = {
|
|
122
|
+
RESET: "\x1b[0m",
|
|
123
|
+
RED: "\x1b[31m",
|
|
124
|
+
GREEN: "\x1b[32m",
|
|
125
|
+
YELLOW: "\x1b[33m"
|
|
126
|
+
};
|
|
127
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
128
|
+
export const log = (level, message, meta) => {
|
|
129
|
+
const timestamp = new Date().toISOString();
|
|
130
|
+
// انتخاب رنگ فقط در dev
|
|
131
|
+
let color = colors.RESET;
|
|
132
|
+
if (!isProduction) {
|
|
133
|
+
if (level === "INFO")
|
|
134
|
+
color = colors.GREEN;
|
|
135
|
+
if (level === "WARN")
|
|
136
|
+
color = colors.YELLOW;
|
|
137
|
+
if (level === "ERROR")
|
|
138
|
+
color = colors.RED;
|
|
139
|
+
}
|
|
140
|
+
// گرفتن stack trace
|
|
141
|
+
const stack = new Error().stack?.split("\n")[2]; // خط Caller
|
|
142
|
+
let location = "";
|
|
143
|
+
if (stack) {
|
|
144
|
+
const match = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
145
|
+
if (match) {
|
|
146
|
+
const fileName = path.basename(match[1]);
|
|
147
|
+
const line = match[2];
|
|
148
|
+
const column = match[3];
|
|
149
|
+
location = `${fileName}:${line}:${column}`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
|
|
153
|
+
// چاپ لاگ
|
|
154
|
+
console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
|
|
155
|
+
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, R
|
|
|
3
3
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
import jwt from "jsonwebtoken";
|
|
6
|
+
import path from "path";
|
|
6
7
|
declare global {
|
|
7
8
|
namespace Express {
|
|
8
9
|
interface Response {
|
|
@@ -173,4 +174,44 @@ const responseHandlerMiddleware = (
|
|
|
173
174
|
|
|
174
175
|
export const RestMiddleware: RestMiddlewareFunctions = {
|
|
175
176
|
responseHandlerMiddleware
|
|
176
|
-
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
const colors = {
|
|
181
|
+
RESET: "\x1b[0m",
|
|
182
|
+
RED: "\x1b[31m",
|
|
183
|
+
GREEN: "\x1b[32m",
|
|
184
|
+
YELLOW: "\x1b[33m"
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
188
|
+
|
|
189
|
+
export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => {
|
|
190
|
+
const timestamp = new Date().toISOString();
|
|
191
|
+
|
|
192
|
+
// انتخاب رنگ فقط در dev
|
|
193
|
+
let color = colors.RESET;
|
|
194
|
+
if (!isProduction) {
|
|
195
|
+
if (level === "INFO") color = colors.GREEN;
|
|
196
|
+
if (level === "WARN") color = colors.YELLOW;
|
|
197
|
+
if (level === "ERROR") color = colors.RED;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// گرفتن stack trace
|
|
201
|
+
const stack = new Error().stack?.split("\n")[2]; // خط Caller
|
|
202
|
+
let location = "";
|
|
203
|
+
if (stack) {
|
|
204
|
+
const match = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
205
|
+
if (match) {
|
|
206
|
+
const fileName = path.basename(match[1]);
|
|
207
|
+
const line = match[2];
|
|
208
|
+
const column = match[3];
|
|
209
|
+
location = `${fileName}:${line}:${column}`;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
|
|
214
|
+
|
|
215
|
+
// چاپ لاگ
|
|
216
|
+
console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
|
|
217
|
+
};
|