From 6959612b9aaa8a7d5580e963da0d48420aadeddf Mon Sep 17 00:00:00 2001 From: Andrei Palchys Date: Mon, 27 Nov 2023 11:02:01 +0200 Subject: [PATCH] fix: errors --- .gitignore | 3 ++- src/functions/conversation-read-messages.ts | 15 +++++++++------ src/functions/create-personal-conversation.ts | 13 ++++++++----- src/functions/delete-personal-conversation.ts | 7 +++---- src/functions/groups-add-message.ts | 9 +++++---- src/functions/groups-create.ts | 16 +++++++++------- src/functions/groups-delete.ts | 7 +++---- src/functions/groups-read-messages.ts | 15 +++++++++------ src/functions/logout.ts | 5 +++-- src/functions/profile-update.ts | 16 ++++++---------- src/functions/registration.ts | 9 +++++---- src/functions/users.ts | 3 ++- 12 files changed, 64 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 8492386..1d2782f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode .DS_Store node_modules/ -cdk.out/ \ No newline at end of file +cdk.out/ +dist/ \ No newline at end of file diff --git a/src/functions/conversation-read-messages.ts b/src/functions/conversation-read-messages.ts index ae6cc32..a684068 100644 --- a/src/functions/conversation-read-messages.ts +++ b/src/functions/conversation-read-messages.ts @@ -1,15 +1,18 @@ -import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb"; +import { + DynamoDBClient, + ScanCommand, + ScanCommandInput, +} from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = event.headers["Authorization"] ?? event.headers["authorization"]; - const contentType = - event.headers["content-type"] ?? event.headers["Content-Type"]; if (!(userEmail && userID && userTokenRaw)) { return { @@ -49,7 +52,7 @@ export const handler = async (event) => { } console.log("-conversation", conversationID, since); - let input = { + let input: ScanCommandInput = { TableName: `conversation-${conversationID}`, ProjectionExpression: "#A, #M, #C", ExpressionAttributeNames: { @@ -83,7 +86,7 @@ export const handler = async (event) => { body: JSON.stringify(result), }; } catch (err) { - if (err.name === "ResourceNotFoundException") { + if ((err as Error).name === "ResourceNotFoundException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/create-personal-conversation.ts b/src/functions/create-personal-conversation.ts index 8a34491..272a2e3 100644 --- a/src/functions/create-personal-conversation.ts +++ b/src/functions/create-personal-conversation.ts @@ -5,11 +5,14 @@ import { PutItemCommand, ScanCommand, CreateTableCommand, + ScanInput, + CreateTableCommandInput, } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = @@ -40,14 +43,14 @@ export const handler = async (event) => { } let body = event.body; - let data; + let data: Record; if (event.isBase64Encoded) { body = new Buffer(body, "base64").toString("utf-8"); } if (contentType === "application/x-www-form-urlencoded") { - data = querystring.parse(body); + data = querystring.parse(body) as Record; } else if (contentType?.startsWith("multipart/form-data")) { const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/); const boundary = match[1] ?? match[2]; @@ -106,7 +109,7 @@ export const handler = async (event) => { const [user1, user2] = sortingData; - const queryInput = { + const queryInput: ScanInput = { TableName: "rsschool-2023-conversations", FilterExpression: "user1 = :user1Value AND user2 = :user2Value", ExpressionAttributeValues: { @@ -138,7 +141,7 @@ export const handler = async (event) => { const conversationID = Math.random().toString(36).substring(2); // create dedicated conversation table - const newTableInput = { + const newTableInput: CreateTableCommandInput = { TableName: `conversation-${conversationID}`, BillingMode: "PAY_PER_REQUEST", TableClass: "STANDARD", diff --git a/src/functions/delete-personal-conversation.ts b/src/functions/delete-personal-conversation.ts index fe22bd7..0a850f0 100644 --- a/src/functions/delete-personal-conversation.ts +++ b/src/functions/delete-personal-conversation.ts @@ -3,17 +3,16 @@ import { DeleteItemCommand, DeleteTableCommand, } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = event.headers["Authorization"] ?? event.headers["authorization"]; - const contentType = - event.headers["content-type"] ?? event.headers["Content-Type"]; if (!(userEmail && userID && userTokenRaw)) { return { @@ -74,7 +73,7 @@ export const handler = async (event) => { console.log("-delete item", result); } catch (err) { - if (err.name === "ConditionalCheckFailedException") { + if ((err as Error).name === "ConditionalCheckFailedException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/groups-add-message.ts b/src/functions/groups-add-message.ts index 6f83dd3..7975d0a 100644 --- a/src/functions/groups-add-message.ts +++ b/src/functions/groups-add-message.ts @@ -1,9 +1,10 @@ import querystring from "querystring"; import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userID = event.headers["rs-uid"]; const userEmail = event.headers["rs-email"]; @@ -37,14 +38,14 @@ export const handler = async (event) => { } let body = event.body; - let data; + let data: Record; if (event.isBase64Encoded) { body = new Buffer(body, "base64").toString("utf-8"); } if (contentType === "application/x-www-form-urlencoded") { - data = querystring.parse(body); + data = querystring.parse(body) as Record; } else if (contentType?.startsWith("multipart/form-data")) { const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/); const boundary = match[1] ?? match[2]; @@ -127,7 +128,7 @@ export const handler = async (event) => { console.log("-result", result); } catch (err) { - if (err.name === "ResourceNotFoundException") { + if ((err as Error).name === "ResourceNotFoundException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/groups-create.ts b/src/functions/groups-create.ts index aa9637d..64bf75e 100644 --- a/src/functions/groups-create.ts +++ b/src/functions/groups-create.ts @@ -1,14 +1,16 @@ import querystring from "querystring"; import { + CreateTableCommand, + CreateTableInput, DynamoDBClient, PutItemCommand, - CreateTableCommand, } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = @@ -41,14 +43,14 @@ export const handler = async (event) => { } let body = event.body; - let data; + let data: Record; if (event.isBase64Encoded) { body = new Buffer(body, "base64").toString("utf-8"); } if (contentType === "application/x-www-form-urlencoded") { - data = querystring.parse(body); + data = querystring.parse(body) as Record; } else if (contentType?.startsWith("multipart/form-data")) { const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/); const boundary = match[1] ?? match[2]; @@ -111,7 +113,7 @@ export const handler = async (event) => { const groupID = Math.random().toString(36).substring(2); // create dedicated group table - const newTableInput = { + const newTableInput: CreateTableInput = { TableName: `group-${groupID}`, BillingMode: "PAY_PER_REQUEST", TableClass: "STANDARD", @@ -133,7 +135,7 @@ export const handler = async (event) => { }; const newTableCommand = new CreateTableCommand(newTableInput); - let result = await client.send(newTableCommand); + await client.send(newTableCommand); // save conversation id in list const input = { @@ -157,7 +159,7 @@ export const handler = async (event) => { const command = new PutItemCommand(input); - result = await client.send(command); + await client.send(command); return { statusCode: 201, diff --git a/src/functions/groups-delete.ts b/src/functions/groups-delete.ts index 288cae2..b168210 100644 --- a/src/functions/groups-delete.ts +++ b/src/functions/groups-delete.ts @@ -3,17 +3,16 @@ import { DeleteItemCommand, DeleteTableCommand, } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = event.headers["Authorization"] ?? event.headers["authorization"]; - const contentType = - event.headers["content-type"] ?? event.headers["Content-Type"]; if (!(userEmail && userID && userTokenRaw)) { return { @@ -76,7 +75,7 @@ export const handler = async (event) => { console.log("-delete item", result); } catch (err) { - if (err.name === "ConditionalCheckFailedException") { + if ((err as Error).name === "ConditionalCheckFailedException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/groups-read-messages.ts b/src/functions/groups-read-messages.ts index 97a77f7..8a547cb 100644 --- a/src/functions/groups-read-messages.ts +++ b/src/functions/groups-read-messages.ts @@ -1,15 +1,18 @@ -import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb"; +import { + DynamoDBClient, + ScanCommand, + ScanCommandInput, +} from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw = event.headers["Authorization"] ?? event.headers["authorization"]; - const contentType = - event.headers["content-type"] ?? event.headers["Content-Type"]; if (!(userEmail && userID && userTokenRaw)) { return { @@ -49,7 +52,7 @@ export const handler = async (event) => { } console.log("-conversation", groupID, since); - let input = { + let input: ScanCommandInput = { TableName: `group-${groupID}`, ProjectionExpression: "#A, #M, #C", ExpressionAttributeNames: { @@ -83,7 +86,7 @@ export const handler = async (event) => { body: JSON.stringify(result), }; } catch (err) { - if (err.name === "ResourceNotFoundException") { + if ((err as Error).name === "ResourceNotFoundException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/logout.ts b/src/functions/logout.ts index 98629c4..77c8d4e 100644 --- a/src/functions/logout.ts +++ b/src/functions/logout.ts @@ -1,8 +1,9 @@ import { DynamoDBClient, UpdateItemCommand } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userID = event.headers["rs-uid"]; @@ -69,7 +70,7 @@ export const handler = async (event) => { } catch (err) { console.log("-error", err); - if (err.name === "ConditionalCheckFailedException") { + if ((err as Error).name === "ConditionalCheckFailedException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/profile-update.ts b/src/functions/profile-update.ts index 50c45da..cb350f9 100644 --- a/src/functions/profile-update.ts +++ b/src/functions/profile-update.ts @@ -1,15 +1,11 @@ +import { APIGatewayProxyEventV2 } from "aws-lambda"; import querystring from "querystring"; -import crypto from "crypto"; -import { - DynamoDBClient, - UpdateItemCommand, - GetItemCommand, -} from "@aws-sdk/client-dynamodb"; +import { DynamoDBClient, UpdateItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const userID = event.headers["rs-uid"]; @@ -44,14 +40,14 @@ export const handler = async (event) => { } let body = event.body; - let data; + let data: Record; if (event.isBase64Encoded) { body = new Buffer(body, "base64").toString("utf-8"); } if (contentType === "application/x-www-form-urlencoded") { - data = querystring.parse(body); + data = querystring.parse(body) as Record; } else if (contentType?.startsWith("multipart/form-data")) { const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/); const boundary = match[1] ?? match[2]; @@ -140,7 +136,7 @@ export const handler = async (event) => { try { await client.send(updateCommand); } catch (err) { - if (err.name === "ConditionalCheckFailedException") { + if ((err as Error).name === "ConditionalCheckFailedException") { return { statusCode: 400, body: JSON.stringify({ diff --git a/src/functions/registration.ts b/src/functions/registration.ts index a31a893..63e1104 100644 --- a/src/functions/registration.ts +++ b/src/functions/registration.ts @@ -1,25 +1,26 @@ import querystring from "querystring"; import crypto from "crypto"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { console.log("-event", event); const contentType = event.headers["content-type"] ?? event.headers["Content-Type"]; let body = event.body; - let data; + let data: Record; if (event.isBase64Encoded) { body = new Buffer(body, "base64").toString("utf-8"); } if (contentType === "application/x-www-form-urlencoded") { - data = querystring.parse(body); + data = querystring.parse(body) as Record; } else if (contentType?.startsWith("multipart/form-data")) { const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/); const boundary = match[1] ?? match[2]; @@ -126,7 +127,7 @@ export const handler = async (event) => { statusCode: 201, }; } catch (err) { - if (err.name === "ConditionalCheckFailedException") { + if ((err as Error).name === "ConditionalCheckFailedException") { // user already exists return { diff --git a/src/functions/users.ts b/src/functions/users.ts index 730c4ea..dcec638 100644 --- a/src/functions/users.ts +++ b/src/functions/users.ts @@ -1,8 +1,9 @@ import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb"; +import { APIGatewayProxyEventV2 } from "aws-lambda"; const client = new DynamoDBClient({ region: "eu-central-1" }); -export const handler = async (event) => { +export const handler = async (event: APIGatewayProxyEventV2) => { const userEmail = event.headers["rs-email"]; const userID = event.headers["rs-uid"]; const userTokenRaw =