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

Commit ebd915e

Browse files
refactor: Split api definitions into separate files
1 parent 06094d1 commit ebd915e

24 files changed

+345
-231
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"multipart-stream": "^2.0.0",
1010
"ndjson": "^1.4.3",
1111
"qs": "^6.0.0",
12+
"require-dir": "^0.3.0",
1213
"vinyl": "^1.1.0",
1314
"vinyl-fs-browser": "^2.1.1-1",
1415
"vinyl-multipart-stream": "^1.2.6",
@@ -50,7 +51,6 @@
5051
"mocha": "^2.3.3",
5152
"pre-commit": "^1.0.6",
5253
"raw-loader": "^0.5.1",
53-
"require-dir": "^0.3.0",
5454
"rimraf": "^2.4.3",
5555
"run-sequence": "^1.1.4",
5656
"stream-equal": "^0.1.7",

src/api/add.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Wreck = require('wreck')
2+
3+
module.exports = send => {
4+
return function add (files, opts, cb) {
5+
if (typeof (opts) === 'function' && cb === undefined) {
6+
cb = opts
7+
opts = {}
8+
}
9+
10+
if (typeof files === 'string' && files.startsWith('http')) {
11+
Wreck.request('GET', files, null, (err, res) => {
12+
if (err) return cb(err)
13+
14+
send('add', null, opts, res, cb)
15+
})
16+
17+
return
18+
}
19+
20+
send('add', null, opts, files, cb)
21+
}
22+
}

src/api/block.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'block/get'),
8+
put (file, cb) {
9+
if (Array.isArray(file)) {
10+
return cb(null, new Error('block.put() only accepts 1 file'))
11+
}
12+
return send('block/put', null, null, file, cb)
13+
}
14+
}
15+
}

src/api/cat.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'cat')
7+
}

src/api/commands.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'commands')
7+
}

src/api/config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'config'),
8+
set (key, value, opts, cb) {
9+
if (typeof (opts) === 'function') {
10+
cb = opts
11+
opts = {}
12+
}
13+
return send('config', [key, value], opts, null, cb)
14+
},
15+
show (cb) {
16+
return send('config/show', null, null, null, true, cb)
17+
},
18+
replace (file, cb) {
19+
return send('config/replace', null, null, file, cb)
20+
}
21+
}
22+
}

src/api/dht.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
findprovs: argCommand(send, 'dht/findprovs'),
8+
get (key, opts, cb) {
9+
if (typeof (opts) === 'function' && !cb) {
10+
cb = opts
11+
opts = null
12+
}
13+
14+
return send('dht/get', key, opts, null, (err, res) => {
15+
if (err) return cb(err)
16+
if (!res) return cb(new Error('empty response'))
17+
if (res.length === 0) return cb(new Error('no value returned for key'))
18+
19+
// Inconsistent return values in the browser vs node
20+
if (Array.isArray(res)) {
21+
res = res[0]
22+
}
23+
24+
if (res.Type === 5) {
25+
cb(null, res.Extra)
26+
} else {
27+
cb(res)
28+
}
29+
})
30+
},
31+
put (key, value, opts, cb) {
32+
if (typeof (opts) === 'function' && !cb) {
33+
cb = opts
34+
opts = null
35+
}
36+
37+
return send('dht/put', [key, value], opts, null, cb)
38+
}
39+
}
40+
}

src/api/diag.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
net: command(send, 'diag/net'),
8+
sys: command(send, 'diag/sys')
9+
}
10+
}

src/api/id.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function id (id, cb) {
5+
if (typeof id === 'function') {
6+
cb = id
7+
id = null
8+
}
9+
return send('id', id, null, null, cb)
10+
}
11+
}

src/api/log.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
const ndjson = require('ndjson')
4+
5+
module.exports = send => {
6+
return {
7+
tail (cb) {
8+
send('log/tail', null, {}, null, false, (err, res) => {
9+
if (err) return cb(err)
10+
cb(null, res.pipe(ndjson.parse()))
11+
})
12+
}
13+
}
14+
}

src/api/ls.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'ls')
7+
}

src/api/mount.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function mount (ipfs, ipns, cb) {
5+
if (typeof ipfs === 'function') {
6+
cb = ipfs
7+
ipfs = null
8+
} else if (typeof ipns === 'function') {
9+
cb = ipns
10+
ipns = null
11+
}
12+
const opts = {}
13+
if (ipfs) opts.f = ipfs
14+
if (ipns) opts.n = ipns
15+
return send('mount', null, opts, null, cb)
16+
}
17+
}

src/api/name.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
publish: argCommand(send, 'name/publish'),
8+
resolve: argCommand(send, 'name/resolve')
9+
}
10+
}

src/api/object.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'object/get'),
8+
put (file, encoding, cb) {
9+
if (typeof encoding === 'function') {
10+
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
11+
}
12+
return send('object/put', encoding, null, file, cb)
13+
},
14+
data: argCommand(send, 'object/data'),
15+
stat: argCommand(send, 'object/stat'),
16+
links: argCommand(send, 'object/links'),
17+
patch (file, opts, cb) {
18+
return send('object/patch', [file].concat(opts), null, null, cb)
19+
}
20+
}
21+
}

src/api/pin.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return {
5+
add (hash, opts, cb) {
6+
if (typeof opts === 'function') {
7+
cb = opts
8+
opts = null
9+
}
10+
11+
send('pin/add', hash, opts, null, cb)
12+
},
13+
remove (hash, opts, cb) {
14+
if (typeof opts === 'function') {
15+
cb = opts
16+
opts = null
17+
}
18+
19+
send('pin/rm', hash, opts, null, cb)
20+
},
21+
list (type, cb) {
22+
if (typeof type === 'function') {
23+
cb = type
24+
type = null
25+
}
26+
let opts = null
27+
if (type) opts = { type: type }
28+
return send('pin/ls', null, opts, null, cb)
29+
}
30+
}
31+
}

src/api/ping.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function ping (id, cb) {
5+
return send('ping', id, { n: 1 }, null, function (err, res) {
6+
if (err) return cb(err, null)
7+
cb(null, res[1])
8+
})
9+
}
10+
}

src/api/refs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
const refs = cmds.argCommand(send, 'refs')
7+
refs.local = cmds.command(send, 'refs/local')
8+
9+
return refs
10+
}

src/api/swarm.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
return {
7+
peers: cmds.command(send, 'swarm/peers'),
8+
connect: cmds.argCommand(send, 'swarm/connect')
9+
}
10+
}

src/api/update.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
apply: command(send, 'update'),
8+
check: command(send, 'update/check'),
9+
log: command(send, 'update/log')
10+
}
11+
}

src/api/version.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'version')
7+
}

src/cmd-helpers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
exports.command = function command (send, name) {
4+
return (opts, cb) => {
5+
if (typeof (opts) === 'function') {
6+
cb = opts
7+
opts = {}
8+
}
9+
return send(name, null, opts, null, cb)
10+
}
11+
}
12+
13+
exports.argCommand = function argCommand (send, name) {
14+
return (arg, opts, cb) => {
15+
if (typeof (opts) === 'function') {
16+
cb = opts
17+
opts = {}
18+
}
19+
return send(name, arg, opts, null, cb)
20+
}
21+
}

0 commit comments

Comments
 (0)