-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
126 lines (106 loc) · 4.69 KB
/
types.go
File metadata and controls
126 lines (106 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package fiberoapi
import (
"reflect"
"github.com/gofiber/fiber/v2"
)
// OApiRouter interface that both OApiApp and OApiGroup implement
type OApiRouter interface {
GetApp() *OApiApp
GetPrefix() string
}
// OApiApp wraps fiber.App with OpenAPI capabilities
type OApiApp struct {
f *fiber.App
operations []OpenAPIOperation
config Config
}
// Implement OApiRouter interface for OApiApp
func (o *OApiApp) GetApp() *OApiApp {
return o
}
func (o *OApiApp) GetPrefix() string {
return ""
}
// Config returns the current configuration
func (o *OApiApp) Config() Config {
return o.config
}
// Use adds middleware to the OApiApp
func (o *OApiApp) Use(middleware fiber.Handler) {
o.f.Use(middleware)
}
// Listen starts the server on the given address
func (o *OApiApp) Listen(addr string) error {
return o.f.Listen(addr)
}
// HandlerFunc represents a handler function with typed input and output
type HandlerFunc[TInput any, TOutput any, TError any] func(c *fiber.Ctx, input TInput) (TOutput, TError)
// PathInfo represents information about a path parameter
type PathInfo struct {
Name string
IsPath bool
Index int // Position in the path for validation
}
// ValidationErrorHandler is a function type for handling validation errors
// It receives the fiber context and the validation error, and returns a fiber error response
type ValidationErrorHandler func(c *fiber.Ctx, err error) error
// AuthErrorHandler is a function type for handling authentication/authorization errors
// It receives the fiber context and the AuthError, and returns a fiber error response
type AuthErrorHandler func(c *fiber.Ctx, err *AuthError) error
// Config represents configuration for the OApi wrapper
type Config struct {
EnableValidation bool // Enable request validation (default: true)
EnableOpenAPIDocs bool // Enable automatic docs setup (default: true)
EnableAuthorization bool // Enable authorization validation (default: false)
OpenAPIDocsPath string // Path for documentation UI (default: "/docs")
OpenAPIJSONPath string // Path for OpenAPI JSON spec (default: "/openapi.json")
OpenAPIYamlPath string // Path for OpenAPI YAML spec (default: "/openapi.yaml")
AuthService AuthorizationService // Service for handling authentication and authorization
SecuritySchemes map[string]SecurityScheme // OpenAPI security schemes
DefaultSecurity []map[string][]string // Default security requirements
ValidationErrorHandler ValidationErrorHandler // Custom handler for validation errors
AuthErrorHandler AuthErrorHandler // Custom handler for auth errors (401/403/5xx)
}
// OpenAPIOptions represents options for OpenAPI operations
type OpenAPIOptions struct {
OperationID string `json:"operationId,omitempty"`
Tags []string `json:"tags,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Parameters []map[string]any `json:"parameters,omitempty"`
Security any `json:"security,omitempty"` // Can be []map[string][]string or "disabled"
RequiredRoles []string `json:"-"` // Roles required to access this route (OR semantics by default)
RequireAllRoles bool `json:"-"` // If true, all RequiredRoles must match (AND semantics)
RequiredPermissions []string `json:"-"` // Ex: ["document:read", "workspace:admin"]
ResourceType string `json:"-"` // Type de ressource concernée
}
// OpenAPIOperation represents a registered operation
type OpenAPIOperation struct {
Method string
Path string
Options OpenAPIOptions
InputType reflect.Type
OutputType reflect.Type
ErrorType reflect.Type
}
type OpenAPIParameter struct {
Name string `json:"name"`
In string `json:"in"` // "path", "query", "header", "cookie"
Required bool `json:"required,omitempty"`
Description string `json:"description,omitempty"`
Schema map[string]any `json:"schema"`
}
type OpenAPIResponse struct {
Description string `json:"description"`
Content map[string]any `json:"content,omitempty"`
}
type OpenAPIRequestBody struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Content map[string]any `json:"content"`
}
type ErrorResponse struct {
Code int `json:"code"`
Details string `json:"details"`
Type string `json:"type"`
}