Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 34e6120

Browse files
committed
Merge branch 'dev-43' of github.com:ksankeerth/core into ksankeerth-dev-43
2 parents ec56036 + 47dfdb6 commit 34e6120

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

function/helpers.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package function
2+
3+
type JSFetchOptionsArg struct {
4+
Method string
5+
Headers map[string]string
6+
Body string
7+
Mode string
8+
Credentials string
9+
Cache string
10+
Redirect string
11+
Referrer string
12+
ReferrerPolicy string
13+
Integrity string
14+
Keepalive string
15+
Signal string
16+
}
17+
18+
type HTTPResponse struct {
19+
Status int `json:"status"`
20+
Body string `json:"body"`
21+
}
22+
23+
func NewJSFetcthOptionArg() JSFetchOptionsArg {
24+
defaultOptions := JSFetchOptionsArg{
25+
Method: "GET",
26+
Headers: make(map[string]string, 0),
27+
Body: "",
28+
Mode: "no-cors",
29+
Credentials: "omit",
30+
Cache: "no-cache",
31+
Redirect: "error",
32+
Referrer: "",
33+
ReferrerPolicy: "",
34+
Integrity: "",
35+
Keepalive: "",
36+
Signal: "",
37+
}
38+
return defaultOptions
39+
}

function/runtime.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
78
"net/http"
89
"strings"
910
"time"
@@ -121,6 +122,69 @@ func (env *ExecutionEnvironment) addHelpers(vm *goja.Runtime) {
121122
env.CurrentRun.Output = append(env.CurrentRun.Output, fmt.Sprint(params...))
122123
return goja.Undefined()
123124
})
125+
vm.Set("fetch", func(call goja.FunctionCall) goja.Value {
126+
url := ""
127+
fetchOptions := NewJSFetcthOptionArg()
128+
if len(call.Arguments) == 0 {
129+
return goja.Undefined()
130+
} else if len(call.Arguments) == 1 {
131+
url = call.Argument(0).Export().(string)
132+
} else {
133+
url = call.Argument(0).Export().(string)
134+
if err := vm.ExportTo(call.Argument(1), &fetchOptions); err != nil {
135+
return vm.ToValue(Result{Content: "the second argument should be an object"})
136+
}
137+
}
138+
if len(url) == 0 {
139+
return vm.ToValue(Result{Content: "the url should not be blank"})
140+
}
141+
142+
responseChan := make(chan interface{})
143+
go func() {
144+
client := http.Client{Timeout: time.Duration(30) * time.Second}
145+
var request *http.Request
146+
var err error
147+
bodyReader := strings.NewReader(fetchOptions.Body)
148+
switch fetchOptions.Method {
149+
case "GET":
150+
request, err = http.NewRequest(http.MethodGet, url, nil)
151+
case "POST":
152+
request, err = http.NewRequest(http.MethodPost, url, bodyReader)
153+
case "PUT":
154+
request, err = http.NewRequest(http.MethodPut, url, bodyReader)
155+
case "DELETE":
156+
request, err = http.NewRequest(http.MethodDelete, url, bodyReader)
157+
case "PATCH":
158+
request, err = http.NewRequest(http.MethodPatch, url, bodyReader)
159+
}
160+
if err != nil {
161+
responseChan <- err
162+
}
163+
for headerKey, headerValue := range fetchOptions.Headers {
164+
if len(headerKey) > 0 && len(headerValue) > 0 {
165+
request.Header.Set(headerKey, headerValue)
166+
}
167+
}
168+
res, err := client.Do(request)
169+
if err != nil {
170+
responseChan <- err
171+
}
172+
responseChan <- res
173+
}()
174+
175+
output := <-responseChan
176+
177+
if err, ok := output.(error); ok {
178+
return vm.ToValue(Result{OK: false, Content: fmt.Sprintf("error calling fetch(): %s", err.Error())})
179+
} else if response, ok := output.(*http.Response); ok {
180+
bodyBytes, err := io.ReadAll(response.Body)
181+
if err != nil {
182+
return vm.ToValue(Result{OK: false, Content: fmt.Sprintf("error calling fetch(): %s", err.Error())})
183+
}
184+
return vm.ToValue(Result{OK: true, Content: HTTPResponse{Status: response.StatusCode, Body: string(bodyBytes)}})
185+
}
186+
return goja.Undefined()
187+
})
124188
}
125189

126190
func (env *ExecutionEnvironment) addDatabaseFunctions(vm *goja.Runtime) {

functions_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,71 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
7272
log("query doc id: " + qres.content.results[0].id);
7373
return;
7474
}
75-
75+
76+
var getRes = fetch("https://run.mocky.io/v3/427873c5-4baa-4f68-b880-b6e3e45b3d4d");
77+
if (!getRes.ok) {
78+
log("ERROR: sending GET request");
79+
log(getRes.content);
80+
return;
81+
}
82+
83+
var postRes = fetch("https://run.mocky.io/v3/427873c5-4baa-4f68-b880-b6e3e45b3d4d", {
84+
method: "POST",
85+
headers: {
86+
"Content-Type" : "application/json"
87+
},
88+
body: {
89+
"test": "test msg"
90+
}
91+
});
92+
if (!postRes.ok) {
93+
log("ERROR: sending POST request");
94+
log(postRes.content);
95+
return;
96+
}
97+
98+
var putRes = fetch("https://run.mocky.io/v3/427873c5-4baa-4f68-b880-b6e3e45b3d4d", {
99+
method: "PUT",
100+
headers: {
101+
"Content-Type" : "application/json"
102+
},
103+
body: {
104+
"test": "test msg"
105+
}
106+
});
107+
if (!putRes.ok) {
108+
log("ERROR: sending PUT request");
109+
log(putRes.content);
110+
return;
111+
}
112+
var patchRes = fetch("https://run.mocky.io/v3/427873c5-4baa-4f68-b880-b6e3e45b3d4d", {
113+
method: "PATCH",
114+
headers: {
115+
"Content-Type" : "application/json"
116+
},
117+
body: {
118+
"test": "test msg"
119+
}
120+
});
121+
if (!patchRes.ok) {
122+
log("ERROR: sending PATCH request");
123+
log(patchRes.content);
124+
return;
125+
}
126+
var delRes = fetch("https://run.mocky.io/v3/427873c5-4baa-4f68-b880-b6e3e45b3d4d", {
127+
method: "DELETE",
128+
headers: {
129+
"Content-Type" : "application/json"
130+
},
131+
body: {
132+
"test": "test msg"
133+
}
134+
});
135+
if (!delRes.ok) {
136+
log("ERROR: sending DELETE request");
137+
log(delRes.content);
138+
return;
139+
}
76140
}`
77141
data := internal.ExecData{
78142
FunctionName: "unittest",

0 commit comments

Comments
 (0)