Probotect0r
Probotect0r
HHono
Created by Probotect0r on 9/6/2024 in #help
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?
2 replies
HHono
Created by Probotect0r on 9/4/2024 in #help
doc endpoint returns 500 with no errors in logs
I am running on Lambda. I have used ts-to-zod to convert some of my types to zod definitions. I have the following code:
const app = new OpenAPIHono<{ Bindings: Bindings }>();
app.use("*", cors());
app.get("/v2/hello", async (c) => {
return c.json({ message: "Hello, World!" });
});
...
app.openapi(
createRoute({
method: apiConfig.method,
path: `/v2${apiConfig.path}`,
responses: schema ? {
200: {
description: "Successful response",
content: {
"application/json": {
schema: schema
},
},
},
} : null,
}),
async (c) => {
console.log("Hello Hono!");
const resp = await config.handlerFunc(c.env.event, c.env.lambdaContext);
console.log("resp", resp);
return c.json(resp);
},
);
...
app.doc("/v2/doc", { info: { title: "My API", version: "v2" }, openapi: "3.1.0"});
app.get('/v2/swagger', swaggerUI({url: '/v2/doc'}));
const app = new OpenAPIHono<{ Bindings: Bindings }>();
app.use("*", cors());
app.get("/v2/hello", async (c) => {
return c.json({ message: "Hello, World!" });
});
...
app.openapi(
createRoute({
method: apiConfig.method,
path: `/v2${apiConfig.path}`,
responses: schema ? {
200: {
description: "Successful response",
content: {
"application/json": {
schema: schema
},
},
},
} : null,
}),
async (c) => {
console.log("Hello Hono!");
const resp = await config.handlerFunc(c.env.event, c.env.lambdaContext);
console.log("resp", resp);
return c.json(resp);
},
);
...
app.doc("/v2/doc", { info: { title: "My API", version: "v2" }, openapi: "3.1.0"});
app.get('/v2/swagger', swaggerUI({url: '/v2/doc'}));
The api endpoints are working as expected, but the swaggerUI doesn't work because the /v2/doc endpoint returns a 500. There are no errors in the logs. Any ideas? I am looking to figure out how I can serve the swagger.json file itself.
4 replies