mikekelly / hal-browser

An API browser for the hal+json media type
MIT License
835 stars 157 forks source link

Add extension points to render custom POST form #63

Closed gregturn closed 9 years ago

gregturn commented 9 years ago

@mikekelly Spring Data REST is using this project (via webjars) as a plugin, so anybody that builds a HAL-based service can easily serve up a HAL Browser front end with next to no effort.

A bonus feature Spring Data REST comes with is ALPS metadata which includes the properties for every resource. A feature I have wanted to add for some time is to use this metadata and make the HAL Browser's POST form more sophisticated by listing each property by name with separate fields, instead of a raw body field where the user must type in all the fields in JSON, and include the right names.

I am tinkering with a fork of the app to craft some extension point where someone could drop in extra JavaScript to fetch this extra metadata, but I'm finding it tricky to do so. I didn't know if you were interested in working on such an extension point for this project, or if you are too busy with other projects.

/cc @olivergierke

gregturn commented 9 years ago

@mikekelly I have written a working solution to plugin a custom non-GET dialog. You can view it at https://github.com/gregturn/hal-browser/commits/use-alps. There is just one commit at the time of this comment to skim over.

  1. It uses Spring Data REST's ALPS metadata to fetch the property names associated with a given resource.
  2. My tentative extension point is called HAL.customPost. In a couple places in the code, there are checks to see whether or not HAL.customPost is defined. If not, proceed with default behavior. If it is, then make some calls to alter the dialog.
  3. To parse the ALPS metadata, I have a JSONPath library hard-coded into my custom.js code. Naturally this needs to migrate to the collection of other libraries. But I didn't want to do that yet if you were on the cusp of migrating to bower or something.
  4. To plug in my custom JavaScript module, I had to customize the browser.html page. THis might be a step best left up to my application. Since I don't know any dynamic way to "look" for other modules, pulling in hal-browser via WebJars and then coding my own HTML frontend may be the best approach and remove the number of changes to this project.

What do you think?

mikekelly commented 9 years ago

Thanks for this. Sounds and looks reasonable at first glance. Please could you strip out custom.js and raise a PR?

mikekelly commented 9 years ago

Also, please could you add some docs for this feature to the README?

gregturn commented 9 years ago

Sure thing.

mikekelly commented 9 years ago

awesome, thanks

gregturn commented 9 years ago

Thanks to this patch, I have now plugged in a nice customization. Thanks!

http://docs.spring.io/spring-data/rest/docs/2.4.0.RC1/reference/html/#_the_hal_browser

mikekelly commented 9 years ago

Awesome! Good stuff