idea-aws 3.15.2 → 3.16.1

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,12 +1,3 @@
1
- import { DynamoDB } from './dynamoDB';
2
- import { Cognito } from './cognito';
3
- import { S3 } from './s3';
4
- import { SES } from './ses';
5
- import { SNS } from './sns';
6
- import { Translate } from './translate';
7
- import { Comprehend } from './comprehend';
8
- import { SecretsManager } from './secretsManager';
9
- import { Attachments } from './attachments';
10
1
  import { Logger } from './logger';
11
2
  /**
12
3
  * An abstract class to inherit to manage some resources with an AWS Lambda function.
@@ -14,24 +5,13 @@ import { Logger } from './logger';
14
5
  export declare abstract class GenericController {
15
6
  protected event: any;
16
7
  protected callback: any;
17
- protected _dynamoDB: DynamoDB;
18
- protected _cognito: Cognito;
19
- protected _s3: S3;
20
- protected _ses: SES;
21
- protected _sns: SNS;
22
- protected _translate: Translate;
23
- protected _comprehend: Comprehend;
24
- protected _secrets: SecretsManager;
25
- protected _attachments: Attachments;
26
- tables: any;
27
8
  protected logger: Logger;
28
- protected logRequestsWithKey: string;
29
9
  /**
30
10
  * Initialize a new GenericController helper object.
31
11
  * @param event the event that invoked the AWS lambda function
32
12
  * @param callback the callback to resolve or reject the execution
33
13
  */
34
- constructor(event: any, callback: any, options?: GenericControllerOptions);
14
+ constructor(event: any, callback: any);
35
15
  /**
36
16
  * The main function, that handle the request and should terminate with an invokation of the method `done`.
37
17
  */
@@ -40,63 +20,4 @@ export declare abstract class GenericController {
40
20
  * Default callback for the Lambda.
41
21
  */
42
22
  protected done(err: any, res?: any): void;
43
- /**
44
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
45
- */
46
- protected get dynamoDB(): DynamoDB;
47
- protected set dynamoDB(dynamoDB: DynamoDB);
48
- /**
49
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
50
- */
51
- protected get cognito(): Cognito;
52
- protected set cognito(cognito: Cognito);
53
- /**
54
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
55
- */
56
- protected get s3(): S3;
57
- protected set s3(s3: S3);
58
- /**
59
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
60
- */
61
- protected get ses(): SES;
62
- protected set ses(ses: SES);
63
- /**
64
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
65
- */
66
- protected get sns(): SNS;
67
- protected set sns(sns: SNS);
68
- /**
69
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
70
- */
71
- protected get translate(): Translate;
72
- protected set translate(translate: Translate);
73
- /**
74
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
75
- */
76
- protected get comprehend(): Comprehend;
77
- protected set comprehend(comprehend: Comprehend);
78
- /**
79
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
80
- */
81
- protected get secrets(): SecretsManager;
82
- protected set secrets(secrets: SecretsManager);
83
- /**
84
- * Manage attachments (through SignedURLs).
85
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
86
- */
87
- get attachments(): Attachments;
88
- set attachments(attachments: Attachments);
89
- }
90
- /**
91
- * The initial options for a constructor of class GenericController.
92
- * @deprecated following new IAC standards.
93
- */
94
- export interface GenericControllerOptions {
95
- /**
96
- * The AWS DDB tables involved, as a map of shortcut and names.
97
- * e.g. `{ users: 'project_users' }`.
98
- */
99
- tables?: {
100
- [table: string]: string;
101
- };
102
23
  }
@@ -1,15 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GenericController = void 0;
4
- const dynamoDB_1 = require("./dynamoDB");
5
- const cognito_1 = require("./cognito");
6
- const s3_1 = require("./s3");
7
- const ses_1 = require("./ses");
8
- const sns_1 = require("./sns");
9
- const translate_1 = require("./translate");
10
- const comprehend_1 = require("./comprehend");
11
- const secretsManager_1 = require("./secretsManager");
12
- const attachments_1 = require("./attachments");
13
4
  const logger_1 = require("./logger");
14
5
  /**
15
6
  * An abstract class to inherit to manage some resources with an AWS Lambda function.
@@ -20,11 +11,10 @@ class GenericController {
20
11
  * @param event the event that invoked the AWS lambda function
21
12
  * @param callback the callback to resolve or reject the execution
22
13
  */
