docs(httputil): correct tier from 3 to 2
httputil depends on xerrors (Tier 0) and valid (Tier 1), placing it at Tier 2. No infrastructure or lifecycle dependencies exist in this module.
This commit is contained in:
59
README.md
Normal file
59
README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# httputil
|
||||
|
||||
Typed HTTP handler adapters and response helpers for stdlib `net/http`.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
go get code.nochebuena.dev/go/httputil
|
||||
```
|
||||
|
||||
## Typed handlers
|
||||
|
||||
```go
|
||||
// JSON body + validation + typed response
|
||||
r.Post("/orders", httputil.Handle(validator, svc.CreateOrder))
|
||||
|
||||
// No request body (GET / DELETE)
|
||||
r.Get("/orders/{id}", httputil.HandleNoBody(svc.GetOrder))
|
||||
|
||||
// Request body, no response body (204)
|
||||
r.Delete("/orders/{id}", httputil.HandleEmpty(validator, svc.DeleteOrder))
|
||||
|
||||
// Manual handler with centralised error mapping
|
||||
r.Get("/raw", httputil.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
data, err := svc.Load(r.Context(), chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return httputil.JSON(w, http.StatusOK, data)
|
||||
}))
|
||||
```
|
||||
|
||||
## Error mapping
|
||||
|
||||
`xerrors.Code` → HTTP status:
|
||||
|
||||
| Code | Status |
|
||||
|---|---|
|
||||
| `ErrInvalidInput` | 400 |
|
||||
| `ErrUnauthorized` | 401 |
|
||||
| `ErrPermissionDenied` | 403 |
|
||||
| `ErrNotFound` | 404 |
|
||||
| `ErrAlreadyExists` | 409 |
|
||||
| `ErrInternal` | 500 |
|
||||
| `ErrNotImplemented` | 501 |
|
||||
| `ErrUnavailable` | 503 |
|
||||
| `ErrDeadlineExceeded` | 504 |
|
||||
| unknown | 500 |
|
||||
|
||||
Error response body:
|
||||
```json
|
||||
{"code": "NOT_FOUND", "message": "record not found"}
|
||||
```
|
||||
Fields from `xerrors.Err` are merged into the top-level response object.
|
||||
|
||||
## Dependencies
|
||||
|
||||
- `code.nochebuena.dev/go/xerrors`
|
||||
- `code.nochebuena.dev/go/valid`
|
||||
Reference in New Issue
Block a user