idea-aws 3.6.2 → 3.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SNS = void 0;
4
+ const aws_sdk_1 = require("aws-sdk");
5
+ const idea_toolbox_1 = require("idea-toolbox");
6
+ // declare libs as global vars to be reused in warm starts by the Lambda function
7
+ let ideaWarmStart_sns = null;
8
+ /**
9
+ * A wrapper for AWS Simple Notification Service.
10
+ */
11
+ class SNS {
12
+ /**
13
+ * Create a new endpoint in the SNS platform specified.
14
+ * @return platform endpoint ARN
15
+ */
16
+ async createPushPlatormEndpoint(platform, token, snsParams) {
17
+ let platformARN;
18
+ switch (platform) {
19
+ case idea_toolbox_1.PushNotificationsPlatforms.APNS:
20
+ platformARN = snsParams.appleArn;
21
+ break;
22
+ case idea_toolbox_1.PushNotificationsPlatforms.APNS_SANDBOX:
23
+ platformARN = snsParams.appleDevArn;
24
+ break;
25
+ case idea_toolbox_1.PushNotificationsPlatforms.FCM:
26
+ platformARN = snsParams.androidArn;
27
+ break;
28
+ default:
29
+ throw new Error('Unsupported platform');
30
+ }
31
+ (0, idea_toolbox_1.logger)('SNS ADD PLATFORM ENDPOINT');
32
+ if (!ideaWarmStart_sns)
33
+ ideaWarmStart_sns = new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region });
34
+ const result = await ideaWarmStart_sns
35
+ .createPlatformEndpoint({ PlatformApplicationArn: platformARN, Token: token })
36
+ .promise();
37
+ return result.EndpointArn;
38
+ }
39
+ /**
40
+ * Publish a message to a SNS endpoint.
41
+ */
42
+ async publish(snsParams) {
43
+ let structuredMessage;
44
+ if (snsParams.json)
45
+ structuredMessage = { default: JSON.stringify(snsParams.json) };
46
+ else
47
+ switch (snsParams.platform) {
48
+ case idea_toolbox_1.PushNotificationsPlatforms.APNS:
49
+ structuredMessage = { APNS: JSON.stringify({ aps: { alert: snsParams.message } }) };
50
+ break;
51
+ case idea_toolbox_1.PushNotificationsPlatforms.APNS_SANDBOX:
52
+ structuredMessage = { APNS_SANDBOX: JSON.stringify({ aps: { alert: snsParams.message } }) };
53
+ break;
54
+ case idea_toolbox_1.PushNotificationsPlatforms.FCM:
55
+ structuredMessage = {
56
+ GCM: JSON.stringify({ notification: { body: snsParams.message, title: snsParams.message } })
57
+ };
58
+ break;
59
+ default:
60
+ throw new Error('Unsupported platform');
61
+ }
62
+ (0, idea_toolbox_1.logger)('SNS PUBLISH IN TOPIC');
63
+ if (!ideaWarmStart_sns)
64
+ ideaWarmStart_sns = new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region });
65
+ return await ideaWarmStart_sns
66
+ .publish({ MessageStructure: 'json', Message: JSON.stringify(structuredMessage), TargetArn: snsParams.endpoint })
67
+ .promise();
68
+ }
69
+ }
70
+ exports.SNS = SNS;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamController = void 0;
4
+ const idea_toolbox_1 = require("idea-toolbox");
5
+ const genericController_1 = require("./genericController");
6
+ /**
7
+ * An abstract class to inherit to manage AWS DDB streams in an AWS Lambda function.
8
+ */
9
+ class StreamController extends genericController_1.GenericController {
10
+ constructor(event, callback, options) {
11
+ super(event, callback, options);
12
+ this.records = event.Records || [];
13
+ (0, idea_toolbox_1.logger)(`START STREAM: ${this.records.length || 0} records`, null, null, true);
14
+ }
15
+ }
16
+ exports.StreamController = StreamController;
@@ -4,6 +4,9 @@ import { Languages, PDFEntity, PDFTemplateSection } from 'idea-toolbox';
4
4
  * A wrapper for Amazon Translate.
5
5
  */
