diff --git a/src/key/export.js b/src/key/export.js index f19db4022..810555e9a 100644 --- a/src/key/export.js +++ b/src/key/export.js @@ -1,16 +1,25 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((name, password, callback) => { - send({ - path: 'key/export', - args: name, - qs: { password: password } - }, (err, pem) => { - if (err) return callback(err) - callback(null, pem.toString()) - }) - }) -} +module.exports = configure(({ ky }) => { + return (name, password, options) => { + if (typeof password !== 'string') { + options = password + password = null + } + + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + if (password) searchParams.set('password', password) + + return ky.get('key/export', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/key/gen.js b/src/key/gen.js index 38612d9af..5fd6acb86 100644 --- a/src/key/gen.js +++ b/src/key/gen.js @@ -1,25 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - name: res.Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, options) => { + options = options || {} -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + if (options.type) searchParams.set('type', options.type) + if (options.size != null) searchParams.set('size', options.size) - send.andTransform({ - path: 'key/gen', - args: args, - qs: opts - }, transform, callback) - }) -} + const res = await ky.post('key/gen', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/import.js b/src/key/import.js index 9cea80e27..7cc8f5ddc 100644 --- a/src/key/import.js +++ b/src/key/import.js @@ -1,23 +1,29 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - name: res.Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, pem, password, options) => { + if (typeof password !== 'string') { + options = password + password = null + } -module.exports = (send) => { - return promisify((name, pem, password, callback) => { - send.andTransform({ - path: 'key/import', - args: name, - qs: { - pem: pem, - password: password - } - }, transform, callback) - }) -} + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + searchParams.set('pem', pem) + if (password) searchParams.set('password', password) + + const res = await ky.post('key/import', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/index.js b/src/key/index.js index 717d719c3..7293236f1 100644 --- a/src/key/index.js +++ b/src/key/index.js @@ -1,16 +1,12 @@ 'use strict' -const moduleConfig = require('../utils/module-config') +const callbackify = require('callbackify') -module.exports = (arg) => { - const send = moduleConfig(arg) - - return { - gen: require('./gen')(send), - list: require('./list')(send), - rename: require('./rename')(send), - rm: require('./rm')(send), - export: require('./export')(send), - import: require('./import')(send) - } -} +module.exports = config => ({ + gen: callbackify.variadic(require('./gen')(config)), + list: callbackify.variadic(require('./list')(config)), + rename: callbackify.variadic(require('./rename')(config)), + rm: callbackify.variadic(require('./rm')(config)), + export: callbackify.variadic(require('./export')(config)), + import: callbackify.variadic(require('./import')(config)) +}) diff --git a/src/key/list.js b/src/key/list.js index bcd9bc8e2..d6f0dd578 100644 --- a/src/key/list.js +++ b/src/key/list.js @@ -1,26 +1,19 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, res.Keys.map(key => { - return { - id: key.Id, - name: key.Name - } - })) -} +module.exports = configure(({ ky }) => { + return async options => { + options = options || {} -module.exports = (send) => { - return promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const res = await ky.get('key/list', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() - send.andTransform({ - path: 'key/list', - qs: opts - }, transform, callback) - }) -} + return (res.Keys || []).map(k => toCamel(k)) + } +}) diff --git a/src/key/rename.js b/src/key/rename.js index 68b45f010..18e069768 100644 --- a/src/key/rename.js +++ b/src/key/rename.js @@ -1,21 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - was: res.Was, - now: res.Now, - overwrite: res.Overwrite - }) -} +module.exports = configure(({ ky }) => { + return async (oldName, newName, options) => { + options = options || {} -module.exports = (send) => { - return promisify((oldName, newName, callback) => { - send.andTransform({ - path: 'key/rename', - args: [oldName, newName] - }, transform, callback) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', oldName) + searchParams.append('arg', newName) + if (options.force != null) searchParams.set('force', options.force) + + const res = await ky.post('key/rename', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/rm.js b/src/key/rm.js index 7e4e98500..edab61060 100644 --- a/src/key/rm.js +++ b/src/key/rm.js @@ -1,19 +1,22 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Keys[0].Id, - name: res.Keys[0].Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, options) => { + options = options || {} -module.exports = (send) => { - return promisify((args, callback) => { - send.andTransform({ - path: 'key/rm', - args: args - }, transform, callback) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + + const res = await ky.post('key/rm', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res.Keys[0]) + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index a4a6dcd07..891dd4f2b 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -116,6 +116,7 @@ function requireCommands (send, config) { dht: require('../dht')(config), diag: require('../diag')(config), files: require('../files')(config), + key: require('../key')(config), pin: require('../pin')(config) } @@ -139,7 +140,6 @@ function requireCommands (send, config) { // Miscellaneous commands: require('../commands'), id: require('../id'), - key: require('../key'), log: require('../log'), mount: require('../mount'), repo: require('../repo'),