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

Commit 6f7bd09

Browse files
committed
follow interface-ipfs-core config spec
1 parent ad51062 commit 6f7bd09

File tree

2 files changed

+48
-144
lines changed

2 files changed

+48
-144
lines changed

src/api/config.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
'use strict'
22

3-
const argCommand = require('../cmd-helpers').argCommand
4-
53
module.exports = (send) => {
64
return {
7-
get: argCommand(send, 'config'),
8-
set (key, value, opts, cb) {
9-
if (typeof (opts) === 'function') {
10-
cb = opts
5+
get (key, callback) {
6+
if (typeof key === 'function') {
7+
callback = key
8+
key = undefined
9+
}
10+
11+
if (!key) {
12+
return send('config/show', null, null, null, true, callback)
13+
}
14+
15+
return send('config', key, null, null, (err, result) => {
16+
if (err) {
17+
return callback(err)
18+
}
19+
callback(null, result.Value)
20+
})
21+
},
22+
set (key, value, opts, callback) {
23+
if (typeof opts === 'function') {
24+
callback = opts
1125
opts = {}
1226
}
27+
if (typeof key !== 'string') {
28+
return callback(new Error('Invalid key type'))
29+
}
30+
31+
if (typeof value !== 'object' &&
32+
typeof value !== 'boolean' &&
33+
typeof value !== 'string') {
34+
return callback(new Error('Invalid value type'))
35+
}
1336

14-
if (typeof (value) === 'object') {
37+
if (typeof value === 'object') {
1538
value = JSON.stringify(value)
1639
opts = { json: true }
17-
} else if (typeof (value) === 'boolean') {
40+
}
41+
42+
if (typeof value === 'boolean') {
1843
value = value.toString()
1944
opts = { bool: true }
2045
}
2146

22-
return send('config', [key, value], opts, null, cb)
23-
},
24-
show (cb) {
25-
return send('config/show', null, null, null, true, cb)
47+
return send('config', [key, value], opts, null, callback)
2648
},
27-
replace (file, cb) {
28-
return send('config/replace', null, null, file, cb)
49+
replace (file, callback) {
50+
return send('config/replace', null, null, file, callback)
2951
}
3052
}
3153
}

test/api/config.spec.js

Lines changed: 12 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,15 @@
22
/* globals apiClients */
33
'use strict'
44

5-
const expect = require('chai').expect
6-
const isNode = require('detect-node')
7-
const path = require('path')
8-
9-
describe('.config', () => {
10-
describe('.config.{set, get}', () => {
11-
it('string', (done) => {
12-
const confKey = 'arbitraryKey'
13-
const confVal = 'arbitraryVal'
14-
15-
apiClients.a.config.set(confKey, confVal, (err, res) => {
16-
expect(err).to.not.exist
17-
apiClients.a.config.get(confKey, (err, res) => {
18-
expect(err).to.not.exist
19-
expect(res).to.have.a.property('Value', confVal)
20-
done()
21-
})
22-
})
23-
})
24-
25-
it('bool', (done) => {
26-
const confKey = 'otherKey'
27-
const confVal = true
28-
29-
apiClients.a.config.set(confKey, confVal, (err, res) => {
30-
expect(err).to.not.exist
31-
apiClients.a.config.get(confKey, (err, res) => {
32-
expect(err).to.not.exist
33-
expect(res.Value).to.deep.equal(confVal)
34-
done()
35-
})
36-
})
37-
})
38-
39-
it('json', (done) => {
40-
const confKey = 'API.HTTPHeaders.Access-Control-Allow-Origin'
41-
const confVal = ['http://example.io']
42-
43-
apiClients.a.config.set(confKey, confVal, (err, res) => {
44-
expect(err).to.not.exist
45-
apiClients.a.config.get(confKey, (err, res) => {
46-
expect(err).to.not.exist
47-
expect(res.Value).to.deep.equal(confVal)
48-
done()
49-
})
50-
})
51-
})
52-
})
53-
54-
it('.config.show', (done) => {
55-
apiClients.c.config.show((err, res) => {
56-
expect(err).to.not.exist
57-
expect(res).to.exist
58-
done()
59-
})
60-
})
61-
62-
it('.config.replace', (done) => {
63-
if (!isNode) {
64-
return done()
65-
}
66-
67-
apiClients.c.config.replace(path.join(__dirname, '/../r-config.json'), (err, res) => {
68-
expect(err).to.not.exist
69-
expect(res).to.be.equal(null)
70-
done()
71-
})
72-
})
73-
74-
describe('promise', () => {
75-
describe('.config.{set, get}', () => {
76-
it('string', () => {
77-
const confKey = 'arbitraryKey'
78-
const confVal = 'arbitraryVal'
79-
80-
return apiClients.a.config.set(confKey, confVal)
81-
.then((res) => {
82-
return apiClients.a.config.get(confKey)
83-
})
84-
.then((res) => {
85-
expect(res).to.have.a.property('Value', confVal)
86-
})
87-
})
88-
89-
it('bool', () => {
90-
const confKey = 'otherKey'
91-
const confVal = true
92-
93-
return apiClients.a.config.set(confKey, confVal)
94-
.then((res) => {
95-
return apiClients.a.config.get(confKey)
96-
})
97-
.then((res) => {
98-
expect(res.Value).to.deep.equal(confVal)
99-
})
100-
})
101-
102-
it('json', () => {
103-
const confKey = 'API.HTTPHeaders.Access-Control-Allow-Origin'
104-
const confVal = ['http://example.com']
105-
106-
return apiClients.a.config.set(confKey, confVal)
107-
.then((res) => {
108-
return apiClients.a.config.get(confKey)
109-
})
110-
.then((res) => {
111-
expect(res.Value).to.deep.equal(confVal)
112-
})
113-
})
114-
})
115-
116-
it('.config.show', () => {
117-
return apiClients.c.config.show()
118-
.then((res) => {
119-
expect(res).to.exist
120-
})
121-
})
122-
123-
it('.config.replace', () => {
124-
if (!isNode) {
125-
return
126-
}
127-
128-
return apiClients.c.config.replace(path.join(__dirname, '/../r-config.json'))
129-
.then((res) => {
130-
expect(res).to.be.equal(null)
131-
})
132-
})
133-
})
134-
})
5+
const test = require('interface-ipfs-core')
6+
7+
const common = {
8+
setup: function (cb) {
9+
cb(null, apiClients.a)
10+
},
11+
teardown: function (cb) {
12+
cb()
13+
}
14+
}
15+
16+
test.config(common)

0 commit comments

Comments
 (0)