utilities

package
v0.0.0-...-f828163 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound         = errors.New("not found")
	ErrDuplicate        = errors.New("duplicate")
	ErrExpectedPageInfo = errors.New("expected page info. make sure to use the fiberpaginate middleware")
)

Functions

func ApplyModifiers

func ApplyModifiers(req *http.Request, modifiers ...RequestModifier) *http.Request

func AtLeastOne

func AtLeastOne[Model any](body Model, model Model) bool

func Client

func Client() *http.Client

func ErrorHandler

func ErrorHandler(c *fiber.Ctx, err error) error

func ExecuteWithTimeout

func ExecuteWithTimeout(ctx context.Context, fn func() error) error

func ExecuteWithTimeoutResult

func ExecuteWithTimeoutResult[Result any](ctx context.Context, fn func() (*Result, error)) (*Result, error)

func Exit

func Exit(format string, a ...interface{})

func FiberMessage

func FiberMessage(c *fiber.Ctx, statusCode int, response string) error

func IntoScope

func IntoScope(p fiberpaginate.PageInfo, db *gorm.DB) func(db *gorm.DB) *gorm.DB

func IsDuplicate

func IsDuplicate(err error) bool

func IsExpectedPageInfo

func IsExpectedPageInfo(err error) bool

func IsNotFound

func IsNotFound(err error) bool

func IsOk

func IsOk(statusCode int) bool

func MapJsonTags

func MapJsonTags[From any, Into any](from From, intoType *Into) (*Into, error)

MapJsonTags maps request data to a target model using mapstructure NOTE: this function does not correctly map time.Time fields from from to time.Time fields in intoType see the following GitHub issues: https://github.com/mitchellh/mapstructure/issues/334 https://github.com/go-viper/mapstructure/issues/20

func MergeMaps

func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V

MergeMaps merges multiple maps into a single map. The keys of maps earlier in the list will take precedence over keys in later maps.

func NormalizeEmail

func NormalizeEmail(email string) string

func ParseTime

func ParseTime(s string, format TimeFormat) (*time.Time, error)

func RegisterCustomValidators

func RegisterCustomValidators() (*validator.Validate, error)

func Request

func Request(method string, url string, body []byte, modifiers ...RequestModifier) (*http.Response, error)

func SplitBearerToken

func SplitBearerToken(accessToken string) (*string, error)

func ToJsonString

func ToJsonString(p interface{}) (string, error)

func ToSnakeCase

func ToSnakeCase(s string) string

func Validate

func Validate(validate *validator.Validate, s any, maybeErrs ...MaybeError) error

func ValidateID

func ValidateID(id string) (*uuid.UUID, error)

Types

type APIError

type APIError struct {
	StatusCode int `json:"statusCode"`
	Message    any `json:"msg"`
}

func BadRequest

func BadRequest(err error) APIError

func Conflict

func Conflict() APIError

func Forbidden

func Forbidden() APIError

func ForbiddenReason

func ForbiddenReason(err error) APIError

func InternalServerError

func InternalServerError() APIError

func InvalidJSON

func InvalidJSON() APIError

func InvalidRequestData

func InvalidRequestData(errors map[string]string) APIError

func NewAPIError

func NewAPIError(statusCode int, err error) APIError

func Unauthorized

func Unauthorized() APIError

func (APIError) Error

func (e APIError) Error() string

type HeaderKVModifier

type HeaderKVModifier struct {
	Key   string
	Value string
}

func (HeaderKVModifier) Modify

func (h HeaderKVModifier) Modify(req *http.Request) *http.Request

type MaybeError

type MaybeError struct {
	Name string
	Err  error
}

func NewMaybeError

func NewMaybeError(name string, err error) *MaybeError

func (*MaybeError) IsError

func (me *MaybeError) IsError() bool

func (*MaybeError) Unwrap

func (me *MaybeError) Unwrap() error

type Platform

type Platform string
const (
	PlatformMobile Platform = "mobile"
	PlatformWeb    Platform = "web"
)

func GetPlatform

func GetPlatform(c *fiber.Ctx) Platform

type RequestModifier

type RequestModifier interface {
	Modify(req *http.Request) *http.Request
}

func AcceptJSON

func AcceptJSON() RequestModifier

func Authorization

func Authorization(value string) RequestModifier

func FormURLEncoded

func FormURLEncoded() RequestModifier

func HeaderKV

func HeaderKV(key, value string) RequestModifier

func JSON

func JSON() RequestModifier

type SuccessResponse

type SuccessResponse struct {
	Message string `json:"message"`
}

For swagger docs:

type TimeFormat

type TimeFormat string
const (
	YYYY_dash_MM_dash_DD TimeFormat = "2006-01-02"
)

Jump to

Keyboard shortcuts

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