Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package @7nohe/adonis-typed-links #114

Closed
wants to merge 3 commits into from

Conversation

7nohe
Copy link
Contributor

@7nohe 7nohe commented Feb 12, 2025

Provides strongly-typed wrappers for Inertia.js's Links, Manual Visits, and Form Helper from AdonisJS routing

@Julien-R44
Copy link
Collaborator

Julien-R44 commented Feb 12, 2025

Hey, thanks for the PR

Why not using Tuyau and specifically @tuyau/inertia?

I see that your router.post is type-safe, whereas in Tuyau, we just have <Link> wrappers.
Why not contribute to Tuyau instead of creating a whole new package that does almost the same thing?
It would be much better for the community to merge efforts rather than having 10 different packages with small variations

What do you think?

@7nohe
Copy link
Contributor Author

7nohe commented Feb 13, 2025

@Julien-R44

I agree with your thoughts and would like to contribute to Tuyau when I get the chance.
However, I don’t yet have a deep understanding of the Tuyau source code. That said, I would like to minimize the setup as much as possible (e.g., removing the need for TuyauProvider and enabling automatic type file generation), similar to what I did with @7nohe/adonis-typed-links. Would this be possible with @tuyau/inertia?

@Julien-R44
Copy link
Collaborator

  • The provider is not necessary if you're only using @tuyau/inertia. That said, it gets installed automatically when you run node ace add, so no manual setup is needed, it's completely transparent for the user
  • Automatic generation: yes. That was literally the initial reason why I developed the onSourceFileChanged hook adonisjs/assembler. It would be very easy to add, but I chose to keep only manual generation for now because triggering it on every file modification felt a bit heavy.. So :
    • For that, I was thinking of adding an option like watch, where you could input a regex/glob pattern, so the generation would only trigger when those specific files change, not constantly. Maybe I can take a look for adding that soon

If you're unfamiliar with the codebase: I would be happy to help! Feel free to reach out to me on Discord or open an issue on the repo, and I will try my best to help.
That said, the most important part is the @tuyau/inertia package, which is currently very, very simple.

https://github.com/Julien-R44/tuyau/blob/main/packages/inertia/src/react/index.tsx

I think adding a router.post (Manual visits) with type safety should be very simple.
Link is already type-safe.
What remains are the useForm calls. I've had this in mind for a while but haven't had the time to work on it yet.
In fact, I would like to take a slightly different approach than you:

Since Tuyau also generates the Input/Output types for each controller/endpoint, it would be entirely possible to have a useForm that is 100% type-safe, even for the data being sent. For example:

// Pass the route name ( or maybe an url ) to the `useForm`
const form = useForm('posts.comment', {
  // And thanks to Tuyau codegen, we are able to detect WHAT data should be sent to this endpoint.
  // Here, username is not a property accepted by our endpoint. So Typescript should yell at us
  username: '',
})

See what I mean?
To conclude, there's no obligation obviously, but yeah, if you would like to contribute certain things to Tuyau, I would be more than happy to help. and I think it's really beneficial for everyone to merge our efforts on the same package. Also, the plan is to add Tuyau to the Adonis doc, so it really will become the “official” way to do this kind of thing

@7nohe
Copy link
Contributor Author

7nohe commented Feb 16, 2025

I see, the input and output type generation is excellent!
I’ll take a look at the Tuyau code.
I also want to contribute to the official package, so I’ll probably start by trying to make route.post type-safe.

@7nohe
Copy link
Contributor Author

7nohe commented Feb 16, 2025

I want to proceed with adding this package’s features to Tuyau, so I’ll close this PR for now.

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.

2 participants