Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 776cccd

Browse files
authored
Merge pull request #313 from ipfs/clean-madness
Reduce de complexity of the js-ipfs-api module
2 parents 73078ea + 11a166e commit 776cccd

Some content is hidden

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

51 files changed

+1134
-492
lines changed

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://exam
105105

106106
### API
107107

108-
> `WIP`
109-
110-
`js-ipfs-api` follows the spec defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor - expect it to be complete in the next few weeks (August 2016). You can use it today to consult the methods available.
108+
> `js-ipfs-api` follows the spec defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor - expect it to be complete in the next few weeks (August 2016). You can use it today to consult the methods available.
111109
112110
### Utility functions
113111

@@ -140,6 +138,7 @@ This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like
140138

141139
```JavaScript
142140
```
141+
>>>>>> refactor(module+tests): Enable running the tests
143142
144143
### Callbacks and promises
145144

@@ -166,8 +165,6 @@ We run tests by executing `npm test` in a terminal window. This will run both No
166165

167166

168167

169-
>>>>>>> kick off ipfs-api next generation
170-
171168
## Contribute
172169

173170
The js-ipfs-api is a work in progress. As such, there's a few things you can do right now to help out:

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "ipfs-api",
33
"version": "6.0.3",
4-
"description": "A client library for the IPFS API",
5-
"main": "src/index.js",
4+
"description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec",
5+
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"test": "gulp test",

src/api/add-files.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ const isNode = require('detect-node')
44
const addToDagNodesTransform = require('../add-to-dagnode-transform')
55

