Skip to content

Commit 86ea629

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 a2db0fa commit 86ea629

File tree

13 files changed

+82
-45
lines changed

13 files changed

+82
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ const ipfs = ipfsClient({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' }
280280
```javascript
281281
const bitswap = require('ipfs-http-client/src/bitswap')('/ip4/127.0.0.1/tcp/5001')
282282
283-
const list = await bitswap.wantlist(key)
283+
const list = await bitswap.wantlist()
284284
// ...
285285
```
286286

src/bitswap/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = config => ({
44
wantlist: require('./wantlist')(config),
5+
wantlistForPeer: require('./wantlist-for-peer')(config),
56
stat: require('./stat')(config),
67
unwant: require('./unwant')(config)
78
})

src/bitswap/wantlist-for-peer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const configure = require('../lib/configure')
5+
const toUrlSearchParams = require('../lib/to-url-search-params')
6+
7+
module.exports = configure(api => {
8+
return async (peerId, options = {}) => {
9+
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()
10+
11+
const res = await (await api.post('bitswap/wantlist', {
12+
timeout: options.timeout,
13+
signal: options.signal,
14+
searchParams: toUrlSearchParams({
15+
...options,
16+
peer: peerId
17+
}),
18+
headers: options.headers
19+
})).json()
20+
21+
return (res.Keys || []).map(k => new CID(k['/']))
22+
}
23+
})

src/bitswap/wantlist.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (peer, options = {}) => {
9-
if (peer && (peer.timeout || peer.signal)) {
10-
options = peer
11-
peer = undefined
12-
}
13-
14-
if (peer) {
15-
options.peer = typeof peer === 'string' ? peer : new CID(peer).toString()
16-
}
17-
8+
return async (options = {}) => {
189
const res = await (await api.post('bitswap/wantlist', {
1910
timeout: options.timeout,
2011
signal: options.signal,

src/bootstrap/clear.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const configure = require('../lib/configure')
4+
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
6+
module.exports = configure(api => {
7+
return async (options = {}) => {
8+
const res = await api.post('bootstrap/rm', {
9+
timeout: options.timeout,
10+
signal: options.signal,
11+
searchParams: toUrlSearchParams({
12+
...options,
13+
all: true
14+
}),
15+
headers: options.headers
16+
})
17+
18+
return res.json()
19+
}
20+
})

src/bootstrap/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module.exports = config => ({
44
add: require('./add')(config),
5+
clear: require('./clear')(config),
56
rm: require('./rm')(config),
7+
reset: require('./reset')(config),
68
list: require('./list')(config)
79
})

src/bootstrap/reset.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const configure = require('../lib/configure')
4+
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
6+
module.exports = configure(api => {
7+
return async (options = {}) => {
8+
const res = await api.post('bootstrap/add', {
9+
timeout: options.timeout,
10+
signal: options.signal,
11+
searchParams: toUrlSearchParams({
12+
...options,
13+
default: true
14+
}),
15+
headers: options.headers
16+
})
17+
18+
return res.json()
19+
}
20+
})

src/dag/get.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ module.exports = configure((api, options) => {
1515
const getBlock = require('../block/get')(options)
1616
const dagResolve = require('./resolve')(options)
1717

18-
return async (cid, path, options = {}) => {
19-
if (path && typeof path === 'object') {
20-
options = path
21-
path = null
22-
}
23-
24-
const resolved = await dagResolve(cid, path, options)
18+
return async (cid, options = {}) => {
19+
const resolved = await dagResolve(cid, options)
2520
const block = await getBlock(resolved.cid, options)
2621
const dagResolver = resolvers[block.cid.codec]
2722

src/dag/resolve.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (cid, path, options = {}) => {
9-
if (path && typeof path === 'object') {
10-
options = path
11-
path = null
12-
}
13-
8+
return async (cid, options = {}) => {
149
const res = await api.post('dag/resolve', {
1510
timeout: options.timeout,
1611
signal: options.signal,
1712
searchParams: toUrlSearchParams({
18-
arg: path ? [cid, path].join(path.startsWith('/') ? '' : '/') : `${cid}`,
13+
arg: `${cid}${options.path ? `/${options.path}`.replace(/\/[/]+/g, '/') : ''}`,
1914
...options
2015
}),
2116
headers: options.headers

src/files/flush.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
88
return async (path, options = {}) => {
9-
if (typeof path !== 'string') {
10-
options = path || {}
11-
path = '/'
9+
if (!path || typeof path !== 'string') {
10+
throw new Error('ipfs.files.flush requires a path')
1211
}
1312

1413
const res = await api.post('files/flush', {

src/files/ls.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
77

88
module.exports = configure(api => {
99
return async function * ls (path, options = {}) {
10-
if (typeof path !== 'string') {
11-
options = path || {}
12-
path = '/'
10+
if (!path || typeof path !== 'string') {
11+
throw new Error('ipfs.files.ls requires a path')
1312
}
1413

1514
const res = await api.post('files/ls', {

src/object/new.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (template, options = {}) => {
9-
if (typeof template !== 'string') {
10-
options = template || {}
11-
template = null
12-
}
13-
8+
return async (options = {}) => {
149
const res = await api.post('object/new', {
1510
timeout: options.timeout,
1611
signal: options.signal,
1712
searchParams: toUrlSearchParams({
18-
arg: template,
13+
arg: options.template,
1914
...options
2015
}),
2116
headers: options.headers

src/pin/ls.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async function * ls (path, options = {}) {
9-
if (path && (path.type || path.timeout)) {
10-
options = path || {}
11-
path = []
8+
return async function * ls (options = {}) {
9+
if (options.paths) {
10+
options.paths = Array.isArray(options.paths) ? options.paths : [options.paths]
1211
}
1312

14-
path = Array.isArray(path) ? path : [path]
15-
1613
const res = await api.post('pin/ls', {
1714
timeout: options.timeout,
1815
signal: options.signal,
1916
searchParams: toUrlSearchParams({
20-
arg: path.map(p => `${p}`),
2117
...options,
18+
arg: (options.paths || []).map(path => `${path}`),
2219
stream: true
2320
}),
2421
headers: options.headers

0 commit comments

Comments
 (0)