idea-aws 3.15.0 → 3.15.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.
@@ -7,7 +7,7 @@ const metrics_1 = require("@aws-lambda-powertools/metrics");
7
7
  */
8
8
  class CloudWatchMetrics {
9
9
  constructor(options) {
10
- const project = options?.project ?? process.env.PROJECT ?? 'unknownProject';
10
+ const project = options?.project ?? 'unknownProject';
11
11
  this.metrics = new metrics_1.Metrics({ namespace: project });
12
12
  }
13
13
  /**
@@ -16,14 +16,18 @@ export declare abstract class ResourceController extends GenericController {
16
16
  protected principalId: string;
17
17
  protected cognitoUser: CognitoUser;
18
18
  protected auth0User: Auth0User;
19
+ protected project: string;
19
20
  protected stage: string;
20
21
  protected httpMethod: string;
21
22
  protected body: any;
22
23
  protected queryParams: any;
23
- protected resource: string;
24
+ protected resourcePath: string;
24
25
  protected path: string;
25
26
  protected pathParameters: any;
27
+ protected resource: string;
26
28
  protected resourceId: string;
29
+ protected clientVersion: string;
30
+ protected clientPlatform: string;
27
31
  protected returnStatusCode?: number;
28
32
  protected logger: Logger;
29
33
  protected logRequestsWithKey: string;
@@ -15,8 +15,13 @@ class ResourceController extends genericController_1.GenericController {
15
15
  constructor(event, callback, options = {}) {
16
16
  super(event, callback, options);
17
17
  this.initError = false;
18
+ this.project = process?.env?.PROJECT;
19
+ this.stage = process?.env?.STAGE;
20
+ this.resource = process?.env?.RESOURCE;
21
+ this.clientVersion = '?';
22
+ this.clientPlatform = '?';
18
23
  this.logger = new logger_1.Logger();
19
- this.metrics = new metrics_1.CloudWatchMetrics();
24
+ this.metrics = new metrics_1.CloudWatchMetrics({ project: this.project });
20
25
  this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
21
26
  ///
22
27
  /// REQUEST HANDLERS
@@ -97,18 +102,23 @@ class ResourceController extends genericController_1.GenericController {
97
102
  this.initFromEventV1(event, options);
98
103
  this.logRequestsWithKey = options.logRequestsWithKey;
99
104
  // acquire some info about the client, if available
100
- let version = '?', platform = '?';
101
105
  if (this.queryParams['_v']) {
102
- version = this.queryParams['_v'];
106
+ this.clientVersion = this.queryParams['_v'];
103
107
  delete this.queryParams['_v'];
104
108
  }
105
109
  if (this.queryParams['_p']) {
106
- platform = this.queryParams['_p'];
110
+ this.clientPlatform = this.queryParams['_p'];
107
111
  delete this.queryParams['_p'];
108
112
  }
109
113
  this.prepareMetrics();
110
114
  // print the initial log
111
- const info = { principalId: this.principalId, queryParams: this.queryParams, body: this.body, version, platform };
115
+ const info = {
116
+ principalId: this.principalId,
117
+ queryParams: this.queryParams,
118
+ body: this.body,
119
+ version: this.clientVersion,
120
+ platform: this.clientPlatform
121
+ };
112
122
  this.logger.info(`START: ${this.httpMethod} ${this.path}`, info);
113
123
  }
114
124
  catch (err) {
@@ -123,9 +133,9 @@ class ResourceController extends genericController_1.GenericController {
123
133
  this.principalId = contextFromAuthorizer.principalId ?? contextFromAuthorizer.sub ?? null;
124
134
  this.cognitoUser = authorizer.jwt?.claims ? new idea_toolbox_1.CognitoUser(authorizer.jwt?.claims) : null;
125
135
  this.auth0User = contextFromAuthorizer.auth0User ? new idea_toolbox_1.Auth0User(contextFromAuthorizer.auth0User) : null;
126
- this.stage = event.requestContext.stage;
136
+ this.stage = this.stage ?? event.requestContext.stage;
127
137
  this.httpMethod = event.requestContext.http.method;
128
- this.resource = event.routeKey.replace('+', ''); // {proxy+} -> {proxy}
138
+ this.resourcePath = event.routeKey.replace('+', ''); // {proxy+} -> {proxy}
129
139
  this.path = event.rawPath;
130
140
  this.pathParameters = {};
131
141
  for (const param in event.pathParameters)
@@ -145,9 +155,9 @@ class ResourceController extends genericController_1.GenericController {
145
155
  this.principalId = this.claims.sub;
146
156
  this.cognitoUser = this.principalId ? new idea_toolbox_1.CognitoUser(this.claims) : null;
147
157
  this.auth0User = null;
148
- this.stage = event.requestContext.stage;
158
+ this.stage = this.stage ?? event.requestContext.stage;
149
159
  this.httpMethod = event.httpMethod;
150
- this.resource = event.resource.replace('+', ''); // {proxy+} -> {proxy}
160
+ this.resourcePath = event.resource.replace('+', ''); // {proxy+} -> {proxy}
151
161
  this.path = event.path;
152
162
  this.pathParameters = {};
153
163
  for (const param in event.pathParameters)
@@ -280,7 +290,7 @@ class ResourceController extends genericController_1.GenericController {
280
290
  const log = new idea_toolbox_1.APIRequestLog({
281
291
  logId: this.logRequestsWithKey,
282
292
  userId: this.principalId,
283
- resource: this.resource,
293
+ resource: this.resourcePath,
284
294
  path: this.path,
285
295
  resourceId: this.resourceId,
286
296
  method: this.httpMethod,
@@ -316,12 +326,14 @@ class ResourceController extends genericController_1.GenericController {
316
326
  * Prepare the CloudWatch metrics at the beginning of a request.
317
327
  */
318
328
  prepareMetrics() {
319
- this.metrics.addDimension('stage', process.env.STAGE);
320
- this.metrics.addDimension('resource', process.env.RESOURCE);
329
+ this.metrics.addDimension('stage', this.stage);
330
+ this.metrics.addDimension('resource', this.resource);
321
331
  this.metrics.addDimension('method', this.httpMethod);
322
332
  this.metrics.addDimension('target', this.resourceId ? 'id' : 'list');
323
333
  this.metrics.addDimension('action', this.body?.action);
324
334
  this.metrics.addDimension('userId', this.principalId);
335
+ this.metrics.addDimension('clientVersion', this.clientVersion);
336
+ this.metrics.addDimension('clientPlatform', this.clientPlatform);
325
337
  this.metrics.addMetadata('resourceId', this.resourceId);
326
338
  }
327
339
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "3.15.0",
3
+ "version": "3.15.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",