-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from danielgtaylor/docs-updates-2
docs: lots of docs updates, a few more tests/examples
- Loading branch information
Showing
15 changed files
with
1,323 additions
and
140 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package huma_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/danielgtaylor/huma/v2" | ||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
// Item represents a single item with a unique ID. | ||
type Item struct { | ||
ID string `json:"id"` | ||
} | ||
|
||
// ItemsResponse is a response containing a list of items. | ||
type ItemsResponse struct { | ||
Body []Item `json:"body"` | ||
} | ||
|
||
// ItemsHandler handles item-related CRUD operations. | ||
type ItemsHandler struct{} | ||
|
||
// RegisterListItems registers the `list-items` operation with the given API. | ||
// Because the method starts with `Register` it will be automatically called | ||
// by `huma.AutoRegister` down below. | ||
func (s *ItemsHandler) RegisterListItems(api huma.API) { | ||
// Register a list operation to get all the items. | ||
huma.Register(api, huma.Operation{ | ||
OperationID: "list-items", | ||
Method: http.MethodGet, | ||
Path: "/items", | ||
}, func(ctx context.Context, input *struct{}) (*ItemsResponse, error) { | ||
resp := &ItemsResponse{} | ||
resp.Body = []Item{{ID: "123"}} | ||
return resp, nil | ||
}) | ||
} | ||
|
||
func ExampleAutoRegister() { | ||
// Create the router and API. | ||
router := chi.NewMux() | ||
api := NewExampleAPI(router, huma.DefaultConfig("My Service", "1.0.0")) | ||
|
||
// Create the item handler and register all of its operations. | ||
itemsHandler := &ItemsHandler{} | ||
huma.AutoRegister(api, itemsHandler) | ||
|
||
// Confirm the list operation was registered. | ||
fmt.Println(api.OpenAPI().Paths["/items"].Get.OperationID) | ||
// Output: list-items | ||
} |
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.