This release includes ES Module support, innovative new features, and some bug fixes. You will like it.
Summary
Support ES Module
We have distributed only CommonJS, but now we will distribute ES Module. ES Module is used when the library is imported using the import
keyword. ESModule may improve performance and reduces bundle size.
StaticRouter and SmartRouter
Now we have a really smart router.
Added a simple router named "StaticRouter" that does not support dynamic routes. Now, we have there router: "TrieRouter", "RegExpRouter", and "StaticRouter".
And we introduce "SmartRouter".
SmartRouter picks the optimal router from three routers the first time it is accessed. Users can use the fastest router without explicitly specifying a router.
// Before this release.
// If we wanted to use optimal router, we have to specify it.
const app = new Hono({ router: new RegExpRouter() })
// This release.
// SmartRouter selects the fastest router automatically.
const app = new Hono()
StaticRouter is used for having only static routes. Otherwise, RegExpRouter will be used, but it has routing that RegExpRouter does not support, TrieRouter will be used.
Validator Middleware
New Validator Middleware as builtin middleware. This validator is magic.
app.post(
'/posts',
validator((v) => ({
post: {
id: v.json('post.id').asNumber().isRequired(),
title: v.json('post.title').isRequired().isLength({ max: 100 }),
published: v.json('post.published').asBoolean(),
body: v.json('post.body').isOptional(),
},
})),
(c) => {
const res = c.req.valid()
const post = res.post
return c.text(`Post: ${post.id} is ${post.title}`)
}
)
They will have "Types"! The value validated with asNumber()
keyword will have "Type".
Not only that.
- Easy to understand the result data because "declaring property name first".
- Easy to writing rules.
v.isLength(400)
is better thanv.isLength, 400
. - Getting only "declared properties". Additional properties are always ignored. It's safe.
For more information: https://honojs.dev/docs/builtin-middleware/validator/
Validator Middleware was provided as third party Middleware: https://github.com/honojs/validator, it will be deprecated.
All changes are below:
What's Changed
- fix(types): correct types for
app.notFound
/app.onError
by @yusukebe in #512 - fix(middleware): support multiple middleware on bearer/basic auth middleware by @yusukebe in #513
- Introduce StaticRouter and SmartRouter by @usualoma in #501
- feat(middleware): introduce "built-in" Validator Middleware by @yusukebe in #505
- Lightweight RegExpRouter reborn by @usualoma in #519
- fix(types): add types to middleware correctly by @yusukebe in #521
- feat(validator): add
isFalsy
andisNotFalsy
by @yusukebe in #523 - docs(readme): update discord invite url by @OnurGvnc in #527
- feat: support ES modules!! by @yusukebe in #526
- feat(validator): add
isBoolean
andisNumber
by @yusukebe in #530 - feat(cors): allow multiple origins by @yusukebe in #531
- Check in Origin header instead of Referer by @usualoma in #532
- feat(cors): Enable to check origin header by a function. by @usualoma in #533
- fix(deno): serve static middleware returns 404 correctly by @yusukebe in #537
- fix(bun): serve static middleware returns 404 correctly by @yusukebe in #538
- feat: another idea of Validator Middleware by @yusukebe in #535
- fix(redirect): don't have to make relative url to absolute one by @yusukebe in #541
- feat: support appending values with
c.header
by @yusukebe in #539 - feat:
c.req.body
andc.req.json
accept generics by @yusukebe in #529 - fix(validator): make "Types" work well by @yusukebe in #545
- feat(validator): Enable verification results to be retrieved as structured data. by @usualoma in #547
New Contributors
Full Changelog: v2.1.4...v2.2.0