-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-lambda.js
More file actions
179 lines (161 loc) · 5.09 KB
/
local-lambda.js
File metadata and controls
179 lines (161 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* eslint-disable @typescript-eslint/no-require-imports */
const { getDefaultCorsHeaders } = require("./shared/cors-config.js");
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const {
DynamoDBDocumentClient,
ScanCommand,
} = require("@aws-sdk/lib-dynamodb");
const {
getBook,
getAllBooks,
getMyBooks,
putBook,
deleteBook,
getHealth,
registerUser,
loginUser,
deleteUserByEmail,
authenticate,
} = require("./shared/lambda-utils");
// Configure DynamoDB client for local development
const client = new DynamoDBClient({
endpoint: process.env.DYNAMODB_ENDPOINT || "http://localhost:8000",
region: process.env.AWS_REGION || "ap-southeast-1",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || "dummy",
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "dummy",
},
});
const dynamo = DynamoDBDocumentClient.from(client);
const tablename = process.env.TABLE_NAME || "books";
const usersTableName = process.env.USERS_TABLE_NAME || "users";
/* ──────────────────────────
Local Lambda handler (for testing)
────────────────────────── */
const handler = async (event) => {
let body;
let statusCode = 200;
const headers = getDefaultCorsHeaders();
try {
const { httpMethod, pathParameters, body: requestBody } = event;
const path = event.routeKey || `${httpMethod} ${event.path}`;
let user = null;
try {
if (event.headers?.Authorization || event.headers?.authorization) {
user = authenticate(event);
}
} catch (err) {
void err; // Ignore authentication errors for public routes
}
switch (path) {
case "GET /books":
body = await getAllBooks(dynamo, tablename, user);
break;
case "GET /books/{id}":
body = await getBook(dynamo, tablename, pathParameters.id);
if (body.isPrivate && (!user || user.userId !== body.ownerId)) {
body = { message: "not found" };
}
break;
case "GET /my-books":
if (!user) throw new Error("Authentication required");
body = await getMyBooks(dynamo, tablename, user);
break;
case "PUT /books": {
const currentUser = authenticate(event);
const bookData = JSON.parse(requestBody);
body = await putBook(dynamo, tablename, bookData, currentUser);
break;
}
case "DELETE /books/{id}": {
const currentUser = authenticate(event);
const bookToDelete = await getBook(
dynamo,
tablename,
pathParameters.id,
);
if (
!bookToDelete.ownerId ||
bookToDelete.ownerId !== currentUser.userId
) {
statusCode = 403;
throw new Error("You are not the owner of this book.");
}
body = await deleteBook(dynamo, tablename, pathParameters.id);
break;
}
case "POST /register": {
const { email, password } = JSON.parse(requestBody);
body = await registerUser(dynamo, usersTableName, email, password);
statusCode = 201;
break;
}
case "POST /login": {
const { email, password } = JSON.parse(requestBody);
body = await loginUser(dynamo, usersTableName, email, password);
break;
}
case "DELETE /account": {
const { email } = JSON.parse(event.body);
body = await deleteUserByEmail(dynamo, usersTableName, email);
break;
}
case "GET /health":
body = getHealth();
break;
default:
statusCode = 404;
body = { error: `Unsupported route: ${path}` };
}
} catch (err) {
console.error("Error:", err);
if (err.message.includes("not the owner")) {
statusCode = 403;
} else if (
err.message.includes("Invalid") ||
err.message.includes("Expired") ||
err.message.includes("malformed") ||
err.message.includes("Authorization") ||
err.message.includes("Authentication required")
) {
statusCode = 401;
} else if (
err.message.includes("exists") ||
err.message.includes("credentials")
) {
statusCode = 400;
} else {
statusCode = 500;
}
body = { error: err.message };
}
return {
statusCode,
body: JSON.stringify(body),
headers,
};
};
/* ──────────────────────────
Helper functions for testing
────────────────────────── */
const testConnection = async () => {
try {
console.log("Testing DynamoDB connection...");
const result = await dynamo.send(new ScanCommand({ TableName: tablename }));
console.log("✅ Connection successful!");
console.log(`Found ${result.Items.length} books in the table`);
return true;
} catch (error) {
console.error("❌ Connection failed:", error.message);
return false;
}
};
// Export functions for testing
module.exports = {
handler,
testConnection,
};
// If running directly, test connection
if (require.main === module) {
testConnection();
}