Skip to content

Commit 5ff8281

Browse files
committed
fix(core/components/dag): make options in put API optional
The [dag.put](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DAG.md#dagput) interface takes an options object which is required, but should be optional with decent defaults. See dedicated test here: ipfs-inactive/interface-js-ipfs-core#316 This commit implements this behaviour. **Before**: ```js ipfs.dag.put(obj, options, (err, cid) => {...}); ``` ^ Prior to this commit, without passing `options`, this call resulted in an error. **After**: ```js ipfs.dag.put(obj, (err, cid) => {...}); ``` ^ This is now perfectly fine. Fixes ipfs#1395 License: MIT Signed-off-by: Pascal Precht <[email protected]>
1 parent 402b865 commit 5ff8281

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/core/components/dag.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ const flattenDeep = require('lodash.flattendeep')
99
module.exports = function dag (self) {
1010
return {
1111
put: promisify((dagNode, options, callback) => {
12+
if (typeof options === 'function') {
13+
callback = options
14+
} else if (options.cid && options.format && options.hashAlg) {
15+
throw new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.')
16+
} else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) {
17+
throw new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.')
18+
}
19+
20+
const optionDefaults = {
21+
format: 'dag-cbor',
22+
hashAlg: 'sha2-255'
23+
}
24+
25+
options = options.cid ? options : Object.assign({}, optionDefaults, options)
26+
1227
self._ipld.put(dagNode, options, callback)
1328
}),
1429

0 commit comments

Comments
 (0)