Skip to content

Commit 6b0e677

Browse files
authored
docs: document the ipfs http client constructor arguments (#3478)
Documents the existing constructor arguments as I don't think they're written down anywhere right now.
1 parent a7f4fc5 commit 6b0e677

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@
3939
- [Install](#install)
4040
- [Next Steps](#next-steps)
4141
- [Usage](#usage)
42+
- [`ipfsHttpClient([options])`](#ipfshttpclientoptions)
43+
- [Parameters](#parameters)
44+
- [Options](#options)
45+
- [Returns](#returns)
46+
- [Example](#example)
4247
- [API](#api)
4348
- [Additional Options](#additional-options)
4449
- [Instance Utils](#instance-utils)
4550
- [Static Types and Utils](#static-types-and-utils)
4651
- [Glob source](#glob-source)
4752
- [`globSource(path, [options])`](#globsourcepath-options)
48-
- [Example](#example)
53+
- [Example](#example-1)
4954
- [URL source](#url-source)
5055
- [`urlSource(url)`](#urlsourceurl)
51-
- [Example](#example-1)
56+
- [Example](#example-2)
5257
- [Running the daemon with the right port](#running-the-daemon-with-the-right-port)
5358
- [Importing the module and usage](#importing-the-module-and-usage)
5459
- [In a web browser](#in-a-web-browser)
@@ -87,6 +92,53 @@ Both the Current and Active LTS versions of Node.js are supported. Please see [n
8792

8893
## Usage
8994

95+
#### `ipfsHttpClient([options])`
96+
97+
> create an instance of the HTTP API client
98+
99+
#### Parameters
100+
101+
None
102+
103+
#### Options
104+
105+
`options` can be a `String`, a `URL` or a `Multiaddr` which will be interpreted as the address of the IPFS node we wish to use the API of.
106+
107+
Alternatively it can be an object which may have the following keys:
108+
109+
| Name | Type | Default | Description |
110+
| ---- | ---- | ------- | ----------- |
111+
| url | `String` or `URL` or `Multiaddr` | `'http://localhost:5001/api/v0'` | A URL that resolves to a running instance of the IPFS HTTP API |
112+
| protocol | `String` | `'http'` | The protocol to used (ignored if url is specified) |
113+
| host | `String` | `'localhost'` | The host to used (ignored if url is specified) |
114+
| port | `number` | `5001` | The port to used (ignored if url is specified) |
115+
| path | `String` | `'api/v0'` | The path to used (ignored if url is specified) |
116+
| agent | [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) | `http.Agent({ keepAlive: true, maxSockets: 6 })` | An `http.Agent` used to control client behaviour (node.js only) |
117+
118+
#### Returns
119+
120+
| Type | Description |
121+
| -------- | -------- |
122+
| `Object` | An object that conforms to the [IPFS Core API](https://github.com/ipfs/js-ipfs/tree/master/docs/core-api) |
123+
124+
#### Example
125+
126+
```JavaScript
127+
const createClient = require('ipfs-http-client')
128+
129+
// connect to the default API address http://localhost:5001
130+
const client = createClient()
131+
132+
// connect to a different API
133+
const client = createClient('http://127.0.0.1:5002')
134+
135+
// connect using a URL
136+
const client = createClient(new URL('http://127.0.0.1:5002'))
137+
138+
// call Core API methods
139+
const { cid } = await client.add('Hello world!')
140+
```
141+
90142
### API
91143

92144
[![IPFS Core API Compatible](https://cdn.rawgit.com/ipfs/interface-ipfs-core/master/img/badge.svg)](https://github.com/ipfs/js-ipfs/tree/master/packages/interface-ipfs-core)
@@ -99,7 +151,6 @@ All core API methods take _additional_ `options` specific to the HTTP API:
99151

100152
* `headers` - An object or [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) instance that can be used to set custom HTTP headers. Note that this option can also be [configured globally](#custom-headers) via the constructor options.
101153
* `searchParams` - An object or [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) instance that can be used to add additional query parameters to the query string sent with each request.
102-
* `agent` - A node [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) used to configure connection persistence and reuse (only supported in node.js)
103154

104155
### Instance Utils
105156

0 commit comments

Comments
 (0)