idea-aws 3.16.2 → 3.16.4

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.
@@ -1,10 +1,14 @@
1
1
  import { DynamoDB as DDB } from 'aws-sdk';
2
+ import { Logger } from './logger';
2
3
  /**
3
4
  * A wrapper for AWS DynamoDB.
4
5
  */
5
6
  export declare class DynamoDB {
6
7
  protected dynamo: DDB.DocumentClient;
7
- constructor();
8
+ logger: Logger;
9
+ constructor(options?: {
10
+ debug: boolean;
11
+ });
8
12
  /**
9
13
  * Convert a JSON object from dynamoDB format to simple JSON.
10
14
  */
@@ -7,13 +7,14 @@ const nanoid_1 = require("nanoid");
7
7
  const NanoID = (0, nanoid_1.customAlphabet)('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 25);
8
8
  const shortid_1 = require("shortid");
9
9
  const logger_1 = require("./logger");
10
- const logger = new logger_1.Logger();
11
10
  /**
12
11
  * A wrapper for AWS DynamoDB.
13
12
  */
14
13
  class DynamoDB {
15
- constructor() {
14
+ constructor(options = { debug: true }) {
15
+ this.logger = new logger_1.Logger();
16
16
  this.dynamo = new aws_sdk_1.DynamoDB.DocumentClient();
17
+ this.logger.level = options.debug ? 'DEBUG' : 'INFO';
17
18
  }
18
19
  /**
19
20
  * Convert a JSON object from dynamoDB format to simple JSON.
@@ -101,7 +102,7 @@ class DynamoDB {
101
102
  * @param key the key of the counter
102
103
  */
103
104
  async getAtomicCounterByKey(key) {
104
- logger.debug(`Get atomic counter for ${key}`);
105
+ this.logger.debug(`Get atomic counter for ${key}`);
105
106
  const result = await this.update({
106
107
  TableName: 'idea_atomicCounters',
107
108
  Key: { key },
@@ -118,7 +119,7 @@ class DynamoDB {
118
119
  * Get an item of a DynamoDB table.
119
120
  */
120
121
  async get(params) {
121
- logger.debug(`Get ${params.TableName}`);
122
+ this.logger.debug(`Get ${params.TableName}`);
122
123
  const result = await this.dynamo.get(params).promise();
123
124
  if (!result?.Item)
124
125
  throw new Error('Not found');
@@ -128,21 +129,21 @@ class DynamoDB {
128
129
  * Put an item in a DynamoDB table.
129
130
  */
130
131
  async put(params) {
131
- logger.debug(`Put ${params.TableName}`);
132
+ this.logger.debug(`Put ${params.TableName}`);
132
133
  return await this.dynamo.put(params).promise();
133
134
  }
134
135
  /**
135
136
  * Update an item of a DynamoDB table.
136
137
  */
137
138
  async update(params) {
138
- logger.debug(`Update ${params.TableName}`);
139
+ this.logger.debug(`Update ${params.TableName}`);
139
140
  return await this.dynamo.update(params).promise();
140
141
  }
141
142
  /**
142
143
  * Delete an item of a DynamoDB table.
143
144
  */
144
145
  async delete(params) {
145
- logger.debug(`Delete ${params.TableName}`);
146
+ this.logger.debug(`Delete ${params.TableName}`);
146
147
  return await this.dynamo.delete(params).promise();
147
148
  }
148
149
  /**
@@ -151,7 +152,7 @@ class DynamoDB {
151
152
  */
152
153
  async batchGet(table, keys, ignoreErr) {
153
154
  if (!keys.length) {
154
- logger.debug(`Batch get ${table}: no elements to get`);
155
+ this.logger.debug(`Batch get ${table}: no elements to get`);
155
156
  return [];
156
157
  }
157
158
  return await this.batchGetHelper(table, keys, [], Boolean(ignoreErr));
@@ -162,7 +163,7 @@ class DynamoDB {
162
163
  [table]: { Keys: keys.slice(currentChunk, currentChunk + chunkSize) }
163
164
  }
164
165
  };
165
- logger.debug(`Batch get ${table}: ${currentChunk} of ${keys.length}`);
166
+ this.logger.debug(`Batch get ${table}: ${currentChunk} of ${keys.length}`);
166
167
  let result;
167
168
  try {
168
169
  result = await this.dynamo.batchGet(batch).promise();
@@ -187,7 +188,7 @@ class DynamoDB {
187
188
  */
188
189
  async batchPut(table, items) {
189
190
  if (!items.length)
190
- return logger.debug(`Batch write (put) ${table}: no elements to write`);
191
+ return this.logger.debug(`Batch write (put) ${table}: no elements to write`);
191
192
  await this.batchWriteHelper(table, items, true);
192
193
  }
193
194
  /**
@@ -197,11 +198,11 @@ class DynamoDB {
197
198
  */
198
199
  async batchDelete(table, keys) {
199
200
  if (!keys.length)
200
- return logger.debug(`Batch write (delete) ${table}: no elements to write`);
201
+ return this.logger.debug(`Batch write (delete) ${table}: no elements to write`);
201
202
  await this.batchWriteHelper(table, keys, false);
202
203
  }
203
204
  async batchWriteHelper(table, itemsOrKeys, isPut, currentChunk = 0, chunkSize = 25) {
204
- logger.debug(`Batch write (${isPut ? 'put' : 'delete'}) ${table}: ${currentChunk} of ${itemsOrKeys.length}`);
205
+ this.logger.debug(`Batch write (${isPut ? 'put' : 'delete'}) ${table}: ${currentChunk} of ${itemsOrKeys.length}`);
205
206
  let requests;
206
207
  if (isPut)
207
208
  requests = itemsOrKeys.slice(currentChunk, currentChunk + chunkSize).map(i => ({ PutRequest: { Item: i } }));
@@ -226,7 +227,7 @@ class DynamoDB {
226
227
  params.RequestItems = response.UnprocessedItems;
227
228
  attempts++;
228
229
  const waitSeconds = getRandomInt(attempts * 5);
229
- logger.debug(`Batch write throttled: waiting ${waitSeconds} seconds to retry`);
230
+ this.logger.debug(`Batch write throttled: waiting ${waitSeconds} seconds to retry`);
230
231
  await wait(waitSeconds);
231
232
  }
232
233
  else {
@@ -239,9 +240,9 @@ class DynamoDB {
239
240
  * @param params the params to apply to DynamoDB's function
240
241
  */
241
242
  async query(params) {
242
- logger.debug(`Query ${params.TableName}`);
243
+ this.logger.debug(`Query ${params.TableName}`);
243
244
  const result = await this.queryScanHelper(params, [], true);
244
- logger.debug(`Results query ${params.TableName}: ${result?.length || 0}`);
245
+ this.logger.debug(`Results query ${params.TableName}: ${result?.length || 0}`);
245
246
  return result;
246
247
  }
247
248
  /**
@@ -249,9 +250,9 @@ class DynamoDB {
249
250
  * @param params the params to apply to DynamoDB's function
250
251
  */
251
252
  async scan(params) {
252
- logger.debug(`Scan ${params.TableName}`);
253
+ this.logger.debug(`Scan ${params.TableName}`);
253
254
  const result = await this.queryScanHelper(params, [], false);
254
- logger.debug(`Results scan ${params.TableName}: ${result?.length || 0}`);
255
+ this.logger.debug(`Results scan ${params.TableName}: ${result?.length || 0}`);
255
256
  return result;
256
257
  }
257
258
  async queryScanHelper(params, items, isQuery) {
@@ -273,9 +274,9 @@ class DynamoDB {
273
274
  * @param params the params to apply to DynamoDB's function
274
275
  */
275
276
  async queryClassic(params) {
276
- logger.debug(`Query classic ${params.TableName}`);
277
+ this.logger.debug(`Query classic ${params.TableName}`);
277
278
  const result = await this.dynamo.query(params).promise();
278
- logger.debug(`Results query classic ${params.TableName}: ${result?.Items?.length || 0}`);
279
+ this.logger.debug(`Results query classic ${params.TableName}: ${result?.Items?.length || 0}`);
279
280
  return result;
280
281
  }
281
282
  /**
@@ -283,9 +284,9 @@ class DynamoDB {
283
284
  * @param params the params to apply to DynamoDB's function
284
285
  */
285
286
  async scanClassic(params) {
286
- logger.debug(`Scan classic ${params.TableName}`);
287
+ this.logger.debug(`Scan classic ${params.TableName}`);
287
288
  const result = await this.dynamo.scan(params).promise();
288
- logger.debug(`Results scan classic ${params.TableName}: ${result?.Items?.length || 0}`);
289
+ this.logger.debug(`Results scan classic ${params.TableName}: ${result?.Items?.length || 0}`);
289
290
  return result;
290
291
  }
291
292
  /**
@@ -294,8 +295,8 @@ class DynamoDB {
294
295
  */
295
296
  async transactWrites(ops) {
296
297
  if (!ops.length)
297
- return logger.debug('Transaction writes: no elements to write');
298
- logger.debug('Transaction writes');
298
+ return this.logger.debug('Transaction writes: no elements to write');
299
+ this.logger.debug('Transaction writes');
299
300
  await this.dynamo.transactWrite({ TransactItems: ops.slice(0, 10) }).promise();
300
301
  }
301
302
  /**
@@ -1,3 +1,4 @@
1
+ import 'source-map-support/register';
1
2
  import { Logger } from './logger';
2
3
  /**
3
4
  * An abstract class to inherit to manage some resources with an AWS Lambda function.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GenericController = void 0;
4
+ require("source-map-support/register");
4
5
  const logger_1 = require("./logger");
5
6
  /**
6
7
  * An abstract class to inherit to manage some resources with an AWS Lambda function.
@@ -1,7 +1,5 @@
1
- import 'source-map-support/register';
2
1
  import { APIGatewayProxyEventV2, APIGatewayProxyEvent, Callback } from 'aws-lambda';
3
2
  import { CognitoUser, Auth0User } from 'idea-toolbox';
4
- import { Logger } from './logger';
5
3
  import { CloudWatchMetrics } from './metrics';
6
4
  import { GenericController } from './genericController';
7
5
  /**
@@ -29,7 +27,6 @@ export declare abstract class ResourceController extends GenericController {
29
27
  protected clientVersion: string;
30
28
  protected clientPlatform: string;
31
29
  protected returnStatusCode?: number;
32
- protected logger: Logger;
33
30
  protected logRequestsWithKey: string;
34
31
  protected metrics: CloudWatchMetrics;
35
32
  protected currentLang: string;
@@ -1,11 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RCError = exports.ResourceController = void 0;
4
- require("source-map-support/register");
5
4
  const fs_1 = require("fs");
6
5
  const aws_sdk_1 = require("aws-sdk");
7
6
  const idea_toolbox_1 = require("idea-toolbox");
8
- const logger_1 = require("./logger");
9
7
  const metrics_1 = require("./metrics");
10
8
  const genericController_1 = require("./genericController");
11
9
  const dynamoDB_1 = require("./dynamoDB");
@@ -21,7 +19,6 @@ class ResourceController extends genericController_1.GenericController {
21
19
  this.resource = process?.env?.RESOURCE;
22
20
  this.clientVersion = '?';
23
21
  this.clientPlatform = '?';
24
- this.logger = new logger_1.Logger();
25
22
  this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
26
23
  ///
27
24
  /// REQUEST HANDLERS
package/dist/src/s3.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { S3 as AWSS3 } from 'aws-sdk';
3
3
  import { SignedURL } from 'idea-toolbox';
4
+ import { Logger } from './logger';
4
5
  /**
5
6
  * A wrapper for AWS Simple Storage Service.
6
7
  */
@@ -10,10 +11,10 @@ export declare class S3 {
10
11
  protected DEFAULT_DOWNLOAD_BUCKET: string;
11
12
  protected DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP: number;
12
13
  protected DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP: number;
13
- /**
14
- * Initialize a new S3 helper object.
15
- */
16
- constructor();
14
+ logger: Logger;
15
+ constructor(options?: {
16
+ debug: boolean;
17
+ });
17
18
  /**
18
19
  * Create a download link of a piece of data (through S3).
19
20
  * *Practically*, it uploads the file on an S3 bucket, generating and returning a url to it.
package/dist/src/s3.js CHANGED
@@ -4,20 +4,18 @@ exports.GetObjectTypes = exports.S3 = void 0;
4
4
  const aws_sdk_1 = require("aws-sdk");
5
5
  const idea_toolbox_1 = require("idea-toolbox");
6
6
  const logger_1 = require("./logger");
7
- const logger = new logger_1.Logger();
8
7
  /**
9
8
  * A wrapper for AWS Simple Storage Service.
10
9
  */
11
10
  class S3 {
12
- /**
13
- * Initialize a new S3 helper object.
14
- */
15
- constructor() {
11
+ constructor(options = { debug: true }) {
16
12
  this.DEFAULT_DOWNLOAD_BUCKET_PREFIX = 'common';
17
13
  this.DEFAULT_DOWNLOAD_BUCKET = 'idea-downloads';
18
14
  this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP = 180;
19
15
  this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP = 300;
16
+ this.logger = new logger_1.Logger();
20
17
  this.s3 = new aws_sdk_1.S3({ apiVersion: '2006-03-01', signatureVersion: 'v4' });
18
+ this.logger.level = options.debug ? 'DEBUG' : 'INFO';
21
19
  }
22
20
  /**
23
21
  * Create a download link of a piece of data (through S3).
@@ -65,14 +63,14 @@ class S3 {
65
63
  * Make a copy of an object of the bucket.
66
64
  */
67
65
  async copyObject(options) {
68
- logger.debug(`S3 copy object: ${options.key}`);
66
+ this.logger.debug(`S3 copy object: ${options.key}`);
69
67
  await this.s3.copyObject({ CopySource: options.copySource, Bucket: options.bucket, Key: options.key }).promise();
70
68
  }
71
69
  /**
72
70
  * Get an object from a S3 bucket.
73
71
  */
74
72
  async getObject(options) {
75
- logger.debug(`S3 get object: ${options.key}`);
73
+ this.logger.debug(`S3 get object: ${options.key}`);
76
74
  const result = await this.s3.getObject({ Bucket: options.bucket, Key: options.key }).promise();
77
75
  switch (options.type) {
78
76
  case GetObjectTypes.JSON:
@@ -94,21 +92,21 @@ class S3 {
94
92
  params.ACL = options.acl;
95
93
  if (options.metadata)
96
94
  params.Metadata = options.metadata;
97
- logger.debug(`S3 put object: ${options.key}`);
95
+ this.logger.debug(`S3 put object: ${options.key}`);
98
96
  return await this.s3.putObject(params).promise();
99
97
  }
100
98
  /**
101
99
  * Delete an object from an S3 bucket.
102
100
  */
103
101
  async deleteObject(options) {
104
- logger.debug(`S3 delete object: ${options.key}`);
102
+ this.logger.debug(`S3 delete object: ${options.key}`);
105
103
  return await this.s3.deleteObject({ Bucket: options.bucket, Key: options.key }).promise();
106
104
  }
107
105
  /**
108
106
  * List the objects of an S3 bucket.
109
107
  */
110
108
  async listObjects(options) {
111
- logger.debug(`S3 list object: ${options.prefix}`);
109
+ this.logger.debug(`S3 list object: ${options.prefix}`);
112
110
  return await this.s3.listObjects({ Bucket: options.bucket, Prefix: options.prefix }).promise();
113
111
  }
114
112
  /**
package/dist/src/ses.d.ts CHANGED
@@ -2,13 +2,16 @@
2
2
  import { SES as AWSSES } from 'aws-sdk';
3
3
  import { SentMessageInfo as NodemailerSentMessageInfo } from 'nodemailer';
4
4
  import { Headers } from 'nodemailer/lib/mailer';
5
+ import { Logger } from './logger';
5
6
  /**
6
7
  * A wrapper for AWS Simple Email Service.
7
8
  */
8
9
  export declare class SES {
9
10
  protected ses: AWSSES;
10
- constructor(params?: {
11
+ logger: Logger;
12
+ constructor(options?: {
11
13
  region?: string;
14
+ debug: boolean;
12
15
  });
13
16
  getTemplate(templateName: string): Promise<AWSSES.Template>;
14
17
  setTemplate(templateName: string, subject: string, content: string, isHTML?: boolean): Promise<void>;
package/dist/src/ses.js CHANGED
@@ -5,13 +5,14 @@ const aws_sdk_1 = require("aws-sdk");
5
5
  const nodemailer_1 = require("nodemailer");
6
6
  const dynamoDB_1 = require("./dynamoDB");
7
7
  const logger_1 = require("./logger");
8
- const logger = new logger_1.Logger();
9
8
  /**
10
9
  * A wrapper for AWS Simple Email Service.
11
10
  */
12
11
  class SES {
13
- constructor(params = {}) {
14
- this.ses = new aws_sdk_1.SES({ region: params.region });
12
+ constructor(options = { debug: true }) {
13
+ this.logger = new logger_1.Logger();
14
+ this.ses = new aws_sdk_1.SES({ region: options.region });
15
+ this.logger.level = options.debug ? 'DEBUG' : 'INFO';
15
16
  }
16
17
  //
17
18
  // CONFIG
@@ -64,7 +65,7 @@ class SES {
64
65
  ses = this.ses;
65
66
  else
66
67
  ses = new aws_sdk_1.SES({ region: sesParams.region });
67
- logger.debug('SES send templated email');
68
+ this.logger.debug('SES send templated email');
68
69
  return await ses.sendTemplatedEmail(request).promise();
69
70
  }
70
71
  /**
@@ -104,7 +105,7 @@ class SES {
104
105
  ses = this.ses;
105
106
  else
106
107
  ses = new aws_sdk_1.SES({ region: sesParams.region });
107
- logger.debug('SES send email');
108
+ this.logger.debug('SES send email');
108
109
  return await ses.sendEmail(request).promise();
109
110
  }
110
111
  async sendEmailWithNodemailer(emailData, sesParams) {
@@ -128,7 +129,7 @@ class SES {
128
129
  ses = this.ses;
129
130
  else
130
131
  ses = new aws_sdk_1.SES({ region: sesParams.region });
131
- logger.debug('SES send email (Nodemailer)');
132
+ this.logger.debug('SES send email (Nodemailer)');
132
133
  return await (0, nodemailer_1.createTransport)({ SES: ses }).sendMail(mailOptions);
133
134
  }
134
135
  prepareEmailDestination(emailData) {
package/dist/src/sns.d.ts CHANGED
@@ -1,8 +1,13 @@
1
1
  import { PushNotificationsPlatforms } from 'idea-toolbox';
2
+ import { Logger } from './logger';
2
3
  /**
3
4
  * A wrapper for AWS Simple Notification Service.
4
5
  */
5
6
  export declare class SNS {
7
+ logger: Logger;
8
+ constructor(options?: {
9
+ debug: boolean;
10
+ });
6
11
  /**
7
12
  * Create a new endpoint in the SNS platform specified.
8
13
  * @return platform endpoint ARN
package/dist/src/sns.js CHANGED
@@ -4,11 +4,14 @@ exports.SNS = void 0;
4
4
  const aws_sdk_1 = require("aws-sdk");
5
5
  const idea_toolbox_1 = require("idea-toolbox");
6
6
  const logger_1 = require("./logger");
7
- const logger = new logger_1.Logger();
8
7
  /**
9
8
  * A wrapper for AWS Simple Notification Service.
10
9
  */
11
10
  class SNS {
11
+ constructor(options = { debug: true }) {
12
+ this.logger = new logger_1.Logger();
13
+ this.logger.level = options.debug ? 'DEBUG' : 'INFO';
14
+ }
12
15
  /**
13
16
  * Create a new endpoint in the SNS platform specified.
14
17
  * @return platform endpoint ARN
@@ -28,7 +31,7 @@ class SNS {
28
31
  default:
29
32
  throw new Error('Unsupported platform');
30
33
  }
31
- logger.debug('SNS ADD PLATFORM ENDPOINT');
34
+ this.logger.debug('SNS ADD PLATFORM ENDPOINT');
32
35
  const result = await new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region })
33
36
  .createPlatformEndpoint({ PlatformApplicationArn: platformARN, Token: token })
34
37
  .promise();
@@ -57,7 +60,7 @@ class SNS {
57
60
  default:
58
61
  throw new Error('Unsupported platform');
59
62
  }
60
- logger.debug('SNS PUBLISH IN TOPIC');
63
+ this.logger.debug('SNS PUBLISH IN TOPIC');
61
64
  return await new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region })
62
65
  .publish({ MessageStructure: 'json', Message: JSON.stringify(structuredMessage), TargetArn: snsParams.endpoint })
63
66
  .promise();
@@ -2,8 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StreamController = void 0;
4
4
  const genericController_1 = require("./genericController");
5
- const logger_1 = require("./logger");
6
- const logger = new logger_1.Logger();
7
5
  /**
8
6
  * An abstract class to inherit to manage AWS DDB streams in an AWS Lambda function.
9
7
  */
@@ -11,7 +9,7 @@ class StreamController extends genericController_1.GenericController {
11
9
  constructor(event, callback) {
12
10
  super(event, callback);
13
11
  this.records = event.Records ?? [];
14
- logger.info(`START STREAM: ${this.records.length ?? 0} records`);
12
+ this.logger.info(`START STREAM: ${this.records.length ?? 0} records`);
15
13
  }
16
14
  }
17
15
  exports.StreamController = StreamController;
@@ -23,7 +23,7 @@ export declare class Translate {
23
23
  /**
24
24
  * Initialize a new Translate helper object.
25
25
  */
26
- constructor(params?: {
26
+ constructor(options?: {
27
27
  region?: string;
28
28
  });
29
29
  /**
@@ -10,8 +10,8 @@ class Translate {
10
10
  /**
11
11
  * Initialize a new Translate helper object.
12
12
  */
13
- constructor(params = {}) {
14
- this.translate = new aws_sdk_1.Translate({ apiVersion: '2017-07-01', region: params.region });
13
+ constructor(options = {}) {
14
+ this.translate = new aws_sdk_1.Translate({ apiVersion: '2017-07-01', region: options.region });
15
15
  this.sourceLanguageCode = 'en';
16
16
  this.targetLanguageCode = 'en';
17
17
  this.terminologyNames = new Array();
package/docs.style.css ADDED
@@ -0,0 +1,4 @@
1
+ .tsd-hierarchy,
2
+ .tsd-index-summary {
3
+ display: none;
4
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "3.16.2",
3
+ "version": "3.16.4",
4
4
  "description": "AWS wrappers to use in IDEA's back-ends",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "lint": "eslint --ext .ts",
9
9
  "compile": "tsc --build",
10
- "docs": "typedoc index.ts",
10
+ "docs": "typedoc index.ts --customCss ./docs.style.css",
11
11
  "build": "./build.sh",
12
12
  "publishPackage": "./build.sh && npm publish"
13
13
  },
@@ -37,7 +37,7 @@
37
37
  "homepage": "https://iter-idea.github.io/IDEA-AWS",
38
38
  "dependencies": {
39
39
  "@aws-lambda-powertools/metrics": "^1.4.1",
40
- "idea-toolbox": "^6.6.3",
40
+ "idea-toolbox": "^6.6.8",
41
41
  "nanoid": "^3.3.4",
42
42
  "nodemailer": "^6.7.7",
43
43
  "shortid": "^2.2.16",