6
6
  export declare class Translate {
7
+ /**
8
+ * The instance of Amazon Translate.
9
+ */
7
10
  protected translate: AWSTranslate;
8
11
  /**
9
12
  * Default input language code.
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Translate = void 0;
4
+ const aws_sdk_1 = require("aws-sdk");
5
+ const idea_toolbox_1 = require("idea-toolbox");
6
+ // declare libs as global vars to be reused in warm starts by the Lambda function
7
+ let ideaWarmStart_translate = null;
8
+ /**
9
+ * A wrapper for Amazon Translate.
10
+ */
11
+ class Translate {
12
+ /**
13
+ * Initialize a new Translate helper object.
14
+ */
15
+ constructor() {
16
+ if (!ideaWarmStart_translate)
17
+ ideaWarmStart_translate = new aws_sdk_1.Translate({ apiVersion: '2017-07-01' });
18
+ this.translate = ideaWarmStart_translate;
19
+ this.sourceLanguageCode = 'en';
20
+ this.targetLanguageCode = 'en';
21
+ this.terminologyNames = new Array();
22
+ }
23
+ /**
24
+ * Translates input text from the source language to the target language.
25
+ * @param params the parameters for translateText
26
+ */
27
+ async text(params) {
28
+ if (params.sourceLanguageCode)
29
+ this.sourceLanguageCode = params.sourceLanguageCode;
30
+ if (params.targetLanguageCode)
31
+ this.targetLanguageCode = params.targetLanguageCode;
32
+ if (params.terminologyNames)
33
+ this.terminologyNames = params.terminologyNames;
34
+ if (!this.sourceLanguageCode || !this.targetLanguageCode || !params.text)
35
+ throw new Error('Bad parameters');
36
+ const result = await this.translate
37
+ .translateText({
38
+ Text: params.text,
39
+ SourceLanguageCode: this.sourceLanguageCode,
40
+ TargetLanguageCode: this.targetLanguageCode,
41
+ TerminologyNames: this.terminologyNames
42
+ })
43
+ .promise();
44
+ return result.TranslatedText;
45
+ }
46
+ /**
47
+ * Get the contents of a PDF template (against a PDFEntity) translated in the desired language,
48
+ * if the latter isn't between the ones already available.
49
+ * @return an object that maps original texts with their translations (or nothing).
50
+ */
51
+ async pdfTemplate(entity, template, language, languages) {
52
+ // if the language is included in the ones supported by the team, skip
53
+ if (languages.available.some(l => l === language))
54
+ return null;
55
+ // analyse the template to extract terms to translate based on the entity (using a sourceLanguage as reference)
56
+ const termsToTranslate = Array.from(await this.analysePDFTemplateForTermsToTranslate(template, entity, languages.default));
57
+ const translations = {};
58
+ for (let i = 0; i < termsToTranslate.length; i++) {
59
+ const original = termsToTranslate[i];
60
+ const translated = await this.text({
61
+ sourceLanguageCode: languages.default,
62
+ targetLanguageCode: language,
63
+ text: original
64
+ });
65
+ translations[original] = translated
66
+ // fix markdown issue (the translations add a space before and after asterisks)
67
+ .replace(/\*\* /gm, '**')
68
+ .replace(/ \*\*/gm, '**');
69
+ }
70
+ return translations;
71
+ }
72
+ /**
73
+ * Analyse a PDFTemplate to extract terms to translate based on a PDFEntity (using a sourceLanguage as reference).
74
+ */
75
+ async analysePDFTemplateForTermsToTranslate(template, entity, sourceLanguage) {
76
+ const toTranslate = new Set();
77
+ // gather the terms to translate from contents available on this level
78
+ template
79
+ .filter(s => s.isEither(idea_toolbox_1.PDFTemplateSectionTypes.ROW, idea_toolbox_1.PDFTemplateSectionTypes.HEADER))
80
+ .forEach(s => {
81
+ switch (s.type) {
82
+ case idea_toolbox_1.PDFTemplateSectionTypes.ROW:
83
+ s.columns
84
+ .filter((_, index) => s.doesColumnContainAField(index))
85
+ .forEach(field => {
86
+ field = field;
87
+ if (field.isComplex()) {
88
+ const complex = field;
89
+ toTranslate.add(complex.content[sourceLanguage]);
90
+ }
91
+ else {
92
+ const simple = field;
93
+ toTranslate.add(simple.label[sourceLanguage]);
94
+ // try to consider only notes (long fields)
95
+ if (typeof entity[simple.code] === 'string' && entity[simple.code].length > 50)
96
+ toTranslate.add(entity[simple.code]);
97
+ }
98
+ });
99
+ break;
100
+ case idea_toolbox_1.PDFTemplateSectionTypes.HEADER:
101
+ toTranslate.add(s.title[sourceLanguage]);
102
+ break;
103
+ }
104
+ });
105
+ // gather inner sections in a flat structure for further elaboraton
106
+ const innerSections = new Array();
107
+ template
108
+ .filter(s => s.isEither(idea_toolbox_1.PDFTemplateSectionTypes.INNER_SECTION, idea_toolbox_1.PDFTemplateSectionTypes.REPEATED_INNER_SECTION))
109
+ .forEach(s => {
110
+ switch (s.type) {
111
+ case idea_toolbox_1.PDFTemplateSectionTypes.INNER_SECTION:
112
+ innerSections.push({ data: entity[s.context], template: s.innerTemplate });
113
+ break;
114
+ case idea_toolbox_1.PDFTemplateSectionTypes.REPEATED_INNER_SECTION:
115
+ entity[s.context].forEach((element) => innerSections.push({ data: element, template: s.innerTemplate }));
116
+ break;
117
+ }
118
+ });
119
+ // run (inception) the inner sections to gather terms to translate from inner levels
120
+ for (let i = 0; i < innerSections.length; i++) {
121
+ const s = innerSections[i];
122
+ const res = await this.analysePDFTemplateForTermsToTranslate(s.template, s.data, sourceLanguage);
123
+ res.forEach(x => toTranslate.add(x));
124
+ }
125
+ return toTranslate;
126
+ }
127
+ }
128
+ exports.Translate = Translate;
package/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export * from './src/genericController';
2
+ export * from './src/resourceController';
3
+ export * from './src/streamController';
4
+
5
+ export * from './src/dynamoDB';
6
+ export * from './src/cognito';
7
+ export * from './src/comprehend';
8
+ export * from './src/s3';
9
+ export * from './src/ses';
10
+ export * from './src/sns';
11
+ export * from './src/translate';
12
+ export * from './src/attachments';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "3.6.2",
3
+ "version": "3.7.2",
4
4
  "description": "AWS wrappers to use in IDEA's back-ends",
5
- "engines": {
6
- "node": ">=6.0.0"
7
- },
8
5
  "main": "dist/index.js",
9
6
  "types": "dist/index.d.ts",
10
7
  "scripts": {
8
+ "lint": "eslint --ext .ts",
9
+ "compile": "tsc --build",
10
+ "docs": "typedoc",
11
11
  "build": "./build.sh",
12
12
  "publishPackage": "./build.sh && npm publish"
13
13
  },
@@ -34,30 +34,24 @@
34
34
  },
