Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpinheiroalmeida committed Dec 18, 2020
1 parent cbc7818 commit 6a7cc4d
Show file tree
Hide file tree
Showing 13 changed files with 8,060 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
dist
reports
*.serverless
.vscode
.DS_Store
.stryker-tmp
3 changes: 3 additions & 0 deletions ioc.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"es6": true
}
62 changes: 62 additions & 0 deletions lib/aws/sqs-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Factory, ObjectFactory, Singleton } from 'typescript-ioc';
import { SQS } from 'aws-sdk';
import { AwsError } from '../erros';

const sqsAdapterFactory: ObjectFactory = () => {
const awsConfig = { region: process.env['AWS_REGION'] };

const sqs: SQS = new SQS(awsConfig);
return new SQSAdapter(sqs);
};


@Factory(sqsAdapterFactory)
@Singleton
export class SQSAdapter {

private sqsClient: SQS;

constructor(client: SQS) {
this.sqsClient = client;
}

public async deleteMessage(queueArn: string, receiptHandle: string): Promise<any> {
try {
const queueUrl = this.buildQueueUrlFromArn(queueArn);
await this.sqsClient.deleteMessage({
QueueUrl: queueUrl,
ReceiptHandle: receiptHandle,
}).promise();
} catch (error) {

throw new AwsError(error.message);
}
}

private buildQueueUrlFromArn(queueArn: string): string {
const region = queueArn.split(':')[3];
const accountId = queueArn.split(':')[4];
const queueName = queueArn.split(':')[5];
const queueUrl = `https://sqs.${region}.amazonaws.com/${accountId}/${queueName}`;

return queueUrl;
}

public async sendMessage(queueUrl: string, message: any): Promise<any> {
try {
const messageResult: SQS.SendMessageResult = await this.sqsClient.sendMessage({
MessageBody: JSON.stringify(message),
QueueUrl: queueUrl
}).promise();

return {
messageId: messageResult.MessageId,
createdAt: new Date().toISOString()
};
} catch (error) {

throw new AwsError(error.message);
}
}

}
7 changes: 7 additions & 0 deletions lib/erros.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class AwsError extends Error {
public readonly code: string = 'AwsError';
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, AwsError.prototype);
}
}
Loading

0 comments on commit 6a7cc4d

Please sign in to comment.