Skip to content

Commit

Permalink
Bolt v4 addendum: stricter typing of AWS API Gateway payloads (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj authored Oct 2, 2024
1 parent 50c9fd0 commit 4820052
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions src/receivers/AwsLambdaReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,41 @@ import { Receiver, ReceiverEvent } from '../types/receiver';
import { ReceiverMultipleAckError } from '../errors';
import { StringIndexed } from '../types/helpers';

export interface AwsEvent {
export type AwsEvent = AwsEventV1 | AwsEventV2;
type AwsEventStringParameters = Record<string, string | undefined>;
type AwsEventMultiValueStringParameters = Record<string, string[] | undefined>;
export interface AwsEventV1 {
// properties shared w/ v2:
body: string | null;
headers: Record<string, string>;
multiValueHeaders: any;
httpMethod: string;
headers: AwsEventStringParameters;
isBase64Encoded: boolean;
path: string;
pathParameters: any;
queryStringParameters: Record<string, string>;
multiValueQueryStringParameters: any;
stageVariables: any;
pathParameters: AwsEventStringParameters | null;
queryStringParameters: AwsEventStringParameters | null;
requestContext: any;
stageVariables: AwsEventStringParameters | null;
// v1-only properties:
httpMethod: string;
multiValueHeaders: AwsEventMultiValueStringParameters;
multiValueQueryStringParameters: AwsEventMultiValueStringParameters;
path: string;
resource: string;
}
export interface AwsEventV2 {
// properties shared w/ v1:
body?: string;
headers: AwsEventStringParameters;
isBase64Encoded: boolean;
pathParameters?: AwsEventStringParameters;
queryStringParameters?: AwsEventStringParameters;
requestContext: any;
stageVariables?: AwsEventStringParameters;
// v2-only properties:
cookies?: string[];
rawPath: string;
rawQueryString: string;
routeKey: string;
version: string;
}

export type AwsCallback = (error?: Error | string | null, result?: any) => void;

Expand Down Expand Up @@ -117,8 +138,7 @@ export default class AwsLambdaReceiver implements Receiver {
// Initialize instance variables, substituting defaults for each value
this.signingSecret = signingSecret;
this.signatureVerification = signatureVerification;
this.logger =
logger ??
this.logger = logger ??
(() => {
const defaultLogger = new ConsoleLogger();
defaultLogger.setLevel(logLevel);
Expand Down Expand Up @@ -252,7 +272,13 @@ export default class AwsLambdaReceiver implements Receiver {
this.logger.debug(`Error details: ${err}, storedResponse: ${storedResponse}`);
return { statusCode: 500, body: 'Internal server error' };
}
this.logger.info(`No request handler matched the request: ${awsEvent.path}`);
let path: string;
if ('path' in awsEvent) {
path = awsEvent.path;
} else {
path = awsEvent.rawPath;
}
this.logger.info(`No request handler matched the request: ${path}`);
return { statusCode: 404, body: '' };
};
}
Expand Down Expand Up @@ -320,7 +346,7 @@ export default class AwsLambdaReceiver implements Receiver {
}

// eslint-disable-next-line class-methods-use-this
private getHeaderValue(headers: Record<string, any>, key: string): string | undefined {
private getHeaderValue(headers: AwsEvent['headers'], key: string): string | undefined {
const caseInsensitiveKey = Object.keys(headers).find((it) => key.toLowerCase() === it.toLowerCase());
return caseInsensitiveKey !== undefined ? headers[caseInsensitiveKey] : undefined;
}
Expand Down

0 comments on commit 4820052

Please sign in to comment.