response-standardizer 1.0.1 → 1.0.2

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
@@ -12,6 +12,10 @@ declare global {
12
12
  }
13
13
  }
14
14
  }
15
+ export declare const initKeycloak: (config: {
16
+ service?: string;
17
+ realm?: string;
18
+ }) => Promise<void>;
15
19
  export declare const protect: (req: Request, res: Response, next: any) => void;
16
20
  export declare const role: (allowedRoles: string[]) => (req: any, res: any, next: any) => void;
17
21
  export declare const RestResponse: RestResponseFunctions;
package/dist/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  import { generateRequestId, getTimestamp } from "./utils.js";
2
2
  import axios from "axios";
3
3
  import jwt from "jsonwebtoken";
4
- async function getKeycloakPublicKey() {
5
- const realmUrl = `http://${process.env.KEYCLOAK_SERVICE ?? "localhost"}/realms/${process.env.KEYCLOAK_REALM ?? "master"}`;
4
+ let KEYCLOAK_PUBLIC_KEY = null;
5
+ export const initKeycloak = async (config) => {
6
+ const KEYCLOAK_SERVICE = config.service ?? "localhost";
7
+ const KEYCLOAK_REALM = config.realm ?? "master";
8
+ const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
6
9
  console.log(`Keycloak PublicKey Url: ${realmUrl}`);
7
10
  const resp = await axios.get(realmUrl);
8
11
  const key = resp.data.public_key;
9
- const pem = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
10
- return pem;
11
- }
12
- const KEYCLOAK_PUBLIC_KEY = await getKeycloakPublicKey();
12
+ KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
13
+ };
13
14
  export const protect = (req, res, next) => {
14
15
  const authHeader = req.headers["authorization"];
15
16
  if (!authHeader)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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
@@ -15,17 +15,16 @@ declare global {
15
15
  }
16
16
  }
17
17
  }
18
-
19
- async function getKeycloakPublicKey(): Promise<string> {
20
- const realmUrl = `http://${process.env.KEYCLOAK_SERVICE??"localhost"}/realms/${process.env.KEYCLOAK_REALM??"master"}`;
18
+ let KEYCLOAK_PUBLIC_KEY: any = null;
19
+ export const initKeycloak = async (config: { service?: string; realm?: string }) => {
20
+ const KEYCLOAK_SERVICE = config.service ?? "localhost";
21
+ const KEYCLOAK_REALM = config.realm ?? "master";
22
+ const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
21
23
  console.log(`Keycloak PublicKey Url: ${realmUrl}`);
22
24
  const resp = await axios.get(realmUrl);
23
25
  const key = resp.data.public_key;
24
- const pem = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
25
- return pem;
26
- }
27
-
28
- const KEYCLOAK_PUBLIC_KEY: any = await getKeycloakPublicKey();
26
+ KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
27
+ };
29
28
 
30
29
  export const protect = (req: Request, res: Response, next: any) => {
31
30
  const authHeader = req.headers["authorization"];