Categorygithub.com/dnsge/gin-standard-api
modulepackage
0.4.0
Repository: https://github.com/dnsge/gin-standard-api.git
Documentation: pkg.go.dev

# README

gin-standard-api

gin-standard-api is a utility library for standardizing API responses and error handling in Gin applications. The API structure is as follows:

Successful Request (2xx)

{
  "status": 200, // HTTP status code, required
  "data": ... // Body data, optional (any JSON value)
}

Unsuccessful Response (4xx/5xx)

{
  "status": 403, // HTTP status code, required
  "error": "Forbidden", // Generic error message, required
  "message": "You do not have permission to do that." // Detailed error message, optional
}

Internal API

Instead of using a regular gin.HandlerFunc, you can instead use an sapi.Handler. The difference is that an sapi.Handler returns (sapi.Res, sapi.Err), indicating either a successful response or an error response.

Handler Example:

package web

import (
	"github.com/dnsge/gin-standard-api"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func HandleCurrentTime(c *gin.Context) (sapi.Res, sapi.Err) {
	now := time.Now()
	if now.Second() == 0 {
		// By returning a sapi.Error instance, we make control flow more explicit.
		return nil, sapi.ErrorStatus(http.StatusServiceUnavailable)
	}
	
	return sapi.Data(http.StatusOK, now.Unix()), nil
}

func Register(r gin.IRoutes) {
	r.GET("/now", sapi.Handle(HandleCurrentTime))
}

Should you need to write a response outside a sapi.Handler, use the API like so:

package web

import (
	"github.com/dnsge/gin-standard-api"
	"github.com/gin-gonic/gin"
	"net/http"
)

func DoSomething(c *gin.Context) {
    sapi.Status(http.StatusNotFound).WriteResponse(c)
}

List of Methods

  • RawStatus(status int) Res
    • Represents a response with an empty body, i.e. Content-Length: 0
  • Status(status int) Res
    • Represents a response with just a status and no data.
  • Data(status int, data any) Res
    • Represents a response with a status and some data.
  • String(status int, text string) Res
    • Represents a response with a plain string body. No JSON, just plain text.
  • Redirect(status int, location string) Res
    • Represents a redirect response to some location.
  • RawErrorStatus(status int) Err
    • Error equivalent for RawStatus
  • ErrorStatus(status int) Err
    • Represents an error with just a status.
  • Error(status int, error string) Err
    • Represents an error with a status and a message.
  • ErrorMessage(status int, error string, message string) Err
    • Represents an error with a status, message, and detailed message.
  • StringError(status int, text string) Err
    • Error equivalent for String

# Packages

No description provided by the author

# Functions

BadRequest is shorthand for ErrorStatus(http.StatusBadRequest).
Conflict is shorthand for ErrorStatus(http.StatusConflict).
Data writes the given status along with the standard API response body containing the status and the data.
Error writes the given status along with the standard API response body containing the given error text.
ErrorMessage writes the given status along with the standard API response body containing the given error text and message.
ErrorStatus writes the given status along with the standard API response body, including the status text for the given status code (as defined by http.StatusText).
Forbidden is shorthand for ErrorStatus(http.StatusForbidden).
Gone is shorthand for ErrorStatus(http.StatusGone).
Handle wraps a Handler into a gin.HandlerFunc instance.
HandleWithInspection wraps a Handler into a gin.HandlerFunc instance.
InternalServerError is shorthand for ErrorStatus(http.StatusInternalServerError).
NotFound is shorthand for ErrorStatus(http.StatusNotFound).
RawErrorStatus writes the given status with no JSON body.
RawStatus writes the given status with no JSON body.
Redirect writes the given status code and sets the Location header.
Status writes the given status along with the standard API response body.
String writes the given status and a *plain* string response.
StringError writes the given status and a *plain* string response.
Unauthorized is shorthand for ErrorStatus(http.StatusUnauthorized).

# Variables

AlreadyHandled is a sentinel Res instance that indicates to Handle that the called handler has already written a response and that no interpretation of the Res or Err is necessary.

# Interfaces

Err represents a response to an unsuccessful request.
Res represents a response to a successful request.

# Type aliases

No description provided by the author
No description provided by the author
Handler is a gin.HandlerFunc which returns a Res or an Err.