idea-aws 3.10.0 → 3.10.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.
@@ -106,6 +106,9 @@ export declare abstract class ResourceController extends GenericController {
106
106
  * @deprecated don't run a Lambda from another Lambda (bad practice)
107
107
  */
108
108
  invokeInternalAPIRequest(params: InternalAPIRequestParams): Promise<any>;
109
+ private invokeInternalAPIRequestWithLambda;
110
+ private invokeInternalAPIRequestWithEventBridge;
111
+ private mapEventForInternalApiRequest;
109
112
  /**
110
113
  * Whether the current request comes from an internal API request, i.e. it was invoked by another controller.
111
114
  * @deprecated don't run a Lambda from another Lambda (bad practice)
@@ -150,13 +153,26 @@ export interface ResourceControllerOptions extends GenericControllerOptions {
150
153
  }
151
154
  /**
152
155
  * The parameters needed to invoke an internal API request.
153
- * @deprecated don't run a Lambda from another Lambda (bad practice)
156
+ * @deprecated don't run a Lambda from another Lambda (bad practice).
154
157
  */
155
158
  export interface InternalAPIRequestParams {
156
159
  /**
157
- * The name of the lambda function receiving the request; e.g. `project_memberships`.
160
+ * The name of the Lambda function receiving the request; e.g. `project_memberships`.
161
+ * Note: the invocation is always syncronous.
162
+ * Either this attribute or `eventBus` must be set.
158
163
  */
159
- lambda: string;
164
+ lambda?: string;
165
+ /**
166
+ * The EventBridge destination of the request.
167
+ * If the bus name or ARN isn't specified, the default one is used.
168
+ * The `target` maps into the `DetailType` of the event.
169
+ * Note: the invocation is always asyncronous.
170
+ * Either this attribute or `lambda` must be set.
171
+ */
172
+ eventBridge?: {
173
+ bus?: string;
174
+ target?: string;
175
+ };
160
176
  /**
161
177
  * The alias of the lambda function to invoke. Default: the value of the current API stage.
162
178
  */
@@ -283,49 +283,54 @@ class ResourceController extends genericController_1.GenericController {
283
283
  * @return the body of the response
284
284
  * @deprecated don't run a Lambda from another Lambda (bad practice)
285
285
  */
286
- invokeInternalAPIRequest(params) {
287
- return new Promise((resolve, reject) => {
288
- // create a copy of the event
289
- const event = JSON.parse(JSON.stringify(this.event));
290
- // change only the event attributes we need; e.g. the authorization is unchanged
291
- if (!event.requestContext)
292
- event.requestContext = {};
293
- event.requestContext.stage = params.stage || this.stage;
294
- event.requestContext.httpMethod = event.httpMethod = params.httpMethod;
295
- event.routeKey = event.resource = params.resource;
296
- event.pathParameters = params.pathParams || {};
297
- event.queryStringParameters = params.queryParams || {};
298
- event.body = JSON.stringify(params.body || {});
299
- // parse the path
300
- event.rawPath = event.path = params.resource;
301
- for (const p in event.pathParameters)
302
- if (event.pathParameters[p])
303
- event.rawPath = event.path = event.path.replace(`{${p}}`, event.pathParameters[p]);
304
- // set a flag to make the invoked to recognise that is an internal request
305
- event.internalAPIRequest = true;
306
- // invoke the lambda with the event prepaired, simulating an API request
307
- new aws_sdk_1.Lambda().invoke({
308
- FunctionName: params.lambda,
309
- Qualifier: event.requestContext.stage,
310
- InvocationType: 'RequestResponse',
311
- Payload: JSON.stringify(event)
312
- }, (err, res) => {
313
- // reject in case of internal error
314
- if (err)
315
- reject(err);
316
- else {
317
- // parse the payload and the body
318
- const payload = JSON.parse(res.Payload);
319
- const body = JSON.parse(payload.body);
320
- // if the response is successfull, return the body
321
- if (Number(payload.statusCode) === 200)
322
- resolve(body);
323
- // otherwise, reject the controlled error
324
- else
325
- reject(new Error(body.message));
326
- }
327
- });
328
- });
286
+ async invokeInternalAPIRequest(params) {
287
+ if (params.lambda)
288
+ return await this.invokeInternalAPIRequestWithLambda(params);
289
+ if (params.eventBridge)
290
+ return await this.invokeInternalAPIRequestWithEventBridge(params);
291
+ throw new Error('Either "lambda" or "eventBus" parameters must be set.');
292
+ }
293
+ async invokeInternalAPIRequestWithLambda(params) {
294
+ const lambdaInvokeParams = {
295
+ FunctionName: params.lambda,
296
+ InvocationType: 'RequestResponse',
297
+ Payload: this.mapEventForInternalApiRequest(params),
298
+ Qualifier: params.stage || this.stage
299
+ };
300
+ const res = await new aws_sdk_1.Lambda().invoke(lambdaInvokeParams).promise();
301
+ const payload = JSON.parse(res.Payload);
302
+ const body = JSON.parse(payload.body);
303
+ if (Number(payload.statusCode) !== 200)
304
+ throw new Error(body.message);
305
+ return body;
306
+ }
307
+ async invokeInternalAPIRequestWithEventBridge(params) {
308
+ const request = {
309
+ EventBusName: params.eventBridge.bus,
310
+ Source: this.constructor.name,
311
+ DetailType: params.eventBridge.target,
312
+ Detail: this.mapEventForInternalApiRequest(params)
313
+ };
314
+ return await new aws_sdk_1.EventBridge().putEvents({ Entries: [request] }).promise();
315
+ }
316
+ mapEventForInternalApiRequest(params) {
317
+ const event = JSON.parse(JSON.stringify(this.event));
318
+ // change only the event attributes we need; e.g. the authorization is unchanged
319
+ if (!event.requestContext)
320
+ event.requestContext = {};
321
+ event.requestContext.stage = params.stage || this.stage;
322
+ event.requestContext.httpMethod = event.httpMethod = params.httpMethod;
323
+ event.routeKey = event.resource = params.resource;
324
+ event.pathParameters = params.pathParams || {};
325
+ event.queryStringParameters = params.queryParams || {};
326
+ event.body = JSON.stringify(params.body || {});
327
+ event.rawPath = event.path = params.resource;
328
+ for (const p in event.pathParameters)
329
+ if (event.pathParameters[p])
330
+ event.rawPath = event.path = event.path.replace(`{${p}}`, event.pathParameters[p]);
331
+ // set a flag to make the invoked to recognise that is an internal request
332
+ event.internalAPIRequest = true;
333
+ return JSON.stringify(event);
329
334
  }
330
335
  /**
331
336
  * Whether the current request comes from an internal API request, i.e. it was invoked by another controller.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "3.10.0",
3
+ "version": "3.10.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",