shop
Version:
41 lines (37 loc) • 1.43 kB
text/typescript
import { APIGatewayEvent, Context } from 'aws-lambda'
import { HttpError } from './error'
export const lfunction =
(fn: (event: APIGatewayEvent, context: Context) => Promise<any>) =>
async (event: APIGatewayEvent, context: Context) => {
try {
console.log('Received request on path', event.path, 'with method', event.httpMethod)
console.log('Headers of the request', event.headers)
console.log('Body of the request', event.body)
const result = await fn(event, context)
if (result) {
if (typeof result === 'object' && result?.statusCode) {
return result
} else {
return {
statusCode: 200,
body: JSON.stringify(result),
}
}
} else {
return { statusCode: 200 }
}
} catch (err) {
console.error(err)
if (err instanceof HttpError) {
return {
statusCode: err.statusCode,
body: JSON.stringify({ message: err.message }),
}
} else {
return {
statusCode: 500,
body: JSON.stringify({ error: typeof err === 'string' ? err : (<any>err).message }),
}
}
}
}