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

Commit a318463

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

File tree

4 files changed

+60
-143
lines changed

4 files changed

+60
-143
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"ndjson": "^1.4.3",
3333
"promisify-es6": "^1.0.1",
3434
"qs": "^6.1.0",
35+
"streamifier": "^0.1.1",
3536
"wreck": "^7.0.2"
3637
},
3738
"engines": {

src/api/config.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
11
'use strict'
22

3-
const argCommand = require('../cmd-helpers').argCommand
3+
const streamifier = require('streamifier')
44

55
module.exports = (send) => {
66
return {
7-
get: argCommand(send, 'config'),
8-
set (key, value, opts, cb) {
9-
if (typeof (opts) === 'function') {
10-
cb = opts
7+
get (key, callback) {
8+
if (typeof key === 'function') {
9+
callback = key
10+
key = undefined
11+
}
12+
13+
if (!key) {
14+
return send('config/show', null, null, null, true, callback)
15+
}
16+
17+
return send('config', key, null, null, (err, result) => {
18+
if (err) {
19+
return callback(err)
20+
}
21+
callback(null, result.Value)
22+
})
23+
},
24+
set (key, value, opts, callback) {
25+
if (typeof opts === 'function') {
26+
callback = opts
1127
opts = {}
1228
}
29+
if (typeof key !== 'string') {
30+
return callback(new Error('Invalid key type'))
31+
}
32+
33+
if (typeof value !== 'object' &&
34+
typeof value !== 'boolean' &&
35+
typeof value !== 'string') {
36+
return callback(new Error('Invalid value type'))
37+
}
1338

14-
if (typeof (value) === 'object') {
39+
if (typeof value === 'object') {
1540
value = JSON.stringify(value)
1641
opts = { json: true }
17-
} else if (typeof (value) === 'boolean') {
42+
}
43+
44+
if (typeof value === 'boolean') {
1845
value = value.toString()
1946
opts = { bool: true }
2047
}
2148

22-
return send('config', [key, value], opts, null, cb)
23-
},
24-
show (cb) {
25-
return send('config/show', null, null, null, true, cb)
49+
return send('config', [key, value], opts, null, callback)
2650
},
27-
replace (file, cb) {
28-
return send('config/replace', null, null, file, cb)
51+
replace (config, callback) {
52+
if (typeof config === 'string') {
53+
const configPath = config
54+
return send('config/replace', null, null, configPath, callback)
55+
}
56+
57+
if (typeof config === 'object') {
58+
config = { path: 'config.json', content: streamifier.createReadStream(new Buffer(JSON.stringify(config))) }
59+
return send('config/replace', null, null, config, callback)
60+
}
2961
}
3062
}
3163
}

src/request-api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function onRes (buffer, cb) {
2323
return cb(err)
2424
}
2525

26+
// console.log('->', res.headers)
27+
2628
const stream = Boolean(res.headers['x-stream-output'])
2729
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
2830
const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0

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)