Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Feature request: Make Inertia extend Macroable

Proposal

Make the Inertia class from @adonisjs/inertia extend @poppinss/macroable, allowing packages and applications to add methods using Inertia.macro() and Inertia.getter(), similar to other AdonisJS core classes such as Request and Response.

Motivation

At the moment, there is no official extension point for the Inertia instance.

Packages that need to add custom methods currently have to patch Inertia.prototype directly, which is:

  • Fragile and dependent on internal implementation details.
  • Easy to apply multiple times accidentally.
  • Less discoverable than the standard AdonisJS extension pattern.
  • Harder to type and maintain.

Providing Macroable support would offer a familiar and officially supported mechanism for extending Inertia.

Example

A package could register a custom method as follows:

Inertia.macro('modal', function (component, props, backdrop) {
  // ...
})

with typings provided through module augmentation.

Real-world use case

I maintain adonis-inertia-modal, a package that provides backend-driven modals for Inertia.js applications on AdonisJS.

Today, the package patches Inertia.prototype.modal. With Macroable support, it could use the standard AdonisJS extension mechanism instead.

Scope

This should be a small, non-breaking change:

  • Make Inertia extend Macroable.
  • Call super() in the constructor.
  • Add @poppinss/macroable as a dependency.

I'd be happy to open a PR if this approach aligns with the project's direction.
https://github.com/filipebraida/inertia/tree/feat/macroable

You must be logged in to vote

Replies: 1 comment · 2 replies

Comment options

While testing adonis-modal I ran into a typing issue worth raising here, and I think it shows that Macroable on its own won't cover everything.

Right now I set up the type augmentation like this:

declare module '@adonisjs/inertia' {
  interface Inertia<Pages> {
    modal(...): ModalResponse
  }
}

It works fine in a normal server tsconfig, but it breaks around the barrel. In a second TS program that type-checks a caller of inertia.modal(...) without importing the @adonisjs/inertia barrel, say a Tuyau client tsconfig that pulls controllers in through the generated registry, the merge silently no-ops. The Inertia class is only nameable through the barrel re-export, while HttpContext.inertia references the class by an internal path, so a third-party interface Inertia merge only attaches when the barrel is actually loaded in that program. The only workaround I found is force-loading the barrel with a type-only import, kept alive by a dummy marker export so tsc doesn't drop it, which is a hack.

Macroable would clean up the runtime side, but not this. The class stays reachable only through the re-export, so the merge keeps failing in programs that don't load the barrel. Compare that with Request/Response, which are declared and exported from the same public module they're augmented on, so third-party augmentation just works.

Would it make sense to also expose an augmentable Inertia interface from the public entry (or anchor the class the way @adonisjs/core/http does with Request), so declare module '@adonisjs/inertia' attaches without consumers having to force-load the barrel?

You must be logged in to vote
2 replies
@thetutlage
Comment options

Hello @filipebraida

Can you send a PR for this, if you already have a solution at hand?

@filipebraida
Comment options

Hi @thetutlage, done! PR here: adonisjs/inertia#102

It covers both the Macroable change and the typing issue above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.