@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.
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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: "path" in err ? err.path : undefined,
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
- require("express-session");
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
- // If no session or no JWT, move on
18
- if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
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
- // Verify JWT and extract user payload
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
- // Invalid token — ignore and continue
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.16",
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-validator": "^7.3.1",
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,9 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class BadRequestError extends CustomError {
3
- message: string;
4
- statusCode: number;
5
- constructor(message: string);
6
- serializeErrors(): {
7
- message: string;
8
- }[];
9
- }
@@ -1,8 +0,0 @@
1
- export declare abstract class CustomError extends Error {
2
- abstract statusCode: number;
3
- constructor(message: string);
4
- abstract serializeErrors(): {
5
- message: string;
6
- field?: string;
7
- }[];
8
- }
@@ -1,9 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class DatabaseConnectionError extends CustomError {
3
- statusCode: number;
4
- reason: string;
5
- constructor();
6
- serializeErrors(): {
7
- message: string;
8
- }[];
9
- }
@@ -1,9 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class NotAuthorizedError extends CustomError {
3
- statusCode: number;
4
- constructor();
5
- serializeErrors(): {
6
- message: string;
7
- field?: string;
8
- }[];
9
- }
@@ -1,8 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class NotFoundError extends CustomError {
3
- statusCode: number;
4
- constructor();
5
- serializeErrors(): {
6
- message: string;
7
- }[];
8
- }
@@ -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
- }
@@ -1,5 +0,0 @@
1
- import { Subjects } from "./subjects/subjects";
2
- export interface Event {
3
- subject: Subjects;
4
- data: any;
5
- }
@@ -1,7 +0,0 @@
1
- import { Subjects } from "../subjects/subjects";
2
- export interface ExpirationCompleteEvent {
3
- subject: Subjects.ExpirationComplete;
4
- data: {
5
- orderId: string;
6
- };
7
- }
@@ -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,11 +0,0 @@
1
- import { Subjects } from "../subjects/subjects";
2
- export interface OrderCancelledEvent {
3
- subject: Subjects.OrderCancelled;
4
- data: {
5
- id: string;
6
- version: number;
7
- ticket: {
8
- id: string;
9
- };
10
- };
11
- }
@@ -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,9 +0,0 @@
1
- import { Subjects } from "../subjects/subjects";
2
- export interface PaymentCreatedEvent {
3
- subject: Subjects.PaymentCreated;
4
- data: {
5
- id: string;
6
- orderId: string;
7
- stripeId: string;
8
- };
9
- }
@@ -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 {};
@@ -1,8 +0,0 @@
1
- export declare enum Subjects {
2
- TicketCreated = "ticket:created",
3
- TicketUpdated = "ticket:updated",
4
- OrderCreated = "order:created",
5
- OrderCancelled = "order:cancelled",
6
- ExpirationComplete = "expiration:complete",
7
- PaymentCreated = "payment:created"
8
- }
@@ -1,12 +0,0 @@
1
- import { Subjects } from "../subjects/subjects";
2
- export interface TicketCreatedEvent {
3
- subject: Subjects.TicketCreated;
4
- data: {
5
- id: string;
6
- version: number;
7
- title: string;
8
- price: number;
9
- userId: string;
10
- orderId?: string;
11
- };
12
- }
@@ -1,12 +0,0 @@
1
- import { Subjects } from "../subjects/subjects";
2
- export interface TicketUpdatedEvent {
3
- subject: Subjects.TicketUpdated;
4
- data: {
5
- id: string;
6
- version: number;
7
- title: string;
8
- price: number;
9
- userId: string;
10
- orderId?: string;
11
- };
12
- }
@@ -1,6 +0,0 @@
1
- export declare enum OrderStatus {
2
- Created = "created",
3
- Cancelled = "cancelled",
4
- AwaitingPayment = "awaiting:payment",
5
- Complete = "complete"
6
- }
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 {};
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;