-
Notifications
You must be signed in to change notification settings - Fork 290
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 custom endpoints to extensions #1613
Conversation
Sure, let's add this if you have a use case for it. |
This fixes #1530 |
da0ed0d
to
46a9959
Compare
Oh thanks, I didn't notice there was already an issue for this. |
I made the
Or with arguments (endpoint name, methods):
I also created an ExtensionApi class in the frontend. This gets set on each extension module, and had get, put, post, and delete functions to call endpoints within the extension. The last thing might be overkill, but it looks through all elements with class name "extension-handler" and goes through each "data-handler-" attribute, converting the value to a handler for that event that has access to the extension module. This normally wouldn't be necessary, and you could just set an id on the required elements and init the event handlers in onExtensionPageLoad, but this makes it easier when the html template creates dynamic elements, like in a loop, where the handler for each element needs the loop variable, e.g. from one of my extensions:
Without something like the "data-handler" thing, it's more difficult to do something like I still need up update the documentation and add an example of the functionality to an included extension, but I wanted to get your opinion on this direction before doing that. |
46a9959
to
1a46e10
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last thing might be overkill, but it looks through all elements with class name "extension-handler" and goes through each "data-handler-" attribute, converting the value to a handler for that event that has access to the extension module.
I'm not a fan of that - I generally dislike inline event handlers. I also think the frontend code should be mostly agnostic of how the extension does frontend logic (unless it's reusing some existing Fava logic so I could imagine having some more direct access to the Svelte components or custom stuff like the router) - it could do so using a full-fledged frontend-rendering framework like React or Svelte or for ergonomics closer to HTML with inline handlers something like htmx or Stimulus.
36cd918
to
4e0d739
Compare
No problem, I figured it was probably too much of a hack. I can achieve most of that functionality in an extension that uses onPageLoad, since like I mentioned some sort of ability to specify things inline makes working with loops in extension html much easier for now. |
Tangentially related: I once did a PoC of loading React in an extension: https://github.com/andreasgerstmayr/fava-dashboards/blob/react/frontend/src/app.tsx. Registering custom routes would be awesome, as currently it's writing JSON in a |
|
6ccb190
to
d49c495
Compare
d49c495
to
725511e
Compare
There's no need for invoking the Svelte compiler at runtime - if you want to build Svelte components, your extension should have a build process and if you want to use Fava's components, we can expose those to extensions. It's really up to the extension to pick a frontend framework and use it. |
The extension_endpoint decorator allows extensions to register endpoints. It also adds the ExtensionContext and class which is passed to all extension module functions. The context currently only contains an ExtensionApi, which allows the extension to call it's endpoints more easily.
a5e67dd
to
81d39aa
Compare
81d39aa
to
ddda62f
Compare
Thanks, yeah I realized that was the intended way to do that after seeing how https://github.com/redstreet/fava_investor works. I have 5+ local extensions that I'm using/testing with so adding svelte/react to a bunch of different extensions would add a bunch of boilerplate, so I was trying to come up with a way that fava could do some of that work for me. But I agree that the flexibility of letting extensions use whatever frontend they want is better. |
Thanks for merging #1600. This is a small WIP addon to that, that adds the ability for extensions to register their own endpoints that can be called at '/beancount/extension/<ext_name>/', so that the template html or the new js module can communicate with the extension backend, which I think opens up the possibility for much more advanced extensions. This is a pretty bare-bones version of the change that doesn't handle arguments particularly well, and might have issues with locking (I'm not sure how locking around requests works), but it has been working well for me in a few personal extensions that I've written in the past few weeks.
I'm not sure if this type of thing is something you'd want to support, so I wanted to see if this was interesting at all before spending too much time cleaning it up.