uapi

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Defines a standard way to define routes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileValidationErrors

func CompileValidationErrors(payload any) map[string]string

func SetupState

func SetupState(s UAPIState)

Types

type APIRouter

type APIRouter interface {
	Routes(r *chi.Mux)
	Tag() (string, string)
}

type AuthData

type AuthData struct {
	TargetType string         `json:"target_type"`
	ID         string         `json:"id"`
	Authorized bool           `json:"authorized"`
	Banned     bool           `json:"banned"`
	Data       map[string]any `json:"data"`
}

type AuthType

type AuthType struct {
	URLVar       string
	Type         string
	AllowedScope string
}

type HttpResponse

type HttpResponse struct {
	// Data is the data to be sent to the client
	Data string
	// Optional, can be used in place of Data
	Bytes []byte
	// Json body to be sent to the client
	Json any
	// Headers to set
	Headers map[string]string
	// Status is the HTTP status code to send
	Status int
	// Redirect to a URL
	Redirect string
}

func DefaultResponse

func DefaultResponse(statusCode int) HttpResponse

Creates a default HTTP response based on the status code 200 is treated as 204 No Content

func MarshalReq

func MarshalReq(r *http.Request, dst any) (resp HttpResponse, ok bool)

func MarshalReqWithHeaders

func MarshalReqWithHeaders(r *http.Request, dst any, headers map[string]string) (resp HttpResponse, ok bool)

func ValidatorErrorResponse

func ValidatorErrorResponse(compiled map[string]string, v validator.ValidationErrors) HttpResponse

type Method

type Method int
const (
	GET Method = iota
	POST
	PATCH
	PUT
	DELETE
	HEAD
)

func (Method) String

func (m Method) String() string

type Route

type Route struct {
	Method       Method
	Pattern      string
	Aliases      map[string]string // Aliases for the route, this is useful for e.g. URL rewrites, format is pattern: reason
	OpId         string
	Handler      func(d RouteData, r *http.Request) HttpResponse
	Setup        func()
	Docs         func() *docs.Doc
	Auth         []AuthType
	ExtData      map[string]any
	AuthOptional bool
	SanityCheck  func() error

	// Disables sanity check that ensures all variables are followed by a /
	//
	// e.g. /{foo}s/
	DisablePathSlashCheck bool
}

Represents a route on the API

func (Route) Route

func (r Route) Route(ro Router)

func (Route) String

func (r Route) String() string

type RouteData

type RouteData struct {
	Context context.Context
	Auth    AuthData
	Props   map[string]string // Stores additional properties
}

type Router

type Router interface {
	Get(pattern string, h http.HandlerFunc)
	Post(pattern string, h http.HandlerFunc)
	Patch(pattern string, h http.HandlerFunc)
	Put(pattern string, h http.HandlerFunc)
	Delete(pattern string, h http.HandlerFunc)
	Head(pattern string, h http.HandlerFunc)
}

type UAPIConstants

type UAPIConstants struct {
	ResourceNotFound    string
	BadRequest          string
	Forbidden           string
	Unauthorized        string
	InternalServerError string
	MethodNotAllowed    string
	BodyRequired        string
}

type UAPIDefaultResponder

type UAPIDefaultResponder interface {
	New(msg string, ctx map[string]string) any
}

type UAPIInitData

type UAPIInitData struct {
	Tag string
}

type UAPIState

type UAPIState struct {
	Logger              *zap.Logger
	Authorize           func(r Route, req *http.Request) (AuthData, HttpResponse, bool)
	AuthTypeMap         map[string]string // E.g. bot => Bot, user => User etc.
	RouteDataMiddleware func(rd *RouteData, req *http.Request) (*RouteData, error)
	BaseSanityCheck     func(r Route) error
	PatchDocs           func(d *docs.Doc) *docs.Doc
	Context             context.Context
	Constants           *UAPIConstants
	DefaultResponder    UAPIDefaultResponder
	InitData            UAPIInitData
}

* * UAPIState is the state of the UAPI plugin. * It is used to store the logger, authorize function, and other data.

var (
	State *UAPIState
)

func (*UAPIState) SetCurrentTag added in v0.2.0

func (s *UAPIState) SetCurrentTag(tag string)

Jump to

Keyboard shortcuts

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