import { AuthResponse , APIGatewayEvent , Callback , Context , CustomAuthorizerEvent } from 'aws-lambda' ; import * as jwt from 'jsonwebtoken' ; import { createItem , deleteItem , getItems , getItemById , updateItem } from './database' ; import Response from './Response' ; import ResponseError from './ResponseError' ; export async function pingHandler ( event , context ) { console . log ( 'pingHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { return new Response ( { statusCode : 200 , body : { message : 'Chur' } } ) ; } catch ( err ) { console . log ( err ) ; throw new ResponseError ( { message : err . message } ) ; } } export const authorizer = ( event , context , callback ) => { console . log ( 'authorizer' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { const authHeader = event . authorizationToken ?. split ( ' ' ) || [ ] ; if ( authHeader . length === 2 && authHeader [ 0 ] . toLowerCase ( ) === 'bearer' ) { const decoded = jwt . verify ( authHeader [ 1 ] , process . env . AUTH0_CLIENT_SECRET as string ) as { sub : string ; } ; const authResponse = { policyDocument : { Version : '2012-10-17' , Statement : [ { Action : 'execute-api:Invoke' , Resource : [ event . methodArn , ] , Effect : 'Allow' , } , ] , } , principalId : decoded . sub , } ; callback ( undefined , authResponse ) ; } else { callback ( 'Unauthorized' , undefined ) ; } } catch ( err ) { console . log ( err ) ; callback ( 'Unauthorized' , undefined ) ; } } ; export async function getAllItemsHandler ( event , context ) { console . log ( 'getAllItemsHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { const items = await getItems ( event . requestContext . authorizer ?. principalId ) ; return new Response ( { statusCode : 200 , body : { items } } ) ; } catch ( err ) { console . log ( err ) ; throw new ResponseError ( { message : err . message } ) ; } } export async function getItemHandler ( event , context ) { console . log ( 'getItemHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { const item = await getItemById ( event . requestContext . authorizer ?. principalId , event . pathParameters ?. id || '' ) ; return new Response ( { statusCode : 200 , body : item } ) ; } catch ( err ) { console . log ( err ) ; throw err instanceof ResponseError ? err : new ResponseError ( { message : err . message } ) ; } } export async function createItemHandler ( event , context ) { console . log ( 'createItemHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { const item = await createItem ( event . requestContext . authorizer ?. principalId , JSON . parse ( event . body as string ) . name ) ; return new Response ( { statusCode : 201 , body : item } ) ; } catch ( err ) { console . log ( err ) ; throw new ResponseError ( { message : err . message } ) ; } } export async function updateItemHandler ( event , context ) { console . log ( 'updateItemHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { await updateItem ( event . requestContext . authorizer ?. principalId , event . pathParameters ?. id || '' , JSON . parse ( event . body as string ) . name ) ; return new Response ( { statusCode : 200 } ) ; } catch ( err ) { console . log ( err ) ; throw err instanceof ResponseError ? err : new ResponseError ( { message : err . message } ) ; } } export async function deleteItemHandler ( event , context ) { console . log ( 'deleteItemHandler' ) ; console . log ( 'event' , JSON . stringify ( event ) ) ; console . log ( 'context' , JSON . stringify ( context ) ) ; try { await deleteItem ( event . requestContext . authorizer ?. principalId , event . pathParameters ?. id || '' ) ; return new Response ( { statusCode : 200 } ) ; } catch ( err ) { console . log ( err ) ; throw err instanceof ResponseError ? err : new ResponseError ( { message : err . message } ) ; } }