This commit is contained in:
2026-01-27 13:27:34 -05:00
parent fb6f260630
commit 28b1ad32f5
4 changed files with 302 additions and 169 deletions

View File

@@ -3,26 +3,66 @@ package standard
import (
"fmt"
"io"
"maps"
"net/http"
"strings"
"github.com/dop251/goja"
"reichard.io/poiesis/internal/runtime/pkg/builtin"
)
type FetchResult struct {
OK bool
Status int
Body string
Headers map[string]string
type FetchArgs struct {
URL string `json:"url"`
Options *FetchOptions `json:"options"`
}
func Fetch(url string, options map[string]any) (*FetchResult, error) {
req, err := http.NewRequest("GET", url, nil)
type FetchOptions struct {
Method string `json:"method"`
Headers *map[string]string `json:"headers"`
}
func (o *FetchOptions) Defaults() *FetchOptions {
if o.Method == "" {
o.Method = "GET"
}
return o
}
type FetchResult struct {
OK bool `json:"ok"`
Status int `json:"status"`
Body string `json:"body"`
Headers map[string]string `json:"headers"`
}
type AddArgs struct {
A int `json:"a"`
B int `json:"b"`
}
type GreetArgs struct {
Name string `json:"name"`
}
func Fetch(args FetchArgs) (*FetchResult, error) {
method := "GET"
headers := make(map[string]string)
if args.Options != nil {
method = args.Options.Method
if args.Options.Headers != nil {
maps.Copy(headers, *args.Options.Headers)
}
}
req, err := http.NewRequest(method, args.URL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch: %w", err)
@@ -36,12 +76,12 @@ func Fetch(url string, options map[string]any) (*FetchResult, error) {
return nil, fmt.Errorf("failed to read body: %w", err)
}
headers := make(map[string]string)
resultHeaders := make(map[string]string)
for key, values := range resp.Header {
if len(values) > 0 {
val := values[0]
headers[key] = val
headers[strings.ToLower(key)] = val
resultHeaders[key] = val
resultHeaders[strings.ToLower(key)] = val
}
}
@@ -49,44 +89,20 @@ func Fetch(url string, options map[string]any) (*FetchResult, error) {
OK: resp.StatusCode >= 200 && resp.StatusCode < 300,
Status: resp.StatusCode,
Body: string(body),
Headers: headers,
Headers: resultHeaders,
}, nil
}
func convertFetchResult(vm *goja.Runtime, result *FetchResult) goja.Value {
if result == nil {
return goja.Null()
}
func add(args AddArgs) int {
return args.A + args.B
}
obj := vm.NewObject()
_ = obj.Set("ok", result.OK)
_ = obj.Set("status", result.Status)
_ = obj.Set("text", func() string {
return result.Body
})
headersObj := vm.NewObject()
headers := result.Headers
_ = headersObj.Set("get", func(c goja.FunctionCall) goja.Value {
if len(c.Arguments) < 1 {
return goja.Undefined()
}
key := c.Arguments[0].String()
return vm.ToValue(headers[key])
})
_ = obj.Set("headers", headersObj)
return obj
func greet(args GreetArgs) string {
return fmt.Sprintf("Hello, %s!", args.Name)
}
func init() {
builtin.RegisterCustomConverter(convertFetchResult)
builtin.RegisterBuiltin("fetch", Fetch)
builtin.RegisterBuiltin("add", func(a, b int) int {
return a + b
})
builtin.RegisterBuiltin("greet", func(name string) string {
return fmt.Sprintf("Hello, %s!", name)
})
builtin.RegisterBuiltin[FetchArgs, *FetchResult]("fetch", Fetch)
builtin.RegisterBuiltin[AddArgs, int]("add", add)
builtin.RegisterBuiltin[GreetArgs, string]("greet", greet)
}