Skip to content

Commit c3115c0

Browse files
Lukas Jeniceklukasjenicek
andauthored
Pass input parameters to fetch function (#4)
* Pass input parameters to fetch function * Revert formatting of index.html file Co-authored-by: lukasjenicek <you@example.com>
1 parent 101e6d4 commit c3115c0

File tree

3 files changed

+49
-66
lines changed

3 files changed

+49
-66
lines changed

_examples/hello-webrpc/webapp/client.gen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ExampleService {
7979
constructor(hostname, fetch) {
8080
this.path = '/rpc/ExampleService/'
8181
this.hostname = hostname
82-
this.fetch = () => fetch
82+
this.fetch = (input, init) => fetch(input, init)
8383
}
8484

8585
url(name) {
Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,55 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>hello webrpc (js)</title>
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<link rel="icon" href="" />
8-
<script
9-
charset="utf-8"
10-
src="./client.gen.js"
11-
type="text/javascript"
12-
></script>
13-
<style></style>
14-
</head>
15-
<body>
16-
<div id="app">
17-
<h1>hello webrpc, simple JS app -- open your console</h1>
18-
</div>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>hello webrpc (js)</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="icon" href="" />
8+
<script charset="utf-8" src="./client.gen.js" type="text/javascript"></script>
9+
<style>
10+
</style>
11+
</head>
12+
<body>
1913

20-
<script>
21-
let svc = new ExampleService("http://127.0.0.1:4242", fetch);
14+
<div id="app">
15+
<h1>hello webrpc, simple JS app -- open your console</h1>
16+
</div>
2217

23-
// Expecting "true"
24-
console.log("[A] webrpc -- calling Ping() rpc method (expecting true):");
18+
<script>
19+
let svc = new ExampleService('http://127.0.0.1:4242', fetch)
2520

26-
svc
27-
.ping()
28-
.then((resp) => {
29-
console.log("[A]", { resp });
30-
})
31-
.catch((err) => {
32-
console.log("[A]", { err });
33-
});
21+
// Expecting "true"
22+
console.log('[A] webrpc -- calling Ping() rpc method (expecting true):')
3423

35-
// Expecting an error
36-
console.log(
37-
"[B] webrpc -- calling GetUser() rpc method of an unknown user (expecting a 404):"
38-
);
24+
svc.ping().then(resp => {
25+
console.log('[A]', {resp})
26+
}).catch(err => {
27+
console.log('[A]', {err})
28+
})
3929

40-
svc
41-
.getUser({ userID: 911 })
42-
.then((resp) => {
43-
console.log("[B]", resp.user);
44-
})
45-
.catch((err) => {
46-
console.log("[B]", { err });
47-
});
30+
// Expecting an error
31+
console.log('[B] webrpc -- calling GetUser() rpc method of an unknown user (expecting a 404):')
4832

49-
// Expecting some user data
50-
console.log(
51-
"[C] webrpc -- calling GetUser() rpc method (expecting User object):"
52-
);
33+
svc.getUser({userID: 911}).then(resp => {
34+
console.log('[B]', resp.user)
35+
}).catch(err => {
36+
console.log('[B]', {err})
37+
})
5338

54-
// svc.GetUser({userID: 966}).then(({ user }) => {
55-
svc
56-
.getUser({ userID: 966 })
57-
.then((resp) => {
58-
const user = resp.user;
59-
console.log("[C]", { user });
60-
console.log(
61-
"[C] welcome user ID",
62-
user.id,
63-
"with username",
64-
user.USERNAME
65-
);
66-
})
67-
.catch((err) => {
68-
console.log("[C]", { err });
69-
});
70-
</script>
71-
</body>
39+
// Expecting some user data
40+
console.log('[C] webrpc -- calling GetUser() rpc method (expecting User object):')
41+
42+
// svc.GetUser({userID: 966}).then(({ user }) => {
43+
svc.getUser({userID: 966}).then(resp => {
44+
const user = resp.user
45+
console.log('[C]', {user})
46+
console.log('[C] welcome user ID', user.id, 'with username', user.USERNAME)
47+
}).catch(err => {
48+
console.log('[C]', {err})
49+
})
50+
51+
52+
</script>
53+
54+
</body>
7255
</html>

client.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
constructor(hostname, fetch) {
1212
this.path = '/rpc/{{.Name}}/'
1313
this.hostname = hostname
14-
this.fetch = () => fetch
14+
this.fetch = (input, init) => fetch(input, init)
1515
}
1616

1717
url(name) {
@@ -41,4 +41,4 @@
4141
}
4242
{{end -}}
4343
{{end -}}
44-
{{end}}
44+
{{end}}

0 commit comments

Comments
 (0)