fix(validator): no return in middleware
The author should do the following, if applicable
- [ ] Add tests
- [x] Run tests
- [x]
bun run format:fix && bun run lint:fixto format the code - [ ] Add TSDoc/JSDoc to document the code
We may not have returned a response yet. In RSSHub, we have a wrapped handler, https://github.com/DIYgod/RSSHub/blob/680abcc00820876176d34cd47c3c13a37ad564dd/lib/registry.ts#L193-L210, and I tried to add a validator. https://github.com/DIYgod/RSSHub/pull/20584 This will result in unexpected errors. Please tell me if I did anything wrong.
~~BTW, can we have the standard validator accept a nullable schema? In that case, validation can be skipped.~~ I can pass an empty middleware.
And is it possible to restrict the schema type to the parameter defined in the path (Is there a type utils for this in Hono?) This is how I wrote it now.
type ParamObjectFromPath<P extends string> = P extends `${string}:${infer Param}/${infer Rest}`
? Param extends `${infer Name}?`
? { [K in Name]?: string } & ParamObjectFromPath<`/${Rest}`>
: { [K in Param]: string } & ParamObjectFromPath<`/${Rest}`>
: P extends `${string}:${infer Param}`
? Param extends `${infer Name}?`
? { [K in Name]?: string }
: { [K in Param]: string }
: Record<string, never>;
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 91.49%. Comparing base (b06005a) to head (fc37c1e).
Additional details and impacted files
@@ Coverage Diff @@
## main #4553 +/- ##
=======================================
Coverage 91.49% 91.49%
=======================================
Files 172 172
Lines 11230 11230
Branches 3257 3256 -1
=======================================
Hits 10275 10275
Misses 954 954
Partials 1 1
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Hey @hyoban
Can you provide a minimal code or project to explain your problem?
Can you provide a minimal code or project to explain your problem?
Like this, I want to handle the return only in the middleware.
import { Hono } from "hono";
import { validator } from "hono/validator";
import { serve } from '@hono/node-server'
const app = new Hono();
app.use(async (c, next) => {
await next();
return c.json({ message: c.get('message') });
});
app.get(
"/",
validator("query", () => {
return 'whatever';
}),
(c) => {
c.set('message', 'Hello, Hono!');
}
);
serve(app)
Hi @hyoban
Thank you for the response.
I understand your problem. Actually, I/we designed the handler after the validator returns a response. So, if you don't return anything, it doesn't behave correctly, and a TypeScript type mismatch occurs.
This means I've not imagined the use case using the no response handler after validation. But we may have to accept it. I'll work on it. Thanks!