mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 09:49:12 +00:00
34 lines
904 B
Go
34 lines
904 B
Go
package fetch
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"syscall/js"
|
|
|
|
"github.com/syumai/workers/internal/jshttp"
|
|
"github.com/syumai/workers/internal/jsutil"
|
|
)
|
|
|
|
// fetch is a function that reproduces cloudflare fetch.
|
|
// Docs: https://developers.cloudflare.com/workers/runtime-apis/fetch/
|
|
func fetch(namespace js.Value, req *http.Request /* TO DO: options here */) (*http.Response, error) {
|
|
if namespace.IsUndefined() {
|
|
return nil, errors.New("fetch function not found")
|
|
}
|
|
|
|
promise := namespace.Call("fetch",
|
|
// The Request object to fetch.
|
|
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
|
|
jshttp.ToJSRequest(req),
|
|
// The content of the request.
|
|
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
|
|
jsutil.NewObject(),
|
|
)
|
|
jsRes, err := jsutil.AwaitPromise(promise)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return jshttp.ToResponse(jsRes)
|
|
}
|