-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.ts
65 lines (54 loc) · 1.59 KB
/
routes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ListQueuesCommand } from '@aws-sdk/client-sqs';
import { BaseClient } from './base';
import {
SNSQueueMessageTranslator,
SQSMessageTranslator
} from './message-translators';
import { SQSClientOptions } from './types';
export class SQSRouter extends BaseClient {
queueName: string;
handler: any;
clientOptions: SQSClientOptions;
constructor(
queueName: string,
handler: any,
clientOptions: SQSClientOptions,
..._args: any[]
) {
super(clientOptions);
if (!queueName) {
throw new Error('Queue name must be provided');
}
clientOptions.messageTranslator = new SNSQueueMessageTranslator();
if (clientOptions.messageSource === 'SQS') {
clientOptions.messageTranslator = new SQSMessageTranslator();
}
this.queueName = queueName;
this.handler = handler;
this.clientOptions = clientOptions;
}
private async handlePrefixBasedQueues(
prefix: string,
handler: any
): Promise<void> {
const data = await this.client.send(
new ListQueuesCommand({
QueueNamePrefix: prefix
})
);
if (!data.QueueUrls) {
throw new Error('No queues found with prefix: ' + prefix);
}
for (const queueUrl of data.QueueUrls) {
const queueHandler = new handler(queueUrl, this.clientOptions);
queueHandler.start();
}
}
public async start(): Promise<void> {
if (this.clientOptions.prefixBasedQueues) {
return this.handlePrefixBasedQueues(this.queueName, this.handler);
}
const queueHandler = new this.handler(this.queueName, this.clientOptions);
queueHandler.start();
}
}