diff --git a/package.json b/package.json index 283a330c5..15cfe30ee 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "bl": "^3.0.0", "bs58": "^4.0.1", "buffer": "^5.4.2", + "callbackify": "^1.1.0", "cids": "~0.7.1", "concat-stream": "github:hugomrdias/concat-stream#feat/smaller", "debug": "^4.1.0", @@ -106,7 +107,7 @@ "browser-process-platform": "~0.1.1", "cross-env": "^6.0.0", "go-ipfs-dep": "^0.4.22", - "interface-ipfs-core": "^0.115.3", + "interface-ipfs-core": "^0.117.0", "ipfsd-ctl": "^0.47.1", "nock": "^11.3.2", "stream-equal": "^1.1.1" diff --git a/src/config/index.js b/src/config/index.js index d690d8099..e88c195b5 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,14 +1,13 @@ 'use strict' -const moduleConfig = require('../utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - +module.exports = (send, config) => { return { get: require('./get')(send), set: require('./set')(send), replace: require('./replace')(send), - profile: require('./profile')(send) + profiles: { + apply: require('./profiles/apply')(config), + list: require('./profiles/list')(config) + } } } diff --git a/src/config/profile.js b/src/config/profile.js deleted file mode 100644 index 809adb5d4..000000000 --- a/src/config/profile.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -const toObject = function (res, callback) { - if (Buffer.isBuffer(res)) { - callback(null, JSON.parse(res.toString())) - } else { - callback(null, res) - } -} - -module.exports = (send) => { - return promisify((profile, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - - opts = normalizeOpts(opts) - - send.andTransform({ - path: 'config/profile/apply', - args: profile, - qs: opts - }, toObject, (err, response) => { - if (err) { - return callback(err) - } - callback(null, { oldCfg: response.OldCfg, newCfg: response.NewCfg }) - }) - }) -} - -function normalizeOpts (opts) { - opts = opts || {} - if (typeof opts.dryRun !== 'undefined') { - opts['dry-run'] = opts.dryRun - } - return opts -} diff --git a/src/config/profiles/apply.js b/src/config/profiles/apply.js new file mode 100644 index 000000000..e4b05560b --- /dev/null +++ b/src/config/profiles/apply.js @@ -0,0 +1,27 @@ +'use strict' + +const callbackify = require('callbackify') +const configure = require('../../lib/configure') + +module.exports = configure(({ ky }) => { + return callbackify.variadic(async (profile, options) => { + options = options || {} + + const res = await ky.post('config/profile/apply', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: { + arg: profile, + // can only pass strings or numbers as values https://github.com/sindresorhus/ky/issues/182 + 'dry-run': options.dryRun ? 'true' : 'false' + } + }) + + const parsed = await res.json() + + return { + original: parsed.OldCfg, updated: parsed.NewCfg + } + }) +}) diff --git a/src/config/profiles/list.js b/src/config/profiles/list.js new file mode 100644 index 000000000..dbfa579cf --- /dev/null +++ b/src/config/profiles/list.js @@ -0,0 +1,22 @@ +'use strict' + +const callbackify = require('callbackify') +const configure = require('../../lib/configure') +const toCamel = require('../../lib/object-to-camel') + +module.exports = configure(({ ky }) => { + return callbackify.variadic(async (options) => { + options = options || {} + + const res = await ky.get('config/profile/list', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers + }) + + const parsed = await res.json() + + return parsed + .map(profile => toCamel(profile)) + }) +}) diff --git a/test/interface.spec.js b/test/interface.spec.js index e3c8ffb19..b6f00342e 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -48,6 +48,10 @@ describe('interface-ipfs-core tests', () => { { name: 'replace', reason: 'FIXME Waiting for fix on go-ipfs https://github.com/ipfs/js-ipfs-http-client/pull/307#discussion_r69281789 and https://github.com/ipfs/go-ipfs/issues/2927' + }, + { + name: 'should list config profiles', + reason: 'TODO: Not implemented in go-ipfs' } ] }) diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 036c0759f..fbecfe637 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -38,7 +38,9 @@ describe('submodules', () => { expect(cfg.get).to.be.a('function') expect(cfg.set).to.be.a('function') expect(cfg.replace).to.be.a('function') - expect(cfg.profile).to.be.a('function') + expect(cfg).to.have.a.property('profiles') + expect(cfg.profiles.list).to.be.a('function') + expect(cfg.profiles.apply).to.be.a('function') }) it('dht', () => {