All files event.utilities.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x 1x   1x 2x 2x       2x 2x 2x 2x 2x   2x                             2x                      
import { get } from 'lodash';
import { log } from './log.utilities';
 
export const processEvent = (event: any) => {
  const { body, pathParameters, queryStringParameters, requestContext } = event;
  const { httpMethod, resourceId, resourcePath, requestId } = requestContext;
  // The following works for offline mode as well as real
  // lambda-proxy with cognito user pool authorization
  // if the 'cognito:username' is set in a JWT-encoded Authorization token
  const userId = get(requestContext, 'authorizer.claims.cognito:username');
  const email = get(requestContext, 'authorizer.claims.email');
  const clientId = get(requestContext, 'authorizer.claims.client_id');
  const isConfirmed = get(requestContext, 'authorizer.claims.scopeIsConfirmed');
  const scopeRoles = get(requestContext, 'authorizer.claims.scopeRoles');
 
  log.debug(
    {
      resourceId,
      resourcePath,
      requestId,
      httpMethod,
      userId,
      email,
      clientId,
      isConfirmed,
      scopeRoles,
    },
    'Request received',
  );
 
  return {
    body: typeof body === 'string' ? JSON.parse(body) : body,
    queryStringParameters,
    pathParameters,
    userId,
    email,
    clientId,
    isConfirmed,
    scopeRoles,
  };
};