UNPKG

1.78 kBPlain TextView Raw
1import * as AWS from 'aws-sdk';
2import {IResponse, response} from './tools';
3
4AWS.config.update({region: 'us-east-1'}); // Virginia
5const uuid = require('uuid/v1'); // version 1 is based on time (not random)
6
7const SES = new AWS.SES(); //{apiVersion: '2010-12-01'}
8const SNS = new AWS.SNS();
9
10export 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
20function 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
31export class Log {
32 /**
33 *
34 * @param {INotify} body
35 * @param subject
36 * @param topicArn
37 * @returns {Promise<>}
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 // Create sns params
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}