getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.97k stars 1.57k forks source link

`Sentry.AWSLambda.wrapHandler` is incorrectly marked as returning `TResult | undefined` #3364

Closed G-Rath closed 2 years ago

G-Rath commented 3 years ago

Package + Version

Version:

6.2.5

Description

https://github.com/getsentry/sentry-javascript/blob/ce40962d91117a675ec7440d3f7e4015f33b95ad/packages/serverless/src/awslambda.ts#L180-L183

By doing | undefined, the result is not assignable to a handler from @types/aws-lambda.

i.e:

import * as Sentry from '@sentry/serverless';
import { APIGatewayTokenAuthorizerHandler, PolicyDocument } from 'aws-lambda';

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      BASIC_AUTH_USERNAME: string;
      BASIC_AUTH_PASSWORD: string;
    }
  }
}

const credentials = Buffer.from(
  `${process.env.BASIC_AUTH_USERNAME}:${process.env.BASIC_AUTH_PASSWORD}`
).toString('base64');

const AllowPolicyDocument: PolicyDocument = {
  Version: '2012-10-17',
  Statement: [
    {
      Effect: 'Allow',
      Action: ['execute-api:Invoke'],
      Resource: ['arn:aws:execute-api:ap-southeast-2:*']
    }
  ]
};

export const handler: APIGatewayTokenAuthorizerHandler = Sentry.AWSLambda.wrapHandler(
  async event => {
    if (event.authorizationToken === `Basic ${credentials}`) {
      console.log('successfully authenticated');

      return Promise.resolve({
        principalId: 'user',
        policyDocument: AllowPolicyDocument
      });
    }

    console.info(event);

    throw new Error('Unauthorized');
  }
);

This produces this error:

src/authorizer/index.ts:28:14 - error TS2322: Type 'Handler<APIGatewayTokenAuthorizerEvent, { principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'APIGatewayTokenAuthorizerHandler'.
  Type 'void | Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'void | Promise<APIGatewayAuthorizerResult>'.
    Type 'Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'void | Promise<APIGatewayAuthorizerResult>'.
      Type 'Promise<{ principalId: string; policyDocument: PolicyDocument; } | undefined>' is not assignable to type 'Promise<APIGatewayAuthorizerResult>'.
        Type '{ principalId: string; policyDocument: PolicyDocument; } | undefined' is not assignable to type 'APIGatewayAuthorizerResult'.
          Type 'undefined' is not assignable to type 'APIGatewayAuthorizerResult'.

28 export const handler: APIGatewayTokenAuthorizerHandler = Sentry.AWSLambda.wrapHandler(
xr0master commented 2 years ago

Hello, I have the same question. Was there a reason to add the undefined to types? Since it does not correspond to the AWS lambda types. Of course, this is easy to fix through casting, but it slightly increases the code and constantly needs to do it for each function.