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

Commit c19c3f5

Browse files
committed
add opts to constructor
1 parent 1b6fd6c commit c19c3f5

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports = module.exports = () => {
77
'api-path': '/api/v0/',
88
'user-agent': `/node-$pkg.name}/${pkg.version}/`,
99
'host': 'localhost',
10-
'port': '5001'
10+
'port': '5001',
11+
'protocol': 'http'
1112
}
1213
}

src/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@ const Wreck = require('wreck')
77

88
exports = module.exports = IpfsAPI
99

10-
function IpfsAPI (host_or_multiaddr, port) {
10+
function IpfsAPI (host_or_multiaddr, port, opts) {
1111
const self = this
1212
const config = getConfig()
1313

1414
if (!(self instanceof IpfsAPI)) {
15-
return new IpfsAPI(host_or_multiaddr, port)
15+
return new IpfsAPI(host_or_multiaddr, port, opts)
1616
}
1717

1818
try {
1919
const maddr = multiaddr(host_or_multiaddr).nodeAddress()
2020
config.host = maddr.address
2121
config.port = maddr.port
2222
} catch (e) {
23-
config.host = host_or_multiaddr
24-
config.port = port || config.port
23+
if (typeof host_or_multiaddr === 'string') {
24+
config.host = host_or_multiaddr
25+
config.port = port && typeof port !== 'object' ? port : config.port
26+
}
27+
}
28+
29+
let lastIndex = arguments.length
30+
while (!opts && lastIndex-- > 0) {
31+
opts = arguments[lastIndex]
32+
if (opts) break
33+
}
34+
35+
if (typeof opts === 'object') {
36+
for (let key in opts)
37+
if (config[key]) {
38+
config[key] = opts[key]
39+
}
2540
}
2641

2742
// autoconfigure in browser

src/request-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
8787

8888
const opts = {
8989
method: files ? 'POST' : 'GET',
90-
uri: `http://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
90+
uri: `${config.protocol}://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
9191
headers: {}
9292
}
9393

test/tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,49 @@ describe('IPFS Node.js API wrapper tests', () => {
766766
})
767767
})
768768
})
769+
770+
describe('constructor parameters', () => {
771+
const apiAddr = apiAddrs.a.split('/')
772+
773+
function clientWorks (client, done) {
774+
client.id((err, res) => {
775+
if (err) throw err
776+
const id = res
777+
assert(id.ID)
778+
assert(id.PublicKey)
779+
done()
780+
})
781+
}
782+
783+
it('opts', (done) => {
784+
clientWorks(ipfsAPI({
785+
host: apiAddr[2],
786+
port: apiAddr[4],
787+
protocol: 'http'
788+
}), done)
789+
})
790+
791+
it('mutliaddr, opts', (done) => {
792+
clientWorks(ipfsAPI(
793+
apiAddrs.a,
794+
{protocol: 'http'}
795+
), done)
796+
})
797+
798+
it('host, port', (done) => {
799+
clientWorks(ipfsAPI(
800+
apiAddr[2],
801+
apiAddr[4]
802+
), done)
803+
})
804+
805+
it('host, port, opts', (done) => {
806+
clientWorks(ipfsAPI(
807+
apiAddr[2],
808+
apiAddr[4],
809+
{protocol: 'http'}
810+
), done)
811+
})
812+
})
769813
})
814+

0 commit comments

Comments
 (0)