This commit is contained in:
2026-01-27 13:57:01 -05:00
parent 28b1ad32f5
commit 7bf4f115b1
4 changed files with 103 additions and 28 deletions

View File

@@ -11,13 +11,14 @@ import (
)
type FetchArgs struct {
URL string `json:"url"`
Options *FetchOptions `json:"options"`
Input string `json:"input"`
Init *FetchOptions `json:"init,omitempty"`
}
type FetchOptions struct {
Method string `json:"method"`
Headers *map[string]string `json:"headers"`
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body *string `json:"body,omitempty"`
}
func (o *FetchOptions) Defaults() *FetchOptions {
@@ -47,14 +48,14 @@ 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)
if args.Init != nil {
method = args.Init.Method
if args.Init.Headers != nil {
maps.Copy(headers, args.Init.Headers)
}
}
req, err := http.NewRequest(method, args.URL, nil)
req, err := http.NewRequest(method, args.Input, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -102,7 +103,7 @@ func greet(args GreetArgs) string {
}
func init() {
builtin.RegisterBuiltin[FetchArgs, *FetchResult]("fetch", Fetch)
builtin.RegisterBuiltin[FetchArgs, *FetchResult]("fetch", Fetch, builtin.WithPromise())
builtin.RegisterBuiltin[AddArgs, int]("add", add)
builtin.RegisterBuiltin[GreetArgs, string]("greet", greet)
}