cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
343 stars 43 forks source link

Put an example of usage with modules syntax #3

Closed gimenete closed 2 years ago

gimenete commented 2 years ago

Based on an example of the itty router library I was doing:

const router = OpenAPIRouter();
// ...
const worker: ExportedHandler<Bindings> = { fetch: router.handle };
export default worker;

By doing this, even if the parameters were correct in a request, I was getting always an empty data argument.

I had to check the code of the library and realized that if you do that then the data argument for the handle methods is not the second argument, but the fourth, because the library passes as many arguments as the router.handle function receives, plus the data. It turns out that the fetch function receives not just the Request argument, but two additional ones: an Env and an ExecutionContext.

This works fine:

const router = OpenAPIRouter();
// ...

const worker: ExportedHandler<Bindings> = {
  fetch: (request /*, env, ctx */) => router.handle(request),
};

export default worker;

Thanks!

G4brym commented 2 years ago

Hey @gimenete I've just merged a commit with a new section that cover this use case Thanks for lettings us know 😃