api

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2025 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrResolveParamType indicates failing to resolve handler parameter type.
	ErrResolveParamType = errors.New("failed to resolve api handler parameter type")
	// ErrProvidedHandlerNil indicates provided handler is nil.
	ErrProvidedHandlerNil = errors.New("provided handler cannot be nil")
	// ErrProvidedHandlerMustFunc indicates provided handler must be a function.
	ErrProvidedHandlerMustFunc = errors.New("provided handler must be a function")
	// ErrProvidedHandlerFuncNil indicates provided handler function is nil.
	ErrProvidedHandlerFuncNil = errors.New("provided handler function cannot be nil")
	// ErrHandlerFactoryRequireDB indicates handler factory requires db.
	ErrHandlerFactoryRequireDB = errors.New("handler factory function requires database connection but none provided")
	// ErrHandlerFactoryMethodRequireDB indicates handler factory method requires db.
	ErrHandlerFactoryMethodRequireDB = errors.New("handler factory method requires database connection but none provided")
	// ErrApiMethodNotFound indicates api action method not found.
	ErrApiMethodNotFound = errors.New("api action method not found in resource")
	// ErrHandlerMethodInvalidReturn indicates handler method invalid return type.
	ErrHandlerMethodInvalidReturn = errors.New("handler method has invalid return type, must be 'error'")
	// ErrHandlerMethodTooManyReturns indicates handler method has too many returns.
	ErrHandlerMethodTooManyReturns = errors.New("handler method has too many return values, must have at most 1 (error) or none")
	// ErrHandlerFactoryInvalidReturn indicates handler factory invalid return count.
	ErrHandlerFactoryInvalidReturn = errors.New("handler factory method should return 1 or 2 values")
)
View Source
var Module = fx.Module(
	"vef:api",
	fx.Provide(
		fx.Annotate(
			NewHandlerParamResolverManager,
			fx.ParamTags(`group:"vef:api:param_resolvers"`),
		),
		fx.Private,
	),
	fx.Provide(

		fx.Annotate(
			NewManager,
			fx.ParamTags(`group:"vef:api:resources"`, `optional:"true"`),
			fx.ResultTags(`name:"vef:api:manager"`),
		),
		fx.Annotate(
			NewManager,
			fx.ParamTags(`group:"vef:openapi:resources"`, `optional:"true"`),
			fx.ResultTags(`name:"vef:openapi:manager"`),
		),

		fx.Annotate(
			NewDefaultApiPolicy,
			fx.ResultTags(`name:"vef:api:policy"`),
		),
		fx.Annotate(
			NewOpenApiPolicy,
			fx.ResultTags(`name:"vef:openapi:policy"`),
		),

		fx.Annotate(
			NewEngine,
			fx.ParamTags(
				`name:"vef:api:manager"`,
				`name:"vef:api:policy"`,
				`optional:"true"`,
				`optional:"true"`,
				``,
				``,
				``,
			),
			fx.ResultTags(`name:"vef:api:engine"`),
		),
		fx.Annotate(
			NewEngine,
			fx.ParamTags(
				`name:"vef:openapi:manager"`,
				`name:"vef:openapi:policy"`,
				`optional:"true"`,
				`optional:"true"`,
				``,
				``,
				``,
			),
			fx.ResultTags(`name:"vef:openapi:engine"`),
		),
	),
)

Functions

func NewManager

func NewManager(resources []apiPkg.Resource, db orm.Db, paramResolver *HandlerParamResolverManager) (apiPkg.Manager, error)

NewManager creates a new Api manager and registers all provided resources. Returns an error if any API registration fails, including duplicate definitions.

Types

type CtxParamResolver

type CtxParamResolver struct{}

func (*CtxParamResolver) Resolve

func (*CtxParamResolver) Resolve(ctx fiber.Ctx) (reflect.Value, error)

func (*CtxParamResolver) Type

func (*CtxParamResolver) Type() reflect.Type

type DbParamResolver

type DbParamResolver struct{}

func (*DbParamResolver) Resolve

func (*DbParamResolver) Resolve(ctx fiber.Ctx) (reflect.Value, error)

func (*DbParamResolver) Type

func (*DbParamResolver) Type() reflect.Type

type DefaultEngine

type DefaultEngine struct {
	// contains filtered or unexported fields
}

func (*DefaultEngine) Connect

func (e *DefaultEngine) Connect(router fiber.Router)

Connect registers the Api engine with the given router.

type DuplicateApiError added in v0.8.0

type DuplicateApiError struct {
	Identifier api.Identifier
	Existing   *api.Definition
	New        *api.Definition
}

DuplicateApiError represents an error when attempting to register a duplicate Api definition. It contains information about both the existing and new Api definitions.

func (*DuplicateApiError) Error added in v0.8.0

func (e *DuplicateApiError) Error() string

Error returns a formatted error message with details about the duplicate Api.

type Engine

type Engine interface {
	// Connect registers the Api engine with the given router.
	Connect(router fiber.Router)
}

Engine defines the interface for Api engines that can connect to a router. It provides the ability to register Api endpoints with a Fiber router.

func NewEngine

func NewEngine(
	manager api.Manager,
	policy Policy,
	checker security.PermissionChecker,
	resolver security.DataPermissionResolver,
	db orm.Db,
	transformer mold.Transformer,
	publisher event.Publisher,
) Engine

NewEngine creates an Engine with the given policy.

type HandlerParamResolverManager

type HandlerParamResolverManager struct {
	// contains filtered or unexported fields
}

HandlerParamResolverManager aggregates all handler parameter resolvers. Resolution uses exact type matching. If needed in the future, assignable/interface matching (e.g., Implements/AssignableTo) can be added without changing the public Api.

func NewHandlerParamResolverManager

func NewHandlerParamResolverManager(userResolvers []api.HandlerParamResolver) *HandlerParamResolverManager

NewHandlerParamResolverManager builds a composite resolver by merging preset and user-provided resolvers. If the same type is registered multiple times, the last one wins (user-provided overrides preset). This constructor is intended to be used by the DI layer to assemble resolvers from groups.

func (*HandlerParamResolverManager) Resolve

func (m *HandlerParamResolverManager) Resolve(target reflect.Value, paramType reflect.Type) (ParamResolverFunc, error)

Resolve looks up a resolver function for the given parameter type. It does not perform the actual value conversion; callers should invoke the returned function with a request context to obtain the value. Returns (resolver, nil) on success, or (nil, error) when no resolver is found.

type LoggerParamResolver

type LoggerParamResolver struct{}

func (*LoggerParamResolver) Resolve

func (*LoggerParamResolver) Resolve(ctx fiber.Ctx) (reflect.Value, error)

func (*LoggerParamResolver) Type

type ParamResolverFunc

type ParamResolverFunc func(ctx fiber.Ctx) (reflect.Value, error)

ParamResolverFunc resolves a value from the current request context.

type Policy

type Policy interface {
	// Path returns the mount path for this Api kind, e.g. "/api" or "/openapi".
	Path() string
	// BuildAuthenticationMiddleware returns the authentication middleware for this Api kind.
	BuildAuthenticationMiddleware(manager api.Manager) fiber.Handler
}

Policy defines how a specific Api kind should behave. It encapsulates authentication, permission, limiter and path strategy.

func NewDefaultApiPolicy

func NewDefaultApiPolicy(auth security.AuthManager) Policy

func NewOpenApiPolicy

func NewOpenApiPolicy(auth security.AuthManager) Policy

type PrincipalParamResolver

type PrincipalParamResolver struct{}

func (*PrincipalParamResolver) Resolve

func (*PrincipalParamResolver) Type

type ResourceDefinition

type ResourceDefinition interface {
	// Register registers all Api definitions with the given manager.
	// Returns an error if any Api registration fails (e.g., duplicate definitions).
	Register(manager api.Manager) error
}

ResourceDefinition represents a collection of Api definitions that can be registered with a manager.

type TransformerParamResolver

type TransformerParamResolver struct{}

func (*TransformerParamResolver) Resolve

func (*TransformerParamResolver) Type

Jump to

Keyboard shortcuts

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