response-standardizer 1.0.5 → 1.0.7

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 CHANGED
@@ -18,3 +18,7 @@ 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 error: (message: string, meta?: any) => void;
22
+ export declare const warn: (message: string, meta?: any) => void;
23
+ export declare const info: (message: string, meta?: any) => void;
24
+ 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,47 @@ 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 error = (message, meta) => {
129
+ log("ERROR", message, meta);
130
+ };
131
+ export const warn = (message, meta) => {
132
+ log("WARN", message, meta);
133
+ };
134
+ export const info = (message, meta) => {
135
+ log("INFO", message, meta);
136
+ };
137
+ export const log = (level, message, meta) => {
138
+ const timestamp = new Date().toISOString();
139
+ // انتخاب رنگ فقط در dev
140
+ let color = colors.RESET;
141
+ if (!isProduction) {
142
+ if (level === "INFO")
143
+ color = colors.GREEN;
144
+ if (level === "WARN")
145
+ color = colors.YELLOW;
146
+ if (level === "ERROR")
147
+ color = colors.RED;
148
+ }
149
+ // گرفتن stack trace
150
+ const stack = new Error().stack?.split("\n")[2]; // خط Caller
151
+ let location = "";
152
+ if (stack) {
153
+ const match = stack.match(/\((.*):(\d+):(\d+)\)/);
154
+ if (match) {
155
+ const fileName = path.basename(match[1]);
156
+ const line = match[2];
157
+ const column = match[3];
158
+ location = `${fileName}:${line}:${column}`;
159
+ }
160
+ }
161
+ const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
162
+ // چاپ لاگ
163
+ console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
164
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
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
@@ -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,52 @@ 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
+ export const error = (message: string, meta?: any) => {
189
+ log("ERROR", message, meta)
190
+ }
191
+ export const warn = (message: string, meta?: any) => {
192
+ log("WARN", message, meta)
193
+ }
194
+ export const info = (message: string, meta?: any) => {
195
+ log("INFO", message, meta)
196
+ }
197
+ export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => {
198
+ const timestamp = new Date().toISOString();
199
+
200
+ // انتخاب رنگ فقط در dev
201
+ let color = colors.RESET;
202
+ if (!isProduction) {
203
+ if (level === "INFO") color = colors.GREEN;
204
+ if (level === "WARN") color = colors.YELLOW;
205
+ if (level === "ERROR") color = colors.RED;
206
+ }
207
+
208
+ // گرفتن stack trace
209
+ const stack = new Error().stack?.split("\n")[2]; // خط Caller
210
+ let location = "";
211
+ if (stack) {
212
+ const match = stack.match(/\((.*):(\d+):(\d+)\)/);
213
+ if (match) {
214
+ const fileName = path.basename(match[1]);
215
+ const line = match[2];
216
+ const column = match[3];
217
+ location = `${fileName}:${line}:${column}`;
218
+ }
219
+ }
220
+
221
+ const metaStr = meta ? `. ${JSON.stringify(meta)}` : "";
222
+
223
+ // چاپ لاگ
224
+ console.log(`${color}[${timestamp}][${location}][${level}] ${message}${metaStr}${colors.RESET}`);
225
+ };