response-standardizer 1.0.2 → 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 +2 -4
- package/dist/index.js +6 -2
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/index.ts +7 -4
- 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 {
|
|
@@ -16,7 +15,6 @@ export declare const initKeycloak: (config: {
|
|
|
16
15
|
service?: string;
|
|
17
16
|
realm?: string;
|
|
18
17
|
}) => Promise<void>;
|
|
19
|
-
export declare const
|
|
20
|
-
export declare const role: (allowedRoles: string[]) => (req: any, res: any, next: any) => void;
|
|
18
|
+
export declare const Keycloak: KeycloakFunctions;
|
|
21
19
|
export declare const RestResponse: RestResponseFunctions;
|
|
22
20
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,7 @@ export const initKeycloak = async (config) => {
|
|
|
11
11
|
const key = resp.data.public_key;
|
|
12
12
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
13
13
|
};
|
|
14
|
-
|
|
14
|
+
const protect = (req, res, next) => {
|
|
15
15
|
const authHeader = req.headers["authorization"];
|
|
16
16
|
if (!authHeader)
|
|
17
17
|
return RestResponse.unauthorized(req, res);
|
|
@@ -28,7 +28,7 @@ export const protect = (req, res, next) => {
|
|
|
28
28
|
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
|
-
|
|
31
|
+
const role = (allowedRoles) => {
|
|
32
32
|
return (req, res, next) => {
|
|
33
33
|
const user = req.user;
|
|
34
34
|
if (!user) {
|
|
@@ -47,6 +47,10 @@ export const role = (allowedRoles) => {
|
|
|
47
47
|
next();
|
|
48
48
|
};
|
|
49
49
|
};
|
|
50
|
+
export const Keycloak = {
|
|
51
|
+
protect,
|
|
52
|
+
role
|
|
53
|
+
};
|
|
50
54
|
const success = (req, res, data, message = null) => {
|
|
51
55
|
res.status(200).json({ data, message });
|
|
52
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";
|
|
@@ -26,7 +26,7 @@ export const initKeycloak = async (config: { service?: string; realm?: string })
|
|
|
26
26
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
const protect = (req: Request, res: Response, next: any) => {
|
|
30
30
|
const authHeader = req.headers["authorization"];
|
|
31
31
|
if (!authHeader)
|
|
32
32
|
return RestResponse.unauthorized(req, res)
|
|
@@ -44,7 +44,7 @@ export const protect = (req: Request, res: Response, next: any) => {
|
|
|
44
44
|
return RestResponse.unauthorized(req, res, "Token is not valid")
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
const role = (allowedRoles: string[]) => {
|
|
48
48
|
return (req: any, res: any, next: any) => {
|
|
49
49
|
const user = req.user;
|
|
50
50
|
if (!user) {
|
|
@@ -66,7 +66,10 @@ export const role = (allowedRoles: string[]) => {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
export const Keycloak: KeycloakFunctions = {
|
|
70
|
+
protect,
|
|
71
|
+
role
|
|
72
|
+
}
|
|
70
73
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
71
74
|
res.status(200).json({ data, message });
|
|
72
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>(
|