async run()

in nodejs12.x/cookiecutter-typescript-app-template/{{cookiecutter.project_name}}/app/src/apps/post-app.ts [21:46]


    async run(event: ApiGatewayEvent): Promise<ApiGatewayResponse> {
        let todo: TodoItem
        try {
            todo = JSON.parse(event.body);
            if (!todo.title) {
                console.log('Body is missing the title');
                return { statusCode: 422 };
            } else if (!todo.isComplete) {
                todo.isComplete = false;
            } else if (!todo.id) {
                console.log('Body is missing the id');
                return { statusCode: 422 };
            }
        } catch (err) {
            console.log('Event body could not be parsed as JSON');
            return { statusCode: 400 };
        }

        try {
            await this.repository.putTodo(todo, this.table);
            return { statusCode: 201, body: JSON.stringify(todo) };
        } catch(err) {
            console.log(err.message);
            return { statusCode: 500 };
        }
    }