Add Hono auth example and lifecycle hooks#10
Conversation
Introduce a new examples/hono-auth demo (app, server, config, schemas, package.json, and README) demonstrating registerRestRoutes lifecycleHooks for bearer-token auth, permission checks, and write normalization. Add typings (JsonDbHonoRestLifecycleHooks + lifecycleHooks option) and implement lifecycle hook support in integrations/hono.js: run beforeRequest, beforeWrite (only for mutating methods), then global and resource hooks; hooks may short-circuit responses. Add tests covering hook order, write-only beforeWrite behavior, and short-circuiting. Update docs and example index references, and include examples/*/package.json in package files list.
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThis PR adds lifecycle hooks ( ChangesHono REST Lifecycle Hooks and Auth Example
CLI Schema Split/Merge Commands and Mixed-Mode Detection
Sequence Diagram(s)sequenceDiagram
participant Client
participant beforeRequest as beforeRequest hook
participant beforeWrite as beforeWrite hook
participant globalHook as Global hook
participant resourceHook as Resource hook
participant Handler as Method handler
Client->>beforeRequest: Execute for all methods
alt short-circuits
beforeRequest-->>Client: Return response, stop
else continues
alt Write method (create/patch/put/delete)
beforeRequest->>beforeWrite: Execute only for writes
alt short-circuits
beforeWrite-->>Client: Return response, stop
else continues
beforeWrite->>globalHook: Continue chain
end
else Read method
beforeRequest->>globalHook: Skip beforeWrite
end
globalHook->>resourceHook: Execute if defined
resourceHook->>Handler: Execute core handler
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Introduce two new schema commands: `jsondb schema split` to extract embedded `seed` into a separate data fixture (with --schema-out and --seed-out options) and `jsondb schema merge` to produce a schema source that includes seed from an existing data fixture (with --out). Add mixed-mode diagnostics: warn when a schema file contains `seed` but a separate data fixture exists. Update CLI help text, project loader, and schema utilities (output path helpers and schema export option includeSeed). Add tests for validate warnings, split, merge, and executable-schema restrictions; update SPEC to document behavior and suggested workflow.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/examples/examples.test.js (1)
77-78: ⚡ Quick winAvoid asserting exact demo bearer token literals in this integration test.
These two checks make the test fail on doc-string changes without affecting lifecycle-hook behavior; keep assertions focused on integration identifiers.
As per coding guidelines, "Tests should create their own `db/` fixtures and avoid depending on generated repo state."Suggested diff
test('hono auth example shows lifecycle hook integration code', async () => { const source = await readFile(path.resolve('examples/hono-auth/src/app.mjs'), 'utf8'); assert.match(source, /registerRestRoutes/); assert.match(source, /lifecycleHooks/); assert.match(source, /beforeRequest/); assert.match(source, /beforeWrite/); - assert.match(source, /Bearer admin-token/); - assert.match(source, /Bearer user-token/); + assert.match(source, /Authorization/); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/examples/examples.test.js` around lines 77 - 78, Replace the brittle exact-token assertions in examples.test.js: instead of assert.match(source, /Bearer admin-token/) and assert.match(source, /Bearer user-token/), assert more robust integration identifiers (e.g., use assert.match(source, /Bearer\s+\w+-token/) or assert.match(source, /Bearer.*(admin|user)/)) so the test no longer depends on literal demo tokens; also ensure the test creates its own db fixtures rather than relying on generated repo state (update any setup in the same test file to build required db/ fixtures).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/cli/cli.test.js`:
- Around line 124-211: Add a new test in test/cli/cli.test.js that exercises the
CLI schema split/merge using relative --schema-out and --seed-out together with
--cwd to ensure paths are resolved relative to the project root created by
makeProject(); specifically invoke execFileAsync(process.execPath,
[path.resolve('src/cli.js'), 'schema', 'split' or 'merge', 'users', '--cwd',
cwd, '--schema-out', './db/custom.schema.json' (or '--seed-out',
'./db/custom.json')]) and then use readFile(path.join(cwd,
'db/custom.schema.json')) or readFile(path.join(cwd, 'db/custom.json')) to
assert the files are actually written under cwd (and assert stdout contains
Generated db/...); reuse helpers from the file (makeProject, writeFixture,
readFile, execFileAsync) and mirror the existing test patterns (assert.match,
assert.deepEqual) for verification.
---
Nitpick comments:
In `@test/examples/examples.test.js`:
- Around line 77-78: Replace the brittle exact-token assertions in
examples.test.js: instead of assert.match(source, /Bearer admin-token/) and
assert.match(source, /Bearer user-token/), assert more robust integration
identifiers (e.g., use assert.match(source, /Bearer\s+\w+-token/) or
assert.match(source, /Bearer.*(admin|user)/)) so the test no longer depends on
literal demo tokens; also ensure the test creates its own db fixtures rather
than relying on generated repo state (update any setup in the same test file to
build required db/ fixtures).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3b026c9a-696a-42c0-93e5-6d7cce73ace0
📒 Files selected for processing (19)
README.mdSPEC.mdexamples/hono-auth/README.mdexamples/hono-auth/db/pages.schema.jsoncexamples/hono-auth/db/users.schema.jsoncexamples/hono-auth/example.jsonexamples/hono-auth/jsondb.config.mjsexamples/hono-auth/package.jsonexamples/hono-auth/src/app.mjsexamples/hono-auth/src/server.mjspackage.jsonsrc/cli/commands/schema.jssrc/cli/output.jssrc/features/schema/project.jssrc/hono.d.tssrc/integrations/hono.jssrc/integrations/hono.test.jstest/cli/cli.test.jstest/examples/examples.test.js
Replace the CLI schema subcommands: `split` -> `unbundle` and `merge` -> `bundle`. Updated SPEC examples, CLI command routing, help/output text, diagnostic hints, and related error codes/messages to reflect the new names. Adjusted tests and expected output filenames to match the new `unbundle`/`bundle` behavior and updated error identifiers for executable schema source checks.
Add safer write semantics and new flags for schema bundle/unbundle. Introduces --force and --empty-seed flags, prevents accidental overwrites by comparing semantic JSON content, and rejects writing bundle outputs inside the active fixture directory unless --force is passed. Warns when unbundling rewrites .schema.jsonc (comments may be lost). Updates CLI help, default bundle example paths (artifacts/...), and adds helper functions (writeOutput, contentMatches, stableJsonStringify, isEmptySeed, isInsidePath, hasFlag). Tests and docs (README, SPEC) updated to cover the new behaviors.
Adds a test that ensures `schema unbundle` honors the provided `--cwd` when writing `--schema-out` and `--seed-out` paths. The test creates a project fixture with a users schema containing an inline seed, runs the CLI with relative output paths and `--cwd`, then verifies stdout messages, that the generated schema no longer contains the seed, and that the seed file was written with the expected data.
Introduce a new examples/hono-auth demo (app, server, config, schemas, package.json, and README) demonstrating registerRestRoutes lifecycleHooks for bearer-token auth, permission checks, and write normalization. Add typings (JsonDbHonoRestLifecycleHooks + lifecycleHooks option) and implement lifecycle hook support in integrations/hono.js: run beforeRequest, beforeWrite (only for mutating methods), then global and resource hooks; hooks may short-circuit responses. Add tests covering hook order, write-only beforeWrite behavior, and short-circuiting. Update docs and example index references, and include examples/*/package.json in package files list.
Summary by CodeRabbit
New Features
lifecycleHooks(beforeRequest, beforeWrite) for REST route authorization and payload normalization.jsondb schema splitandjsondb schema mergefor schema manipulation.Documentation