mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 09:27:08 +00:00
split SetResponse func of Hono
This commit is contained in:
parent
bd88dd6151
commit
fa22eea00b
@ -50,12 +50,11 @@ func (c *Context) ResponseBody() io.ReadCloser {
|
||||
}
|
||||
|
||||
func (c *Context) SetBody(body io.ReadCloser) {
|
||||
var bodyObj js.Value
|
||||
if sr, ok := body.(jsutil.RawJSBodyGetter); ok {
|
||||
bodyObj = sr.GetRawJSBody()
|
||||
} else {
|
||||
bodyObj = jsutil.ConvertReaderToReadableStream(body)
|
||||
}
|
||||
bodyObj := convertBodyToJS(body)
|
||||
respObj := c.ctxObj.Call("body", bodyObj)
|
||||
c.ctxObj.Set("res", respObj)
|
||||
}
|
||||
|
||||
func (c *Context) SetResponse(respObj js.Value) {
|
||||
c.ctxObj.Set("res", respObj)
|
||||
}
|
||||
|
35
exp/hono/response.go
Normal file
35
exp/hono/response.go
Normal file
@ -0,0 +1,35 @@
|
||||
package hono
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/syumai/workers/internal/jshttp"
|
||||
"github.com/syumai/workers/internal/jsutil"
|
||||
)
|
||||
|
||||
func convertBodyToJS(body io.ReadCloser) js.Value {
|
||||
if sr, ok := body.(jsutil.RawJSBodyGetter); ok {
|
||||
return sr.GetRawJSBody()
|
||||
}
|
||||
return jsutil.ConvertReaderToReadableStream(body)
|
||||
}
|
||||
|
||||
func NewJSResponse(body io.ReadCloser, statusCode int, headers http.Header) js.Value {
|
||||
bodyObj := convertBodyToJS(body)
|
||||
opts := jsutil.ObjectClass.New()
|
||||
if statusCode != 0 {
|
||||
opts.Set("status", statusCode)
|
||||
}
|
||||
if headers != nil {
|
||||
headersObj := jshttp.ToJSHeader(headers)
|
||||
opts.Set("headers", headersObj)
|
||||
}
|
||||
return jsutil.ResponseClass.New(bodyObj, opts)
|
||||
}
|
||||
|
||||
func NewJSResponseWithBase(body io.ReadCloser, baseRespObj js.Value) js.Value {
|
||||
bodyObj := convertBodyToJS(body)
|
||||
return jsutil.ResponseClass.New(bodyObj, baseRespObj)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user