Skip to content

Commit e788c2d

Browse files
f3rnoJacobPlaster
authored andcommittedJun 9, 2020
(meta) add JSDoc config, update docs
1 parent d217c1d commit e788c2d

File tree

6 files changed

+410
-199
lines changed

6 files changed

+410
-199
lines changed
 

‎.jsdoc.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"tags": {
3+
"allowUnknownTags": false,
4+
"dictionaries": ["jsdoc"]
5+
},
6+
"source": {
7+
"include": ["lib", "README.md"],
8+
"includePattern": ".js$",
9+
"excludePattern": "(node_modules/|docs)"
10+
},
11+
"plugins": [
12+
"plugins/markdown"
13+
],
14+
"templates": {
15+
"cleverLinks": false,
16+
"monospaceLinks": true,
17+
"smallSourceLink": true,
18+
"hideAuthor": true
19+
},
20+
"opts": {
21+
"destination": "./docs/",
22+
"encoding": "utf8",
23+
"private": false,
24+
"recurse": true,
25+
"template": "./node_modules/docdash"
26+
},
27+
"package": "",
28+
"docdash": {
29+
"static": true,
30+
"sort": true,
31+
"search": true
32+
}
33+
}

‎README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Bitfinex API Mock Server
22

3-
This repo hosts mock servers for the WSv2 and RESTv2 Bitfinex APIs, and is intended for testing the Bitfinex API libraries.
3+
This repo hosts mock servers for the WSv2 and RESTv2 Bitfinex APIs, and is
4+
intended for testing the Bitfinex API libraries.
45

56
### Features
67

