@bloggie/library 1.0.0
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/index.ts +0 -0
- package/package.json +23 -0
- package/src/events/base-listener.ts +51 -0
- package/src/events/otp-verification-event.ts +9 -0
- package/src/events/resend-otp-event.ts +9 -0
- package/src/events/subjects.ts +5 -0
- package/tsconfig.json +20 -0
package/index.ts
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bloggie/library",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@types/cookie-parser": "^1.4.10",
|
|
13
|
+
"@types/express": "^5.0.6",
|
|
14
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
15
|
+
"cookie-parser": "^1.4.7",
|
|
16
|
+
"express": "^5.2.1",
|
|
17
|
+
"express-validator": "^7.3.2",
|
|
18
|
+
"jsonwebtoken": "^9.0.3",
|
|
19
|
+
"node-nats-streaming": "^0.3.2",
|
|
20
|
+
"ts-node-dev": "^2.0.0",
|
|
21
|
+
"typescript": "^7.0.2"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Message, Stan } from 'node-nats-streaming';
|
|
2
|
+
import { Subjects } from './subjects';
|
|
3
|
+
|
|
4
|
+
interface Events {
|
|
5
|
+
subject: Subjects;
|
|
6
|
+
data: any;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export abstract class Listener<T extends Events> {
|
|
10
|
+
abstract subject: T['subject'];
|
|
11
|
+
private client: Stan;
|
|
12
|
+
readonly ackWait = 5 * 60; // 5 mins
|
|
13
|
+
abstract onMessage(data: T['data'], msg: Message): void;
|
|
14
|
+
abstract queueGroupName: string;
|
|
15
|
+
|
|
16
|
+
constructor(client: Stan) {
|
|
17
|
+
this.client = client;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
subscriptionOptions() {
|
|
21
|
+
return this.client
|
|
22
|
+
.subscriptionOptions()
|
|
23
|
+
.setDeliverAllAvailable()
|
|
24
|
+
.setManualAckMode(true)
|
|
25
|
+
.setDurableName(this.queueGroupName)
|
|
26
|
+
.setAckWait(this.ackWait);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
listen() {
|
|
30
|
+
const subscriptions = this.client.subscribe(
|
|
31
|
+
this.subject,
|
|
32
|
+
this.queueGroupName,
|
|
33
|
+
this.subscriptionOptions(),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
subscriptions.on('message', (msg: Message) => {
|
|
37
|
+
console.log(`Message received: ${this.subject}/${this.queueGroupName}`);
|
|
38
|
+
const parsedData = this.parsedData(msg);
|
|
39
|
+
|
|
40
|
+
this.onMessage(parsedData, msg);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
parsedData(msg: Message) {
|
|
45
|
+
const data = msg.getData();
|
|
46
|
+
|
|
47
|
+
return typeof data === 'string'
|
|
48
|
+
? JSON.parse(data)
|
|
49
|
+
: JSON.parse(data.toString('utf-8'));
|
|
50
|
+
}
|
|
51
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// tsconfig.json
|
|
2
|
+
{
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"module": "nodenext",
|
|
7
|
+
"moduleResolution": "nodenext",
|
|
8
|
+
"target": "es2020",
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"sourceMap": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"moduleDetection": "force",
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"types": ["node"]
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "build"]
|
|
20
|
+
}
|