Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8cb8c73

Browse files
fix: optional arguments go in the options object (#3118)
We have a few older APIs that take multiple optional arguments, which makes our code more complicated as it has to guess the users' intent, sometimes by inspecting properties on the passed args to see if they happen to correspond with properties on the actual options object. The options object was recently added to all API calls and is the right place for optional arguments to go, so the change here is to move all optional arguments into the options object, except where the presence of an optional argument dramatically changes the behaviour of the call (`ipfs.bootstrap` I'm mostly looking at you), in which case the methods are split out into multiple versions that do distinct things. Only the programatic API is affected, the CLI and HTTP APIs do not change. BREAKING CHANGES: - `ipfs.bitswap.wantlist([peer], [options])` is split into: - `ipfs.bitswap.wantlist([options])` - `ipfs.bitswap.wantlistForPeer(peer, [options])` - `ipfs.bootstrap.add([addr], [options])` is split into: - `ipfs.bootstrap.add(addr, [options])` - add a bootstrap node - `ipfs.bootstrap.reset()` - restore the default list of bootstrap nodes - `ipfs.bootstrap.rm([addr], [options])` is split into: - `ipfs.bootstrap.rm(addr, [options])` - remove a bootstrap node - `ipfs.bootstrap.clear([options])` - empty the bootstrap list - `ipfs.dag.get(cid, [path], [options])` becomes `ipfs.dag.get(cid, [options])` - `path` is moved into the `options` object - `ipfs.dag.tree(cid, [path], [options])` becomes `ipfs.dag.tree(cid, [options])` - `path` is moved into the `options` object - `ipfs.dag.resolve(cid, [path], [options])` becomes `ipfs.dag.resolve(cid, [options])` - `path` is moved into the `options` object - `ipfs.files.flush([path], [options])` becomes `ipfs.files.flush(path, [options])` - `ipfs.files.ls([path], [options])` becomes `ipfs.files.ls(path, [options])` - `ipfs.object.new([template], [options])` becomes `ipfs.object.new([options])` - `template` is moved into the `options` object - `ipfs.pin.ls([paths], [options])` becomes `ipfs.pin.ls([options])` - `paths` is moved into the `options` object Co-authored-by: Hugo Dias <[email protected]>
1 parent 65f8b23 commit 8cb8c73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1088
-502
lines changed

docs/core-api/BITSWAP.md

+44-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
# Bitswap API <!-- omit in toc -->
22