23
- constructor(event, callback, options = {}) {
14
+ constructor(event, callback) {
24
15
  this.logger = new logger_1.Logger();
25
16
  this.event = event;
26
17
  this.callback = callback;
27
- this.tables = options.tables || {};
28
18
  }
29
19
  /**
30
20
  * Default callback for the Lambda.
@@ -36,111 +26,5 @@ class GenericController {
36
26
  this.logger.info('END-SUCCESS');
37
27
  this.callback(err, res);
38
28
  }
39
- ///
40
- /// AWS SERVICES
41
- ///
42
- /**
43
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
44
- */
45
- get dynamoDB() {
46
- if (!this._dynamoDB)
47
- this._dynamoDB = new dynamoDB_1.DynamoDB();
48
- return this._dynamoDB;
49
- }
50
- set dynamoDB(dynamoDB) {
51
- this._dynamoDB = dynamoDB;
52
- }
53
- /**
54
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
55
- */
56
- get cognito() {
57
- if (!this._cognito)
58
- this._cognito = new cognito_1.Cognito();
59
- return this._cognito;
60
- }
61
- set cognito(cognito) {
62
- this._cognito = cognito;
63
- }
64
- /**
65
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
66
- */
67
- get s3() {
68
- if (!this._s3)
69
- this._s3 = new s3_1.S3();
70
- return this._s3;
71
- }
72
- set s3(s3) {
73
- this._s3 = s3;
74
- }
75
- /**
76
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
77
- */
78
- get ses() {
79
- if (!this._ses)
80
- this._ses = new ses_1.SES();
81
- return this._ses;
82
- }
83
- set ses(ses) {
84
- this._ses = ses;
85
- }
86
- /**
87
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
88
- */
89
- get sns() {
90
- if (!this._sns)
91
- this._sns = new sns_1.SNS();
92
- return this._sns;
93
- }
94
- set sns(sns) {
95
- this._sns = sns;
96
- }
97
- /**
98
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
99
- */
100
- get translate() {
101
- if (!this._translate)
102
- this._translate = new translate_1.Translate();
103
- return this._translate;
104
- }
105
- set translate(translate) {
106
- this._translate = translate;
107
- }
108
- /**
109
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
110
- */
111
- get comprehend() {
112
- if (!this._comprehend)
113
- this._comprehend = new comprehend_1.Comprehend();
114
- return this._comprehend;
115
- }
116
- set comprehend(comprehend) {
117
- this._comprehend = comprehend;
118
- }
119
- /**
120
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
121
- */
122
- get secrets() {
123
- if (!this._secrets)
124
- this._secrets = new secretsManager_1.SecretsManager();
125
- return this._secrets;
126
- }
127
- set secrets(secrets) {
128
- this._secrets = secrets;
129
- }
130
- ///
131
- /// HELPERS
132
- ///
133
- /**
134
- * Manage attachments (through SignedURLs).
135
- * @deprecated use external object to improve cold starts. See most recent projects for reference.
136
- */
137
- get attachments() {
138
- if (!this._attachments)
139
- this._attachments = new attachments_1.Attachments();
140
- return this._attachments;
141
- }
142
- set attachments(attachments) {
143
- this._attachments = attachments;
144
- }
145
29
  }
146
30
  exports.GenericController = GenericController;
@@ -3,7 +3,8 @@ import { APIGatewayProxyEventV2, APIGatewayProxyEvent, Callback } from 'aws-lamb
3
3
  import { CognitoUser, Auth0User } from 'idea-toolbox';
4
4
  import { Logger } from './logger';
5
5
  import { CloudWatchMetrics } from './metrics';
6
- import { GenericController, GenericControllerOptions } from './genericController';
6
+ import { GenericController } from './genericController';
7
+ import { DynamoDB } from './dynamoDB';
7
8
  /**
8
9
  * An abstract class to inherit to manage API requests (AWS API Gateway) in an AWS Lambda function.
9
10
  */
