rest

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 10 Imported by: 0

README


Rest Logo

GitHub Release GitHub License

Tired of having to do everything by hand every time you start a new go project with the chi router?

This set of pre-ready functions will help you save time and make you more productive.


Binding
Request body bind and validation with Validator.

import "github.com/ogabrielrodrigues/rest"

type Body struct {
  Name string `json:"name" validate:"required,min=2"`
  Age int `json:"age" validate:"required,min=1,max=140"`
}

func ExampleHandler(w http.ResponseWriter, r *http.Request) {
  body := Body{}

  if err := rest.Bind(r.Body, &body); err != nil {
    rest.JSON(w, err.Code, err)
    return
  }

  // Rest of your handler logic...
}

Error handling
Better handling of major http status errors.

import "github.com/ogabrielrodrigues/rest"

func ErrorHandler(w http.ResponseWriter, r *http.Request) {
  rest_error := rest.NewInternalServerErr(
    "this error occurred because the logic is not ready",
  )

  rest.JSON(w, rest_error.Code, rest_error)
}

// Output in JSON response:
{
  "message": "this error occurred because the logic is not ready",
  "code": 500,
  "error": "internal_server_error"
}

Response
Simple way to return data with ResponseWriter.

import (
  "net/http"
  "github.com/ogabrielrodrigues/rest"
)

func ResponseJSONHandler(w http.ResponseWriter, r *http.Request) {
  response := map[string]string{
    "message": "Thank you if you are reading this docs!!",
  }

  rest.JSON(w, http.StatusOK, response)
}

// Output in JSON response:
{
  "message": "Thank you if you are reading this docs!!",
}

Or prefer not to return data:

import (
  "net/http"
  "github.com/ogabrielrodrigues/rest"
)

func OnlyStatusHandler(w http.ResponseWriter, r *http.Request) {
  rest.End(w, http.StatusOK)
}

// Output in response headers:
HTTP/1.1 200 OK

Validation
Validate your request body or URL params with Validator.

import "github.com/ogabrielrodrigues/rest"

type Body struct {
  Name string `json:"name" validate:"required,min=2"`
  Age int `json:"age" validate:"required,min=1,max=140"`
}

func StructValidationHandler(w http.ResponseWriter, r *http.Request) {
  body := Body{}

  if err := rest.Validate.Struct(&body); err != nil {
    rest_error := rest.ValidateStructErr(err)
    rest.JSON(w, rest_error.Code, rest_error)
    return
  }

  // Rest of your handler logic...
}

Or validate one URL param:

import (
  "github.com/go-chi/chi/v5"
  "github.com/ogabrielrodrigues/rest"
)

func VarValidationHandler(w http.ResponseWriter, r *http.Request) {
  id := chi.URLParam(r, "id")

  if err := rest.Validate.Var(id, "uuid4"); err != nil {
    rest_error := rest.ValidateVarErr(err)
    rest.JSON(w, rest_error.Code, rest_error)
    return
  }

  // Rest of your handler logic...
}

Make with ❤️ by ogabrielrodrigues

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Validate = validator.New()
)

Functions

func End

func End(w http.ResponseWriter, code int)

Write http status code and return empty

func JSON

func JSON(w http.ResponseWriter, code int, data interface{})

Write http status code and return data in JSON format

Types

type Cause

type Cause struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

Describes the cause of the error occurrence.

type Data added in v1.0.4

type Data map[string]interface{}

Data is one shortcut to map[string]interface{}

type Err

type Err struct {
	Message string  `json:"message"`
	Code    int     `json:"code"`
	Err     string  `json:"error"`
	Causes  []Cause `json:"causes,omitempty"`
}

Default rest error type.

func Bind

func Bind(src io.ReadCloser, dst interface{}) *Err

Bind the src JSON request to dst struct. Returns a rest.Err if an error occurs in the binding process

func NewBadRequestErr

func NewBadRequestErr(message string, causes []Cause) *Err

Creates a new Bad Request Error.

func NewErr

func NewErr(message, err string, code int, causes []Cause) *Err

Creates a new rest.Err.

func NewForbiddenErr

func NewForbiddenErr(message string) *Err

Creates a new Forbidden Error.

func NewInternalServerErr

func NewInternalServerErr(message string) *Err

Creates a new Internal Server Error.

func NewNotFoundErr

func NewNotFoundErr(message string) *Err

Creates a new Not Found Error.

func NewUnauthorizedErr

func NewUnauthorizedErr() *Err

Creates a new Unauthorized Error.

func ValidateStructErr

func ValidateStructErr(err error) *Err

Validate one struct error. Returns rest.Err with the cause of the error occurrence.

func ValidateVarErr

func ValidateVarErr(name string, err error) *Err

Validate one variable error. Returns rest.Err with the cause of the error occurrence.

func (*Err) Error

func (r *Err) Error() string

Returns error message on string format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL