Skip to content

Commit

Permalink
refactor: Renamed AddHeader, AddCookie, WithQueryParams to Header, Co…
Browse files Browse the repository at this point in the history
…okie, QueryParam

Uses the same Param method
  • Loading branch information
EwenQuim committed Feb 21, 2024
1 parent 9e26c7d commit 4b73489
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
Message: "Hello, " + data.Name,
BestFramework: "Fuego!",
}, nil
}).WithDescription("Say hello to the world").AddHeader("test").AddCookie("test")
}).WithDescription("Say hello to the world").Header("test", "").Cookie("test", "")

// Standard net/http handler with automatic OpenAPI route declaration
fuego.GetStd(s, "/std", func(w http.ResponseWriter, r *http.Request) {
Expand Down
52 changes: 36 additions & 16 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,47 @@ func (r Route[ResponseBody, RequestBody]) SetTags(tags ...string) Route[Response
return r
}

func (r Route[ResponseBody, RequestBody]) AddHeader(header string) Route[ResponseBody, RequestBody] {
r.operation.Parameters = append(r.operation.Parameters, &openapi3.ParameterRef{
Value: openapi3.NewHeaderParameter(header),
})
type OpenAPIParam struct {
Required bool
Example string
Type string // "query", "header", "cookie"
}

// Param registers a parameter for the route.
// The paramType can be "query", "header" or "cookie".
// [Cookie], [Header], [QueryParam] are shortcuts for Param.
func (r Route[ResponseBody, RequestBody]) Param(paramType, name, description string, params ...OpenAPIParam) Route[ResponseBody, RequestBody] {
openapiParam := openapi3.NewHeaderParameter(name)
openapiParam.Description = description
openapiParam.Schema = openapi3.NewStringSchema().NewRef()
openapiParam.In = paramType

for _, param := range params {
openapiParam.Required = param.Required
openapiParam.Example = param.Example
openapiParam.In = param.Type
}

r.operation.AddParameter(openapiParam)

return r
}

// Header registers a header parameter for the route.
func (r Route[ResponseBody, RequestBody]) Header(name, description string, params ...OpenAPIParam) Route[ResponseBody, RequestBody] {
r.Param("header", name, description, params...)
return r
}

func (r Route[ResponseBody, RequestBody]) AddCookie(cookie string) Route[ResponseBody, RequestBody] {
r.operation.Parameters = append(r.operation.Parameters, &openapi3.ParameterRef{
Value: openapi3.NewCookieParameter(cookie),
})
// Cookie registers a cookie parameter for the route.
func (r Route[ResponseBody, RequestBody]) Cookie(name, description string, params ...OpenAPIParam) Route[ResponseBody, RequestBody] {
r.Param("cookie", name, description, params...)
return r
}

// QueryParam registers a query parameter for the route.
func (r Route[ResponseBody, RequestBody]) QueryParam(name, description string, params ...OpenAPIParam) Route[ResponseBody, RequestBody] {
r.Param("query", name, description, params...)
return r
}

Expand All @@ -155,14 +183,6 @@ func (r Route[ResponseBody, RequestBody]) SetDeprecated() Route[ResponseBody, Re
return r
}

func (r Route[ResponseBody, RequestBody]) WithQueryParam(name, description string) Route[ResponseBody, RequestBody] {
parameter := openapi3.NewQueryParameter(name)
parameter.Description = description
parameter.Schema = openapi3.NewStringSchema().NewRef()
r.operation.AddParameter(parameter)
return r
}

func UseStd(s *Server, middlewares ...func(http.Handler) http.Handler) {
Use(s, middlewares...)
}
Expand Down
2 changes: 1 addition & 1 deletion mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestWithQueryParams(t *testing.T) {
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
WithQueryParam("my-param", "my description")
QueryParam("my-param", "my description")

require.Equal(t, "my description", route.operation.Parameters.GetByInAndName("query", "my-param").Description)
}
Expand Down

0 comments on commit 4b73489

Please sign in to comment.