@uber-clone/common 1.0.8 → 1.0.10
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/events/driver-events.d.ts +37 -0
- package/build/events/driver-events.js +2 -0
- package/build/events/kafka-client.d.ts +2 -0
- package/build/events/kafka-client.js +57 -37
- package/build/events/passenger-events.d.ts +25 -0
- package/build/events/passenger-events.js +2 -0
- package/build/events/subjects.d.ts +4 -0
- package/build/events/subjects.js +4 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Subjects } from "./subjects";
|
|
2
|
+
export interface DriverCreatedEvent {
|
|
3
|
+
type: Subjects.DriverCreated;
|
|
4
|
+
data: {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
email: string;
|
|
8
|
+
phone: string;
|
|
9
|
+
city: string;
|
|
10
|
+
vehicle: {
|
|
11
|
+
make: string;
|
|
12
|
+
model: string;
|
|
13
|
+
year: number;
|
|
14
|
+
plate: string;
|
|
15
|
+
};
|
|
16
|
+
userId: string;
|
|
17
|
+
version: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface DriverUpdatedEvent {
|
|
21
|
+
type: Subjects.DriverUpdated;
|
|
22
|
+
data: {
|
|
23
|
+
id: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
email?: string;
|
|
26
|
+
phone?: string;
|
|
27
|
+
city?: string;
|
|
28
|
+
vehicle?: {
|
|
29
|
+
make: string;
|
|
30
|
+
model: string;
|
|
31
|
+
year: number;
|
|
32
|
+
plate: string;
|
|
33
|
+
};
|
|
34
|
+
userId: string;
|
|
35
|
+
version: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -15,6 +15,8 @@ const subjects_1 = require("./subjects");
|
|
|
15
15
|
class KafkaClient {
|
|
16
16
|
constructor() {
|
|
17
17
|
this.isConnected = false;
|
|
18
|
+
this.isConsumerRunning = false;
|
|
19
|
+
this.subscriptions = new Map();
|
|
18
20
|
this.config = this.getKafkaConfig();
|
|
19
21
|
this.kafka = new kafkajs_1.Kafka({
|
|
20
22
|
clientId: this.config.clientId,
|
|
@@ -77,53 +79,54 @@ class KafkaClient {
|
|
|
77
79
|
}
|
|
78
80
|
ensureTopics() {
|
|
79
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const replicationFactor = parseInt(process.env.KAFKA_REPLICATION_FACTOR || "-1");
|
|
80
83
|
const topics = [
|
|
81
84
|
// User events
|
|
82
|
-
{ topic: subjects_1.Subjects.UserCreated, partitions: 3, replicationFactor
|
|
83
|
-
{ topic: subjects_1.Subjects.UserUpdated, partitions: 3, replicationFactor
|
|
84
|
-
{ topic: subjects_1.Subjects.UserDeleted, partitions: 3, replicationFactor
|
|
85
|
+
{ topic: subjects_1.Subjects.UserCreated, partitions: 3, replicationFactor },
|
|
86
|
+
{ topic: subjects_1.Subjects.UserUpdated, partitions: 3, replicationFactor },
|
|
87
|
+
{ topic: subjects_1.Subjects.UserDeleted, partitions: 3, replicationFactor },
|
|
85
88
|
{
|
|
86
89
|
topic: subjects_1.Subjects.UserProfileUpdated,
|
|
87
90
|
partitions: 3,
|
|
88
|
-
replicationFactor
|
|
91
|
+
replicationFactor,
|
|
89
92
|
},
|
|
90
93
|
// Auth events
|
|
91
|
-
{ topic: subjects_1.Subjects.UserSignedIn, partitions: 3, replicationFactor
|
|
92
|
-
{ topic: subjects_1.Subjects.UserSignedOut, partitions: 3, replicationFactor
|
|
94
|
+
{ topic: subjects_1.Subjects.UserSignedIn, partitions: 3, replicationFactor },
|
|
95
|
+
{ topic: subjects_1.Subjects.UserSignedOut, partitions: 3, replicationFactor },
|
|
93
96
|
{
|
|
94
97
|
topic: subjects_1.Subjects.UserPasswordChanged,
|
|
95
98
|
partitions: 3,
|
|
96
|
-
replicationFactor
|
|
99
|
+
replicationFactor,
|
|
97
100
|
},
|
|
98
101
|
{
|
|
99
102
|
topic: subjects_1.Subjects.UserAccountLocked,
|
|
100
103
|
partitions: 3,
|
|
101
|
-
replicationFactor
|
|
104
|
+
replicationFactor,
|
|
102
105
|
},
|
|
103
106
|
// // Ride events
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
{ topic: subjects_1.Subjects.RideRequested, partitions: 6, replicationFactor },
|
|
108
|
+
{ topic: subjects_1.Subjects.RideAccepted, partitions: 6, replicationFactor },
|
|
109
|
+
{ topic: subjects_1.Subjects.RideStarted, partitions: 6, replicationFactor },
|
|
110
|
+
{ topic: subjects_1.Subjects.RideCompleted, partitions: 6, replicationFactor },
|
|
111
|
+
{ topic: subjects_1.Subjects.RideCancelled, partitions: 6, replicationFactor },
|
|
109
112
|
// // Driver events
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
113
|
+
{ topic: subjects_1.Subjects.DriverOnline, partitions: 6, replicationFactor },
|
|
114
|
+
{ topic: subjects_1.Subjects.DriverOffline, partitions: 6, replicationFactor },
|
|
115
|
+
{
|
|
116
|
+
topic: subjects_1.Subjects.DriverLocationUpdated,
|
|
117
|
+
partitions: 12,
|
|
118
|
+
replicationFactor,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
topic: subjects_1.Subjects.DriverRideAccepted,
|
|
122
|
+
partitions: 6,
|
|
123
|
+
replicationFactor,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
topic: subjects_1.Subjects.DriverRideCompleted,
|
|
127
|
+
partitions: 6,
|
|
128
|
+
replicationFactor,
|
|
129
|
+
},
|
|
127
130
|
// // Payment events
|
|
128
131
|
// { topic: Subjects.PaymentInitiated, partitions: 4, replicationFactor: 3 },
|
|
129
132
|
// { topic: Subjects.PaymentCompleted, partitions: 4, replicationFactor: 3 },
|
|
@@ -208,12 +211,23 @@ class KafkaClient {
|
|
|
208
211
|
subscribe(topic, handler, options) {
|
|
209
212
|
return __awaiter(this, void 0, void 0, function* () {
|
|
210
213
|
yield this.ensureConnection();
|
|
214
|
+
// Store the handler so we can restart the consumer with all topics if needed
|
|
215
|
+
this.subscriptions.set(topic, handler);
|
|
211
216
|
try {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
+
if (this.isConsumerRunning) {
|
|
218
|
+
// Consumer is already running — stop it, re-subscribe all topics, then restart
|
|
219
|
+
yield this.consumer.stop();
|
|
220
|
+
this.isConsumerRunning = false;
|
|
221
|
+
}
|
|
222
|
+
// Subscribe all known topics (KafkaJS requires this before consumer.run())
|
|
223
|
+
for (const [t,] of this.subscriptions) {
|
|
224
|
+
yield this.consumer.subscribe({
|
|
225
|
+
topic: t,
|
|
226
|
+
fromBeginning: (options === null || options === void 0 ? void 0 : options.fromBeginning) || false,
|
|
227
|
+
});
|
|
228
|
+
console.log(`Subscribed to topic: ${t}`);
|
|
229
|
+
}
|
|
230
|
+
// Start the consumer loop once with a dispatcher that routes to the correct handler
|
|
217
231
|
yield this.consumer.run({
|
|
218
232
|
eachMessage: (_a) => __awaiter(this, [_a], void 0, function* ({ topic, partition, message }) {
|
|
219
233
|
try {
|
|
@@ -226,14 +240,20 @@ class KafkaClient {
|
|
|
226
240
|
offset: message.offset,
|
|
227
241
|
eventType: event.type || "unknown",
|
|
228
242
|
});
|
|
229
|
-
|
|
243
|
+
const topicHandler = this.subscriptions.get(topic);
|
|
244
|
+
if (topicHandler) {
|
|
245
|
+
yield topicHandler(event);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
console.warn(`No handler registered for topic: ${topic}`);
|
|
249
|
+
}
|
|
230
250
|
}
|
|
231
251
|
catch (error) {
|
|
232
252
|
console.error(`Error processing message from ${topic}:${partition}:`, error);
|
|
233
|
-
// In production, you might want to implement dead letter queue or retry logic here
|
|
234
253
|
}
|
|
235
254
|
}),
|
|
236
255
|
});
|
|
256
|
+
this.isConsumerRunning = true;
|
|
237
257
|
}
|
|
238
258
|
catch (error) {
|
|
239
259
|
console.error(`Failed to subscribe to topic ${topic}:`, error);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Subjects } from "./subjects";
|
|
2
|
+
export interface PassengerCreatedEvent {
|
|
3
|
+
type: Subjects.PassengerCreated;
|
|
4
|
+
data: {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
email: string;
|
|
8
|
+
phone: string;
|
|
9
|
+
paymentMethod: string;
|
|
10
|
+
userId: string;
|
|
11
|
+
version: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface PassengerUpdatedEvent {
|
|
15
|
+
type: Subjects.PassengerUpdated;
|
|
16
|
+
data: {
|
|
17
|
+
id: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
email?: string;
|
|
20
|
+
phone?: string;
|
|
21
|
+
paymentMethod?: string;
|
|
22
|
+
userId: string;
|
|
23
|
+
version: number;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -17,6 +17,10 @@ export declare enum Subjects {
|
|
|
17
17
|
DriverLocationUpdated = "driver.location-updated",
|
|
18
18
|
DriverRideAccepted = "driver.ride-accepted",
|
|
19
19
|
DriverRideCompleted = "driver.ride-completed",
|
|
20
|
+
DriverCreated = "driver.created",
|
|
21
|
+
DriverUpdated = "driver.updated",
|
|
22
|
+
PassengerCreated = "passenger.created",
|
|
23
|
+
PassengerUpdated = "passenger.updated",
|
|
20
24
|
PaymentInitiated = "payment.initiated",
|
|
21
25
|
PaymentCompleted = "payment.completed",
|
|
22
26
|
PaymentFailed = "payment.failed",
|
package/build/events/subjects.js
CHANGED
|
@@ -26,6 +26,10 @@ var Subjects;
|
|
|
26
26
|
Subjects["DriverLocationUpdated"] = "driver.location-updated";
|
|
27
27
|
Subjects["DriverRideAccepted"] = "driver.ride-accepted";
|
|
28
28
|
Subjects["DriverRideCompleted"] = "driver.ride-completed";
|
|
29
|
+
Subjects["DriverCreated"] = "driver.created";
|
|
30
|
+
Subjects["DriverUpdated"] = "driver.updated";
|
|
31
|
+
Subjects["PassengerCreated"] = "passenger.created";
|
|
32
|
+
Subjects["PassengerUpdated"] = "passenger.updated";
|
|
29
33
|
// Payment events
|
|
30
34
|
Subjects["PaymentInitiated"] = "payment.initiated";
|
|
31
35
|
Subjects["PaymentCompleted"] = "payment.completed";
|
package/build/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export * from "./middlewares/validate-request";
|
|
|
11
11
|
export * from "./events/kafka-client";
|
|
12
12
|
export * from "./events/types";
|
|
13
13
|
export * from "./events/subjects";
|
|
14
|
+
export * from "./events/driver-events";
|
|
15
|
+
export * from "./events/passenger-events";
|
|
14
16
|
export * from "./metrics/app.metrics";
|
|
15
17
|
export * from "./utils/logger";
|
|
16
18
|
export * from "./redis/redis-client";
|
package/build/index.js
CHANGED
|
@@ -27,6 +27,8 @@ __exportStar(require("./middlewares/validate-request"), exports);
|
|
|
27
27
|
__exportStar(require("./events/kafka-client"), exports);
|
|
28
28
|
__exportStar(require("./events/types"), exports);
|
|
29
29
|
__exportStar(require("./events/subjects"), exports);
|
|
30
|
+
__exportStar(require("./events/driver-events"), exports);
|
|
31
|
+
__exportStar(require("./events/passenger-events"), exports);
|
|
30
32
|
__exportStar(require("./metrics/app.metrics"), exports);
|
|
31
33
|
__exportStar(require("./utils/logger"), exports);
|
|
32
34
|
__exportStar(require("./redis/redis-client"), exports);
|