idea-aws 3.10.0 → 3.10.4
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.
package/dist/src/cognito.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ import { CognitoUser } from 'idea-toolbox';
|
|
|
5
5
|
*/
|
|
6
6
|
export declare class Cognito {
|
|
7
7
|
protected cognito: CognitoIdentityServiceProvider;
|
|
8
|
-
constructor(
|
|
8
|
+
constructor(params?: {
|
|
9
|
+
region?: string;
|
|
10
|
+
});
|
|
9
11
|
/**
|
|
10
12
|
* Change the region in which to find the user pool.
|
|
11
13
|
* Default: the runner's (e.g. Lambda function) region.
|
package/dist/src/cognito.js
CHANGED
|
@@ -7,8 +7,8 @@ const idea_toolbox_1 = require("idea-toolbox");
|
|
|
7
7
|
* A wrapper for AWS Cognito.
|
|
8
8
|
*/
|
|
9
9
|
class Cognito {
|
|
10
|
-
constructor() {
|
|
11
|
-
this.cognito = new aws_sdk_1.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
|
|
10
|
+
constructor(params = {}) {
|
|
11
|
+
this.cognito = new aws_sdk_1.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18', region: params.region });
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Change the region in which to find the user pool.
|
|
@@ -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
|
*/
|
|
@@ -106,8 +106,10 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
106
106
|
}
|
|
107
107
|
initFromEventV2(event, options) {
|
|
108
108
|
this.authorization = event.headers.authorization;
|
|
109
|
-
const
|
|
110
|
-
|
|
109
|
+
const authorizer = event.requestContext?.authorizer ?? {};
|
|
110
|
+
const contextFromAuthorizer = authorizer.lambda ?? authorizer.jwt?.claims ?? {};
|
|
111
|
+
this.principalId = contextFromAuthorizer.principalId ?? contextFromAuthorizer.sub ?? null;
|
|
112
|
+
this.cognitoUser = authorizer.jwt?.claims ? new idea_toolbox_1.CognitoUser(authorizer.jwt?.claims) : null;
|
|
111
113
|
this.stage = event.requestContext.stage;
|
|
112
114
|
this.httpMethod = event.requestContext.http.method;
|
|
113
115
|
this.resource = event.routeKey.replace('+', ''); // {proxy+} -> {proxy}
|
|
@@ -283,49 +285,56 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
283
285
|
* @return the body of the response
|
|
284
286
|
* @deprecated don't run a Lambda from another Lambda (bad practice)
|
|
285
287
|
*/
|
|
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
|
-
});
|
|
288
|
+
async invokeInternalAPIRequest(params) {
|
|
289
|
+
if (params.lambda)
|
|
290
|
+
return await this.invokeInternalAPIRequestWithLambda(params);
|
|
291
|
+
if (params.eventBridge)
|
|
292
|
+
return await this.invokeInternalAPIRequestWithEventBridge(params);
|
|
293
|
+
throw new Error('Either "lambda" or "eventBus" parameters must be set.');
|
|
294
|
+
}
|
|
295
|
+
async invokeInternalAPIRequestWithLambda(params) {
|
|
296
|
+
const lambdaInvokeParams = {
|
|
297
|
+
FunctionName: params.lambda,
|
|
298
|
+
InvocationType: 'RequestResponse',
|
|
299
|
+
Payload: this.mapEventForInternalApiRequest(params),
|
|
300
|
+
Qualifier: params.stage || this.stage
|
|
301
|
+
};
|
|
302
|
+
const res = await new aws_sdk_1.Lambda().invoke(lambdaInvokeParams).promise();
|
|
303
|
+
const payload = JSON.parse(res.Payload);
|
|
304
|
+
const body = JSON.parse(payload.body);
|
|
305
|
+
if (Number(payload.statusCode) !== 200)
|
|
306
|
+
throw new Error(body.message);
|
|
307
|
+
return body;
|
|
308
|
+
}
|
|
309
|
+
async invokeInternalAPIRequestWithEventBridge(params) {
|
|
310
|
+
const request = {
|
|
311
|
+
EventBusName: params.eventBridge.bus,
|
|
312
|
+
Source: this.constructor.name,
|
|
313
|
+
DetailType: params.eventBridge.target,
|
|
314
|
+
Detail: this.mapEventForInternalApiRequest(params)
|
|
315
|
+
};
|
|
316
|
+
return await new aws_sdk_1.EventBridge().putEvents({ Entries: [request] }).promise();
|
|
317
|
+
}
|
|
318
|
+
mapEventForInternalApiRequest(params) {
|
|
319
|
+
const event = JSON.parse(JSON.stringify(this.event));
|
|
320
|
+
// change only the event attributes we need; e.g. the authorization is unchanged
|
|
321
|
+
if (!event.requestContext)
|
|
322
|
+
event.requestContext = {};
|
|
323
|
+
event.requestContext.stage = params.stage || this.stage;
|
|
324
|
+
if (!event.requestContext.http)
|
|
325
|
+
event.requestContext.http;
|
|
326
|
+
event.requestContext.http.method = event.httpMethod = params.httpMethod;
|
|
327
|
+
event.routeKey = event.resource = params.resource;
|
|
328
|
+
event.pathParameters = params.pathParams || {};
|
|
329
|
+
event.queryStringParameters = params.queryParams || {};
|
|
330
|
+
event.body = JSON.stringify(params.body || {});
|
|
331
|
+
event.rawPath = event.path = params.resource;
|
|
332
|
+
for (const p in event.pathParameters)
|
|
333
|
+
if (event.pathParameters[p])
|
|
334
|
+
event.rawPath = event.path = event.path.replace(`{${p}}`, event.pathParameters[p]);
|
|
335
|
+
// set a flag to make the invoked to recognise that is an internal request
|
|
336
|
+
event.internalAPIRequest = true;
|
|
337
|
+
return JSON.stringify(event);
|
|
329
338
|
}
|
|
330
339
|
/**
|
|
331
340
|
* Whether the current request comes from an internal API request, i.e. it was invoked by another controller.
|