This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert name API to async/await
Depends on: * [ ] ipfs-inactive/interface-js-ipfs-core#561 License: MIT Signed-off-by: Alan Shaw <[email protected]>
- Loading branch information
Alan Shaw
committed
Nov 19, 2019
1 parent
12b7a2e
commit cdf9390
Showing
9 changed files
with
102 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
publish: require('./publish')(send), | ||
resolve: require('./resolve')(send), | ||
pubsub: require('./pubsub')(send) | ||
} | ||
} | ||
module.exports = config => ({ | ||
publish: callbackify.variadic(require('./publish')(config)), | ||
resolve: callbackify.variadic(require('./resolve')(config)), | ||
pubsub: require('./pubsub')(config) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
'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, { | ||
name: res.Name, | ||
value: res.Value | ||
}) | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (path, 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', path) | ||
if (options.allowOffline != null) searchParams.set('allow-offline', options.allowOffline) | ||
if (options.key) searchParams.set('key', options.key) | ||
if (options.lifetime) searchParams.set('lifetime', options.lifetime) | ||
if (options.quieter != null) searchParams.set('quieter', options.quieter) | ||
if (options.resolve != null) searchParams.set('resolve', options.resolve) | ||
if (options.ttl) searchParams.set('ttl', options.ttl) | ||
|
||
send.andTransform({ | ||
path: 'name/publish', | ||
args: args, | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
const res = await ky.post('name/publish', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
return toCamel(res) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +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, { | ||
canceled: res.Canceled === undefined || res.Canceled === true | ||
}) | ||
} | ||
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) | ||
|
||
send.andTransform({ | ||
path: 'name/pubsub/cancel', | ||
args: args, | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
const res = await ky.post('name/pubsub/cancel', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
return toCamel(res) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
'use strict' | ||
|
||
module.exports = (send) => ({ | ||
cancel: require('./cancel')(send), | ||
state: require('./state')(send), | ||
subs: require('./subs')(send) | ||
const callbackify = require('callbackify') | ||
|
||
module.exports = config => ({ | ||
cancel: callbackify.variadic(require('./cancel')(config)), | ||
state: callbackify.variadic(require('./state')(config)), | ||
subs: callbackify.variadic(require('./subs')(config)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +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, { | ||
enabled: res.Enabled | ||
}) | ||
} | ||
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.post('name/pubsub/state', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams: options.searchParams | ||
}).json() | ||
|
||
send.andTransform({ | ||
path: 'name/pubsub/state', | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
return toCamel(res) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const configure = require('../../lib/configure') | ||
|
||
const transform = function (res, callback) { | ||
callback(null, res.Strings || []) | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (name, options) => { | ||
options = options || {} | ||
|
||
module.exports = (send) => { | ||
return promisify((opts, callback) => { | ||
if (typeof (opts) === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
const res = await ky.get('name/pubsub/subs', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams: options.searchParams | ||
}).json() | ||
|
||
send.andTransform({ | ||
path: 'name/pubsub/subs', | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
return res.Strings | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const configure = require('../lib/configure') | ||
|
||
const transform = function (res, callback) { | ||
callback(null, res.Path) | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (path, 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', path) | ||
if (options.dhtRecordCount != null) searchParams.set('dht-record-count', options.dhtRecordCount) | ||
if (options.dhtTimeout != null) searchParams.set('dht-timeout', options.dhtTimeout) | ||
if (options.noCache != null) searchParams.set('nocache', options.noCache) | ||
if (options.recursive != null) searchParams.set('recursive', options.recursive) | ||
|
||
send.andTransform({ | ||
path: 'name/resolve', | ||
args: args, | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
const res = await ky.post('name/resolve', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
return res.Path | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters