Skip to content

feat: make the Inertia class macroable#102

Open
filipebraida wants to merge 4 commits into
adonisjs:4.xfrom
filipebraida:feat/macroable
Open

feat: make the Inertia class macroable#102
filipebraida wants to merge 4 commits into
adonisjs:4.xfrom
filipebraida:feat/macroable

Conversation

@filipebraida

Copy link
Copy Markdown

🔗 Linked issue

Resolves the feature request discussed in adonisjs/core#5121.

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Makes the Inertia class extensible through the standard AdonisJS extension
pattern, in two parts:

1. Make Inertia extend Macroable

Packages can register methods with Inertia.macro() / Inertia.getter()
instead of patching Inertia.prototype — same pattern as Request,
Response, BriskRoute, etc. @poppinss/macroable is added as a dependency
(already used across AdonisJS core packages).

2. Type ctx.inertia against the package entrypoint

The HttpContext augmentation in inertia_middleware.ts now imports the
Inertia type from the package entrypoint instead of the internal module
path. Without this, a third-party declare module '@adonisjs/inertia'
augmentation silently no-ops in TS programs that never import the entrypoint
themselves (e.g. a Tuyau client tsconfig that pulls controllers in through the
generated registry):

error TS2664: Invalid module name in augmentation, module '@adonisjs/inertia' cannot be found.
error TS2339: Property 'modal' does not exist on type 'Inertia'.

With the entrypoint import, any program that type-checks ctx.inertia also
loads the entrypoint declarations, so the interface → class merge always
attaches. The import is type-only, so the compiled middleware gains no runtime
import.

Real-world validation

I migrated adonis-inertia-modal
to this branch — diff:
main...feat/inertia-macroable

  • The provider now registers modal() via Inertia.macro(); the package's
    test suite (116 tests, provider booted for real) passes against this branch.
  • The type augmentation workaround (force-loading the barrel with a type-only
    import kept alive by a dummy marker export) is deleted entirely, and both
    the server tsconfig and the Tuyau-style client tsconfig of a real app
    type-check inertia.modal(...) correctly.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Extend @poppinss/macroable so packages can add methods via Inertia.macro()
instead of monkey-patching the prototype, matching how other AdonisJS core
classes (Request, Response, ...) are extended.

Use case: adonis-inertia-modal currently patches Inertia.prototype.modal;
with this it can register Inertia.macro('modal', ...) idiomatically.
Import the Inertia type from the package entrypoint instead of the
internal module path, so that every program type-checking ctx.inertia
also loads the entrypoint declarations. Without this, third-party
augmentations via `declare module '@adonisjs/inertia'` silently fail in
programs that never import the entrypoint themselves (e.g. a Tuyau
client tsconfig pulling controllers in through the generated registry).

The import is type-only, so no runtime import of the entrypoint is
added to the compiled middleware.
filipebraida added a commit to filipebraida/adonis-inertia-modal that referenced this pull request Jul 20, 2026
This branch depends on unreleased @adonisjs/inertia changes
(adonisjs/inertia#102). Build and install the PR branch in the typecheck
and tests jobs until it ships. To be reverted once the upstream release
is out and the peer dependency is bumped.
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.

1 participant