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

[Draft] Multi return #137

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

[Draft] Multi return #137

wants to merge 2 commits into from

Conversation

EwenQuim
Copy link
Member

@EwenQuim EwenQuim commented Jul 2, 2024

⚠️ Implementation in draft, don't hesitate to ask for spec changes.

Spec

Currently, return type is either JSON or XML for the whole app. This needs to be improved.

The route /recipes should send HTML with a list of recipes if the Accept Header is "text/html", or JSON if the Accept Header is "application/json", or XML if the Accept Header is "application/xml". This allows to build a traditional website in the same time as building an API.

This change is of course backward-compatible and existing controller will not change current behaviour. Performance should also not be degraded.

Example

A controller might look like this (see multi_return_test.go for more examples).

fuego.Get(s, "/data", func(c ContextNoBody) (DataOrTemplate[MyType], error) {
	entity := MyType{Name: "Ewen"}

	return DataOrTemplate[MyType]{
		Data: entity, // Used when asking for JSON or XML
		Template: MyTemplate(entity) // std template, Templ or Gomponent rendered when asking for HTML
	}, nil
})

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.

None yet

2 participants