package
0.24.0
Repository: https://github.com/lushdigital/core.git
Documentation: pkg.go.dev

# README

REST

The response package provides ways to create HTTP + JSON responses in a consistent format. Below are usage example which demonstrate it's use.

Respond without data

package handlers

import (
	"net/http"
	
	"github.com/LUSHDigital/core/pagination"
	"github.com/LUSHDigital/core/rest"
)

func someHandler(w http.ResponseWriter, r *http.Request) {
    resp := &rest.Response{
        Code:    http.StatusOK,
        Message: "some helpful message",
    }
    resp.WriteTo(w)
}

Output

{
  "code": 200,
  "message": "some helpful message"
}

Respond with data

package handlers

import (
	"net/http"
	
	"github.com/LUSHDigital/core/pagination"
	"github.com/LUSHDigital/core/rest"
)

func someHandler(w http.ResponseWriter, r *http.Request) {
    someData := map[string]interface{}{
        "hello": "world",
    }
    
    resp := &rest.Response{
        Code:    http.StatusOK,
        Message: "some helpful message",
        Data: &rest.Data{
            Type:    "something",
            Content: someData,
        },
    }
    resp.WriteTo(w)
}

Output

{
  "code": 200,
  "message": "some helpful message",
  "data": {
    "something": {
      "hello": "world"
    }
  }
}

Respond with data and pagination

package handlers

import (
	"net/http"
	
	"github.com/LUSHDigital/core/pagination"
	"github.com/LUSHDigital/core/rest"
)

func someHandler(w http.ResponseWriter, r *http.Request) {
    someData := map[string]interface{}{
        "hello": "world",
    }
    
    preq := pagination.Request{
        PerPage: 10,
        Page:    1,
    }
    paginator := pagination.MakeResponse(preq, 100)
    resp := &rest.Response{
        Code:    http.StatusOK,
        Message: "some helpful message",
        Data: &rest.Data{
            Type:    "something",
            Content: someData,
        },
        Pagination: &paginator,
    }
    resp.WriteTo(w)
}

Output

{
  "code": 200,
  "message": "some helpful message",
  "data": {
    "something": {
      "hello": "world"
    }
  },
  "pagination": {
    "per_page": 10,
    "page": 1,
    "offset": 0,
    "total": 100,
    "last_page": 10
  }
}

# Functions

BadRequestError returns a prepared 400 Bad Request Server Error, including the error message in the message field of the response object.
ConflictErr returns a prepared 409 Conflict response, including the message passed by the user in the message field of the response object.
ConflictError returns a prepared 409 Conflict response, including the message passed by the user in the message field of the response object.
CreatedResponse returns a prepared 201 Created response.
DBError returns a prepared 500 Internal Server Error response.
DBErrorf returns a prepared 500 Internal Server Error response, using the user provided formatted message.
Errorf returns a prepared error response using the provided code and formatted message.
InternalError returns a prepared 500 Internal Server Error, including the error message in the message field of the response object.
JSONError returns a prepared 422 Unprocessable Entity response if the JSON is found to contain syntax errors, or invalid values for types.
NoContentResponse returns a prepared 204 No Content response.
NotFoundErr returns a prepared 404 Not Found response, including the message passed by the user in the message field of the response object.
NotFoundError returns a prepared 404 Not Found response, including the message passed by the user in the message field of the response object.
OKResponse returns a prepared 200 OK response.
ParamError returns a prepared 422 Unprocessable Entity response, including the name of the failing parameter in the message field of the response object.
ParameterError returns a prepared 422 Unprocessable Entity response, including the name of the failing parameter in the message field of the response object.
Unauthorized returns a prepared 401 Unauthorized error.
UnauthorizedError returns a prepared 401 Unauthorized error.
UnmarshalJSONResponse will unmarshal the data from legacy response envelope.
ValidationError returns a prepared 422 Unprocessable Entity response, including the name of the failing validation/validator in the message field of the response object.
WriteTo writes any JSON response to a HTTP writer.

# Structs

Data represents the collection data the the response will return to the consumer.
EmptyResponse is used to send no content to the API consumer.
Response defines a JSON response body over HTTP.

# Interfaces

Responder defines the behaviour of a response for JSON over HTTP.