@microservice_udemy/common 1.0.16 → 1.0.20
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/auth/user-payload.js +2 -0
- package/build/errors/request-validation-error.js +4 -1
- package/build/middlewares/current-user.js +14 -14
- package/package.json +4 -5
- package/build/errors/bad-request-error.d.ts +0 -9
- package/build/errors/custom-error.d.ts +0 -8
- package/build/errors/database-connection-error.d.ts +0 -9
- package/build/errors/not-authorized-error.d.ts +0 -9
- package/build/errors/not-found-error.d.ts +0 -8
- package/build/errors/request-validation-error.d.ts +0 -11
- package/build/events/event.d.ts +0 -5
- package/build/events/expiration-events/expiration-complete-event.d.ts +0 -7
- package/build/events/listener.d.ts +0 -18
- package/build/events/order-events/order-cancelled-event.d.ts +0 -11
- package/build/events/order-events/order-created-event.d.ts +0 -16
- package/build/events/payment-events/payment-created-event.d.ts +0 -9
- package/build/events/publisher.d.ts +0 -13
- package/build/events/subjects/subjects.d.ts +0 -8
- package/build/events/ticket-events/ticket-created-event.d.ts +0 -12
- package/build/events/ticket-events/ticket-updated-event.d.ts +0 -12
- package/build/events/types/order-status.d.ts +0 -6
- package/build/index.d.ts +0 -20
- package/build/middlewares/current-user.d.ts +0 -40
- package/build/middlewares/error-handler.d.ts +0 -2
- package/build/middlewares/require-auth.d.ts +0 -2
- package/build/middlewares/validate-request.d.ts +0 -2
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestValidationError = void 0;
|
|
4
4
|
const custom_error_1 = require("./custom-error");
|
|
5
|
+
function isFieldError(err) {
|
|
6
|
+
return "path" in err;
|
|
7
|
+
}
|
|
5
8
|
class RequestValidationError extends custom_error_1.CustomError {
|
|
6
9
|
constructor(errors) {
|
|
7
10
|
super("Request Validation Error");
|
|
@@ -14,7 +17,7 @@ class RequestValidationError extends custom_error_1.CustomError {
|
|
|
14
17
|
return this.errors.map((err) => {
|
|
15
18
|
return {
|
|
16
19
|
message: err.msg,
|
|
17
|
-
field:
|
|
20
|
+
field: isFieldError(err) ? err.path : undefined,
|
|
18
21
|
};
|
|
19
22
|
});
|
|
20
23
|
}
|
|
@@ -5,27 +5,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.currentUser = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Middleware that:
|
|
11
|
-
* 1. Reads the JWT from the session
|
|
12
|
-
* 2. Verifies it
|
|
13
|
-
* 3. Attaches the decoded user to `req.currentUser`
|
|
14
|
-
*/
|
|
15
|
-
const currentUser = (req, res, next) => {
|
|
8
|
+
const currentUser = (req, _res, next) => {
|
|
16
9
|
var _a;
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
let token;
|
|
11
|
+
// Cookie-based (browser)
|
|
12
|
+
if ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a.session) {
|
|
13
|
+
token = req.cookies.session;
|
|
14
|
+
}
|
|
15
|
+
// Header-based (microservices)
|
|
16
|
+
const authHeader = req.headers.authorization;
|
|
17
|
+
if (!token && (authHeader === null || authHeader === void 0 ? void 0 : authHeader.startsWith("Bearer "))) {
|
|
18
|
+
token = authHeader.replace("Bearer ", "");
|
|
19
|
+
}
|
|
20
|
+
if (!token) {
|
|
19
21
|
return next();
|
|
20
22
|
}
|
|
21
23
|
try {
|
|
22
|
-
|
|
23
|
-
const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_SECRET);
|
|
24
|
-
// Attach user to request object
|
|
24
|
+
const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_SECRET);
|
|
25
25
|
req.currentUser = payload;
|
|
26
26
|
}
|
|
27
27
|
catch (err) {
|
|
28
|
-
//
|
|
28
|
+
// ignore invalid token
|
|
29
29
|
}
|
|
30
30
|
next();
|
|
31
31
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microservice_udemy/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.20",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -20,17 +20,16 @@
|
|
|
20
20
|
"@types/express-session": "^1.18.2",
|
|
21
21
|
"@types/node": "^25.0.2",
|
|
22
22
|
"del-cli": "^7.0.0",
|
|
23
|
-
"typescript": "^5.9.3"
|
|
24
|
-
"@types/express": "^4.17.21"
|
|
23
|
+
"typescript": "^5.9.3"
|
|
25
24
|
},
|
|
26
25
|
"dependencies": {
|
|
27
26
|
"@types/cookie-session": "^2.0.49",
|
|
28
27
|
"@types/express": "^5.0.6",
|
|
29
28
|
"@types/jsonwebtoken": "^9.0.10",
|
|
30
|
-
"express": "^4.21.2",
|
|
31
29
|
"cookie-session": "^2.1.1",
|
|
32
|
-
"express
|
|
30
|
+
"express": "^5.2.1",
|
|
33
31
|
"express-session": "^1.18.2",
|
|
32
|
+
"express-validator": "^7.3.1",
|
|
34
33
|
"jsonwebtoken": "^9.0.3",
|
|
35
34
|
"node-nats-streaming": "^0.3.2"
|
|
36
35
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ValidationError } from "express-validator";
|
|
2
|
-
import { CustomError } from "./custom-error";
|
|
3
|
-
export declare class RequestValidationError extends CustomError {
|
|
4
|
-
errors: ValidationError[];
|
|
5
|
-
statusCode: number;
|
|
6
|
-
constructor(errors: ValidationError[]);
|
|
7
|
-
serializeErrors(): {
|
|
8
|
-
message: any;
|
|
9
|
-
field: string | undefined;
|
|
10
|
-
}[];
|
|
11
|
-
}
|
package/build/events/event.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Message, Stan } from "node-nats-streaming";
|
|
2
|
-
import { Subjects } from "./subjects/subjects";
|
|
3
|
-
interface events {
|
|
4
|
-
subject: Subjects;
|
|
5
|
-
data: any;
|
|
6
|
-
}
|
|
7
|
-
export declare abstract class Listener<T extends events> {
|
|
8
|
-
abstract subject: T["subject"];
|
|
9
|
-
abstract queueGroupName: string;
|
|
10
|
-
abstract onMessage(data: T["data"], msg: Message): void;
|
|
11
|
-
protected client: Stan;
|
|
12
|
-
protected ackWait: number;
|
|
13
|
-
constructor(client: Stan);
|
|
14
|
-
subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
|
|
15
|
-
listen(): void;
|
|
16
|
-
parseMessage(msg: Message): any;
|
|
17
|
-
}
|
|
18
|
-
export {};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Subjects } from "../subjects/subjects";
|
|
2
|
-
import { OrderStatus } from "../types/order-status";
|
|
3
|
-
export interface OrderCreatedEvent {
|
|
4
|
-
subject: Subjects.OrderCreated;
|
|
5
|
-
data: {
|
|
6
|
-
id: string;
|
|
7
|
-
version: number;
|
|
8
|
-
status: OrderStatus;
|
|
9
|
-
userId: string;
|
|
10
|
-
expiresAt: string;
|
|
11
|
-
ticket: {
|
|
12
|
-
id: string;
|
|
13
|
-
price: number;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Stan } from "node-nats-streaming";
|
|
2
|
-
import { Subjects } from "./subjects/subjects";
|
|
3
|
-
interface Event {
|
|
4
|
-
subject: Subjects;
|
|
5
|
-
data: any;
|
|
6
|
-
}
|
|
7
|
-
export declare abstract class Publisher<T extends Event> {
|
|
8
|
-
abstract subject: T["subject"];
|
|
9
|
-
protected client: Stan;
|
|
10
|
-
constructor(client: Stan);
|
|
11
|
-
publish(data: T["data"]): Promise<void>;
|
|
12
|
-
}
|
|
13
|
-
export {};
|
package/build/index.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export * from "./errors/bad-request-error";
|
|
2
|
-
export * from "./errors/custom-error";
|
|
3
|
-
export * from "./errors/database-connection-error";
|
|
4
|
-
export * from "./errors/not-authorized-error";
|
|
5
|
-
export * from "./errors/not-found-error";
|
|
6
|
-
export * from "./errors/request-validation-error";
|
|
7
|
-
export * from "./middlewares/current-user";
|
|
8
|
-
export * from "./middlewares/error-handler";
|
|
9
|
-
export * from "./middlewares/require-auth";
|
|
10
|
-
export * from "./middlewares/validate-request";
|
|
11
|
-
export * from "./events/listener";
|
|
12
|
-
export * from "./events/publisher";
|
|
13
|
-
export * from "./events/subjects/subjects";
|
|
14
|
-
export * from "./events/ticket-events/ticket-created-event";
|
|
15
|
-
export * from "./events/ticket-events/ticket-updated-event";
|
|
16
|
-
export * from "./events/order-events/order-created-event";
|
|
17
|
-
export * from "./events/order-events/order-cancelled-event";
|
|
18
|
-
export * from "./events/payment-events/payment-created-event";
|
|
19
|
-
export * from "./events/expiration-events/expiration-complete-event";
|
|
20
|
-
export * from "./events/types/order-status";
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
import "express-session";
|
|
3
|
-
/**
|
|
4
|
-
* Extend express-session's SessionData interface
|
|
5
|
-
* to tell TypeScript that our session may contain a JWT.
|
|
6
|
-
*
|
|
7
|
-
* This fixes: Property 'jwt' does not exist on type 'SessionData'
|
|
8
|
-
*/
|
|
9
|
-
declare module "express-session" {
|
|
10
|
-
interface SessionData {
|
|
11
|
-
jwt?: string;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Shape of the JWT payload stored in the session
|
|
16
|
-
*/
|
|
17
|
-
interface UserPayload {
|
|
18
|
-
id: string;
|
|
19
|
-
email: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Extend Express.Request to include a `currentUser` property.
|
|
23
|
-
* This allows us to attach the authenticated user to the request
|
|
24
|
-
* and access it safely in later middleware and route handlers.
|
|
25
|
-
*/
|
|
26
|
-
declare global {
|
|
27
|
-
namespace Express {
|
|
28
|
-
interface Request {
|
|
29
|
-
currentUser?: UserPayload;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Middleware that:
|
|
35
|
-
* 1. Reads the JWT from the session
|
|
36
|
-
* 2. Verifies it
|
|
37
|
-
* 3. Attaches the decoded user to `req.currentUser`
|
|
38
|
-
*/
|
|
39
|
-
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
40
|
-
export {};
|