response-standardizer 1.0.6 → 1.0.8

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,4 +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;
21
24
  export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ export const initKeycloak = async (config) => {
7
7
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
8
8
  const KEYCLOAK_REALM = config.realm ?? "master";
9
9
  const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
10
- console.log(`Keycloak PublicKey Url: ${realmUrl}`);
10
+ info(`Keycloak PublicKey Url: ${realmUrl}`);
11
11
  const resp = await axios.get(realmUrl);
12
12
  const key = resp.data.public_key;
13
13
  KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
@@ -23,6 +23,7 @@ export const protect = (allowedRoles) => {
23
23
  try {
24
24
  const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
25
25
  req.user = decoded;
26
+ req.token = token;
26
27
  if (allowedRoles)
27
28
  return role(req, res, next, allowedRoles);
28
29
  next();
@@ -125,6 +126,15 @@ const colors = {
125
126
  YELLOW: "\x1b[33m"
126
127
  };
127
128
  const isProduction = process.env.NODE_ENV === "production";
129
+ export const error = (message, meta) => {
130
+ log("ERROR", message, meta);
131
+ };
132
+ export const warn = (message, meta) => {
133
+ log("WARN", message, meta);
134
+ };
135
+ export const info = (message, meta) => {
136
+ log("INFO", message, meta);
137
+ };
128
138
  export const log = (level, message, meta) => {
129
139
  const timestamp = new Date().toISOString();
130
140
  // انتخاب رنگ فقط در dev
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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
@@ -21,7 +21,7 @@ export const initKeycloak = async (config: { service?: string; realm?: string })
21
21
  const KEYCLOAK_SERVICE = config.service ?? "localhost";
22
22
  const KEYCLOAK_REALM = config.realm ?? "master";
23
23
  const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
24
- console.log(`Keycloak PublicKey Url: ${realmUrl}`);
24
+ info(`Keycloak PublicKey Url: ${realmUrl}`);
25
25
  const resp = await axios.get(realmUrl);
26
26
  const key = resp.data.public_key;
27
27
  KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
@@ -40,6 +40,7 @@ export const protect = (allowedRoles?: string[]) => {
40
40
  try {
41
41
  const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
42
42
  (req as any).user = decoded;
43
+ (req as any).token = token;
43
44
  if(allowedRoles)
44
45
  return role(req, res, next, allowedRoles);
45
46
 
@@ -185,7 +186,15 @@ const colors = {
185
186
  };
186
187
 
187
188
  const isProduction = process.env.NODE_ENV === "production";
188
-
189
+ export const error = (message: string, meta?: any) => {
190
+ log("ERROR", message, meta)
191
+ }
192
+ export const warn = (message: string, meta?: any) => {
193
+ log("WARN", message, meta)
194
+ }
195
+ export const info = (message: string, meta?: any) => {
196
+ log("INFO", message, meta)
197
+ }
189
198
  export const log = (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => {
190
199
  const timestamp = new Date().toISOString();
191
200