-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.mz
140 lines (97 loc) · 3.66 KB
/
client.mz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# ./client.mjs
<!-- toc -->
The furver client is a powerful addition to the furver server. It makes it
easy to use the server and bundles requests into a single request when
possible.
> This project contains both a bundled browser friendly version of the client
> and a programmer friendly module. We'll be using the module version in this
> example.
The furver module exports the following functions:
## Import client
```javascript cat - > .tmp.client.mjs
import {
// The function which creates a client.
client,
// Fetch preset function for fetching the schema from the server.
schema,
// A helper to create helpers for bundled requests.
bulk,
// Fetch preset functions that can be passed into the client config.
get,
post // default
} from './client.mjs'
```
## Initialize client
Here is an example of a client that performs get requests instead of the
default post requests.
```javascript cat - >> .tmp.client.mjs
const furverGet = await client({
endpoint: `http://localhost:${process.env.PORT}`,
fetch: get,
})
```
> Using get can enable the use of HTTP caching either in a proxy or a client.
> Caching might cause confusion; it can also enable optimization.
The `get` method should be used when no mutations occur. Using `get` has the
benefits that HTTP GET requests might have in your application/infrastructure.
## Client initialization config
The options object can have the following properties:
|name|type|default|
|---|---|---|
|endpoint|`string`|http://localhost:3000|
|fetch|`(url: string, options: object) => Promise<any>`|client.post|
|schema|`false`\|`(endpoint) => Array<[name, arity]>`|client.schema|
No function will be set on the client if `schema` is set to `false` and you can
only use the `client.call` method that takes a furver lisp program.
Note that the `fetch`, `schema`, and `method` options can be customized by
passing your own functions or values to the client constructor.
Once you have an instance of the client, you can call methods on it as defined
by the Furver API schema.
## Calling server functions
```javascript cat .tmp.client.mjs - > .tmp.client.example.mjs; node .tmp.client.example.mjs
await furverGet.push(1)
await furverGet.push(2)
await furverGet.push(3)
console.log(await furverGet.items())
```
You can also perform multiple calls that are bundled into a single request. We
can prove this by enabling the logger and performing multiple function calls.
```javascript|javascript cat .tmp.client.mjs - > .tmp.client.example.mjs; DEBUG_HIDE_DATE=1 DEBUG="furver:client" node .tmp.client.example.mjs 2>&1
await Promise.all([
furverGet.push('hello'),
furverGet.push('silly'),
furverGet.push('world')
])
console.log(await furverGet.items())
```
The client also has a `call` method that can be used to call any method on the
API, even if it's not defined in the schema. For example:
```javascript
const result = await furverGet.call(['customMethod', 'arg1', 'arg2', ...rest])
```
This will call the `customMethod` method on the API with the specified arguments.
**What if I want to call the `call` function?** You can:
```javascript|javascript cat .tmp.client.mjs - > .tmp.client.example.mjs; node .tmp.client.example.mjs
console.log(
await furverGet.call([
'call',
'arg1',
'arg2'
]),
// or
await furverGet.call([
['ref', 'call'], // "ref" has a special meaning in the furver lisp.
'arg1',
'arg2'
]),
// or
await furverGet.call([
furverGet.call, // Which expands to ['ref', 'call'].
'arg1',
'arg2'
])
)
```
The `furverGet.call` method takes any furver lisp program. These can be as
complicated or simple as your use-case requires.
[Read more about the furver lisp.](./lisp.md)