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

Commit 43a1dfa

Browse files
committed
refactor(request-api): mov away from arg flags, use option object instead, migrate every call to it
1 parent 108414b commit 43a1dfa

31 files changed

+712
-289
lines changed

src/api/add-files.js

Lines changed: 13 additions & 8 deletions
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

Lines changed: 16 additions & 8 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 19 additions & 7 deletions
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

Lines changed: 32 additions & 9 deletions
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

Lines changed: 28 additions & 11 deletions
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 23 additions & 11 deletions
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)