@skillstew/common 1.0.17 → 1.0.19
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/build/errors/MessageQueueErrors.js +2 -2
- package/build/events/Consumer.d.ts +2 -1
- package/build/middlewares/authMiddleware.js +21 -1
- package/package.json +1 -1
- package/src/errors/MessageQueueErrors.ts +2 -2
- package/src/events/Consumer.ts +5 -3
- package/src/middlewares/authMiddleware.ts +20 -1
|
@@ -5,13 +5,13 @@ const AppError_1 = require("./AppError");
|
|
|
5
5
|
const MessageQueueErrorCodes_1 = require("./codes/MessageQueueErrorCodes");
|
|
6
6
|
class InvalidEventPayloadError extends AppError_1.InfrastructureError {
|
|
7
7
|
constructor(eventName, errorString) {
|
|
8
|
-
super(MessageQueueErrorCodes_1.MessageQueueErrorCodes.INVALID_EVENT_PAYLOAD + `for ${eventName}`, "INVALID_EVENT_PAYLOAD", { zodErrorString: errorString });
|
|
8
|
+
super(MessageQueueErrorCodes_1.MessageQueueErrorCodes.INVALID_EVENT_PAYLOAD + ` for ${eventName}`, "INVALID_EVENT_PAYLOAD", { zodErrorString: errorString });
|
|
9
9
|
}
|
|
10
10
|
toJSON() {
|
|
11
11
|
return {
|
|
12
12
|
message: this.message,
|
|
13
13
|
code: this.code,
|
|
14
|
-
error: this.context.
|
|
14
|
+
error: this.context.zodErrorString,
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Channel } from "amqplib";
|
|
2
2
|
import { EventName } from "./EventMap";
|
|
3
3
|
import { AppEvent } from "./AppEvent";
|
|
4
|
+
type EventHandler<T extends EventName> = (event: AppEvent<T>) => Promise<HandlerResult>;
|
|
4
5
|
export declare class Consumer {
|
|
5
6
|
private _channel;
|
|
6
7
|
private _exchange;
|
|
@@ -9,7 +10,7 @@ export declare class Consumer {
|
|
|
9
10
|
constructor();
|
|
10
11
|
init: (channel: Channel, exchange: string, serviceName: string, interestedEvents: (string | EventName)[]) => Promise<void>;
|
|
11
12
|
private handleEvent;
|
|
12
|
-
registerHandler: <T extends EventName>(eventName: T, handler:
|
|
13
|
+
registerHandler: <T extends EventName>(eventName: T, handler: EventHandler<T>) => void;
|
|
13
14
|
}
|
|
14
15
|
interface HandlerResult {
|
|
15
16
|
success: boolean;
|
|
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AuthMiddleware = void 0;
|
|
4
4
|
const UnauthenticatedError_1 = require("../errors/UnauthenticatedError");
|
|
5
5
|
const JwtHelper_1 = require("../jwt-utils/JwtHelper");
|
|
6
|
+
const JwtErrors_1 = require("../errors/JwtErrors");
|
|
7
|
+
const HttpStatus_1 = require("../constants/HttpStatus");
|
|
6
8
|
class AuthMiddleware {
|
|
7
9
|
constructor(userAccessTokenSecret, expertAccessTokenSecret, adminAccessTokenSecret) {
|
|
8
|
-
this.verify = (req,
|
|
10
|
+
this.verify = (req, res, next) => {
|
|
9
11
|
var _a;
|
|
10
12
|
try {
|
|
11
13
|
const token = (_a = req.headers["authorization"]) === null || _a === void 0 ? void 0 : _a.split(" ")[1];
|
|
@@ -19,6 +21,24 @@ class AuthMiddleware {
|
|
|
19
21
|
next();
|
|
20
22
|
}
|
|
21
23
|
catch (err) {
|
|
24
|
+
if (err instanceof JwtErrors_1.AccessTokenVerifyError) {
|
|
25
|
+
res
|
|
26
|
+
.status(HttpStatus_1.HttpStatus.BAD_REQUEST)
|
|
27
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
else if (err instanceof JwtErrors_1.InvalidTokenError) {
|
|
31
|
+
res
|
|
32
|
+
.status(HttpStatus_1.HttpStatus.UNAUTHORIZED)
|
|
33
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
else if (err instanceof UnauthenticatedError_1.UnauthenticatedError) {
|
|
37
|
+
res
|
|
38
|
+
.status(HttpStatus_1.HttpStatus.UNAUTHORIZED)
|
|
39
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
22
42
|
next(err);
|
|
23
43
|
}
|
|
24
44
|
};
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@ import { MessageQueueErrorCodes } from "./codes/MessageQueueErrorCodes";
|
|
|
5
5
|
export class InvalidEventPayloadError extends InfrastructureError {
|
|
6
6
|
constructor(eventName: EventName, errorString: string) {
|
|
7
7
|
super(
|
|
8
|
-
MessageQueueErrorCodes.INVALID_EVENT_PAYLOAD + `for ${eventName}`,
|
|
8
|
+
MessageQueueErrorCodes.INVALID_EVENT_PAYLOAD + ` for ${eventName}`,
|
|
9
9
|
"INVALID_EVENT_PAYLOAD",
|
|
10
10
|
{ zodErrorString: errorString },
|
|
11
11
|
);
|
|
@@ -14,7 +14,7 @@ export class InvalidEventPayloadError extends InfrastructureError {
|
|
|
14
14
|
return {
|
|
15
15
|
message: this.message,
|
|
16
16
|
code: this.code,
|
|
17
|
-
error: this.context!.
|
|
17
|
+
error: this.context!.zodErrorString,
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
}
|
package/src/events/Consumer.ts
CHANGED
|
@@ -8,13 +8,15 @@ import {
|
|
|
8
8
|
} from "../errors/MessageQueueErrors";
|
|
9
9
|
import z from "zod";
|
|
10
10
|
|
|
11
|
+
type EventHandler<T extends EventName> = (event: AppEvent<T>) => Promise<HandlerResult>;
|
|
12
|
+
|
|
11
13
|
export class Consumer {
|
|
12
14
|
private _channel!: Channel;
|
|
13
15
|
private _exchange!: string;
|
|
14
16
|
private _initialized: boolean = false;
|
|
15
17
|
private _handlers = new Map<
|
|
16
18
|
EventName,
|
|
17
|
-
|
|
19
|
+
EventHandler<EventName>
|
|
18
20
|
>();
|
|
19
21
|
constructor() {}
|
|
20
22
|
|
|
@@ -93,12 +95,12 @@ export class Consumer {
|
|
|
93
95
|
|
|
94
96
|
registerHandler = <T extends EventName>(
|
|
95
97
|
eventName: T,
|
|
96
|
-
handler:
|
|
98
|
+
handler: EventHandler<T>,
|
|
97
99
|
) => {
|
|
98
100
|
if (!this._initialized) {
|
|
99
101
|
throw new ConsumerUsedBeforeInitializationError();
|
|
100
102
|
}
|
|
101
|
-
this._handlers.set(eventName, handler);
|
|
103
|
+
this._handlers.set(eventName, handler as EventHandler<EventName>);
|
|
102
104
|
};
|
|
103
105
|
}
|
|
104
106
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { RequestHandler } from "express";
|
|
2
2
|
import { UnauthenticatedError } from "../errors/UnauthenticatedError";
|
|
3
3
|
import { JwtHelper } from "../jwt-utils/JwtHelper";
|
|
4
|
+
import { AccessTokenVerifyError, InvalidTokenError } from "../errors/JwtErrors";
|
|
5
|
+
import { HttpStatus } from "../constants/HttpStatus";
|
|
4
6
|
|
|
5
7
|
export class AuthMiddleware {
|
|
6
8
|
private _jwtHelper: JwtHelper;
|
|
@@ -17,12 +19,13 @@ export class AuthMiddleware {
|
|
|
17
19
|
});
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
verify: RequestHandler = (req,
|
|
22
|
+
verify: RequestHandler = (req, res, next) => {
|
|
21
23
|
try {
|
|
22
24
|
const token = req.headers["authorization"]?.split(" ")[1];
|
|
23
25
|
if (!token) {
|
|
24
26
|
throw new UnauthenticatedError();
|
|
25
27
|
}
|
|
28
|
+
|
|
26
29
|
const payload = this._jwtHelper.verifyAccessToken(token);
|
|
27
30
|
req.user = {
|
|
28
31
|
id: payload.userId,
|
|
@@ -33,6 +36,22 @@ export class AuthMiddleware {
|
|
|
33
36
|
};
|
|
34
37
|
next();
|
|
35
38
|
} catch (err) {
|
|
39
|
+
if (err instanceof AccessTokenVerifyError) {
|
|
40
|
+
res
|
|
41
|
+
.status(HttpStatus.BAD_REQUEST)
|
|
42
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
43
|
+
return;
|
|
44
|
+
} else if (err instanceof InvalidTokenError) {
|
|
45
|
+
res
|
|
46
|
+
.status(HttpStatus.UNAUTHORIZED)
|
|
47
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
48
|
+
return;
|
|
49
|
+
} else if (err instanceof UnauthenticatedError) {
|
|
50
|
+
res
|
|
51
|
+
.status(HttpStatus.UNAUTHORIZED)
|
|
52
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
36
55
|
next(err);
|
|
37
56
|
}
|
|
38
57
|
};
|