-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unsafe handlers for library use
- Loading branch information
1 parent
626a54b
commit 26a54c3
Showing
8 changed files
with
279 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/danielgtaylor/huma" | ||
"github.com/danielgtaylor/huma/schema" | ||
) | ||
|
||
// Item stores some value. | ||
type Item struct { | ||
ID string `json:"id"` | ||
Value int32 `json:"value"` | ||
} | ||
|
||
func main() { | ||
r := huma.NewRouter("Unsafe Test", "1.0.0") | ||
|
||
// Generate response schema for docs. | ||
s, _ := schema.Generate(reflect.TypeOf(Item{})) | ||
|
||
r.Resource("/unsafe", | ||
huma.PathParam("id", "desc"), | ||
huma.ResponseJSON(http.StatusOK, "doc", huma.Schema(*s)), | ||
).Get("doc", huma.UnsafeHandler(func(inputs ...interface{}) []interface{} { | ||
// Get the ID, which is the first input and will be a string since it's | ||
// a path parameter. | ||
id := inputs[0].(string) | ||
|
||
// Return an item with the passed in ID. | ||
return []interface{}{&Item{ | ||
ID: id, | ||
Value: 123, | ||
}} | ||
})) | ||
|
||
r.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.