Skip to content

Instantly share code, notes, and snippets.

@ssoroka
Created December 5, 2024 03:53
Show Gist options
  • Save ssoroka/50630ae9ad83055fd37b58b693fee6db to your computer and use it in GitHub Desktop.
Save ssoroka/50630ae9ad83055fd37b58b693fee6db to your computer and use it in GitHub Desktop.
func get[I, O any](api huma.API, path string, handler func(context.Context, *I) (*O, error), roles ...models.Role) {
huma.Register(api, huma.Operation{
DefaultStatus: http.StatusOK,
Method: http.MethodGet,
Path: path,
Security: []map[string][]string{
{
"bearer": {"jwt"},
"roles": rolesToStrings(roles...),
},
},
}, handler)
}
func post[I, O any](api huma.API, path string, handler func(context.Context, *I) (*O, error), roles ...models.Role) {
huma.Register(api, huma.Operation{
DefaultStatus: http.StatusCreated,
Method: http.MethodPost,
Path: path,
Security: []map[string][]string{
{
"bearer": {"jwt"},
"roles": rolesToStrings(roles...),
},
},
}, handler)
}
func unauthenticatedPost[I, O any](api huma.API, path string, handler func(context.Context, *I) (*O, error)) {
huma.Register(api, huma.Operation{
DefaultStatus: http.StatusCreated,
Method: http.MethodPost,
Path: path,
}, handler)
}
func put[I, O any](api huma.API, path string, handler func(context.Context, *I) (*O, error), roles ...models.Role) {
huma.Register(api, huma.Operation{
DefaultStatus: http.StatusOK,
Method: http.MethodPut,
Path: path,
Security: []map[string][]string{
{
"bearer": {"jwt"},
"roles": rolesToStrings(roles...),
},
},
}, handler)
}
func delete[I, O any](api huma.API, path string, handler func(context.Context, *I) (*O, error), roles ...models.Role) {
huma.Register(api, huma.Operation{
DefaultStatus: http.StatusNoContent,
Method: http.MethodDelete,
Path: path,
Security: []map[string][]string{
{
"bearer": {"jwt"},
"roles": rolesToStrings(roles...),
},
},
}, handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment