aws-lambda#APIGatewayProxyResultV2 TypeScript Examples

The following examples show how to use aws-lambda#APIGatewayProxyResultV2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From cdk-examples with MIT License 6 votes vote down vote up
export async function deleteTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const { body } = event

    if (!body) {

        return sendFail('invalid request')
    }

    const { id } = JSON.parse(body) as DeleteTodo

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const todoParams: DeleteItemInput = {
        Key: marshall({ id }),
        ReturnValues: 'ALL_OLD',
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        const { Attributes } = await dynamoClient.deleteItem(todoParams)

        const todo = Attributes ? unmarshall(Attributes) : null
        
        return {
            statusCode: 200,
            body: JSON.stringify({ todo })    
        }

    } catch (err) {

        console.log(err)

        return sendFail('something went wrong')
    }
}
Example #2
Source File: index.ts    From cdk-examples with MIT License 6 votes vote down vote up
export async function getAll(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const scanTodo: ScanInput = {
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        const { Items } = await dynamoClient.scan(scanTodo)

        const userData = Items ? Items.map(item => unmarshall(item)) : []

        return {
            statusCode: 200,
            body: JSON.stringify({ listTodo: userData})
        }

    } catch (err) {

        console.log(err)

        return {
            statusCode: 400,
            body: JSON.stringify({
                message: 'something went wrong'
            })
        }
    }
}
Example #3
Source File: index.ts    From cdk-examples with MIT License 6 votes vote down vote up
export async function getOne(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const { body } = event

    if (!body) return sendError('invalid request')
    
    const data = JSON.parse(body) as UserInput

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const getTodo: GetItemInput = {
        Key: marshall({
            id: data.id
        }),
        TableName: process.env.TODO_TABLE_NAME
    }
    
    try {

        const { Item } = await dynamoClient.getItem(getTodo)

        const todo = Item ? unmarshall(Item) : null

        return {
            statusCode: 200,
            body: JSON.stringify({ todo })
        }

    } catch (err) {

        console.log(err)

        return sendError('something went wrong')
    }
}
Example #4
Source File: index.ts    From cdk-examples with MIT License 6 votes vote down vote up
export async function deleteTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
    
    const body = event.body

    if (!body) {

        return sendFailResponse()
    }

    const { id } = JSON.parse(body) as Todo

    const dynamoClient = new DynamoDB({ region: 'us-east-1' })

    const deleteInput: DeleteItemInput = {
        Key: marshall({ id }),
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        await dynamoClient.deleteItem(deleteInput)

        return {
            statusCode: 200,
            body: JSON.stringify({ message: 'success' })
        }

    } catch (err) {

        console.log('failed to delete todo', err)

        return sendFailResponse()
    }
}
Example #5
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function update(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const { body } = event

    if (!body) {

        return sendFail('invalid request')
    }

    const { id, done } = JSON.parse(body) as UpdateTodo

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const todoParams: UpdateItemInput = {
        Key: marshall({ id }),
        UpdateExpression: 'set done = :done',
        ExpressionAttributeValues: marshall({
            ':done': done
        }),
        ReturnValues: 'ALL_NEW',
        TableName: process.env.UPDATE_TABLE_NAME
    }

    try {

        const { Attributes } = await dynamoClient.updateItem(todoParams)

        const todo = Attributes ? unmarshall(Attributes) : null
        
        return {
            statusCode: 200,
            body: JSON.stringify({ todo })    
        }

    } catch (err) {

        console.log(err)

        return sendFail('something went wrong')
    }
}
Example #6
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function myFunction(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
    
    return {
        statusCode: 200,
        body: JSON.stringify({ message: 'hello from ts lambda' })
    }
}
Example #7
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFailResponse(): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 400,
        body: JSON.stringify({ message: 'fail' })
    }
}
Example #8
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function getTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
    
    const body = event.body

    if (!body) {

        return sendFailResponse()
    }

    const { id } = JSON.parse(body) as TodoInput

    const dynamoClient = new DynamoDB({ region: 'us-east-1' })
    
    const getTodoInput: GetItemInput = {
        Key: marshall({ id }),
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        const { Item } = await dynamoClient.getItem(getTodoInput)

        if (!Item) {

            return sendFailResponse()
        }

        const todo = unmarshall(Item) as Todo

        return {
            statusCode: 200,
            body: JSON.stringify({ todo })
        }

    } catch (err) {

        console.log('get item failed', err)

        return sendFailResponse()
    }
}
Example #9
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFailResponse(): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 200,
        body: JSON.stringify({ message: 'fail' })
    }
}
Example #10
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFailResponse(): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 200,
        body: JSON.stringify({ message: 'fail' })
    }
}
Example #11
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function createTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
    
    const body = event.body

    if (!body) {

        return sendFailResponse()
    }

    const { title, isComplete } = JSON.parse(body) as TodoInput

    const dynamoClient = new DynamoDB({ region: 'us-east-1' })

    const newTodo: Todo = { id: uuid(), title, isComplete }

    const newTodoInput: PutItemInput = {
        Item: marshall({
            'id': newTodo.id,
            'title': title,
            'isComplete': isComplete
        }),
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        await dynamoClient.putItem(newTodoInput)

        return {
            statusCode: 200,
            body: JSON.stringify({ id: newTodo.id })
        }

    } catch (err) {

        console.log("failed to save new todo", err)

        return sendFailResponse()
    }
}
Example #12
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFail(message: string): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 200,
        body: JSON.stringify({ message })
    }
}
Example #13
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function createTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const { body } = event

    if (!body) {

        return sendFail('invalid request')
    }

    const { id, owner, title, done } = JSON.parse(body) as TodoInput

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const newTodo: Todo = {
        id: id ?? uuid(),
        owner, title, done
    }

    const todoParams: PutItemInput = {
        Item: marshall(newTodo),
        TableName: process.env.TODO_TABLE_NAME
    }

    try {

        await dynamoClient.putItem(todoParams)
        
        return {
            statusCode: 200,
            body: JSON.stringify({ newTodo })    
        }

    } catch (err) {

        console.log(err)

        return sendFail('something went wrong')
    }
}
Example #14
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendError(message: string): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 400,
        body: JSON.stringify({ message })
    }
}
Example #15
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
export async function queryTodo(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {

    const { body } = event

    if (!body) return sendError('invalid request')
    
    const data = JSON.parse(body) as UserInput

    const dynamoClient = new DynamoDB({ 
        region: 'us-east-1' 
    })

    const queryTodo: QueryInput = {
        KeyConditionExpression: '#todoOwner = :userId',
        ExpressionAttributeNames: {
            '#todoOwner': 'owner'
        }, 
        ExpressionAttributeValues: marshall({
            ':userId': data.owner
        }),
        IndexName: 'ownerIndex',
        TableName: process.env.TODO_TABLE_NAME
    }
    
    try {

        const { Items } = await dynamoClient.query(queryTodo)

        const listTodo = Items ? Items.map(item => unmarshall(item)) : []

        return {
            statusCode: 200,
            body: JSON.stringify({ listTodo })
        }

    } catch (err) {

        console.log(err)

        return sendError('something went wrong')
    }
}
Example #16
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendError(message: string): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 400,
        body: JSON.stringify({ message })
    }
}
Example #17
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFail(message: string): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 400,
        body: JSON.stringify({ message })
    }
}
Example #18
Source File: index.ts    From cdk-examples with MIT License 5 votes vote down vote up
function sendFail(message: string): APIGatewayProxyResultV2 {
    
    return {
        statusCode: 400,
        body: JSON.stringify({ message })
    }
}