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
|
|
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
|
|
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
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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.
|