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

     1  package http
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  )
     7  
     8  type Json map[string]any
     9  
    10  //go:generate mockery --name=Response
    11  type Response interface {
    12  	Render() error
    13  }
    14  
    15  //go:generate mockery --name=ContextResponse
    16  type ContextResponse interface {
    17  	// Data write the given data to the response.
    18  	Data(code int, contentType string, data []byte) Response
    19  	// Download initiates a file download by specifying the file path and the desired filename
    20  	Download(filepath, filename string) Response
    21  	// File serves a file located at the specified file path as the response.
    22  	File(filepath string) Response
    23  	// Header sets an HTTP header field with the given key and value.
    24  	Header(key, value string) ContextResponse
    25  	// Json sends a JSON response with the specified status code and data object.
    26  	Json(code int, obj any) Response
    27  	// Origin returns the ResponseOrigin
    28  	Origin() ResponseOrigin
    29  	// Redirect performs an HTTP redirect to the specified location with the given status code.
    30  	Redirect(code int, location string) Response
    31  	// String writes a string response with the specified status code and format.
    32  	// The 'values' parameter can be used to replace placeholders in the format string.
    33  	String(code int, format string, values ...any) Response
    34  	// Success returns ResponseSuccess
    35  	Success() ResponseSuccess
    36  	// Status sets the HTTP response status code and returns the ResponseStatus.
    37  	Status(code int) ResponseStatus
    38  	// View returns ResponseView
    39  	View() ResponseView
    40  	// Writer returns the underlying http.ResponseWriter associated with the response.
    41  	Writer() http.ResponseWriter
    42  	// Flush flushes any buffered data to the client.
    43  	Flush()
    44  }
    45  
    46  //go:generate mockery --name=ResponseStatus
    47  type ResponseStatus interface {
    48  	// Data write the given data to the Response.
    49  	Data(contentType string, data []byte) Response
    50  	// Json sends a JSON Response with the specified data object.
    51  	Json(obj any) Response
    52  	// String writes a string Response with the specified format and values.
    53  	String(format string, values ...any) Response
    54  }
    55  
    56  //go:generate mockery --name=ResponseSuccess
    57  type ResponseSuccess interface {
    58  	// Data write the given data to the Response.
    59  	Data(contentType string, data []byte) Response
    60  	// Json sends a JSON Response with the specified data object.
    61  	Json(obj any) Response
    62  	// String writes a string Response with the specified format and values.
    63  	String(format string, values ...any) Response
    64  }
    65  
    66  //go:generate mockery --name=ResponseOrigin
    67  type ResponseOrigin interface {
    68  	// Body returns the response's body content as a *bytes.Buffer.
    69  	Body() *bytes.Buffer
    70  	// Header returns the response's HTTP header.
    71  	Header() http.Header
    72  	// Size returns the size, in bytes, of the response's body content.
    73  	Size() int
    74  	// Status returns the HTTP status code of the response.
    75  	Status() int
    76  }
    77  
    78  type ResponseView interface {
    79  	// Make generates a Response for the specified view with optional data.
    80  	Make(view string, data ...any) Response
    81  	// First generates a response for the first available view from the provided list.
    82  	First(views []string, data ...any) Response
    83  }