Skip to content

Commit 1a37ec2

Browse files
line-oduncdrum
authored andcommitted
doc(readme): clarify REST, connection and more
- fix TOC - rename and improve Configuration section to Connection Options - clarify wording in Use - REST - clarify that Components is using XML-RPC
1 parent e6d87f9 commit 1a37ec2

File tree

1 file changed

+101
-39
lines changed

1 file changed

+101
-39
lines changed

readme.md

Lines changed: 101 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Attempts to translate terminologies into node world. Uses promises.
88

99
- [Install](#install)
1010
- [Use](#use)
11+
- [Connection Options](#connection-options)
12+
- [Components](#components)
1113
- [Command Line Scripts](#command-line-scripts)
1214
- [Test](#test)
1315
- [Roadmap](#roadmap)
@@ -39,14 +41,16 @@ to download the source code of XQuery files is
3941
prohibited by default (`_source=yes` is only availabe if enabled in `descriptor.xml`).
4042
For details of available `options` for each method please see the [REST-API documentation](https://exist-db.org/exist/apps/doc/devguide_rest.xml)
4143

42-
First, we need an instance of the restClient (see also [Configuration](#configuration) for connection options).
44+
First, we need an instance of the restClient.
4345

4446
```js
4547
import { getRestClient } from '@existdb/node-exist'
4648
const rc = await getRestClient()
4749
```
4850

49-
The rest client has 4 methods
51+
For more information see also [Connection Options](#connection-options).
52+
53+
The returned HTTP client has 4 methods
5054

5155
- `post(query, path[, options])`: execute `query` in the context of `path`.
5256
The `query` is expected to be a XQuery main module and will be wrapped in the XML-fragment that exist expects.
@@ -55,19 +59,20 @@ The rest client has 4 methods
5559
await rc.post('count(//p)', '/db')
5660
```
5761

58-
- `put(data, path)` which allows to create resources in the database.
59-
If sub-collections are missing they will be created for you.
62+
- `put(data, path)`: create resources in the database.
63+
If sub-collections in path are missing, they will be created for you.
6064
The server will respond with StatusCode 400, Bad Request, for not-well-
61-
formed XML resources.
65+
formed XML resources. In this case, the body contains a detailed
66+
description of the validation error.
6267

6368
```js
6469
await rc.put('<root />', '/db/rest-test/test.xml')
6570
```
6671

67-
- `get(path [, options][, writableStream])` to read data from the database.
72+
- `get(path [, options][, writableStream])`: read data from the database.
6873
The response body will contain the contents of the resource or
69-
a file listing if the provided path is a collection.
70-
If a writableStream is passed in the response body will be streamed into it.
74+
a file listing, if the provided path is a collection.
75+
If a writableStream is passed in, the response body will be streamed into it.
7176

7277
```js
7378
const { body } = await rc.get('/db/rest-test/test.xml')
@@ -144,34 +149,102 @@ db.collections.describe('/db/apps')
144149
You can also have a look at the
145150
[examples](spec/examples) for more use-cases.
146151

147-
## Configuration
152+
## Connection Options
153+
154+
In the previous section you learned that there are two
155+
APIs you can use to interact with an exist-db instance.
148156

149-
Connect as someone else than guest
157+
Both client constructor functions do accept an option argument of type
158+
`NodeExistConnectionOptions`.
159+
Calling them without arguments, as in the examples above will use
160+
default options.
150161

151162
```js
152-
exist.connect({
163+
{
153164
basic_auth: {
154-
user: 'me',
155-
pass: '1 troubadour artisanal #compost'
156-
}
157-
})
165+
user: 'guest',
166+
pass: 'guest'
167+
},
168+
protocol: 'https:',
169+
host: 'localhost',
170+
port: '8443',
171+
path: '/exist/rest'|'/exist/xmlrpc'
172+
}
158173
```
159174

160-
Connect to a **local development** server using HTTP
175+
**NOTE:** The `path` property, the endpoint to reach an API,
176+
is different for REST (`'/exist/xmlrpc'`) and XML-RPC (`'/exist/xmlrpc'`).
177+
You most likely do not need to change it. However, if you need
178+
to you can override those.
179+
180+
### RESTClient with defaults
161181

162182
```js
163-
exist.connect({ secure: false, port: 8080 })
183+
import {getRestClient} from '@existdb/node-exist'
184+
const rest = await getRestClient({
185+
basic_auth: {
186+
user: 'guest',
187+
pass: 'guest'
188+
},
189+
protocol: 'https:',
190+
host: 'localhost',
191+
port: '8443',
192+
path: '/exist/rest'
193+
})
164194
```
165195

166-
Connect to a server with an **invalid** or **expired** certificate
196+
### XMLRPCClient with defaults
167197

168198
```js
169-
exist.connect({ rejectUnauthorized: false })
199+
import {connect} from '@existdb/node-exist'
200+
const db = connect({
201+
basic_auth: {
202+
user: 'guest',
203+
pass: 'guest'
204+
},
205+
protocol: 'https:',
206+
host: 'localhost',
207+
port: '8443',
208+
path: '/exist/xmlrpc'
209+
})
170210
```
171211

172-
### Read configuration from environment variables
212+
### Examples
173213

174-
`readOptionsFromEnv` offers an comfortable way to read the connection options
214+
- Connect as someone else than guest
215+
```js
216+
{
217+
basic_auth: {
218+
user: 'me',
219+
pass: '1 troubadour artisanal #compost'
220+
}
221+
}
222+
```
223+
224+
- Connect to a **local development** server using HTTP
225+
```js
226+
{
227+
protocol: 'http:',
228+
port: 8080
229+
}
230+
```
231+
232+
- Connect to a server with an **invalid** or **expired** certificate.
233+
```js
234+
{
235+
host: 'here.be.dragons',
236+
rejectUnauthorized: false
237+
}
238+
```
239+
**NOTE:** For remote hosts this is considered _bad practice_ as it does only
240+
offer a false sense of security.
241+
For hosts considered local - `localhost`, `127.0.0.1` and `[::1]` -
242+
this is set automatically, because it is impossible to have trusted certificates
243+
for local hosts.
244+
245+
### Read options from environment
246+
247+
`readOptionsFromEnv` offers a comfortable way to read the connection options
175248
from a set of environment variables
176249

177250
| variable name | default | description
@@ -180,32 +253,21 @@ from a set of environment variables
180253
| `EXISTDB_PASS` | _none_ | the password to authenticate the user against the database
181254
| `EXISTDB_SERVER` | `https://localhost:8443` | the URL of the database instance to connect to (only http and https protocol allowed)
182255

256+
**NOTE:** In order to connect to an instance as a user other than `guest`
257+
_both_ `EXISTDB_USER` _and_ `EXISTDB_PASS` have to be set!
258+
183259
```js
184-
const {connect, readOptionsFromEnv} = require('@existdb/node-exist')
260+
const {connect, restClient, readOptionsFromEnv} = require('@existdb/node-exist')
185261
const db = connect(readOptionsFromEnv())
262+
const rest = await getRestClient(readOptionsFromEnv())
186263
```
187264

188265
For more details you can have a look how it is used in the [connection script](spec/connection.js)
189-
that is used for testing and in all example scripts and the [examples](spec/examples).
190-
191-
### Defaults
192-
193-
```js
194-
{
195-
host: 'localhost',
196-
port: '8443',
197-
path: '/exist/xmlrpc', // you most likely do not need to change that
198-
basic_auth: {
199-
user: 'guest',
200-
pass: 'guest'
201-
},
202-
secure: true
203-
}
204-
```
266+
that is used for testing and in all [example scripts](spec/examples).
205267

206268
## Components
207269

208-
The methods are grouped into components by what they operate on.
270+
The **XML-RPC** commands are grouped into components by what they operate on.
209271
Every method returns a promise.
210272

211273
### Queries

0 commit comments

Comments
 (0)