Skip to content

Commit 310c39f

Browse files
author
Benedikt Pauwels
committed
removed null assertions
1 parent 6b55f2d commit 310c39f

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Diff for: examples/lambda-functions/get-by-id.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ export const getByIdHandler = async (event: APIGatewayProxyEvent, context: Conte
5151
awsRequestId: context.awsRequestId,
5252
});
5353

54-
// Get id from pathParameters from APIGateway because of `/{id}` at template.yaml
55-
const id = event.pathParameters!.id;
56-
5754
// Get the item from the table
5855
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property
5956
let response;
6057
try {
6158
if (!tableName) {
6259
throw new Error('SAMPLE_TABLE environment variable is not set');
6360
}
61+
if (!event.pathParameters) {
62+
throw new Error('event does not contain pathParameters')
63+
}
64+
if (!event.pathParameters.id) {
65+
throw new Error('PathParameter id is missing')
66+
}
6467

6568
const data = await docClient.get({
6669
TableName: tableName,
67-
Key: { id: id },
70+
Key: { id: event.pathParameters.id },
6871
}).promise();
6972
const item = data.Item;
7073
response = {

Diff for: examples/lambda-functions/put-item.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ export const putItemHandler = async (event: APIGatewayProxyEvent, context: Conte
5151
awsRequestId: context.awsRequestId,
5252
});
5353

54-
// Get id and name from the body of the request
55-
const body = JSON.parse(event.body!);
56-
const id = body.id;
57-
const name = body.name;
58-
5954
// Creates a new item, or replaces an old item with a new item
6055
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
6156
let response;
6257
try {
6358
if (!tableName) {
6459
throw new Error('SAMPLE_TABLE environment variable is not set');
6560
}
61+
if (!event.body) {
62+
throw new Error('Event does not contain body')
63+
}
64+
65+
// Get id and name from the body of the request
66+
const body = JSON.parse(event.body);
67+
const id = body.id;
68+
const name = body.name;
6669

6770
await docClient.put({
6871
TableName: tableName,

0 commit comments

Comments
 (0)