-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbc7818
commit 6a7cc4d
Showing
13 changed files
with
8,060 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules | ||
dist | ||
reports | ||
*.serverless | ||
.vscode | ||
.DS_Store | ||
.stryker-tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"es6": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.