github.com/goravel/framework@v1.13.9/contracts/http/request.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/goravel/framework/contracts/filesystem"
     7  	"github.com/goravel/framework/contracts/validation"
     8  )
     9  
    10  //go:generate mockery --name=ContextRequest
    11  type ContextRequest interface {
    12  	// Header retrieves the value of the specified HTTP header by its key.
    13  	// If the header is not found, it returns the optional default value (if provided).
    14  	Header(key string, defaultValue ...string) string
    15  	// Headers return all the HTTP headers of the request.
    16  	Headers() http.Header
    17  	// Method retrieves the HTTP request method (e.g., GET, POST, PUT).
    18  	Method() string
    19  	// Path retrieves the current path information for the request.
    20  	Path() string
    21  	// Url retrieves the URL (excluding the query string) for the request.
    22  	Url() string
    23  	// FullUrl retrieves the full URL, including the query string, for the request.
    24  	FullUrl() string
    25  	// Ip retrieves the client's IP address.
    26  	Ip() string
    27  	// Host retrieves the host name.
    28  	Host() string
    29  	// All retrieves data from JSON, form, and query parameters.
    30  	All() map[string]any
    31  	// Bind retrieve json and bind to obj
    32  	Bind(obj any) error
    33  	// Route retrieves a route parameter from the request path (e.g., /users/{id}).
    34  	Route(key string) string
    35  	// RouteInt retrieves a route parameter from the request path and attempts to parse it as an integer.
    36  	RouteInt(key string) int
    37  	// RouteInt64 retrieves a route parameter from the request path and attempts to parse it as a 64-bit integer.
    38  	RouteInt64(key string) int64
    39  	// Query retrieves a query string parameter from the request (e.g., /users?id=1).
    40  	Query(key string, defaultValue ...string) string
    41  	// QueryInt retrieves a query string parameter from the request and attempts to parse it as an integer.
    42  	QueryInt(key string, defaultValue ...int) int
    43  	// QueryInt64 retrieves a query string parameter from the request and attempts to parse it as a 64-bit integer.
    44  	QueryInt64(key string, defaultValue ...int64) int64
    45  	// QueryBool retrieves a query string parameter from the request and attempts to parse it as a boolean.
    46  	QueryBool(key string, defaultValue ...bool) bool
    47  	// QueryArray retrieves a query string parameter from the request and returns it as a slice of strings.
    48  	QueryArray(key string) []string
    49  	// QueryMap retrieves a query string parameter from the request and returns it as a map of key-value pairs.
    50  	QueryMap(key string) map[string]string
    51  	// Queries returns all the query string parameters from the request as a map of key-value pairs.
    52  	Queries() map[string]string
    53  
    54  	// Input retrieves data from the request in the following order: JSON, form, query, and route parameters.
    55  	Input(key string, defaultValue ...string) string
    56  	InputArray(key string, defaultValue ...[]string) []string
    57  	InputMap(key string, defaultValue ...map[string]string) map[string]string
    58  	InputInt(key string, defaultValue ...int) int
    59  	InputInt64(key string, defaultValue ...int64) int64
    60  	InputBool(key string, defaultValue ...bool) bool
    61  	// File retrieves a file by its key from the request.
    62  	File(name string) (filesystem.File, error)
    63  
    64  	// AbortWithStatus aborts the request with the specified HTTP status code.
    65  	AbortWithStatus(code int)
    66  	// AbortWithStatusJson aborts the request with the specified HTTP status code
    67  	// and returns a JSON response object.
    68  	AbortWithStatusJson(code int, jsonObj any)
    69  	// Next skips the current request handler, allowing the next middleware or handler to be executed.
    70  	Next()
    71  	// Origin retrieves the underlying *http.Request object for advanced request handling.
    72  	Origin() *http.Request
    73  
    74  	// Validate performs request data validation using specified rules and options.
    75  	Validate(rules map[string]string, options ...validation.Option) (validation.Validator, error)
    76  	// ValidateRequest validates the request data against a pre-defined FormRequest structure
    77  	// and returns validation errors, if any.
    78  	ValidateRequest(request FormRequest) (validation.Errors, error)
    79  }
    80  
    81  type FormRequest interface {
    82  	// Authorize determine if the user is authorized to make this request.
    83  	Authorize(ctx Context) error
    84  	// Rules get the validation rules that apply to the request.
    85  	Rules(ctx Context) map[string]string
    86  	// Messages get the validation messages that apply to the request.
    87  	Messages(ctx Context) map[string]string
    88  	// Attributes get custom attributes for validator errors.
    89  	Attributes(ctx Context) map[string]string
    90  	// PrepareForValidation prepare the data for validation.
    91  	PrepareForValidation(ctx Context, data validation.Data) error
    92  }