Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bolt v4 addendum: stricter typing of AWS API Gateway payloads #2277

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading