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

Commit

Permalink
feat (breaking change): normalize fields on files.ls and flatten outp…
Browse files Browse the repository at this point in the history
…ut (#670)
  • Loading branch information
hacdias authored and daviddias committed Jan 21, 2018
1 parent 5803d39 commit 98000af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
15 changes: 13 additions & 2 deletions src/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, res.Entries.map((entry) => {
return {
name: entry.Name,
type: entry.Type,
size: entry.Size,
hash: entry.Hash
}
}))
}

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return send({
return send.andTransform({
path: 'files/ls',
args: args,
qs: opts
}, callback)
}, transform, callback)
})
}
14 changes: 12 additions & 2 deletions src/files/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
type: res.Type,
blocks: res.Blocks,
size: res.Size,
hash: res.Hash,
cumulativeSize: res.CumulativeSize
})
}

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
send.andTransform({
path: 'files/stat',
args: args,
qs: opts
}, callback)
}, transform, callback)
})
}
24 changes: 12 additions & 12 deletions test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe('.files (the MFS API part)', function () {
it('files.ls', (done) => {
ipfs.files.ls('/test-folder', (err, res) => {
expect(err).to.not.exist()
expect(res.Entries.length).to.equal(1)
expect(res.length).to.equal(1)
done()
})
})
Expand Down Expand Up @@ -286,11 +286,11 @@ describe('.files (the MFS API part)', function () {
ipfs.files.stat('/test-folder/test-file', (err, res) => {
expect(err).to.not.exist()
expect(res).to.deep.equal({
Hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
Size: 12,
CumulativeSize: 20,
Blocks: 0,
Type: 'file'
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
size: 12,
cumulativeSize: 20,
blocks: 0,
type: 'file'
})

done()
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('.files (the MFS API part)', function () {
it('files.ls', () => {
return ipfs.files.ls('/test-folder')
.then((res) => {
expect(res.Entries.length).to.equal(1)
expect(res.length).to.equal(1)
})
})

Expand Down Expand Up @@ -458,11 +458,11 @@ describe('.files (the MFS API part)', function () {
return ipfs.files.stat('/test-folder/test-file')
.then((res) => {
expect(res).to.deep.equal({
Hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
Size: 12,
CumulativeSize: 20,
Blocks: 0,
Type: 'file'
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
size: 12,
cumulativeSize: 20,
blocks: 0,
type: 'file'
})
})
})
Expand Down

0 comments on commit 98000af

Please sign in to comment.