idea-aws 4.4.4 → 4.4.5

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,31 @@
1
+ import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
2
+ /**
3
+ * A wrapper for simple uses of CloudWatch Metrics.
4
+ */
5
+ export declare class CloudWatchMetrics {
6
+ private metrics;
7
+ constructor(options?: {
8
+ project?: string;
9
+ });
10
+ /**
11
+ * Get the raw Metrics object. To use for custom purposes.
12
+ */
13
+ __raw(): Metrics;
14
+ /**
15
+ * Add an entry for the metrics.
16
+ */
17
+ addMetric(metricName: string, value?: number, unit?: MetricUnits): void;
18
+ /**
19
+ * Add a metadata useful when you want to search highly contextual information along with your metrics in your logs.
20
+ */
21
+ addMetadata(key: string, value: string): void;
22
+ /**
23
+ * Add an additional metrics dimension.
24
+ */
25
+ addDimension(name: string, value: string, defaultValue?: string): void;
26
+ /**
27
+ * Synchronous function to actually publish your metrics.
28
+ * It will create a new EMF blob and log it to be then ingested by Cloudwatch logs and processed for metrics creation.
29
+ */
30
+ publishStoredMetrics(): void;
31
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudWatchMetrics = void 0;
4
+ const metrics_1 = require("@aws-lambda-powertools/metrics");
5
+ /**
6
+ * A wrapper for simple uses of CloudWatch Metrics.
7
+ */
8
+ class CloudWatchMetrics {
9
+ constructor(options = {}) {
10
+ const namespace = options.project ?? 'unknownProject';
11
+ this.metrics = new metrics_1.Metrics({ namespace });
12
+ }
13
+ /**
14
+ * Get the raw Metrics object. To use for custom purposes.
15
+ */
16
+ __raw() {
17
+ return this.metrics;
18
+ }
19
+ /**
20
+ * Add an entry for the metrics.
21
+ */
22
+ addMetric(metricName, value = 1, unit = metrics_1.MetricUnits.Count) {
23
+ this.metrics.addMetric(metricName, unit, value);
24
+ }
25
+ /**
26
+ * Add a metadata useful when you want to search highly contextual information along with your metrics in your logs.
27
+ */
28
+ addMetadata(key, value) {
29
+ this.metrics.addMetadata(key, value);
30
+ }
31
+ /**
32
+ * Add an additional metrics dimension.
33
+ */
34
+ addDimension(name, value, defaultValue = '-') {
35
+ this.metrics.addDimension(name, value ?? defaultValue);
36
+ }
37
+ /**
38
+ * Synchronous function to actually publish your metrics.
39
+ * It will create a new EMF blob and log it to be then ingested by Cloudwatch logs and processed for metrics creation.
40
+ */
41
+ publishStoredMetrics() {
42
+ this.metrics.publishStoredMetrics();
43
+ }
44
+ }
45
+ exports.CloudWatchMetrics = CloudWatchMetrics;
@@ -0,0 +1,232 @@
1
+ import { Tracer } from '@aws-lambda-powertools/tracer';
2
+ import { APIGatewayProxyEventV2, APIGatewayProxyEvent, Callback } from 'aws-lambda';
3
+ import { CognitoUser, Auth0User } from 'idea-toolbox';
4
+ import { CloudWatchMetrics } from './metrics';
5
+ import { GenericController } from './genericController';
6
+ /**
7
+ * An abstract class to inherit to manage API requests (AWS API Gateway) in an AWS Lambda function.
8
+ */
9
+ export declare abstract class ResourceController extends GenericController {
10
+ protected event: APIGatewayProxyEventV2 | APIGatewayProxyEvent;
11
+ protected callback: Callback;
12
+ protected initError: boolean;
13
+ protected authorization: string;
14
+ protected claims: any;
15
+ protected principalId: string;
16
+ protected cognitoUser: CognitoUser;
17
+ protected auth0User: Auth0User;
18
+ protected project: string;
19
+ protected stage: string;
20
+ protected httpMethod: string;
21
+ protected body: any;
22
+ protected queryParams: any;
23
+ protected resourcePath: string;
24
+ protected path: string;
25
+ protected pathParameters: any;
26
+ protected resource: string;
27
+ protected resourceId: string;
28
+ protected clientVersion: string;
29
+ protected clientPlatform: string;
30
+ protected clientBundle: string;
31
+ protected returnStatusCode?: number;
32
+ protected logRequestsWithKey: string;
33
+ protected metrics: CloudWatchMetrics;
34
+ protected tracer: Tracer;
35
+ protected currentLang: string;
36
+ protected defaultLang: string;
37
+ protected translations: any;
38
+ protected templateMatcher: RegExp;
39
+ constructor(event: APIGatewayProxyEventV2 | APIGatewayProxyEvent, callback: Callback, options?: ResourceControllerOptions);
40
+ private initFromEventV2;
41
+ private initFromEventV1;
42
+ protected getEventSummary(): Record<string, any>;
43
+ /**
44
+ * Force the parsing of a query parameter as an array of strings.
45
+ */
46
+ protected getQueryParamAsArray(paramName: string): string[];
47
+ handleRequest: () => Promise<void>;
48
+ protected done(error?: Error | any, rawResult?: any, statusCode?: number): void;
49
+ /**
50
+ * To @override
51
+ */
52
+ protected checkAuthBeforeRequest(): Promise<void>;
53
+ /**
54
+ * To @override
55
+ */
56
+ protected getResource(): Promise<any>;
57
+ /**
58
+ * To @override
59
+ */
60
+ protected postResource(): Promise<any>;
61
+ /**
62
+ * To @override
63
+ */
64
+ protected putResource(): Promise<any>;
65
+ /**
66
+ * To @override
67
+ */
68
+ protected deleteResource(): Promise<any>;
69
+ /**
70
+ * To @override
71
+ */
72
+ protected headResource(): Promise<any>;
73
+ /**
74
+ * To @override
75
+ */
76
+ protected getResources(): Promise<any>;
77
+ /**
78
+ * To @override
79
+ */
80
+ protected postResources(): Promise<any>;
81
+ /**
82
+ * To @override
83
+ */
84
+ protected putResources(): Promise<any>;
85
+ /**
86
+ * To @override
87
+ */
88
+ protected patchResource(): Promise<any>;
89
+ /**
90
+ * To @override
91
+ */
92
+ protected patchResources(): Promise<any>;
93
+ /**
94
+ * To @override
95
+ */
96
+ protected deleteResources(): Promise<any>;
97
+ /**
98
+ * To @override
99
+ */
100
+ protected headResources(): Promise<any>;
101
+ /**
102
+ * Store the log associated to the request (no response/error handling).
103
+ */
104
+ protected storeLog(succeeded: boolean): Promise<void>;
105
+ /**
106
+ * Check whether shared resource exists in the back-end (translation, template, etc.).
107
+ * Search for the specified file path in both the Lambda function's main folder and the layers folder.
108
+ */
109
+ protected sharedResourceExists(filePath: string): boolean;
110
+ /**
111
+ * Load a shared resource in the back-end (translation, template, etc.).
112
+ * Search for the specified file path in both the Lambda function's main folder and the layers folder.
113
+ */
114
+ protected loadSharedResource(filePath: string): string;
115
+ /**
116
+ * Prepare the CloudWatch metrics at the beginning of a request.
117
+ */
118
+ protected prepareMetrics(): void;
119
+ /**
120
+ * Publish the CloudWatch metrics (default and custom-defined) at the end of a reqeust.
121
+ */
122
+ protected publishMetrics(statusCode: number, error?: any): void;
123
+ /**
124
+ * Simulate an internal API request, invoking directly the lambda and therefore saving resources.
125
+ * @return the body of the response
126
+ * @deprecated don't run a Lambda from another Lambda (bad practice)
127
+ */
128
+ invokeInternalAPIRequest(params: InternalAPIRequestParams): Promise<any>;
129
+ private invokeInternalAPIRequestWithLambda;
130
+ private invokeInternalAPIRequestWithEventBridge;
131
+ private mapEventForInternalApiRequest;
132
+ /**
133
+ * Whether the current request comes from an internal API request, i.e. it was invoked by another controller.
134
+ * @deprecated don't run a Lambda from another Lambda (bad practice)
135
+ */
136
+ comesFromInternalRequest(): boolean;
137
+ /**
138
+ * Load the translations from the shared resources and set them with a fallback language.
139
+ */
140
+ loadTranslations(lang: string, defLang?: string): void;
141
+ /**
142
+ * Get a translated term by key, optionally interpolating variables (e.g. `{{user}}`).
143
+ * If the term doesn't exist in the current language, it is searched in the default language.
144
+ */
145
+ t(key: string, interpolateParams?: any): string;
146
+ /**
147
+ * Interpolates a string to replace parameters.
148
+ * `"This is a {{ key }}"` ==> `"This is a value", with params = { key: "value" }`.
149
+ */
150
+ private interpolate;
151
+ /**
152
+ * Gets a value from an object by composed key.
153
+ * `getValue({ key1: { keyA: 'valueI' }}, 'key1.keyA')` ==> `'valueI'`.
154
+ */
155
+ private getValue;
156
+ /**
157
+ * Helper to quicly check if the value is defined.
158
+ */
159
+ private isDefined;
160
+ }
161
+ /**
162
+ * The initial options for a constructor of class ResourceController.
163
+ */
164
+ export interface ResourceControllerOptions {
165
+ /**
166
+ * The resourceId of the API request, to specify if different from "proxy".
167
+ */
168
+ resourceId?: string;
169
+ /**
170
+ * If set, the logs of the API requests on this resource will be stored (using this key).
171
+ */
172
+ logRequestsWithKey?: string;
173
+ /**
174
+ * Whether to automatically store usage metrics on CloudWatch.
175
+ */
176
+ useMetrics?: boolean;
177
+ /**
178
+ * The instance of the tracer to use in case of advanced monitoring.
179
+ */
180
+ tracer?: Tracer;
181
+ }
182
+ /**
183
+ * The parameters needed to invoke an internal API request.
184
+ * @deprecated don't run a Lambda from another Lambda (bad practice).
185
+ */
186
+ export interface InternalAPIRequestParams {
187
+ /**
188
+ * The name of the Lambda function receiving the request; e.g. `project_memberships`.
189
+ * Note: the invocation is always syncronous.
190
+ * Either this attribute or `eventBus` must be set.
191
+ */
192
+ lambda?: string;
193
+ /**
194
+ * The EventBridge destination of the request.
195
+ * If the bus name or ARN isn't specified, the default one is used.
196
+ * The `target` maps into the `DetailType` of the event.
197
+ * Note: the invocation is always asyncronous.
198
+ * Either this attribute or `lambda` must be set.
199
+ */
200
+ eventBridge?: {
201
+ bus?: string;
202
+ target?: string;
203
+ };
204
+ /**
205
+ * The alias of the lambda function to invoke. Default: the value of the current API stage.
206
+ */
207
+ stage?: string;
208
+ /**
209
+ * The http method to use.
210
+ */
211
+ httpMethod: string;
212
+ /**
213
+ * The path (in the internal API) to the resource we need; e.g. `teams/{teamId}/memberships/{userId}`.
214
+ */
215
+ resource: string;
216
+ /**
217
+ * The parameters to substitute in the path.
218
+ */
219
+ pathParams?: {
220
+ [index: string]: string | number;
221
+ };
222
+ /**
223
+ * The parameters to substitute in the path.
224
+ */
225
+ queryParams?: {
226
+ [index: string]: string | number;
227
+ };
228
+ /**
229
+ * The body of the request.
230
+ */
231
+ body?: any;
232
+ }