Closed G4brym closed 3 weeks ago
🧪 A prerelease is available for testing 🧪
You can install this latest build in your project with:
npm install --save https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/9555081565/npm-package-itty-router-openapi-151
Or you can immediately run this with npx
:
npx https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/9555081565/npm-package-itty-router-openapi-151
That's great! From a quick glance, it looks like the current openAPIRoute's would be compatible with this new version, that's good news on my book, since that could have been the main migration cost. Gotta say, the class based endpoints it's a beautiful abstraction. That's mostly why I prefer this package over hono-zod. It makes much more sense to encapsulate the handler with the schema, than the route with the schema. And I have not found any other router with this capabilities. I know this is not a very contributing comment, but nevertheless, sometimes, a few honest words are good to keep up the spirit, and therefore, keep the good work coming down the river. I can see this package becoming a standard for api development.
Awesome! I'll check the new stuff
This refactors the OpenAPI logic and adds support for latest versions of itty-router and possibly other router (like hono)
The new version will require users to instantiate their own
itty-router
router like:Breaking changes:
OpenAPIRouter()
methodaiPlugin
optionskipValidation
option (see new validation method bellow)itty-router
is no longer a dependency and users must manually add it to their projectschema
is no longer a static variableschema.parameters
was renamed toschema.request
to follow zod-to-openapi structurePath
parameters are now calledparams
to match the validated Data object nameschema.requestBody
was renamed toschema.request.body
to follow zod-to-openapi structurehandle
function is called, developers must manually call thegetValidatedData
to execute the data validation steptry {} catch (e) {}
to handle input errors, otherwise this library will return the errors in a Json response with http 400Additions:
await this.getValidatedData<typeof this.schema>()
method to retrieve all validated data, when sending<typeof this.schema>
in the generic the returned value will have full typescript inference based on the schemafromIttyRouter()
instantiate methodfromHono()
instantiate methodMigration guide:
npm install itty-router@latest --save
)const router = OpenAPIRouter({...})
const router = fromIttyRouter(Router(), {...})
new
from all "legacy type" call fromnew Str(...)
intoStr(...)
async handle(request: Request, env: Env, ctx: Context, data: any) {...}
async handle(request: Request, env: Env, ctx: Context) {...}
const data = await this.getValidatedData<typeof this.schema>()
static schema
to:schema
schema.parameters.query
intoschema.request.query
schema.parameters.path
intoschema.request.params
schema.parameters.headers
intoschema.request.headers
z.object({...})
schema.requestBody
intoschema.request.body: contentJson(z.object({...params.newModel}))
Minimal Hono example