• v1.0.0 e8adc266a6

    Rene Nochebuena released this 2026-05-12 11:38:44 -06:00 | 0 commits to main since this release

    v1.0.0

    code.nochebuena.dev/go/httputil

    Overview

    httputil removes HTTP boilerplate from business logic. Generic adapter functions
    (Handle, HandleNoBody, HandleEmpty) wrap pure Go functions into
    http.HandlerFunc values, handling JSON decode, struct validation, JSON encode,
    and error-to-HTTP-status mapping automatically.

    v1.0.0 commits the full API as stable and bumps all micro-lib dependencies to their
    v1 releases (xerrors v1.0.1, valid v1.0.1).

    What Changed Since v0.10.0

    • Dependency xerrors bumped from v0.10.0 to v1.0.1
    • Dependency valid bumped from v0.9.0 to v1.0.1
    • Go directive bumped from 1.25 to 1.26
    • API unchanged — all adapters and helpers from v0.10.0 are stable

    What's Included

    Handler adapters (generic):

    • Handle[Req, Res any](v Validator, fn func(ctx context.Context, req Req) (Res, error)) http.HandlerFunc
    • HandleNoBody[Res any](fn func(ctx context.Context) (Res, error)) http.HandlerFunc
    • HandleEmpty[Req any](v Validator, fn func(ctx context.Context, req Req) error) http.HandlerFunc

    Manual handler type:

    • HandlerFunc func(w, r) error — implements http.Handler

    Response helpers:

    • JSON(w, status, v) — encodes v as JSON, sets Content-Type: application/json
    • NoContent(w) — writes 204 No Content
    • Error(w, err) — maps *xerrors.Err to HTTP status; includes platformCode when present

    xerrors.Code → HTTP status mapping (12 codes):

    Code Status
    ErrInvalidInput 400 Bad Request
    ErrUnauthorized 401 Unauthorized
    ErrPermissionDenied 403 Forbidden
    ErrNotFound 404 Not Found
    ErrAlreadyExists 409 Conflict
    ErrGone 410 Gone
    ErrPreconditionFailed 412 Precondition Failed
    ErrRateLimited 429 Too Many Requests
    ErrInternal 500 Internal Server Error
    ErrNotImplemented 501 Not Implemented
    ErrUnavailable 503 Service Unavailable
    ErrDeadlineExceeded 504 Gateway Timeout

    Installation

    go get code.nochebuena.dev/go/httputil@v1.0.0
    

    Changelog

    See CHANGELOG.md.

    Downloads