Skip to content

Commit

Permalink
fix: errors
Browse files Browse the repository at this point in the history
  • Loading branch information
apalchys committed Nov 27, 2023
1 parent 786fad5 commit 6959612
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.DS_Store
node_modules/
cdk.out/
cdk.out/
dist/
15 changes: 9 additions & 6 deletions src/functions/conversation-read-messages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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({
Expand Down
13 changes: 8 additions & 5 deletions src/functions/create-personal-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -40,14 +43,14 @@ export const handler = async (event) => {
}

let body = event.body;
let data;
let data: Record<string, string>;

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<string, string>;
} else if (contentType?.startsWith("multipart/form-data")) {
const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/);
const boundary = match[1] ?? match[2];
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/functions/delete-personal-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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({
Expand Down
9 changes: 5 additions & 4 deletions src/functions/groups-add-message.ts
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -37,14 +38,14 @@ export const handler = async (event) => {
}

let body = event.body;
let data;
let data: Record<string, string>;

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<string, string>;
} else if (contentType?.startsWith("multipart/form-data")) {
const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/);
const boundary = match[1] ?? match[2];
Expand Down Expand Up @@ -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({
Expand Down
16 changes: 9 additions & 7 deletions src/functions/groups-create.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -41,14 +43,14 @@ export const handler = async (event) => {
}

let body = event.body;
let data;
let data: Record<string, string>;

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<string, string>;
} else if (contentType?.startsWith("multipart/form-data")) {
const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/);
const boundary = match[1] ?? match[2];
Expand Down Expand Up @@ -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",
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/functions/groups-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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({
Expand Down
15 changes: 9 additions & 6 deletions src/functions/groups-read-messages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions src/functions/logout.ts
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -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({
Expand Down
16 changes: 6 additions & 10 deletions src/functions/profile-update.ts
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -44,14 +40,14 @@ export const handler = async (event) => {
}

let body = event.body;
let data;
let data: Record<string, string>;

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<string, string>;
} else if (contentType?.startsWith("multipart/form-data")) {
const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/);
const boundary = match[1] ?? match[2];
Expand Down Expand Up @@ -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({
Expand Down
Loading

0 comments on commit 6959612

Please sign in to comment.