35
35
  "homepage": "https://uatisdeproblem.github.io/IDEA-AWS",
36
36
  "dependencies": {
37
- "idea-toolbox": "^6.3.2",
38
- "nanoid": "^3.1.23",
39
- "nodemailer": "^6.6.0",
37
+ "idea-toolbox": "^6.4.1",
38
+ "nanoid": "^3.1.25",
39
+ "nodemailer": "^6.6.3",
40
40
  "shortid": "^2.2.16",
41
- "uuid": "^3.4.0"
41
+ "uuid": "^8.3.2"
42
42
  },
43
43
  "devDependencies": {
44
- "@types/aws-lambda": "^8.10.19",
45
- "@types/node": "^11.9.5",
46
- "@types/nodemailer": "^4.6.6",
44
+ "@tsconfig/node14": "^1.0.0",
45
+ "@types/aws-lambda": "^8.10.72",
46
+ "@types/node": "^14.14.26",
47
+ "@types/nodemailer": "^6.4.4",
47
48
  "@types/shortid": "0.0.29",
48
- "@types/uuid": "^3.4.4",
49
- "@typescript-eslint/eslint-plugin": "4.3.0",
50
- "@typescript-eslint/parser": "4.3.0",
51
- "aws-sdk": "^2.411.0",
52
- "eslint": "^7.6.0",
53
- "eslint-plugin-import": "^2.22.1",
54
- "eslint-plugin-jsdoc": "^30.7.9",
55
- "eslint-plugin-prefer-arrow": "^1.2.2",
56
- "ts-loader": "^8.0.14",
57
- "typedoc": "^0.17.8",
58
- "typescript": "^3.9.7",
59
- "webpack": "^5.11.1",
60
- "webpack-cli": "^4.3.1",
61
- "webpack-node-externals": "^2.5.2"
49
+ "@types/uuid": "^8.3.1",
50
+ "@typescript-eslint/eslint-plugin": "^4.31.1",
51
+ "@typescript-eslint/parser": "^4.31.1",
52
+ "aws-sdk": "^2.991.0",
53
+ "eslint": "^7.32.0",
54
+ "typedoc": "^0.22.4",
55
+ "typescript": "^4.4.3"
62
56
  }
63
57
  }
package/webpack.config.js DELETED
@@ -1,18 +0,0 @@
1
- var nodeExternals = require('webpack-node-externals');
2
-
3
- module.exports = {
4
- name: 'prod',
5
- entry: './src/index.ts',
6
- resolve: { extensions: ['.ts', '.tsx', '.js'] },
7
- module: { rules: [{ test: /\.tsx?$/, loader: 'ts-loader', options: { transpileOnly: false } }] },
8
- output: {
9
- filename: 'index.js',
10
- /* path: SPECIFIED BY COMMAND LINE */
11
- libraryTarget: 'umd'
12
- },
13
- target: 'node',
14
- externals: [nodeExternals()],
15
- mode: 'production',
16
- optimization: { minimize: false },
17
- stats: 'normal'
18
- };