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

Support for Hypermedia Links #690

Open
Mcklmo opened this issue Dec 26, 2024 · 2 comments
Open

Support for Hypermedia Links #690

Mcklmo opened this issue Dec 26, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Mcklmo
Copy link

Mcklmo commented Dec 26, 2024

It would be useful to be able to link operations to another. Hypermedia links can link endpoints that are commonly used as follow-up to the current endpoint. The links could be defined on operation creation and be added to the API response automatically.

Example response to GET /users/1

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "_links": {
    "self": {
      "href": "https://api.example.com/users/1"
    },
    "update": {
      "href": "https://api.example.com/users/1",
      "method": "PUT"
    },
    "delete": {
      "href": "https://api.example.com/users/1",
      "method": "DELETE"
    }
  }
}

This could, for example, be achieved by adding a parameter to the Operation struct that is called "HypermediaLinks" that takes a string slice of operation ids. On the api creation, it should panic if a linked operation is not found.

@danielgtaylor danielgtaylor added the enhancement New feature or request label Jan 20, 2025
@danielgtaylor
Copy link
Owner

@Mcklmo this should be possible today to some degree via OpenAPI's support for links in the documentation, e.g. using the huma.Response.Links field https://pkg.go.dev/github.com/danielgtaylor/huma/v2#Response. Of course this is out-of-band of the response, but more efficient than sending it each time.

As for in-band response links, you can just use a utility function to add them. I do so in my own services for self and next links but given the many different ways (and semi-standards) to send such links I haven't built anything into Huma's core for it. Huma itself is meant to be easy to wrap with such functionality, so you could for example create your own register function that takes info about your links format and generates the OpenAPI and/or response data via a transformer to automate it all, or just write a few utility functions your handlers can call to populate the links as needed. Up to you!

@Mcklmo
Copy link
Author

Mcklmo commented Jan 20, 2025

Awesome, thanks for elaborating on how this can achieved with Huma already. This was insightful, as I'm still new to your framework. I'll definitely give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants