-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ts
42 lines (35 loc) · 1.06 KB
/
create.ts
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
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import { handleErrorResponse } from './lib/error'
import ServiceError from './lib/ServiceError'
import Task from './model/Task'
const tableName = process.env.DYNAMODB_TABLE
export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
try {
if (!tableName) {
throw new ServiceError(
'DYNAMODB_TABLE environment variable not defined',
500
)
}
if (!event.body) {
throw new ServiceError('Bad Request: No data provided', 400)
}
const { text } = JSON.parse(Buffer.from(event.body, 'base64').toString())
if (!text) {
throw new ServiceError('Bad Request: Missing required field "text"', 400)
}
const task = new Task(tableName)
await task.create(text)
return {
statusCode: 201,
body: JSON.stringify({ message: 'Task created successfully' }),
headers: {
'Content-Type': 'application/json',
},
}
} catch (error) {
return handleErrorResponse(error)
}
}