event.pathParameters not populated on AWS Lambda event object

I have an existing AWS Lambda + API Gateway application that I am moving onto Hono. Here is my basic code:
type Bindings = {
event: LambdaEvent;
lambdaContext: LambdaContext;
};
const apiApp = new Hono<{ Bindings: Bindings }>();
apiApp.on(
'GET',
'/hello/:world/:test',
async (c) => {
console.log("Route invoked.");
console.log("routePath", c.req.routePath) // prints /hello/:world/:test
const id = c.req.param('world')
console.log(id) // prints the correct path parameter
console.log(c.env.event) // does not contain event.pathParameters object that would normally be populated with the path params
const resp = await lambdaHandlerFunc(c.env.event, c.env.lambdaContext);
return c.json(JSON.parse(resp.body), resp.statusCode);
},
);
type Bindings = {
event: LambdaEvent;
lambdaContext: LambdaContext;
};
const apiApp = new Hono<{ Bindings: Bindings }>();
apiApp.on(
'GET',
'/hello/:world/:test',
async (c) => {
console.log("Route invoked.");
console.log("routePath", c.req.routePath) // prints /hello/:world/:test
const id = c.req.param('world')
console.log(id) // prints the correct path parameter
console.log(c.env.event) // does not contain event.pathParameters object that would normally be populated with the path params
const resp = await lambdaHandlerFunc(c.env.event, c.env.lambdaContext);
return c.json(JSON.parse(resp.body), resp.statusCode);
},
);
The lambda event object does not seem to have the event.pathParameters object, which is normally populated when you use lambda with API Gateway. I am guessing this is because the route registered with API Gateway is the default route, which doesn't have path param information, and so it isn't included in the event object that is sent to my lambda function. But all my existing code relies on event.pathParameters. Is there an easy way to copy over all the params from c.req to c.env.event?
1 Reply
Probotect0r
Probotect0rOP5mo ago
I ended up doing this:
const event = c.env.event as APIGatewayProxyEventV2
event.pathParameters = c.req.param()
const event = c.env.event as APIGatewayProxyEventV2
event.pathParameters = c.req.param()
And had to import the type like this:
import { APIGatewayProxyEventV2 } from "hono/dist/types/adapter/aws-lambda/handler";
import { APIGatewayProxyEventV2 } from "hono/dist/types/adapter/aws-lambda/handler";

Did you find this page helpful?