@@ -15,6 +16,7 @@ npm i --save bfx-api-mock-srv
1516
```
1617

1718
### Quickstart & Example
19+
1820
```js
1921
const assert = require('assert')
2022
const debug = require('debug')('bfx:api:mock-srv:examples:endpoint-test')
@@ -51,10 +53,8 @@ rest.fundingOffers('fUSD').then(([incomingFundingOffer]) => {
5153

5254
### Docs
5355

54-
* [See `docs/base_server.md`](/docs/base_server.md) for API docs covering the base server
55-
* [See `docs/rest2.md`](/docs/rest2.md) for API docs convering the RESTv2 mock server
56-
* [See `docs/ws2.md`](/docs/ws2.md) for API docs convering the WSv2 mock server
57-
* [Refer to `examples/`](/examples) for executable examples
56+
* See [`docs/reference.md`](docs/reference.md) for API documentation
57+
* Refer to [`examples/`](examples) for executable examples
5858

5959
### Contributing
6060

‎docs/base_server.md

-63
This file was deleted.

‎docs/reference.md

+372
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
## Modules
2+
3+
<dl>
4+
<dt><a href="#module_bfx-api-mock-srv">bfx-api-mock-srv</a></dt>
5+
<dd><p>This module hosts mock servers for the
6+
<a href="#module_bfx-api-mock-srv.MockWSv2Server">WSv2</a> and
7+
<a href="#module_bfx-api-mock-srv.MockRESTv2Server">RESTv2</a> Bitfinex APIs, and
8+
is intended for testing the Bitfinex API libraries.</p>
9+
</dd>
10+
</dl>
11+
12+
## Typedefs
13+
14+
<dl>
15+
<dt><a href="#MockWSv2ServerResponse">MockWSv2ServerResponse</a> : <code>object</code> | <code>Array</code></dt>
16+
<dd><p>A WSv2 mock response packet</p>
17+
</dd>
18+
</dl>
19+
20+
21+
<br><a name="module_bfx-api-mock-srv"></a>
22+
23+
## bfx-api-mock-srv
24+
> This module hosts mock servers for the
25+
> [WSv2](#module_bfx-api-mock-srv.MockWSv2Server) and
26+
> [RESTv2](#module_bfx-api-mock-srv.MockRESTv2Server) Bitfinex APIs, and
27+
> is intended for testing the Bitfinex API libraries.
28+
29+
**License**: Apache-2.0
30+
**Example**
31+
```js
32+
const { MockRESTv2Server } = require('bfx-api-mock-srv')
33+
34+
const FUNDING_OFFER = [
35+
41215275, 'fUSD', 1524784806000, 1524784806000, 1000, 1000, 'FRRDELTAVAR',
36+
null, null, 0, 'ACTIVE', null, null, null, 0, 30, 0, 0, null, 0,
37+
0.00207328
38+
]
39+
40+
debug('spawning mock server...')
41+
42+
const srv = new MockRESTv2Server({ listen: true })
43+
const rest = new RESTv2({
44+
apiKey: 'dummy',
45+
apiSecret: 'dummy',
46+
url: 'http://localhost:9999'
47+
})
48+
49+
srv.setResponse('f_offers.fUSD', [FUNDING_OFFER])
50+
51+
debug('requesting preset response...')
52+
53+
rest.fundingOffers('fUSD').then(([incomingFundingOffer]) => {
54+
assert.deepStrictEqual(incomingFundingOffer, FUNDING_OFFER)
55+
56+
debug('correct response received')
57+
srv.close()
58+
59+
return null
60+
}).catch((e) => {
61+
debug(`error: ${e.message}`)
62+
})
63+
```
64+
65+
* [bfx-api-mock-srv](#module_bfx-api-mock-srv)
66+
* [~MockServer](#module_bfx-api-mock-srv.MockServer) ⇐ <code>EventEmitter</code>
67+
* [new MockServer(args, dataPath)](#new_module_bfx-api-mock-srv.MockServer_new)
68+
* [.listen()](#module_bfx-api-mock-srv.MockServer+listen)
69+
* [.close()](#module_bfx-api-mock-srv.MockServer+close) ⇒ <code>Promise</code>
70+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
71+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
72+
* [~MockRESTv2Server](#module_bfx-api-mock-srv.MockRESTv2Server)[<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
73+
* [new MockRESTv2Server([args])](#new_module_bfx-api-mock-srv.MockRESTv2Server_new)
74+
* _instance_
75+
* [.listen()](#module_bfx-api-mock-srv.MockRESTv2Server+listen)
76+
* [.close()](#module_bfx-api-mock-srv.MockRESTv2Server+close) ⇒ <code>Promise</code>
77+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
78+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
79+
* _static_
80+
* [.keysForRoute(req, routeKey)](#module_bfx-api-mock-srv.MockRESTv2Server.keysForRoute) ⇒ <code>Array.&lt;string&gt;</code>
81+
* [~MockWSv2Server](#module_bfx-api-mock-srv.MockWSv2Server)[<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
82+
* [new MockWSv2Server([args])](#new_module_bfx-api-mock-srv.MockWSv2Server_new)
83+
* [.isOpen()](#module_bfx-api-mock-srv.MockWSv2Server+isOpen) ⇒ <code>boolean</code>
84+
* [.listen()](#module_bfx-api-mock-srv.MockWSv2Server+listen)
85+
* [.close()](#module_bfx-api-mock-srv.MockWSv2Server+close) ⇒ <code>Promise</code>
86+
* [.once(eventName, cb)](#module_bfx-api-mock-srv.MockWSv2Server+once)
87+
* [.send(packet)](#module_bfx-api-mock-srv.MockWSv2Server+send)
88+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
89+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
90+
91+
92+
<br><a name="module_bfx-api-mock-srv.MockServer"></a>
93+
94+
### bfx-api-mock-srv~MockServer ⇐ <code>EventEmitter</code>
95+
> Mock server base class, listens for commands to get/set responses
96+
97+
**Extends**: <code>EventEmitter</code>
98+
**See**
99+
100+
- [MockRESTv2Server](#module_bfx-api-mock-srv.MockRESTv2Server)
101+
- [MockWSv2Server](#module_bfx-api-mock-srv.MockWSv2Server)
102+
103+
104+
* [~MockServer](#module_bfx-api-mock-srv.MockServer) ⇐ <code>EventEmitter</code>
105+
* [new MockServer(args, dataPath)](#new_module_bfx-api-mock-srv.MockServer_new)
106+
* [.listen()](#module_bfx-api-mock-srv.MockServer+listen)
107+
* [.close()](#module_bfx-api-mock-srv.MockServer+close) ⇒ <code>Promise</code>
108+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
109+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
110+
111+
112+
<br><a name="new_module_bfx-api-mock-srv.MockServer_new"></a>
113+
114+
#### new MockServer(args, dataPath)
115+
116+
| Param | Type | Default | Description |
117+
| --- | --- | --- | --- |
118+
| args | <code>object</code> | | args |
119+
| [args.cmdPort] | <code>number</code> | <code>9998</code> | port to listen on for HTTP commands |
120+
| dataPath | <code>string</code> | | path to JSON file with responses |
121+
122+
123+
<br><a name="module_bfx-api-mock-srv.MockServer+listen"></a>
124+
125+
#### mockServer.listen()
126+
> Starts the HTTP command server listening on the configured port. This is
127+
> a no-op if the server is already up.
128+
129+
130+
<br><a name="module_bfx-api-mock-srv.MockServer+close"></a>
131+
132+
#### mockServer.close() ⇒ <code>Promise</code>
133+
> Closes the command server if it is running, no-op if not.
134+
135+
**Returns**: <code>Promise</code> - p
136+
137+
<br><a name="module_bfx-api-mock-srv.MockServer+getResponse"></a>
138+
139+
#### mockServer.getResponse(key) ⇒ <code>string</code>
140+
> Returns the configured server response for the given key
141+
142+
**Returns**: <code>string</code> - response - JSON
143+
144+
| Param | Type | Description |
145+
| --- | --- | --- |
146+
| key | <code>string</code> | key |
147+
148+
149+
<br><a name="module_bfx-api-mock-srv.MockServer+setResponse"></a>
150+
151+
#### mockServer.setResponse(key, data)
152+
> Sets the provided data as the server response for the given key.
153+
154+
155+
| Param | Type | Description |
156+
| --- | --- | --- |
157+
| key | <code>string</code> | key |
158+
| data | <code>Array</code>, <code>object</code> | data |
159+
160+
161+
<br><a name="module_bfx-api-mock-srv.MockRESTv2Server"></a>
162+
163+
### bfx-api-mock-srv~MockRESTv2Server ⇐ [<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
164+
> REST v2 API server mock
165+
>
166+
> Exposes the same routes as the real API, and maps them to a response table.
167+
> Multiple potential responses can be defined for endpoints with arguments,
168+
> with the best match sent to clients on request.
169+
>
170+
> i.e. If the following responses are configured:
171+
> - `orders.tBTCUSD: [42]`
172+
> - `orders: [41]`
173+
>
174+
> A `GET` on `/v2/auth/r/orders/tBTCUSD/hist` would return `[42]`, but a query
175+
> for a different symbol (`tETHUSD`) would return `[41]`.
176+
177+
**Extends**: [<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
178+
179+
* [~MockRESTv2Server](#module_bfx-api-mock-srv.MockRESTv2Server)[<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
180+
* [new MockRESTv2Server([args])](#new_module_bfx-api-mock-srv.MockRESTv2Server_new)
181+
* _instance_
182+
* [.listen()](#module_bfx-api-mock-srv.MockRESTv2Server+listen)
183+
* [.close()](#module_bfx-api-mock-srv.MockRESTv2Server+close) ⇒ <code>Promise</code>
184+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
185+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
186+
* _static_
187+
* [.keysForRoute(req, routeKey)](#module_bfx-api-mock-srv.MockRESTv2Server.keysForRoute) ⇒ <code>Array.&lt;string&gt;</code>
188+
189+
190+
<br><a name="new_module_bfx-api-mock-srv.MockRESTv2Server_new"></a>
191+
192+
#### new MockRESTv2Server([args])
193+
194+
| Param | Type | Default | Description |
195+
| --- | --- | --- | --- |
196+
| [args] | <code>object</code> | <code>{}</code> | args |
197+
| [args.apiPort] | <code>number</code> | <code>9999</code> | API port number |
198+
| [args.cmdPort] | <code>number</code> | <code>9998</code> | command port number |
199+
| [args.listen] | <code>boolean</code> | <code>true</code> | enables auto listen() |
200+
201+
202+
<br><a name="module_bfx-api-mock-srv.MockRESTv2Server+listen"></a>
203+
204+
#### mockRESTv2Server.listen()
205+
> Starts the API server listening on the configured port. This is a no-op if
206+
> the server is already up
207+
208+
**Overrides**: [<code>listen</code>](#module_bfx-api-mock-srv.MockServer+listen)
209+
210+
<br><a name="module_bfx-api-mock-srv.MockRESTv2Server+close"></a>
211+
212+
#### mockRESTv2Server.close() ⇒ <code>Promise</code>
213+
> Closes the API server if it is running; This is a no-op if it is not.
214+
215+
**Overrides**: [<code>close</code>](#module_bfx-api-mock-srv.MockServer+close)
216+
**Returns**: <code>Promise</code> - p
217+
218+
<br><a name="module_bfx-api-mock-srv.MockServer+getResponse"></a>
219+
220+
#### mockRESTv2Server.getResponse(key) ⇒ <code>string</code>
221+
> Returns the configured server response for the given key
222+
223+
**Overrides**: [<code>getResponse</code>](#module_bfx-api-mock-srv.MockServer+getResponse)
224+
**Returns**: <code>string</code> - response - JSON
225+
226+
| Param | Type | Description |
227+
| --- | --- | --- |
228+
| key | <code>string</code> | key |
229+
230+
231+
<br><a name="module_bfx-api-mock-srv.MockServer+setResponse"></a>
232+
233+
#### mockRESTv2Server.setResponse(key, data)
234+
> Sets the provided data as the server response for the given key.
235+
236+
**Overrides**: [<code>setResponse</code>](#module_bfx-api-mock-srv.MockServer+setResponse)
237+
238+
| Param | Type | Description |
239+
| --- | --- | --- |
240+
| key | <code>string</code> | key |
241+
| data | <code>Array</code>, <code>object</code> | data |
242+
243+
244+
<br><a name="module_bfx-api-mock-srv.MockRESTv2Server.keysForRoute"></a>
245+
246+
#### MockRESTv2Server.keysForRoute(req, routeKey) ⇒ <code>Array.&lt;string&gt;</code>
247+
**Returns**: <code>Array.&lt;string&gt;</code> - keys
248+
249+
| Param | Type | Description |
250+
| --- | --- | --- |
251+
| req | <code>express.Request</code> | request |
252+
| routeKey | <code>string</code> | key |
253+
254+
255+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server"></a>
256+
257+
### bfx-api-mock-srv~MockWSv2Server ⇐ [<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
258+
> Acts as a mock for v2 of the Bitfinex websocket API. Responses to available
259+
> commands are loaded from data/ws2.json and can be modified at runtime. The
260+
> command API allows for arbitrary packets to be injected into the ws stream.
261+
>
262+
> Responses are of the form `[{ packets: [...] }]`, where mulitple packets are
263+
> sent in order. A packet can be a string referencing another response by key.
264+
265+
**Extends**: [<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
266+
267+
* [~MockWSv2Server](#module_bfx-api-mock-srv.MockWSv2Server)[<code>MockServer</code>](#module_bfx-api-mock-srv.MockServer)
268+
* [new MockWSv2Server([args])](#new_module_bfx-api-mock-srv.MockWSv2Server_new)
269+
* [.isOpen()](#module_bfx-api-mock-srv.MockWSv2Server+isOpen) ⇒ <code>boolean</code>
270+
* [.listen()](#module_bfx-api-mock-srv.MockWSv2Server+listen)
271+
* [.close()](#module_bfx-api-mock-srv.MockWSv2Server+close) ⇒ <code>Promise</code>
272+
* [.once(eventName, cb)](#module_bfx-api-mock-srv.MockWSv2Server+once)
273+
* [.send(packet)](#module_bfx-api-mock-srv.MockWSv2Server+send)
274+
* [.getResponse(key)](#module_bfx-api-mock-srv.MockServer+getResponse) ⇒ <code>string</code>
275+
* [.setResponse(key, data)](#module_bfx-api-mock-srv.MockServer+setResponse)
276+
277+
278+
<br><a name="new_module_bfx-api-mock-srv.MockWSv2Server_new"></a>
279+
280+
#### new MockWSv2Server([args])
281+
> Spawns a new mock WS2 API server. Supported commands:
282+
> - POST /send - body is parsed as JSON and sent to all clients
283+
> - POST /config - body is parsed as JSON, and valid config keys are saved
284+
285+
286+
| Param | Type | Default | Description |
287+
| --- | --- | --- | --- |
288+
| [args] | <code>object</code> | <code>[]</code> | arguments |
289+
| [args.apiPort] | <code>number</code> | <code>9997</code> | which port to listen on for ws clients |
290+
| [args.cmdPort] | <code>number</code> | <code>9996</code> | which port to listen on for commands |
291+
| [args.syncOnConnect] | <code>boolean</code> | <code>true</code> | send snapshots to clients on connect |
292+
| [args.listen] | <code>boolean</code> | <code>true</code> | if true, listen() is called automatically |
293+
294+
295+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server+isOpen"></a>
296+
297+
#### mockWSv2Server.isOpen() ⇒ <code>boolean</code>
298+
> Returns server active status
299+
300+
**Returns**: <code>boolean</code> - open
301+
302+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server+listen"></a>
303+
304+
#### mockWSv2Server.listen()
305+
> Starts the API server listening on the configured port. This is a no-op if
306+
> the server is already up
307+
308+
**Overrides**: [<code>listen</code>](#module_bfx-api-mock-srv.MockServer+listen)
309+
310+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server+close"></a>
311+
312+
#### mockWSv2Server.close() ⇒ <code>Promise</code>
313+
> Closes the API server if it is running; This is a no-op if it is not.
314+
315+
**Overrides**: [<code>close</code>](#module_bfx-api-mock-srv.MockServer+close)
316+
**Returns**: <code>Promise</code> - p
317+
318+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server+once"></a>
319+
320+
#### mockWSv2Server.once(eventName, cb)
321+
> Configures an event handler to be called once when the specified event is
322+
> emitted by the API server. No-op if the server is not yet up.
323+
324+
325+
| Param | Type | Description |
326+
| --- | --- | --- |
327+
| eventName | <code>string</code> | event name |
328+
| cb | <code>function</code> | callback |
329+
330+
331+
<br><a name="module_bfx-api-mock-srv.MockWSv2Server+send"></a>
332+
333+
#### mockWSv2Server.send(packet)
334+
> Sends the provided packet to all connected clients
335+
336+
337+
| Param | Type | Description |
338+
| --- | --- | --- |
339+
| packet | <code>object</code>, <code>Array</code> | stringifed before being sent |
340+
341+
342+
<br><a name="module_bfx-api-mock-srv.MockServer+getResponse"></a>
343+
344+
#### mockWSv2Server.getResponse(key) ⇒ <code>string</code>
345+
> Returns the configured server response for the given key
346+
347+
**Overrides**: [<code>getResponse</code>](#module_bfx-api-mock-srv.MockServer+getResponse)
348+
**Returns**: <code>string</code> - response - JSON
349+
350+
| Param | Type | Description |
351+
| --- | --- | --- |
352+
| key | <code>string</code> | key |
353+
354+
355+
<br><a name="module_bfx-api-mock-srv.MockServer+setResponse"></a>
356+
357+
#### mockWSv2Server.setResponse(key, data)
358+
> Sets the provided data as the server response for the given key.
359+
360+
**Overrides**: [<code>setResponse</code>](#module_bfx-api-mock-srv.MockServer+setResponse)
361+
362+
| Param | Type | Description |
363+
| --- | --- | --- |
364+
| key | <code>string</code> | key |
365+
| data | <code>Array</code>, <code>object</code> | data |
366+
367+
368+
<br><a name="MockWSv2ServerResponse"></a>
369+
370+
## MockWSv2ServerResponse : <code>object</code> \| <code>Array</code>
371+
> A WSv2 mock response packet
372+

‎docs/rest2.md

-47
This file was deleted.

‎docs/ws2.md

-84
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.