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

Commit

Permalink
Merge pull request #313 from ipfs/clean-madness
Browse files Browse the repository at this point in the history
Reduce de complexity of the js-ipfs-api module
  • Loading branch information
daviddias committed Aug 15, 2016
2 parents 73078ea + 11a166e commit 776cccd
Show file tree
Hide file tree
Showing 51 changed files with 1,134 additions and 492 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://exam

### API

> `WIP`
`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.
> `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.
### Utility functions

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

```JavaScript
```
>>>>>> refactor(module+tests): Enable running the tests
### Callbacks and promises

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



>>>>>>> kick off ipfs-api next generation
## Contribute

The js-ipfs-api is a work in progress. As such, there's a few things you can do right now to help out:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "ipfs-api",
"version": "6.0.3",
"description": "A client library for the IPFS API",
"main": "src/index.js",
"description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"test": "gulp test",
Expand Down
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 776cccd

Please sign in to comment.