Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5ff633e

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 #1395 License: MIT Signed-off-by: Pascal Precht <[email protected]>
1 parent 18888be commit 5ff633e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/core/components/dag.js

+15
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+
return callback(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+
return callback(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)