From d299ed77c288d82ce807af0ec7a5b2605336626f Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 3 Jul 2018 12:12:00 +0200 Subject: [PATCH] fix(dag): make options in `put` API optional (#1415) * 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: https://github.com/ipfs/interface-ipfs-core/pull/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 * chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- src/core/components/dag.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 279fde99e3..864dc977a7 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "expose-loader": "~0.7.5", "form-data": "^2.3.2", "hat": "0.0.3", - "interface-ipfs-core": "~0.70.2", + "interface-ipfs-core": "~0.70.3", "ipfsd-ctl": "~0.37.3", "mocha": "^5.1.1", "ncp": "^2.0.0", diff --git a/src/core/components/dag.js b/src/core/components/dag.js index 78b1f50145..6cdccf8885 100644 --- a/src/core/components/dag.js +++ b/src/core/components/dag.js @@ -9,6 +9,21 @@ const flattenDeep = require('lodash/flattenDeep') module.exports = function dag (self) { return { put: promisify((dagNode, options, callback) => { + if (typeof options === 'function') { + callback = options + } else if (options.cid && (options.format || options.hashAlg)) { + return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.')) + } else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) { + return callback(new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.')) + } + + const optionDefaults = { + format: 'dag-cbor', + hashAlg: 'sha2-255' + } + + options = options.cid ? options : Object.assign({}, optionDefaults, options) + self._ipld.put(dagNode, options, callback) }),