Skip to content

Commit 4820052

Browse files
authored
Bolt v4 addendum: stricter typing of AWS API Gateway payloads (#2277)
1 parent 50c9fd0 commit 4820052

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/receivers/AwsLambdaReceiver.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,41 @@ import { Receiver, ReceiverEvent } from '../types/receiver';
88
import { ReceiverMultipleAckError } from '../errors';
99
import { StringIndexed } from '../types/helpers';
1010

11-
export interface AwsEvent {
11+
export type AwsEvent = AwsEventV1 | AwsEventV2;
12+
type AwsEventStringParameters = Record<string, string | undefined>;
13+
type AwsEventMultiValueStringParameters = Record<string, string[] | undefined>;
14+
export interface AwsEventV1 {
15+
// properties shared w/ v2:
1216
body: string | null;
13-
headers: Record<string, string>;
14-
multiValueHeaders: any;
15-
httpMethod: string;
17+
headers: AwsEventStringParameters;
1618
isBase64Encoded: boolean;
17-
path: string;
18-
pathParameters: any;
19-
queryStringParameters: Record<string, string>;
20-
multiValueQueryStringParameters: any;
21-
stageVariables: any;
19+
pathParameters: AwsEventStringParameters | null;
20+
queryStringParameters: AwsEventStringParameters | null;
2221
requestContext: any;
22+
stageVariables: AwsEventStringParameters | null;
23+
// v1-only properties:
24+
httpMethod: string;
25+
multiValueHeaders: AwsEventMultiValueStringParameters;
26+
multiValueQueryStringParameters: AwsEventMultiValueStringParameters;
27+
path: string;
2328
resource: string;
2429
}
30+
export interface AwsEventV2 {
31+
// properties shared w/ v1:
32+
body?: string;
33+
headers: AwsEventStringParameters;
34+
isBase64Encoded: boolean;
35+
pathParameters?: AwsEventStringParameters;
36+
queryStringParameters?: AwsEventStringParameters;
37+
requestContext: any;
38+
stageVariables?: AwsEventStringParameters;
39+
// v2-only properties:
40+
cookies?: string[];
41+
rawPath: string;
42+
rawQueryString: string;
43+
routeKey: string;
44+
version: string;
45+
}
2546

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

@@ -117,8 +138,7 @@ export default class AwsLambdaReceiver implements Receiver {
117138
// Initialize instance variables, substituting defaults for each value
118139
this.signingSecret = signingSecret;
119140
this.signatureVerification = signatureVerification;
120-
this.logger =
121-
logger ??
141+
this.logger = logger ??
122142
(() => {
123143
const defaultLogger = new ConsoleLogger();
124144
defaultLogger.setLevel(logLevel);
@@ -252,7 +272,13 @@ export default class AwsLambdaReceiver implements Receiver {
252272
this.logger.debug(`Error details: ${err}, storedResponse: ${storedResponse}`);
253273
return { statusCode: 500, body: 'Internal server error' };
254274
}
255-
this.logger.info(`No request handler matched the request: ${awsEvent.path}`);
275+
let path: string;
276+
if ('path' in awsEvent) {
277+
path = awsEvent.path;
278+
} else {
279+
path = awsEvent.rawPath;
280+
}
281+
this.logger.info(`No request handler matched the request: ${path}`);
256282
return { statusCode: 404, body: '' };
257283
};
258284
}
@@ -320,7 +346,7 @@ export default class AwsLambdaReceiver implements Receiver {
320346
}
321347

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

0 commit comments

Comments
 (0)