response-standardizer 1.0.1 → 1.0.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 +6 -4
- package/dist/index.js +13 -8
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/index.ts +14 -12
- package/src/types.ts +4 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
1
|
+
import { RestResponseFunctions, RestMiddlewareFunctions, KeycloakFunctions } from "./types.js";
|
|
3
2
|
declare global {
|
|
4
3
|
namespace Express {
|
|
5
4
|
interface Response {
|
|
@@ -12,7 +11,10 @@ declare global {
|
|
|
12
11
|
}
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
|
-
export declare const
|
|
16
|
-
|
|
14
|
+
export declare const initKeycloak: (config: {
|
|
15
|
+
service?: string;
|
|
16
|
+
realm?: string;
|
|
17
|
+
}) => Promise<void>;
|
|
18
|
+
export declare const Keycloak: KeycloakFunctions;
|
|
17
19
|
export declare const RestResponse: RestResponseFunctions;
|
|
18
20
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
import jwt from "jsonwebtoken";
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const KEYCLOAK_PUBLIC_KEY = await getKeycloakPublicKey();
|
|
13
|
-
export const protect = (req, res, next) => {
|
|
12
|
+
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
13
|
+
};
|
|
14
|
+
const protect = (req, res, next) => {
|
|
14
15
|
const authHeader = req.headers["authorization"];
|
|
15
16
|
if (!authHeader)
|
|
16
17
|
return RestResponse.unauthorized(req, res);
|
|
@@ -27,7 +28,7 @@ export const protect = (req, res, next) => {
|
|
|
27
28
|
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
28
29
|
}
|
|
29
30
|
};
|
|
30
|
-
|
|
31
|
+
const role = (allowedRoles) => {
|
|
31
32
|
return (req, res, next) => {
|
|
32
33
|
const user = req.user;
|
|
33
34
|
if (!user) {
|
|
@@ -46,6 +47,10 @@ export const role = (allowedRoles) => {
|
|
|
46
47
|
next();
|
|
47
48
|
};
|
|
48
49
|
};
|
|
50
|
+
export const Keycloak = {
|
|
51
|
+
protect,
|
|
52
|
+
role
|
|
53
|
+
};
|
|
49
54
|
const success = (req, res, data, message = null) => {
|
|
50
55
|
res.status(200).json({ data, message });
|
|
51
56
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ export type Middleware = (req: Request, res: Response, next: NextFunction) => vo
|
|
|
22
22
|
export interface RestMiddlewareFunctions {
|
|
23
23
|
responseHandlerMiddleware: <T>(req: Request, res: Response, next: NextFunction) => void;
|
|
24
24
|
}
|
|
25
|
+
export interface KeycloakFunctions {
|
|
26
|
+
protect: (req: Request, res: Response, next: any) => void;
|
|
27
|
+
role: (allowedRoles: string[]) => void;
|
|
28
|
+
}
|
|
25
29
|
export interface RestResponseFunctions {
|
|
26
30
|
success: <T>(req: Request, res: Response, data: T, message?: string | null) => void;
|
|
27
31
|
paginate: <T>(req: Request, res: Response, data: T, pagination: PaginationMeta, message?: string | null) => void;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
2
|
+
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions, KeycloakFunctions } from "./types.js";
|
|
3
3
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
import jwt from "jsonwebtoken";
|
|
@@ -15,19 +15,18 @@ declare global {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
async
|
|
20
|
-
const
|
|
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
|
-
|
|
25
|
-
|
|
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
|
+
const protect = (req: Request, res: Response, next: any) => {
|
|
31
30
|
const authHeader = req.headers["authorization"];
|
|
32
31
|
if (!authHeader)
|
|
33
32
|
return RestResponse.unauthorized(req, res)
|
|
@@ -45,7 +44,7 @@ export const protect = (req: Request, res: Response, next: any) => {
|
|
|
45
44
|
return RestResponse.unauthorized(req, res, "Token is not valid")
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
|
-
|
|
47
|
+
const role = (allowedRoles: string[]) => {
|
|
49
48
|
return (req: any, res: any, next: any) => {
|
|
50
49
|
const user = req.user;
|
|
51
50
|
if (!user) {
|
|
@@ -67,7 +66,10 @@ export const role = (allowedRoles: string[]) => {
|
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
}
|
|
70
|
-
|
|
69
|
+
export const Keycloak: KeycloakFunctions = {
|
|
70
|
+
protect,
|
|
71
|
+
role
|
|
72
|
+
}
|
|
71
73
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
72
74
|
res.status(200).json({ data, message });
|
|
73
75
|
};
|
package/src/types.ts
CHANGED
|
@@ -27,6 +27,10 @@ export type Middleware = (req: Request, res: Response, next: NextFunction) => vo
|
|
|
27
27
|
export interface RestMiddlewareFunctions {
|
|
28
28
|
responseHandlerMiddleware: <T>(req: Request, res: Response, next: NextFunction) => void
|
|
29
29
|
}
|
|
30
|
+
export interface KeycloakFunctions {
|
|
31
|
+
protect:(req: Request, res: Response, next: any) => void;
|
|
32
|
+
role: (allowedRoles: string[]) => void;
|
|
33
|
+
}
|
|
30
34
|
export interface RestResponseFunctions {
|
|
31
35
|
success: <T>(req: Request, res: Response, data: T, message?: string | null) => void;
|
|
32
36
|
paginate: <T>(
|