3-
- [`ipfs.bitswap.wantlist([peerId,] [options])`](#ipfsbitswapwantlistpeerid-options)
3+
- [`ipfs.bitswap.wantlist([options])`](#ipfsbitswapwantlistoptions)
44
- [Parameters](#parameters)
55
- [Options](#options)
66
- [Returns](#returns)
77
- [Example](#example)
8-
- [`ipfs.bitswap.unwant(cids, [options])`](#ipfsbitswapunwantcids-options)
8+
- [`ipfs.bitswap.wantlistForPeer(peerId, [options])`](#ipfsbitswapwantlistforpeerpeerid-options)
99
- [Parameters](#parameters-1)
1010
- [Options](#options-1)
1111
- [Returns](#returns-1)
1212
- [Example](#example-1)
13-
- [`ipfs.bitswap.stat([options])`](#ipfsbitswapstatoptions)
13+
- [`ipfs.bitswap.unwant(cids, [options])`](#ipfsbitswapunwantcids-options)
1414
- [Parameters](#parameters-2)
1515
- [Options](#options-2)
1616
- [Returns](#returns-2)
1717
- [Example](#example-2)
18+
- [`ipfs.bitswap.stat([options])`](#ipfsbitswapstatoptions)
19+
- [Parameters](#parameters-3)
20+
- [Options](#options-3)
21+
- [Returns](#returns-3)
22+
- [Example](#example-3)
1823

19-
## `ipfs.bitswap.wantlist([peerId,] [options])`
24+
## `ipfs.bitswap.wantlist([options])`
2025

21-
> Returns the wantlist, optionally filtered by peer ID
26+
> Returns the wantlist for your node
2227
2328
### Parameters
2429

25-
| Name | Type | Description |
26-
| ---- | ---- | ----------- |
27-
| peerId | [PeerId][], [CID][], `String` or `Buffer` | An optional peer ID to return the wantlist for |
30+
None
2831

2932
### Options
3033

@@ -47,9 +50,40 @@ An optional object which may have the following keys:
4750
const list = await ipfs.bitswap.wantlist()
4851
console.log(list)
4952
// [ CID('QmHash') ]
53+
```
54+
55+
A great source of [examples][] can be found in the tests for this API.
56+
57+
## `ipfs.bitswap.wantlistForPeer(peerId, [options])`
58+
59+
> Returns the wantlist for a connected peer
60+
61+
### Parameters
62+
63+
| Name | Type | Default | Description |
64+
| ---- | ---- | ------- | ----------- |
65+
| peerId | [PeerId][], [CID][], `String` or `Buffer` | A peer ID to return the wantlist for |
66+
67+
### Options
68+
69+
An optional object which may have the following keys:
70+
71+
| Name | Type | Default | Description |
72+
| ---- | ---- | ------- | ----------- |
73+
| timeout | `Number` | `undefined` | A timeout in ms |
74+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
75+
76+
### Returns
77+
78+
| Type | Description |
79+
| -------- | -------- |
80+
| `Promise<CID[]>` | An array of [CID][]s currently in the wantlist |
5081

51-
const list2 = await ipfs.bitswap.wantlist(peerId)
52-
console.log(list2)
82+
### Example
83+
84+
```JavaScript
85+
const list = await ipfs.bitswap.wantlistForPeer(peerId)
86+
console.log(list)
5387
// [ CID('QmHash') ]
5488
```
5589

docs/core-api/BOOTSTRAP.md

+101-15
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,53 @@
44
55
Warning: your node requires bootstrappers to join the network and find other peers.
66

7-
If you edit this list, you may find you have reduced or no connectivity. If this is the case, please reset your node's bootstrapper list with `ipfs.bootstrap.add({ default: true })`.
7+
If you edit this list, you may find you have reduced or no connectivity. If this is the case, please reset your node's bootstrapper list with `ipfs.bootstrap.reset()`.
88

9-
- [`ipfs.bootstrap.add([addr,] [options])`](#ipfsbootstrapaddaddr-options)
9+
- [`ipfs.bootstrap.add(addr, [options])`](#ipfsbootstrapaddaddr-options)
1010
- [Parameters](#parameters)
1111
- [Options](#options)
1212
- [Returns](#returns)
1313
- [Example](#example)
14-
- [`ipfs.bootstrap.list([options])`](#ipfsbootstraplistoptions)
14+
- [`ipfs.bootstrap.reset([options])`](#ipfsbootstrapresetoptions)
1515
- [Parameters](#parameters-1)
1616
- [Options](#options-1)
1717
- [Returns](#returns-1)
1818
- [Example](#example-1)
19-
- [`ipfs.bootstrap.rm([addr,] [options])`](#ipfsbootstraprmaddr-options)
19+
- [`ipfs.bootstrap.list([options])`](#ipfsbootstraplistoptions)
2020
- [Parameters](#parameters-2)
2121
- [Options](#options-2)
2222
- [Returns](#returns-2)
2323
- [Example](#example-2)
24-
25-
## `ipfs.bootstrap.add([addr,] [options])`
24+
- [`ipfs.bootstrap.rm(addr, [options])`](#ipfsbootstraprmaddr-options)
25+
- [Parameters](#parameters-3)
26+
- [Options](#options-3)
27+
- [Returns](#returns-3)
28+
- [Example](#example-3)
29+
- [`ipfs.bootstrap.clear([options])`](#ipfsbootstrapclearoptions)
30+
- [Parameters](#parameters-4)
31+
- [Options](#options-4)
32+
- [Returns](#returns-4)
33+
- [Example](#example-4)
34+
35+
## `ipfs.bootstrap.add(addr, [options])`
2636

2737
> Add a peer address to the bootstrap list
2838
2939
### Parameters
3040

3141
| Name | Type | Description |
3242
| ---- | ---- | ----------- |
33-
| addr | [MultiAddr][] | The address of a network peer. If omitted the `default` option must be passed as `true`. |
43+
| addr | [MultiAddr][] | The address of a network peer |
3444

3545
### Options
3646

3747
An optional object which may have the following keys:
3848

3949
| Name | Type | Default | Description |
4050
| ---- | ---- | ------- | ----------- |
41-
| default | `boolean` | `false` | If true, add the default peers to the list |
4251
| timeout | `Number` | `undefined` | A timeout in ms |
4352
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
4453

45-
Note: If passing the `default` option, `addr` is an optional parameter (may be `undefined`/`null`) and options may be passed as the first argument. i.e. `ipfs.bootstrap.add({ default: true })`
46-
4754
### Returns
4855

4956
| Type | Description |
@@ -71,6 +78,48 @@ console.log(res.Peers)
7178

7279
A great source of [examples][] can be found in the tests for this API.
7380

81+
## `ipfs.bootstrap.reset([options])`
82+
83+
> Reset the bootstrap list to contain only the default bootstrap nodes
84+
85+
### Parameters
86+
87+
None.
88+
89+
### Options
90+
91+
An optional object which may have the following keys:
92+
93+
| Name | Type | Default | Description |
94+
| ---- | ---- | ------- | ----------- |
95+
| timeout | `Number` | `undefined` | A timeout in ms |
96+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
97+
98+
### Returns
99+
100+
| Type | Description |
101+
| -------- | -------- |
102+
| `Promise<{ Peers: Array<MultiAddr> }>` | An object that contains an array with all the added addresses |
103+
104+
example of the returned object:
105+
106+
```JavaScript
107+
{
108+
Peers: [address1, address2, ...]
109+
}
110+
```
111+
112+
### Example
113+
114+
```JavaScript
115+
const res = await ipfs.bootstrap.reset()
116+
console.log(res.Peers)
117+
// Logs:
118+
// ['/ip4/104....9z']
119+
```
120+
121+
A great source of [examples][] can be found in the tests for this API.
122+
74123
## `ipfs.bootstrap.list([options])`
75124

76125
> List all peer addresses in the bootstrap list
@@ -113,27 +162,64 @@ console.log(res.Peers)
113162

114163
A great source of [examples][] can be found in the tests for this API.
115164

116-
## `ipfs.bootstrap.rm([addr,] [options])`
165+
## `ipfs.bootstrap.rm(addr, [options])`
117166

118167
> Remove a peer address from the bootstrap list
119168
120169
### Parameters
121170

122171
| Name | Type | Description |
123172
| ---- | ---- | ----------- |
124-
| addr | [MultiAddr][] | The address of a network peer. If omitted the `all` option must be passed as `true`. |
173+
| addr | [MultiAddr][] | The address of a network peer |
125174

126175
### Options
127176

128177
An optional object which may have the following keys:
129178

130179
| Name | Type | Default | Description |
131180
| ---- | ---- | ------- | ----------- |
132-
| all | `boolean` | `false` | If true, remove all peers from the bootstrap list |
133181
| timeout | `Number` | `undefined` | A timeout in ms |
134182
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
135183

136-
Note: If passing the `all` option, `addr` is an optional parameter (may be `undefined`/`null`) and options may be passed as the first argument. i.e. `ipfs.bootstrap.rm({ all: true })`
184+
### Returns
185+
186+
| Type | Description |
187+
| -------- | -------- |
188+
| `Promise<{ Peers: Array<MultiAddr> }>` | An object that contains an array with all the removed addresses |
189+
190+
```JavaScript
191+
{
192+
Peers: [address1, address2, ...]
193+
}
194+
```
195+
196+
### Example
197+
198+
```JavaScript
199+
const res = await ipfs.bootstrap.rm('address1')
200+
console.log(res.Peers)
201+
// Logs:
202+
// [address1, ...]
203+
```
204+
205+
A great source of [examples][] can be found in the tests for this API.
206+
207+
## `ipfs.bootstrap.clear([options])`
208+
209+
> Remove all peer addresses from the bootstrap list
210+
211+
### Parameters
212+
213+
None.
214+
215+
### Options
216+
217+
An optional object which may have the following keys:
218+
219+
| Name | Type | Default | Description |
220+
| ---- | ---- | ------- | ----------- |
221+
| timeout | `Number` | `undefined` | A timeout in ms |
222+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
137223

138224
### Returns
139225

@@ -150,7 +236,7 @@ Note: If passing the `all` option, `addr` is an optional parameter (may be `unde
150236
### Example
151237

152238
```JavaScript
153-
const res = await ipfs.bootstrap.rm(null, { all: true })
239+
const res = await ipfs.bootstrap.clear()
154240
console.log(res.Peers)
155241
// Logs:
156242
// [address1, address2, ...]

docs/core-api/DAG.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
- [Options](#options)
88
- [Returns](#returns)
99
- [Example](#example)
10-
- [`ipfs.dag.get(cid, [path], [options])`](#ipfsdaggetcid-path-options)
10+
- [`ipfs.dag.get(cid, [options])`](#ipfsdaggetcid-path-options)
1111
- [Parameters](#parameters-1)
1212
- [Options](#options-1)
1313
- [Returns](#returns-1)
1414
- [Example](#example-1)
15-
- [`ipfs.dag.tree(cid, [path,] [options])`](#ipfsdagtreecid-path-options)
15+
- [`ipfs.dag.tree(cid, [options])`](#ipfsdagtreecid-path-options)
1616
- [Parameters](#parameters-2)
1717
- [Options](#options-2)
1818
- [Returns](#returns-2)
@@ -65,23 +65,23 @@ console.log(cid.toString())
6565

6666
A great source of [examples][] can be found in the tests for this API.
6767

68-
## `ipfs.dag.get(cid, [path], [options])`
68+
## `ipfs.dag.get(cid, [options])`
6969

7070
> Retrieve an IPLD format node
7171
7272
### Parameters
7373

7474
| Name | Type | Description |
7575
| ---- | ---- | ----------- |
76-
| cid | [CID][] | A DAG node that follows one of the supported IPLD formats |
77-
| path | `String` | An optional path within the DAG to resolve |
76+
| cid | [CID][] | A CID that resolves to a node to get |
7877

7978
### Options
8079

8180
An optional object which may have the following keys:
8281

8382
| Name | Type | Default | Description |
8483
| ---- | ---- | ------- | ----------- |
84+
| path | `String` | An optional path within the DAG to resolve |
8585
| localResolve | `boolean` | `false` | If set to true, it will avoid resolving through different objects |
8686
| timeout | `Number` | `undefined` | A timeout in ms |
8787
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
@@ -114,34 +114,34 @@ const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
114114
console.log(cid.toString())
115115
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5
116116

117-
async function getAndLog(cidPath) {
118-
const result = await ipfs.dag.get(cidPath)
117+
async function getAndLog(cid, path) {
118+
const result = await ipfs.dag.get(cid, { path })
119119
console.log(result.value)
120120
}
121121

122-
await getAndLog('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/a')
122+
await getAndLog(cid, '/a')
123123
// Logs:
124124
// 1
125125

126-
await getAndLog('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/b')
126+
await getAndLog(cid, '/b')
127127
// Logs:
128128
// [1, 2, 3]
129129

130-
await getAndLog('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/c')
130+
await getAndLog(cid, '/c')
131131
// Logs:
132132
// {
133133
// ca: [5, 6, 7],
134134
// cb: 'foo'
135135
// }
136136

137-
await getAndLog('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/c/ca/1')
137+
await getAndLog(cid, '/c/ca/1')
138138
// Logs:
139139
// 6
140140
```
141141

142142
A great source of [examples][] can be found in the tests for this API.
143143

144-
## `ipfs.dag.tree(cid, [path,] [options])`
144+
## `ipfs.dag.tree(cid, [options])`
145145

146146
> Enumerate all the entries in a graph
147147
@@ -150,14 +150,14 @@ A great source of [examples][] can be found in the tests for this API.
150150
| Name | Type | Description |
151151
| ---- | ---- | ----------- |
152152
| cid | [CID][] | A DAG node that follows one of the supported IPLD formats |
153-
| path | `String` | An optional path within the DAG to resolve |
154153

155154
### Options
156155

157156
An optional object which may have the following keys:
158157

159158
| Name | Type | Default | Description |
160159
| ---- | ---- | ------- | ----------- |
160+
| path | `String` | An optional path within the DAG to resolve |
161161
| recursive | `boolean` | `false` | If set to true, it will follow the links and continuously run tree on them, returning all the paths in the graph |
162162
| timeout | `Number` | `undefined` | A timeout in ms |
163163
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |

0 commit comments

Comments
 (0)