Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

refactor: convert key API to async/await #1164

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/key/export.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
39 changes: 19 additions & 20 deletions src/key/gen.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
44 changes: 25 additions & 19 deletions src/key/import.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
22 changes: 9 additions & 13 deletions src/key/index.js
Original file line number Diff line number Diff line change
@@ -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))
})
35 changes: 14 additions & 21 deletions src/key/list.js
Original file line number Diff line number Diff line change
@@ -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))
}
})
37 changes: 20 additions & 17 deletions src/key/rename.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
33 changes: 18 additions & 15 deletions src/key/rm.js
Original file line number Diff line number Diff line change
@@ -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])
}
})
2 changes: 1 addition & 1 deletion src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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'),
Expand Down