api

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VersionV1 = "v1" // v1 is the default version
	VersionV2 = "v2"
	VersionV3 = "v3"
	VersionV4 = "v4"
	VersionV5 = "v5"
	VersionV6 = "v6"
	VersionV7 = "v7"
	VersionV8 = "v8"
	VersionV9 = "v9"
)

Variables

This section is empty.

Functions

func WithAPIs

func WithAPIs(apis ...Spec) resourceOption

WithAPIs configures the API endpoints for the resource. It accepts a variadic list of Spec objects that define the available APIs.

func WithVersion

func WithVersion(version string) resourceOption

WithVersion sets the version for the resource. This option allows overriding the default v1 version with a custom version string.

Types

type Definition

type Definition struct {
	Identifier

	// EnableAudit indicates whether to enable audit logging for this endpoint
	EnableAudit bool
	// Timeout is the request timeout duration
	Timeout time.Duration
	// Public indicates whether this endpoint is publicly accessible
	Public bool
	// PermToken is the permission token required for access
	PermToken string
	// Limit represents the rate limit for an API endpoint
	Limit RateLimit
	// Handler is the actual handler function for this endpoint
	Handler fiber.Handler
}

Definition represents a complete API definition with identifier, configuration, and handler.

func (*Definition) GetTimeout

func (d *Definition) GetTimeout() time.Duration

GetTimeout returns the configured timeout or a default value.

func (*Definition) HasRateLimit

func (d *Definition) HasRateLimit() bool

HasRateLimit returns true if the endpoint has a rate limit configured.

func (*Definition) IsPublic

func (d *Definition) IsPublic() bool

IsPublic returns true if the endpoint is publicly accessible.

func (*Definition) RequiresPermission

func (d *Definition) RequiresPermission() bool

RequiresPermission returns true if the endpoint requires a permission token.

type HandlerParamResolver

type HandlerParamResolver interface {
	Type() reflect.Type
	Resolve(ctx fiber.Ctx) (reflect.Value, error)
}

HandlerParamResolver declares a pluggable strategy to resolve a single handler parameter from the current request context. Implementations should be pure and fast, as they are invoked for every handler call.

Contract:

  • Type() must return the exact parameter type this resolver handles.
  • Resolve(ctx) returns the concrete value for that type (or nil if unavailable). Returning nil signals "not resolvable" for the current request.

Extensibility:

  • Multiple resolvers can be registered. When types overlap, user-provided resolvers override built-in ones at composition time.
  • Prefer inexpensive lookups (e.g., values cached in context) to avoid per-request overhead.

type Identifier

type Identifier struct {
	// The version of the API endpoint
	Version string `json:"version"  form:"version"  validate:"required,alphanum" label_i18n:"api_request_version"`
	// The resource name of the API endpoint
	Resource string `json:"resource" form:"resource" validate:"required,ascii"    label_i18n:"api_request_resource"`
	// The action name of the API endpoint
	Action string `json:"action"   form:"action"   validate:"required,alphanum" label_i18n:"api_request_action"`
}

Identifier uniquely identifies an API endpoint. It consists of version, resource name, and action name.

func (Identifier) Equals

func (id Identifier) Equals(other Identifier) bool

Equals checks if two identifiers are equal.

func (Identifier) IsValid

func (id Identifier) IsValid() bool

IsValid checks if the identifier has all required fields.

func (Identifier) String

func (id Identifier) String() string

String returns a string representation of the identifier.

type In

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

In is a struct that can be used to inject parameters into an API handler.

type Manager

type Manager interface {
	// Register adds a new API definition to the manager.
	Register(api *Definition)
	// Remove removes an API definition by its identifier.
	Remove(id Identifier)
	// Lookup retrieves an API definition by its identifier.
	// Returns nil if the definition is not found.
	Lookup(id Identifier) *Definition
	// List returns all registered API definitions.
	List() []*Definition
}

Manager defines the interface for managing API definitions. It provides methods to register, remove, and lookup API definitions by their identifiers.

type Meta

type Meta map[string]any

type Params

type Params map[string]any

type Provider

type Provider interface {
	// Provide returns an API specification.
	Provide() Spec
}

Provider defines the interface for providing API specifications. It provides a method to generate or retrieve API specifications.

type RateLimit

type RateLimit struct {
	// Max is the rate limit per time window (0 means no limit)
	Max int
	// Expiration is the rate limit expiration time
	Expiration time.Duration
}

RateLimit represents the rate limit for an API endpoint.

type Request

type Request struct {
	Identifier

	// The params of the request
	Params Params `json:"params"`
	// The meta of the request
	Meta Meta `json:"meta"`
}

Request represents an API request with identifier, params, and metadata.

func (*Request) GetMeta

func (r *Request) GetMeta(key string) (any, bool)

GetMeta retrieves a value from the request metadata by key.

func (*Request) GetParam

func (r *Request) GetParam(key string) (any, bool)

GetParam retrieves a value from the request param by key.

type Resource

type Resource interface {
	// Version returns the version of the resource.
	Version() string
	// Name returns the name of the resource.
	Name() string
	// APIs returns the list of API specifications for this resource.
	APIs() []Spec
}

Resource represents an API resource that contains multiple API specifications. It defines the version, name, and list of API specifications for a resource.

func NewResource

func NewResource(name string, opts ...resourceOption) Resource

NewResource creates a new resource with the given name and optional configuration. It initializes the resource with version v1 by default and applies any provided options.

type Spec

type Spec struct {
	// Action is the action name for the API endpoint
	Action string
	// Version is the version of the API endpoint
	Version string
	// EnableAudit indicates whether to enable audit logging for this endpoint
	EnableAudit bool
	// Timeout is the request timeout duration
	Timeout time.Duration
	// Public indicates whether this endpoint is publicly accessible
	Public bool
	// PermToken is the permission token required for access
	PermToken string
	// Limit represents the rate limit for an API endpoint
	Limit RateLimit
	// Handler is optional. If not provided, the system will automatically search for a method
	// in the struct using the Action name converted to PascalCase format.
	// The search supports both direct methods and methods from embedded anonymous structs.
	// For example, if Action is "create_user", the system will look for "CreateUser" method.
	// The handler function should be compatible with the API framework's handler signature.
	Handler any
}

Spec defines the specification for an API endpoint.

Jump to

Keyboard shortcuts

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