I am working with two functions:
import { handler } from '../lib/handler-lib'
import { APIGatewayEvent, Context } from 'aws-lambda'
export const producer = handler(async (
_event: APIGatewayEvent,
_context: Context
): Promise<object> => {
return {
some: 'data result'
}
})
import {
APIGatewayEvent,
APIGatewayProxyResult,
Context
} from 'aws-lambda'
export const handler = (lambda: Function): object => {
return async (
event: APIGatewayEvent,
context: Context
): Promise<APIGatewayProxyResult> => {
let body: object
let statusCode: number
try {
body = await lambda(event, context)
statusCode = 200
} catch (e) {
body = { error: e.message }
statusCode = 500
}
return {
body: JSON.stringify(body),
statusCode
}
}
}
Is there a way to simplify these functions?
Both functions currently define types for event
and context
. If only the second function does this, all callers could benefit from type declarations automatically.
Should the arguments of lambda: Function
be explicitly defined?