From cee2a54d9908885230357d0b448c82f100afafa5 Mon Sep 17 00:00:00 2001 From: pozeus Date: Wed, 26 Aug 2020 13:47:17 +0200 Subject: [PATCH] get.ts function Adding GET logic --- .../todos/get.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 aws-node-typescript-rest-api-with-dynamodb/todos/get.ts diff --git a/aws-node-typescript-rest-api-with-dynamodb/todos/get.ts b/aws-node-typescript-rest-api-with-dynamodb/todos/get.ts new file mode 100644 index 000000000..0e36542bd --- /dev/null +++ b/aws-node-typescript-rest-api-with-dynamodb/todos/get.ts @@ -0,0 +1,36 @@ +'use strict'; + +import { DynamoDB } from 'aws-sdk' + +const dynamoDb = new DynamoDB.DocumentClient() + + +module.exports.get = (event, context, callback) => { + const params = { + TableName: process.env.DYNAMODB_TABLE, + Key: { + id: event.pathParameters.id, + }, + }; + + // fetch todo from the database + dynamoDb.get(params, (error, result) => { + // handle potential errors + if (error) { + console.error(error); + callback(null, { + statusCode: error.statusCode || 501, + headers: { 'Content-Type': 'text/plain' }, + body: 'Couldn\'t fetch the todo item.', + }); + return; + } + + // create a response + const response = { + statusCode: 200, + body: JSON.stringify(result.Item), + }; + callback(null, response); + }); +};