66
module.exports = (send) => {
7-
return function add (path, opts, cb) {
8-
if (typeof (opts) === 'function' && cb === undefined) {
9-
cb = opts
7+
return function add (path, opts, callback) {
8+
if (typeof (opts) === 'function' &&
9+
callback === undefined) {
10+
callback = opts
1011
opts = {}
1112
}
1213

1314
if (!isNode) {
14-
return cb(new Error('Recursive uploads are not supported in the browser'))
15+
return callback(new Error('Recursive uploads are not supported in the browser'))
1516
}
1617

17-
if (typeof (path) !== 'string') {
18-
return cb(new Error('"path" must be a string'))
18+
if (typeof path !== 'string') {
19+
return callback(new Error('"path" must be a string'))
1920
}
2021

21-
var sendWithTransform = send.withTransform(addToDagNodesTransform)
22+
const sendWithTransform = send.withTransform(addToDagNodesTransform)
2223

23-
return sendWithTransform('add', null, opts, path, cb)
24+
return sendWithTransform({
25+
path: 'add',
26+
qs: opts,
27+
files: path
28+
}, callback)
2429
}
2530
}

src/api/add-url.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ const Wreck = require('wreck')
44
const addToDagNodesTransform = require('../add-to-dagnode-transform')
55

66
module.exports = (send) => {
7-
return function add (url, opts, cb) {
8-
if (typeof (opts) === 'function' && cb === undefined) {
9-
cb = opts
7+
return function add (url, opts, callback) {
8+
if (typeof (opts) === 'function' &&
9+
callback === undefined) {
10+
callback = opts
1011
opts = {}
1112
}
1213

13-
if (typeof url !== 'string' || !url.startsWith('http')) {
14-
return cb(new Error('"url" param must be an http(s) url'))
14+
if (typeof url !== 'string' ||
15+
!url.startsWith('http')) {
16+
return callback(new Error('"url" param must be an http(s) url'))
1517
}
1618

17-
var sendWithTransform = send.withTransform(addToDagNodesTransform)
19+
const sendWithTransform = send.withTransform(addToDagNodesTransform)
1820

1921
Wreck.request('GET', url, null, (err, res) => {
20-
if (err) return cb(err)
22+
if (err) {
23+
return callback(err)
24+
}
2125

22-
sendWithTransform('add', null, opts, res, cb)
26+
sendWithTransform({
27+
path: 'add',
28+
qs: opts,
29+
files: res
30+
}, callback)
2331
})
2432
}
2533
}

src/api/add.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module.exports = (send) => {
1616

1717
const sendWithTransform = send.withTransform(addToDagNodesTransform)
1818

19-
sendWithTransform('add', null, {}, files, callback)
19+
return sendWithTransform({
20+
path: 'add',
21+
files: files
22+
}, callback)
2023
})
2124
}

src/api/bitswap.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
'use strict'
22

3-
const argCommand = require('../cmd-helpers').argCommand
4-
53
module.exports = (send) => {
64
return {
7-
wantlist (cb) {
8-
return send('bitswap/wantlist', {}, null, null, cb)
5+
wantlist (callback) {
6+
return send({
7+
path: 'bitswap/wantlist'
8+
}, callback)
99
},
10-
stat (cb) {
11-
return send('bitswap/stat', {}, null, null, cb)
10+
stat (callback) {
11+
return send({
12+
path: 'bitswap/stat'
13+
}, callback)
1214
},
13-
unwant: argCommand(send, 'bitswap/unwant')
15+
unwant (args, opts, callback) {
16+
if (typeof (opts) === 'function') {
17+
callback = opts
18+
opts = {}
19+
}
20+
return send({
21+
path: 'bitswap/unwant',
22+
args: args,
23+
qs: opts
24+
}, callback)
25+
}
1426
}
1527
}

src/api/block.js

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
'use strict'
22

3-
const argCommand = require('../cmd-helpers').argCommand
4-
53
module.exports = (send) => {
64
return {
7-
get: argCommand(send, 'block/get'),
8-
stat: argCommand(send, 'block/stat'),
9-
put (file, cb) {
5+
get (args, opts, callback) {
6+
if (typeof (opts) === 'function') {
7+
callback = opts
8+
opts = {}
9+
}
10+
return send({
11+
path: 'block/get',
12+
args: args,
13+
qs: opts
14+
}, callback)
15+
},
16+
stat (args, opts, callback) {
17+
if (typeof (opts) === 'function') {
18+
callback = opts
19+
opts = {}
20+
}
21+
return send({
22+
path: 'block/stat',
23+
args: args,
24+
qs: opts
25+
}, callback)
26+
},
27+
put (file, callback) {
1028
if (Array.isArray(file)) {
11-
let err = new Error('block.put() only accepts 1 file')
12-
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
29+
const err = new Error('block.put() only accepts 1 file')
30+
if (typeof callback !== 'function' &&
31+
typeof Promise !== 'undefined') {
1332
return new Promise((resolve, reject) => reject(err))
1433
}
15-
return cb(err)
34+
return callback(err)
1635
}
17-
return send('block/put', null, null, file, cb)
36+
37+
return send({
38+
path: 'block/put',
39+
files: file
40+
}, callback)
1841
}
1942
}
2043
}

src/api/bootstrap.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
'use strict'
22

3-
const command = require('../cmd-helpers').command
4-
53
module.exports = (send) => {
64
return {
7-
add: (arg, opts, cb) => {
8-
if (typeof opts === 'function' && cb === undefined) {
9-
cb = opts
5+
add (args, opts, callback) {
6+
if (typeof opts === 'function' &&
7+
callback === undefined) {
8+
callback = opts
109
opts = {}
1110
}
12-
return send('bootstrap/add', arg, opts, null, cb)
11+
return send({
12+
path: 'bootstrap/add',
13+
args: args,
14+
qs: opts
15+
}, callback)
1316
},
14-
rm: (arg, opts, cb) => {
15-
if (typeof opts === 'function' && cb === undefined) {
16-
cb = opts
17+
rm (args, opts, callback) {
18+
if (typeof opts === 'function' &&
19+
callback === undefined) {
20+
callback = opts
1721
opts = {}
1822
}
19-
return send('bootstrap/rm', arg, opts, null, cb)
23+
return send({
24+
path: 'bootstrap/rm',
25+
args: args,
26+
qs: opts
27+
}, callback)
2028
},
21-
list: command(send, 'bootstrap/list')
29+
list (opts, callback) {
30+
if (typeof (opts) === 'function') {
31+
callback = opts
32+
opts = {}
33+
}
34+
return send({
35+
path: 'bootstrap/list',
36+
qs: opts
37+
}, callback)
38+
}
2239
}
2340
}

src/api/cat.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ const promisify = require('promisify-es6')
44
const cleanMultihash = require('../clean-multihash')
55

66
module.exports = (send) => {
7-
const cat = promisify((multihash, callback) => {
7+
return promisify((hash, callback) => {
88
try {
9-
multihash = cleanMultihash(multihash)
9+
hash = cleanMultihash(hash)
1010
} catch (err) {
1111
return callback(err)
1212
}
13-
send('cat', multihash, null, null, callback)
13+
14+
return send({
15+
path: 'cat',
16+
args: hash
17+
}, callback)
1418
})
15-
return cat
1619
}

src/api/commands.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict'
22

3-
const command = require('../cmd-helpers').command
4-
53
module.exports = (send) => {
6-
return command(send, 'commands')
4+
return (callback) => {
5+
return send({
6+
path: 'commands'
7+
}, callback)
8+
}
79
}

src/api/config.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ module.exports = (send) => {
1111
}
1212

1313
if (!key) {
14-
return send('config/show', null, null, null, true, callback)
14+
return send({
15+
path: 'config/show',
16+
buffer: true
17+
}, callback)
1518
}
1619

17-
return send('config', key, null, null, (err, result) => {
20+
return send({
21+
path: 'config',
22+
args: key,
23+
buffer: true
24+
}, (err, response) => {
1825
if (err) {
1926
return callback(err)
2027
}
21-
callback(null, result.Value)
28+
callback(null, response.Value)
2229
})
2330
},
2431
set (key, value, opts, callback) {
@@ -46,19 +53,24 @@ module.exports = (send) => {
4653
opts = { bool: true }
4754
}
4855

49-
return send('config', [key, value], opts, null, callback)
56+
return send({
57+
path: 'config',
58+
args: [key, value],
59+
qs: opts,
60+
files: undefined,
61+
buffer: true
62+
}, callback)
5063
},
5164
replace (config, callback) {
52-
// Its a path
53-
if (typeof config === 'string') {
54-
return send('config/replace', null, null, config, callback)
55-
}
56-
57-
// Its a config obj
5865
if (typeof config === 'object') {
5966
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
60-
return send('config/replace', null, null, config, callback)
6167
}
68+
69+
return send({
70+
path: 'config/replace',
71+
files: config,
72+
buffer: true
73+
}, callback)
6274
}
6375
}
6476
}

0 commit comments

Comments
 (0)