@@ -31,6 +32,7 @@ export declare abstract class ResourceController extends GenericController {
31
32
  protected returnStatusCode?: number;
32
33
  protected logger: Logger;
33
34
  protected logRequestsWithKey: string;
35
+ protected ddbToLogRequests: DynamoDB;
34
36
  protected metrics: CloudWatchMetrics;
35
37
  protected currentLang: string;
36
38
  protected defaultLang: string;
@@ -101,7 +103,7 @@ export declare abstract class ResourceController extends GenericController {
101
103
  /**
102
104
  * Store the log associated to the request (no response/error handling).
103
105
  */
104
- protected storeLog(succeeded: boolean): void;
106
+ protected storeLog(succeeded: boolean): Promise<void>;
105
107
  /**
106
108
  * Check whether shared resource exists in the back-end (translation, template, etc.).
107
109
  * Search for the specified file path in both the Lambda function's main folder and the layers folder.
@@ -161,7 +163,7 @@ export declare abstract class ResourceController extends GenericController {
161
163
  /**
162
164
  * The initial options for a constructor of class ResourceController.
163
165
  */
164
- export interface ResourceControllerOptions extends GenericControllerOptions {
166
+ export interface ResourceControllerOptions {
165
167
  /**
166
168
  * The resourceId of the API request, to specify if different from "proxy".
167
169
  */
@@ -170,6 +172,10 @@ export interface ResourceControllerOptions extends GenericControllerOptions {
170
172
  * If set, the logs of the API requests on this resource will be stored (using this key).
171
173
  */
172
174
  logRequestsWithKey?: string;
175
+ /**
176
+ * Whether to automatically store usage metrics on CloudWatch.
177
+ */
178
+ useMetrics?: boolean;
173
179
  }
174
180
  /**
175
181
  * The parameters needed to invoke an internal API request.
@@ -8,12 +8,13 @@ const idea_toolbox_1 = require("idea-toolbox");
8
8
  const logger_1 = require("./logger");
9
9
  const metrics_1 = require("./metrics");
10
10
  const genericController_1 = require("./genericController");
11
+ const dynamoDB_1 = require("./dynamoDB");
11
12
  /**
12
13
  * An abstract class to inherit to manage API requests (AWS API Gateway) in an AWS Lambda function.
13
14
  */
14
15
  class ResourceController extends genericController_1.GenericController {
15
16
  constructor(event, callback, options = {}) {
16
- super(event, callback, options);
17
+ super(event, callback);
17
18
  this.initError = false;
18
19
  this.project = process?.env?.PROJECT;
19
20
  this.stage = process?.env?.STAGE;
@@ -21,7 +22,6 @@ class ResourceController extends genericController_1.GenericController {
21
22
  this.clientVersion = '?';
22
23
  this.clientPlatform = '?';
23
24
  this.logger = new logger_1.Logger();
24
- this.metrics = new metrics_1.CloudWatchMetrics({ project: this.project });
25
25
  this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
26
26
  ///
27
27
  /// REQUEST HANDLERS
@@ -101,6 +101,8 @@ class ResourceController extends genericController_1.GenericController {
101
101
  else
102
102
  this.initFromEventV1(event, options);
103
103
  this.logRequestsWithKey = options.logRequestsWithKey;
104
+ if (this.logRequestsWithKey)
105
+ this.ddbToLogRequests = new dynamoDB_1.DynamoDB();
104
106
  // acquire some info about the client, if available
105
107
  if (this.queryParams['_v']) {
106
108
  this.clientVersion = this.queryParams['_v'];
@@ -110,7 +112,8 @@ class ResourceController extends genericController_1.GenericController {
110
112
  this.clientPlatform = this.queryParams['_p'];
111
113
  delete this.queryParams['_p'];
112
114
  }
113
- this.prepareMetrics();
115
+ if (options.useMetrics)
116
+ this.prepareMetrics();
114
117
  // print the initial log
115
118
  const info = {
116
119
  principalId: this.principalId,
@@ -195,7 +198,8 @@ class ResourceController extends genericController_1.GenericController {
195
198
  this.logger.info('END-SUCCESS', { statusCode });
196
199
  if (this.logRequestsWithKey)
197
200
  this.storeLog(!err);
198
- this.publishMetrics(statusCode, err);
201
+ if (this.metrics)
202
+ this.publishMetrics(statusCode, err);
199
203
  this.callback(null, {
200
204
  statusCode: String(statusCode),
201
205
  body: err ? JSON.stringify({ message: err.message }) : JSON.stringify(res || {}),
@@ -286,7 +290,7 @@ class ResourceController extends genericController_1.GenericController {
286
290
  /**
287
291
  * Store the log associated to the request (no response/error handling).
288
292
  */
289
- storeLog(succeeded) {
293
+ async storeLog(succeeded) {
290
294
  const log = new idea_toolbox_1.APIRequestLog({
291
295
  logId: this.logRequestsWithKey,
292
296
  userId: this.principalId,
@@ -299,9 +303,12 @@ class ResourceController extends genericController_1.GenericController {
299
303
  // optionally add a track of the action
300
304
  if (this.httpMethod === 'PATCH' && this.body && this.body.action)
301
305
  log.action = this.body.action;
302
- this.dynamoDB.put({ TableName: 'idea_logs', Item: log }).catch(() => {
303
- /* ignore */
304
- });
306
+ try {
307
+ await this.ddbToLogRequests.put({ TableName: 'idea_logs', Item: log });
308
+ }
309
+ catch (error) {
310
+ // ignore
311
+ }
305
312
  }
306
313
  /**
307
314
  * Check whether shared resource exists in the back-end (translation, template, etc.).
@@ -326,6 +333,7 @@ class ResourceController extends genericController_1.GenericController {
326
333
  * Prepare the CloudWatch metrics at the beginning of a request.
327
334
  */
328
335
  prepareMetrics() {
336
+ this.metrics = new metrics_1.CloudWatchMetrics({ project: this.project });
329
337
  this.metrics.addDimension('stage', this.stage);
330
338
  this.metrics.addDimension('resource', this.resource);
331
339
  this.metrics.addDimension('method', this.httpMethod);
@@ -340,6 +348,8 @@ class ResourceController extends genericController_1.GenericController {
340
348
  * Publish the CloudWatch metrics (default and custom-defined) at the end of a reqeust.
341
349
  */
342
350
  publishMetrics(statusCode, error) {
351
+ if (!this.metrics)
352
+ return;
343
353
  this.metrics.addMetric('request');
344
354
  this.metrics.addMetric('statusCode', statusCode);
345
355
  if (error) {
package/dist/src/ses.d.ts CHANGED
@@ -130,7 +130,7 @@ export interface TemplatedEmailData extends BasicEmailData {
130
130
  /**
131
131
  * The name of the configuration set to use for the sending.
132
132
  */
133
- configurationSet: string;
133
+ configurationSet?: string;
134
134
  }
135
135
  /**
136
136
  * SES configuration.
package/dist/src/ses.js CHANGED
@@ -53,7 +53,7 @@ class SES {
53
53
  const request = {
54
54
  Destination: this.prepareEmailDestination(emailData),
55
55
  Template: emailData.template,
56
- TemplateData: JSON.stringify(emailData.templateData || {}),
56
+ TemplateData: JSON.stringify(emailData.templateData ?? {}),
57
57
  ConfigurationSetName: emailData.configurationSet,
58
58
  ReplyToAddresses: emailData.replyToAddresses,
59
59
  Source: sesParams.sourceName ? `${sesParams.sourceName} <${sesParams.source}>` : sesParams.source,
@@ -1,8 +1,8 @@
1
- import { GenericController, GenericControllerOptions } from './genericController';
1
+ import { GenericController } from './genericController';
2
2
  /**
3
3
  * An abstract class to inherit to manage AWS DDB streams in an AWS Lambda function.
4
4
  */
5
5
  export declare abstract class StreamController extends GenericController {
6
6
  records: any[];
7
- constructor(event: any, callback: any, options?: GenericControllerOptions);
7
+ constructor(event: any, callback: any);
8
8
  }
@@ -8,10 +8,10 @@ const logger = new logger_1.Logger();
8
8
  * An abstract class to inherit to manage AWS DDB streams in an AWS Lambda function.
9
9
  */
10
10
  class StreamController extends genericController_1.GenericController {
11
- constructor(event, callback, options = {}) {
12
- super(event, callback, options);
13
- this.records = event.Records || [];
14
- logger.info(`START STREAM: ${this.records.length || 0} records`);
11
+ constructor(event, callback) {
12
+ super(event, callback);
13
+ this.records = event.Records ?? [];
14
+ logger.info(`START STREAM: ${this.records.length ?? 0} records`);
15
15
  }
16
16
  }
17
17
  exports.StreamController = StreamController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "3.15.2",
3
+ "version": "3.16.1",
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",