https://serverless-stack.com/ logo
#help
Title
# help
n

Noah D

02/16/2022, 12:02 PM
Hey does anyone know what type I should be using for my API gateway events if i am using cognito identity pool auth like seen in the guide? So far have been using
APIGatewayProxyEventV2
from
@types/aws-lambda
fine, however have been trying to get the identity pool id like seen in the guide using
event.requestContext.authorizer.iam.cognitoIdentity.identityId
however the type seems to not think this field exists (it definitely does). My version is
"@types/aws-lambda": "^8.10.92",
so might be related to that, but figure someone might have some insight into correct version to use if so 🙏
d

Derek Kershner

02/16/2022, 3:56 PM
There are now derivations of that event. I forget the exact names, but they are like
ApiGatewayProxyWithAuthorizerEventV2
.
n

Noah D

02/17/2022, 8:34 AM
Ok thanks guys, for any future travellers, my solution looked something like the following, not perfect and could be improved i imagine, but better than before 👌
Copy code
import { APIGatewayProxyEventV2WithRequestContext } from 'aws-lambda';

interface IAMContext {
    accessKey: string,
    accountId: string,
    callerId: string,
    cognitoIdentity: {
      amr: string[],
      identityId: string,
      identityPoolId: string
    },
    principalOrgId: string,
    userArn: string,
    userId: string
}

interface AuthorizerContext {
  accountId: string,
  apiId: string,
  authorizer: {
    iam: IAMContext
  }
}

type EndpointEvent = APIGatewayProxyEventV2WithRequestContext<AuthorizerContext>

export { EndpointEvent }
Where I use EndpointEvent as the type for my APIGateway endpoint events
d

Derek Kershner

02/17/2022, 3:01 PM
Looks perfect to me.
actually, you can probably use this: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/369ecc516615fb782639bd4d707be43a01c90df4/types/aws-lambda/trigger/api-gateway-proxy.d.ts#L144
interface AuthorizerContext extends APIGatewayEventRequestContextV2 {
n

Noah D

02/18/2022, 9:45 PM
I did try that one, though found the properties didn't match up with what was being sent through when testing, i.e. doesn't have properties to get
authorizer.iam.cognitoIdentity.identityId
d

Derek Kershner

02/18/2022, 11:08 PM
I meant to
extend
it, but not the end of the world.
n

Noah D

02/18/2022, 11:08 PM
Ahh gotcha aha, thanks for the advice with this one Derek, has been super helpful 🙏