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

Commit

Permalink
refactor(request-api): mov away from arg flags, use option object ins…
Browse files Browse the repository at this point in the history
…tead, migrate every call to it
  • Loading branch information
daviddias committed Aug 15, 2016
1 parent 108414b commit 43a1dfa
Show file tree
Hide file tree
Showing 31 changed files with 712 additions and 289 deletions.
21 changes: 13 additions & 8 deletions src/api/add-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ const isNode = require('detect-node')
const addToDagNodesTransform = require('../add-to-dagnode-transform')

module.exports = (send) => {
return function add (path, opts, cb) {
if (typeof (opts) === 'function' && cb === undefined) {
cb = opts
return function add (path, opts, callback) {
if (typeof (opts) === 'function' &&
callback === undefined) {
callback = opts
opts = {}
}

if (!isNode) {
return cb(new Error('Recursive uploads are not supported in the browser'))
return callback(new Error('Recursive uploads are not supported in the browser'))
}

if (typeof (path) !== 'string') {
return cb(new Error('"path" must be a string'))
if (typeof path !== 'string') {
return callback(new Error('"path" must be a string'))
}

var sendWithTransform = send.withTransform(addToDagNodesTransform)
const sendWithTransform = send.withTransform(addToDagNodesTransform)

return sendWithTransform('add', null, opts, path, cb)
return sendWithTransform({
path: 'add',
qs: opts,
files: path
}, callback)
}
}
24 changes: 16 additions & 8 deletions src/api/add-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ const Wreck = require('wreck')
const addToDagNodesTransform = require('../add-to-dagnode-transform')

module.exports = (send) => {
return function add (url, opts, cb) {
if (typeof (opts) === 'function' && cb === undefined) {
cb = opts
return function add (url, opts, callback) {
if (typeof (opts) === 'function' &&
callback === undefined) {
callback = opts
opts = {}
}

if (typeof url !== 'string' || !url.startsWith('http')) {
return cb(new Error('"url" param must be an http(s) url'))
if (typeof url !== 'string' ||
!url.startsWith('http')) {
return callback(new Error('"url" param must be an http(s) url'))
}

var sendWithTransform = send.withTransform(addToDagNodesTransform)
const sendWithTransform = send.withTransform(addToDagNodesTransform)

Wreck.request('GET', url, null, (err, res) => {
if (err) return cb(err)
if (err) {
return callback(err)
}

sendWithTransform('add', null, opts, res, cb)
sendWithTransform({
path: 'add',
qs: opts,
files: res
}, callback)
})
}
}
5 changes: 4 additions & 1 deletion src/api/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module.exports = (send) => {

const sendWithTransform = send.withTransform(addToDagNodesTransform)

sendWithTransform('add', null, {}, files, callback)
return sendWithTransform({
path: 'add',
files: files
}, callback)
})
}
26 changes: 19 additions & 7 deletions src/api/bitswap.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
'use strict'

const argCommand = require('../cmd-helpers').argCommand

module.exports = (send) => {
return {
wantlist (cb) {
return send('bitswap/wantlist', {}, null, null, cb)
wantlist (callback) {
return send({
path: 'bitswap/wantlist'
}, callback)
},
stat (cb) {
return send('bitswap/stat', {}, null, null, cb)
stat (callback) {
return send({
path: 'bitswap/stat'
}, callback)
},
unwant: argCommand(send, 'bitswap/unwant')
unwant (args, opts, callback) {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return send({
path: 'bitswap/unwant',
args: args,
qs: opts
}, callback)
}
}
}
41 changes: 32 additions & 9 deletions src/api/block.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
'use strict'

const argCommand = require('../cmd-helpers').argCommand

module.exports = (send) => {
return {
get: argCommand(send, 'block/get'),
stat: argCommand(send, 'block/stat'),
put (file, cb) {
get (args, opts, callback) {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return send({
path: 'block/get',
args: args,
qs: opts
}, callback)
},
stat (args, opts, callback) {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return send({
path: 'block/stat',
args: args,
qs: opts
}, callback)
},
put (file, callback) {
if (Array.isArray(file)) {
let err = new Error('block.put() only accepts 1 file')
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
const err = new Error('block.put() only accepts 1 file')
if (typeof callback !== 'function' &&
typeof Promise !== 'undefined') {
return new Promise((resolve, reject) => reject(err))
}
return cb(err)
return callback(err)
}
return send('block/put', null, null, file, cb)

return send({
path: 'block/put',
files: file
}, callback)
}
}
}
39 changes: 28 additions & 11 deletions src/api/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
'use strict'

const command = require('../cmd-helpers').command

module.exports = (send) => {
return {
add: (arg, opts, cb) => {
if (typeof opts === 'function' && cb === undefined) {
cb = opts
add (args, opts, callback) {
if (typeof opts === 'function' &&
callback === undefined) {
callback = opts
opts = {}
}
return send('bootstrap/add', arg, opts, null, cb)
return send({
path: 'bootstrap/add',
args: args,
qs: opts
}, callback)
},
rm: (arg, opts, cb) => {
if (typeof opts === 'function' && cb === undefined) {
cb = opts
rm (args, opts, callback) {
if (typeof opts === 'function' &&
callback === undefined) {
callback = opts
opts = {}
}
return send('bootstrap/rm', arg, opts, null, cb)
return send({
path: 'bootstrap/rm',
args: args,
qs: opts
}, callback)
},
list: command(send, 'bootstrap/list')
list (opts, callback) {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return send({
path: 'bootstrap/list',
qs: opts
}, callback)
}
}
}
11 changes: 7 additions & 4 deletions src/api/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ const promisify = require('promisify-es6')
const cleanMultihash = require('../clean-multihash')

module.exports = (send) => {
const cat = promisify((multihash, callback) => {
return promisify((hash, callback) => {
try {
multihash = cleanMultihash(multihash)
hash = cleanMultihash(hash)
} catch (err) {
return callback(err)
}
send('cat', multihash, null, null, callback)

return send({
path: 'cat',
args: hash
}, callback)
})
return cat
}
8 changes: 5 additions & 3 deletions src/api/commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const command = require('../cmd-helpers').command

module.exports = (send) => {
return command(send, 'commands')
return (callback) => {
return send({
path: 'commands'
}, callback)
}
}
34 changes: 23 additions & 11 deletions src/api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ module.exports = (send) => {
}

if (!key) {
return send('config/show', null, null, null, true, callback)
return send({
path: 'config/show',
buffer: true
}, callback)
}

return send('config', key, null, null, (err, result) => {
return send({
path: 'config',
args: key,
buffer: true
}, (err, response) => {
if (err) {
return callback(err)
}
callback(null, result.Value)
callback(null, response.Value)
})
},
set (key, value, opts, callback) {
Expand Down Expand Up @@ -46,19 +53,24 @@ module.exports = (send) => {
opts = { bool: true }
}

return send('config', [key, value], opts, null, callback)
return send({
path: 'config',
args: [key, value],
qs: opts,
files: undefined,
buffer: true
}, callback)
},
replace (config, callback) {
// Its a path
if (typeof config === 'string') {
return send('config/replace', null, null, config, callback)
}

// Its a config obj
if (typeof config === 'object') {
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
return send('config/replace', null, null, config, callback)
}

return send({
path: 'config/replace',
files: config,
buffer: true
}, callback)
}
}
}
Loading

0 comments on commit 43a1dfa

Please sign in to comment.