@car-parts/common 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.
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error';
2
+ export declare class ForbiddenError extends CustomError {
3
+ statusCode: number;
4
+ constructor(message?: string);
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForbiddenError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class ForbiddenError extends custom_error_1.CustomError {
6
+ constructor(message = 'You do not have permission to perform this action') {
7
+ super(message);
8
+ this.statusCode = 403;
9
+ Object.setPrototypeOf(this, ForbiddenError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [
13
+ {
14
+ message: this.message,
15
+ },
16
+ ];
17
+ }
18
+ }
19
+ exports.ForbiddenError = ForbiddenError;
@@ -4,7 +4,7 @@ exports.NotAuthorizedError = void 0;
4
4
  const custom_error_1 = require("./custom-error");
5
5
  class NotAuthorizedError extends custom_error_1.CustomError {
6
6
  constructor() {
7
- super('Not authorized');
7
+ super('You are not logged in');
8
8
  this.statusCode = 401;
9
9
  // Only because we are extending a built-in class
10
10
  Object.setPrototypeOf(this, NotAuthorizedError.prototype);
@@ -0,0 +1,21 @@
1
+ import { Consumer, Kafka, ConsumerConfig, KafkaMessage, TopicPartitionOffsetAndMetadata } from 'kafkajs';
2
+ import { Subjects } from './subjects';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class KafkaListener<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ abstract groupId: string;
10
+ abstract onMessage(data: T['data'], message: KafkaMessage): Promise<void>;
11
+ protected kafka: Kafka;
12
+ protected consumer: Consumer;
13
+ protected sessionTimeout: number;
14
+ protected consumerConfig?: Partial<ConsumerConfig>;
15
+ constructor(kafka: Kafka, consumerConfig?: Partial<ConsumerConfig>);
16
+ listen(): Promise<void>;
17
+ parseMessage(message: KafkaMessage): any;
18
+ disconnect(): Promise<void>;
19
+ commitOffsets(offsets: Array<TopicPartitionOffsetAndMetadata>): Promise<void>;
20
+ }
21
+ export {};
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.KafkaListener = void 0;
13
+ class KafkaListener {
14
+ constructor(kafka, consumerConfig) {
15
+ this.sessionTimeout = 30000; // 30 seconds
16
+ this.kafka = kafka;
17
+ this.consumerConfig = consumerConfig;
18
+ }
19
+ listen() {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ // Initialize consumer here where groupId is available
22
+ this.consumer = this.kafka.consumer(Object.assign({ groupId: this.groupId, sessionTimeout: this.sessionTimeout }, this.consumerConfig));
23
+ yield this.consumer.connect();
24
+ console.log(`Kafka consumer connected: ${this.groupId}`);
25
+ yield this.consumer.subscribe({
26
+ topic: this.subject,
27
+ fromBeginning: true, // Similar to setDeliverAllAvailable in NATS
28
+ });
29
+ yield this.consumer.run({
30
+ eachMessage: (_a) => __awaiter(this, [_a], void 0, function* ({ topic, partition, message, }) {
31
+ console.log(`Message received: ${topic} / ${this.groupId} / Partition: ${partition}`);
32
+ try {
33
+ const parsedData = this.parseMessage(message);
34
+ yield this.onMessage(parsedData, message);
35
+ // Kafka automatically commits offsets by default (autoCommit: true)
36
+ // If you want manual commit, set autoCommit to false in run() options
37
+ }
38
+ catch (err) {
39
+ console.error('Error processing message:', err);
40
+ // Implement your error handling strategy here
41
+ // You might want to:
42
+ // 1. Skip the message and continue
43
+ // 2. Send to dead letter queue
44
+ // 3. Retry with backoff
45
+ throw err; // This will pause the consumer
46
+ }
47
+ }),
48
+ });
49
+ // Handle errors
50
+ this.consumer.on('consumer.crash', (event) => {
51
+ console.error('Consumer crashed:', event.payload.error);
52
+ });
53
+ this.consumer.on('consumer.disconnect', () => {
54
+ console.log('Consumer disconnected');
55
+ });
56
+ });
57
+ }
58
+ parseMessage(message) {
59
+ if (!message.value) {
60
+ throw new Error('Message value is null');
61
+ }
62
+ const data = message.value.toString('utf8');
63
+ return JSON.parse(data);
64
+ }
65
+ disconnect() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ yield this.consumer.disconnect();
68
+ console.log('Kafka consumer disconnected');
69
+ });
70
+ }
71
+ // Manual commit if needed (set autoCommit: false in consumer.run())
72
+ commitOffsets(offsets) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ yield this.consumer.commitOffsets(offsets);
75
+ });
76
+ }
77
+ }
78
+ exports.KafkaListener = KafkaListener;
@@ -0,0 +1,16 @@
1
+ import { Producer, RecordMetadata } from 'kafkajs';
2
+ import { Subjects } from './subjects';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class KafkaPublisher<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ protected producer: Producer;
10
+ constructor(producer: Producer);
11
+ publish(data: T['data']): Promise<RecordMetadata[]>;
12
+ publishBatch(dataArray: T['data'][]): Promise<RecordMetadata[]>;
13
+ publishWithKey(data: T['data'], key: string): Promise<RecordMetadata[]>;
14
+ disconnect(): Promise<void>;
15
+ }
16
+ export {};
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.KafkaPublisher = void 0;
13
+ class KafkaPublisher {
14
+ constructor(producer) {
15
+ this.producer = producer;
16
+ }
17
+ publish(data) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ try {
20
+ const result = yield this.producer.send({
21
+ topic: this.subject,
22
+ messages: [
23
+ {
24
+ value: JSON.stringify(data),
25
+ // Optional: add key for partitioning
26
+ // key: data.id,
27
+ // Optional: add headers
28
+ // headers: {
29
+ // 'correlation-id': 'some-id',
30
+ // },
31
+ },
32
+ ],
33
+ });
34
+ console.log('Event published to topic:', this.subject);
35
+ return result;
36
+ }
37
+ catch (err) {
38
+ console.error('Error publishing event:', err);
39
+ throw err;
40
+ }
41
+ });
42
+ }
43
+ // Publish multiple messages at once (batch)
44
+ publishBatch(dataArray) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ try {
47
+ const result = yield this.producer.send({
48
+ topic: this.subject,
49
+ messages: dataArray.map((data) => ({
50
+ value: JSON.stringify(data),
51
+ })),
52
+ });
53
+ console.log(`${dataArray.length} events published to topic:`, this.subject);
54
+ return result;
55
+ }
56
+ catch (err) {
57
+ console.error('Error publishing batch events:', err);
58
+ throw err;
59
+ }
60
+ });
61
+ }
62
+ // Publish with custom partition key
63
+ publishWithKey(data, key) {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ try {
66
+ const result = yield this.producer.send({
67
+ topic: this.subject,
68
+ messages: [
69
+ {
70
+ key: key,
71
+ value: JSON.stringify(data),
72
+ },
73
+ ],
74
+ });
75
+ console.log('Event published to topic:', this.subject, 'with key:', key);
76
+ return result;
77
+ }
78
+ catch (err) {
79
+ console.error('Error publishing event:', err);
80
+ throw err;
81
+ }
82
+ });
83
+ }
84
+ disconnect() {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ yield this.producer.disconnect();
87
+ console.log('Kafka producer disconnected');
88
+ });
89
+ }
90
+ }
91
+ exports.KafkaPublisher = KafkaPublisher;
@@ -0,0 +1,3 @@
1
+ export declare enum Subjects {
2
+ UserCreated = "user:created"
3
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subjects = void 0;
4
+ var Subjects;
5
+ (function (Subjects) {
6
+ Subjects["UserCreated"] = "user:created";
7
+ })(Subjects || (exports.Subjects = Subjects = {}));
8
+ // interface to make sure event names are consistent
@@ -0,0 +1,9 @@
1
+ import { Subjects } from './subjects';
2
+ export interface UserCreatedEvent {
3
+ subject: Subjects.UserCreated;
4
+ data: {
5
+ id: string;
6
+ name: string;
7
+ email: string;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/build/index.d.ts CHANGED
@@ -4,7 +4,9 @@ export * from './errors/database-connection-error';
4
4
  export * from './errors/not-authorized-error';
5
5
  export * from './errors/not-found-error';
6
6
  export * from './errors/request-validation-error';
7
+ export * from './errors/forbidden-error';
7
8
  export * from './middlewares/current-user';
8
9
  export * from './middlewares/error-handler';
9
10
  export * from './middlewares/require-auth';
10
11
  export * from './middlewares/validate-request';
12
+ export * from './middlewares/restrinct-to';
package/build/index.js CHANGED
@@ -20,7 +20,9 @@ __exportStar(require("./errors/database-connection-error"), exports);
20
20
  __exportStar(require("./errors/not-authorized-error"), exports);
21
21
  __exportStar(require("./errors/not-found-error"), exports);
22
22
  __exportStar(require("./errors/request-validation-error"), exports);
23
+ __exportStar(require("./errors/forbidden-error"), exports);
23
24
  __exportStar(require("./middlewares/current-user"), exports);
24
25
  __exportStar(require("./middlewares/error-handler"), exports);
25
26
  __exportStar(require("./middlewares/require-auth"), exports);
26
27
  __exportStar(require("./middlewares/validate-request"), exports);
28
+ __exportStar(require("./middlewares/restrinct-to"), exports);
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from 'express';
2
2
  interface UserPayload {
3
3
  id: string;
4
4
  email: string;
5
+ role: string;
5
6
  }
6
7
  declare global {
7
8
  namespace Express {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const forbidden_error_1 = require("../errors/forbidden-error");
4
+ const restrictTo = (...roles) => (req, res, next) => {
5
+ // roles is an array ['admin']
6
+ if (!roles.includes(req.currentUser.role)) {
7
+ throw new forbidden_error_1.ForbiddenError(); // forbidden
8
+ }
9
+ next();
10
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@car-parts/common",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -16,6 +16,7 @@
16
16
  "license": "ISC",
17
17
  "description": "",
18
18
  "devDependencies": {
19
+ "@types/node": "^24.10.1",
19
20
  "del-cli": "^6.0.0",
20
21
  "typescript": "^5.8.3"
21
22
  },
@@ -27,6 +28,7 @@
27
28
  "express": "^5.1.0",
28
29
  "express-validator": "^7.2.1",
29
30
  "jsonwebtoken": "^9.0.2",
31
+ "kafkajs": "^2.2.4",
30
32
  "node-nats-streaming": "^0.3.2"
31
33
  }
32
34
  }