Skip to content

fix(hono): properly handle basePath() for route matching and schema generation#306

Merged
G4brym merged 2 commits intomainfrom
fix/hono-basepath-support
Feb 24, 2026
Merged

fix(hono): properly handle basePath() for route matching and schema generation#306
G4brym merged 2 commits intomainfrom
fix/hono-basepath-support

Conversation

@G4brym
Copy link
Copy Markdown
Member

@G4brym G4brym commented Feb 24, 2026

Summary

Builds on #303 by @hambergerpls — takes the original PR's approach of supporting Hono's basePath() and reworks it with defensive runtime guards, better error handling, and additional bug fixes.

  • Adds proper support for Hono's basePath() so that route matching, OpenAPI schema generation, and doc UI routes all use the correct prefix automatically
  • Detects pre-existing basePath() on Hono instances via _basePath (with runtime type guards and trailing-slash normalization)
  • Throws a clear error when both Hono's basePath() and chanfana's base option are combined
  • Validates base format (must start with /, must not end with /) in both adapters
  • Fixes nested router schema paths not including the outer router's base prefix
  • Adds schema property to OpenAPIRouterType type

Changes

Source

  • src/adapters/hono.tsgetHonoBasePath() helper, rewrote fromHono() with detection/validation/error-handling, fixed stale router reference, routerHandlesBasePrefix getter
  • src/openapi.tsrouterHandlesBasePrefix getter, routeBase in createDocsRoutes(), schema on OpenAPIRouterType, validateBasePath() usage, fixed registerNestedRouter base prepend
  • src/utils.tsvalidateBasePath() helper

Tests

  • tests/integration/router-options.test.ts — 28 tests covering basePath detection, base option, combined error, format validation, openapi.yaml, nested routers with base

Docs

  • Updated 5 doc files and added changeset

Related

Supersedes #303

hambergerpls and others added 2 commits February 24, 2026 12:00
…eration

Hono's basePath() method was not being utilized correctly, causing the
base option to only affect schema generation but not actual route matching.

Changes:
- Apply chanfana base option via Hono's basePath() method for proper routing
- Read effective base from Hono's _basePath to include any pre-existing basePath
- Remove redundant base prefix from doc routes (now handled by basePath)
- Expose options via proxy for runtime access

This ensures routes, OpenAPI schema paths, and documentation URLs all
use the correct combined base path when using basePath on Hono directly
or via chanfana's base option.
…eration

- Auto-detect Hono's basePath() for schema generation and doc routes
- Apply Hono's basePath() internally when chanfana base option is used
- Throw error when both Hono basePath() and chanfana base are combined
- Validate base format: must start with '/' and not end with '/'
- Add routerHandlesBasePrefix flag so Hono skips base prefix on doc routes
- Fix stale router reference in nested route handling
- Expose options via proxy for runtime access
- Update docs and migration guide with new basePath behavior
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/chanfana@306

commit: 946c084

@G4brym G4brym force-pushed the fix/hono-basepath-support branch from b29c65f to 946c084 Compare February 24, 2026 12:00
Comment thread src/adapters/hono.ts Dismissed
@G4brym G4brym merged commit 9470a04 into main Feb 24, 2026
9 checks passed
@G4brym G4brym deleted the fix/hono-basepath-support branch February 24, 2026 12:05
@github-actions github-actions bot mentioned this pull request Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants