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

refactor: convert pin API to async/await #1168

Merged
merged 1 commit into from
Nov 19, 2019
Merged
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
38 changes: 19 additions & 19 deletions src/pin/add.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict'

const promisify = require('promisify-es6')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((hash, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = null
}
send({
path: 'pin/add',
args: hash,
qs: opts
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, res.Pins.map((hash) => ({ hash: hash })))
})
})
}
module.exports = configure(({ ky }) => {
return async (path, options) => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', `${path}`)
if (options.recursive != null) searchParams.set('recursive', options.recursive)

const res = await ky.post('pin/add', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return (res.Pins || []).map(hash => ({ hash }))
}
})
16 changes: 6 additions & 10 deletions src/pin/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict'

const moduleConfig = require('../utils/module-config')
const callbackify = require('callbackify')

module.exports = (arg) => {
const send = moduleConfig(arg)

return {
add: require('./add')(send),
rm: require('./rm')(send),
ls: require('./ls')(send)
}
}
module.exports = config => ({
add: callbackify.variadic(require('./add')(config)),
rm: callbackify.variadic(require('./rm')(config)),
ls: callbackify.variadic(require('./ls')(config))
})
52 changes: 23 additions & 29 deletions src/pin/ls.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
'use strict'

const promisify = require('promisify-es6')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((hash, opts, callback) => {
if (typeof hash === 'function') {
callback = hash
opts = null
hash = null
module.exports = configure(({ ky }) => {
return async (path, options) => {
if (path && path.type) {
options = path
path = null
}
if (typeof opts === 'function') {
callback = opts
opts = null
}
if (hash && hash.type) {
opts = hash
hash = null
}
send({
path: 'pin/ls',
args: hash,
qs: opts
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, Object.keys(res.Keys).map(hash => (
{ hash, type: res.Keys[hash].Type }
)))
})
})
}

options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
if (path) searchParams.set('arg', `${path}`)
if (options.type) searchParams.set('type', options.type)

const { Keys } = await ky.get('pin/ls', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return Object.keys(Keys).map(hash => ({ hash, type: Keys[hash].Type }))
}
})
38 changes: 19 additions & 19 deletions src/pin/rm.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict'

const promisify = require('promisify-es6')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((hash, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = null
}
send({
path: 'pin/rm',
args: hash,
qs: opts
}, (err, res) => {
if (err) {
return callback(err)
}
callback(null, res.Pins.map((hash) => ({ hash: hash })))
})
})
}
module.exports = configure(({ ky }) => {
return async (path, options) => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', `${path}`)
if (options.recursive != null) searchParams.set('recursive', options.recursive)

const res = await ky.post('pin/rm', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return (res.Pins || []).map(hash => ({ hash }))
}
})
4 changes: 2 additions & 2 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ function requireCommands (send, config) {
config: require('../config')(config),
dag: require('../dag')(config),
dht: require('../dht')(config),
diag: require('../diag')(config)
diag: require('../diag')(config),
pin: require('../pin')(config)
}

Object.assign(cmds.refs, {
Expand All @@ -129,7 +130,6 @@ function requireCommands (send, config) {

// Graph
object: require('../object'),
pin: require('../pin'),

// Network
name: require('../name'),
Expand Down