BindHTTP registers $http.* namespaced object with common utils for sending HTTP requests. See https://pocketbase.io/jsvm/modules/_http.html.
(vm *goja.Runtime)
| 886 | // |
| 887 | // See https://pocketbase.io/jsvm/modules/_http.html. |
| 888 | func BindHTTP(vm *goja.Runtime) { |
| 889 | obj := vm.NewObject() |
| 890 | vm.Set("$http", obj) |
| 891 | |
| 892 | vm.Set("FormData", func(call goja.ConstructorCall) *goja.Object { |
| 893 | instance := FormData{} |
| 894 | |
| 895 | instanceValue := vm.ToValue(instance).(*goja.Object) |
| 896 | instanceValue.SetPrototype(call.This.Prototype()) |
| 897 | |
| 898 | return instanceValue |
| 899 | }) |
| 900 | |
| 901 | type sendResult struct { |
| 902 | JSON any `json:"json"` |
| 903 | Headers map[string][]string `json:"headers"` |
| 904 | Cookies map[string]*http.Cookie `json:"cookies"` |
| 905 | |
| 906 | // Deprecated: consider using Body instead |
| 907 | Raw string `json:"raw"` |
| 908 | |
| 909 | Body []byte `json:"body"` |
| 910 | StatusCode int `json:"statusCode"` |
| 911 | } |
| 912 | |
| 913 | type sendConfig struct { |
| 914 | // Deprecated: consider using Body instead |
| 915 | Data map[string]any |
| 916 | |
| 917 | Body any // raw string or FormData |
| 918 | Headers map[string]string |
| 919 | Method string |
| 920 | Url string |
| 921 | Timeout int // seconds (default to 120) |
| 922 | } |
| 923 | |
| 924 | obj.Set("send", func(params map[string]any) (*sendResult, error) { |
| 925 | config := sendConfig{ |
| 926 | Method: "GET", |
| 927 | } |
| 928 | |
| 929 | if v, ok := params["data"]; ok { |
| 930 | config.Data = cast.ToStringMap(v) |
| 931 | } |
| 932 | |
| 933 | if v, ok := params["body"]; ok { |
| 934 | config.Body = v |
| 935 | } |
| 936 | |
| 937 | if v, ok := params["headers"]; ok { |
| 938 | config.Headers = cast.ToStringMapString(v) |
| 939 | } |
| 940 | |
| 941 | if v, ok := params["method"]; ok { |
| 942 | config.Method = cast.ToString(v) |
| 943 | } |
| 944 | |
| 945 | if v, ok := params["url"]; ok { |
searching dependent graphs…