Documentation
¶
Index ¶
- func Delete[T any](url string, config ...Config) (T, error)
- func Do[T any](url string, config ...Config) (T, error)
- func Get[T any](url string, config ...Config) (T, error)
- func Head[T any](url string, config ...Config) (T, error)
- func Marshal(v any) (string, error)
- func Options[T any](url string, config ...Config) (T, error)
- func Patch[T any](url string, body any, config ...Config) (T, error)
- func Post[T any](url string, body any, config ...Config) (T, error)
- func Put[T any](url string, body any, config ...Config) (T, error)
- func SetBaseURL(b string)
- func SetHandlerConfig(hc HandlerConfig)
- func SetHandlerErrorFormat(format string)
- func SetHttpClient(c *http.Client)
- func ToHandlerFunc[In any, Out any](apply ApplyFunc[In, Out]) http.HandlerFunc
- func ToHandlerFuncEmptyIn[Out any](supply SupplyFunc[Out]) http.HandlerFunc
- func ToHandlerFuncEmptyOut[In any](consume ConsumeFunc[In]) http.HandlerFunc
- func Unmarshal[T any](j string) (T, error)
- func UnmarshalInto(j string, v any) error
- func UnmarshalJ[T any](j J) (T, error)
- type A
- type ApplyFunc
- type B
- type Config
- type ConsumeFunc
- type Empty
- type Error
- type F
- type HandlerConfig
- type J
- type M
- type Nil
- func (n Nil) AsArray() ([]any, bool)
- func (n Nil) AsBoolean() (bool, bool)
- func (n Nil) AsNumber() (float64, bool)
- func (n Nil) AsObject() (map[string]any, bool)
- func (n Nil) AsString() (string, bool)
- func (n Nil) Elem() any
- func (n Nil) IsNil() bool
- func (n Nil) Q(string) J
- func (n Nil) String() string
- type Request
- type RequestEmpty
- type Response
- type ResponseEmpty
- type S
- type SupplyFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal calls the patched json.Marshal function. There are only two patches
- It lowercases the first letter of the public struct fields.
- It omits empty fields by default.
They are only applied if the `json` tag is not specified.
func SetBaseURL ¶
func SetBaseURL(b string)
func SetHandlerConfig ¶ added in v1.0.0
func SetHandlerConfig(hc HandlerConfig)
SetHandlerConfig sets HandlerConfig globally to be applied for every ToHandlerFunc.
func SetHandlerErrorFormat ¶ added in v1.0.0
func SetHandlerErrorFormat(format string)
SetHandlerErrorFormat is a global setter configuring how ToHandlerFunc converts errors returned from ApplyFunc. format argument must contain only one %s verb which would be the error message. Defaults to {"error":"%s"} Examples: fetch.SetHandlerErrorFormat(`{"msg":"%s"}`) fetch.SetHandlerErrorFormat("%s") - just plain error text fetch.SetHandlerErrorFormat(`{"error":{"message":"%s"}}`)
func SetHttpClient ¶
func ToHandlerFunc ¶ added in v0.4.4
func ToHandlerFunc[In any, Out any](apply ApplyFunc[In, Out]) http.HandlerFunc
ToHandlerFunc converts ApplyFunc into http.HandlerFunc, which can be used later in http.ServeMux#HandleFunc. It unmarshals the HTTP request body into the ApplyFunc argument and then marshals the returned value into the HTTP response body. To access HTTP request attributes, wrap your input in fetch.Request.
func ToHandlerFuncEmptyIn ¶ added in v1.0.1
func ToHandlerFuncEmptyIn[Out any](supply SupplyFunc[Out]) http.HandlerFunc
func ToHandlerFuncEmptyOut ¶ added in v1.0.1
func ToHandlerFuncEmptyOut[In any](consume ConsumeFunc[In]) http.HandlerFunc
func UnmarshalInto ¶
UnmarshalInto calls the patched json.Unmarshal function. The only difference between them is it handles `fetch.J` and transforms `any` into fetch.J.
func UnmarshalJ ¶ added in v0.2.2
UnmarshalJ unmarshalls J into the generic value.
Types ¶
type ApplyFunc ¶ added in v0.4.4
ApplyFunc represents a simple function to be converted to http.Handler with In type as a request body and Out type as a response body.
type ConsumeFunc ¶ added in v1.0.1
ConsumeFunc serves as ApplyFunc ignoring HTTP response
type Empty ¶ added in v0.4.4
type Empty struct{}
Empty represents an empty response or request body, skipping JSON handling. Can be used with the wrappers Response and Request or to fit the signature of ApplyFunc.
type Error ¶
type HandlerConfig ¶ added in v0.4.4
type HandlerConfig struct {
// ErrorHook is called if an error happens while sending an HTTP response
ErrorHook func(err error)
// Middleware is applied before ToHandlerFunc processes the request.
// Return true to end the request processing.
Middleware func(w http.ResponseWriter, r *http.Request) bool
}
type J ¶
type J interface {
/*
Q parses jq-like patterns and returns according to the path value.
If the value wasn't found or syntax is incorrect, Q will return Nil.
Examples:
j := fetch.Parse(`{
"name": "Jason",
"category": {
"name":"dogs"
}
"tags": [{"name":"briard"}]
}`)
Print json: fmt.Println(j)
Retrieve name: j.Q(".name")
Retrieve category name: j.Q(".category.name")
Retrieve first tag's name: j.Q(".tags[0].name")
*/
Q(pattern string) J
// String returns JSON formatted string.
String() string
// Elem converts J to its definition and returns it.
// Type-Definitions:
// M -> map[string]any
// A -> []any
// F -> float64
// S -> string
// B -> bool
// Nil -> nil
Elem() any
// AsObject is a convenient type assertion if J is a map[string]any.
AsObject() (map[string]any, bool)
// AsArray is a convenient type assertion if J is a slice of type []any.
AsArray() ([]any, bool)
// AsNumber is a convenient type assertion if J is a float64.
AsNumber() (float64, bool)
// AsString is a convenient type assertion if J is a string.
AsString() (string, bool)
// AsBoolean is a convenient type assertion if J is a bool.
AsBoolean() (bool, bool)
// IsNil check if J is fetch.Nil
IsNil() bool
}
J represents arbitrary JSON. Depending on the JSON data type the queried `fetch.J` could be one of these types
| Type | Go definition | JSON data type | |-----------|-----------------|-------------------------------------| | fetch.M | map[string]any | object | | fetch.A | []any | array | | fetch.F | float64 | number | | fetch.S | string | string | | fetch.B | bool | boolean | | fetch.Nil | (nil) *struct{} | null, undefined, anything not found |
type Nil ¶ added in v0.2.0
type Nil = *nilStruct
Nil represents any not found or null values. It is also used for syntax errors. Nil is pointer which value is always nil. However, when returned from any method, it doesn't equal nil, because a Go interface is not nil when it has a type. It exists to prevent nil pointer dereference when retrieving Elem value. It can be the root of J tree, because null alone is a valid JSON.
type Request ¶
type Request[T any] struct { Context context.Context // Only available in go1.23 and above. // PathValue was introduced in go1.22 but // there was no reliable way to extract them. // go1.23 introduced http.Request.Pattern allowing to list the wildcards. PathValues map[string]string // URL parameters. Parameters map[string]string // HTTP headers. Headers map[string]string Body T }
Request can be used in ApplyFunc as a wrapper for the input entity to access http attributes. e.g.
type Pet struct {
Name string
}
http.HandleFunc("POST /pets/{id}", fetch.ToHandlerFunc(func(in fetch.Request[Pet]) (fetch.Empty, error) {
in.Context()
return fetch.Empty{}, nil
}))
func (Request[T]) WithHeader ¶ added in v1.0.0
func (Request[T]) WithParameter ¶ added in v1.0.0
func (Request[T]) WithPathValue ¶ added in v1.0.0
type RequestEmpty ¶ added in v1.0.2
type Response ¶
Response is a wrapper type for (generic) ReturnType to be used in the HTTP methods. It allows you to access HTTP attributes of the HTTP response and unmarshal the HTTP body. e.g.
type User struct {
FirstName string
}
res, err := Get[Response[User]]("/users/1")
if err != nil {panic(err)}
if res.Status != 202 {
panic("unexpected status")
}
// Body is User type
fmt.Println(res.Body.FirstName)
type ResponseEmpty ¶ added in v0.2.6
type SupplyFunc ¶ added in v1.0.1
SupplyFunc serves as ApplyFunc ignoring HTTP request