1 | import * as AWS from 'aws-sdk';
|
2 | import {IResponse, response} from './tools';
|
3 |
|
4 | AWS.config.update({region: 'us-east-1'});
|
5 | const uuid = require('uuid/v1');
|
6 |
|
7 | const SES = new AWS.SES();
|
8 | const SNS = new AWS.SNS();
|
9 |
|
10 | export interface ILog {
|
11 | id?: string;
|
12 | kind: string;
|
13 | event: string;
|
14 | message: string;
|
15 | referenceId: string;
|
16 | data: any;
|
17 | type: string;
|
18 | }
|
19 |
|
20 | function parseValues(content: string, values: any): string {
|
21 | const keys = Object.keys(values);
|
22 |
|
23 | for (let i = 0; i < keys.length; i++) {
|
24 | const reg = new RegExp('{{' + keys[i] + '}}', 'g');
|
25 | content = content.toString().replace(reg, values[keys[i]]);
|
26 | }
|
27 |
|
28 | return content;
|
29 | }
|
30 |
|
31 | export class Log {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | async sendLog(body: ILog, subject: string, topicArn?: string): Promise<IResponse> {
|
40 |
|
41 | if (!topicArn && process.env.SNS_TOPIC_ARN_HISTORY_LOG) {
|
42 | topicArn = process.env.SNS_TOPIC_ARN_HISTORY_LOG;
|
43 | } else if (!topicArn && !process.env.SNS_TOPIC_ARN_HISTORY_LOG) {
|
44 | return response(false, 'bln-ignore');
|
45 | }
|
46 |
|
47 |
|
48 | const eventText = JSON.stringify(body);
|
49 | let sns = new AWS.SNS();
|
50 | let params = {
|
51 | Message: eventText,
|
52 | Subject: subject,
|
53 | TopicArn: topicArn
|
54 | };
|
55 | try {
|
56 | params['ResponseMetadata'] = await sns.publish(params).promise();
|
57 | return response(true, null, params);
|
58 |
|
59 | } catch (error) {
|
60 | console.log(60, error);
|
61 | return response(false, 'bln-ignore-try-catch', error.message);
|
62 | }
|
63 | }
|
64 | }
|