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

Commit f991a9c

Browse files
committed
follow interface-ipfs-core config spec
1 parent 7574a5d commit f991a9c

File tree

4 files changed

+64
-147
lines changed

4 files changed

+64
-147
lines changed

src/api/config.js

+46-13
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,64 @@
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+
// Its a path
53+
if (typeof config === 'string') {
54+
return send('config/replace', null, null, config, callback)
55+
}
56+
57+
// Its a config obj
58+
if (typeof config === 'object') {
59+
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
60+
return send('config/replace', null, null, config, callback)
61+
}
2962
}
3063
}
3164
}

src/get-files-stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ function loadPaths (opts, file) {
9393
}
9494

9595
function getFilesStream (files, opts) {
96-
if (!files) return null
96+
if (!files) {
97+
return null
98+
}
9799

98100
const mp = new Multipart()
99101

src/request-api.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function onRes (buffer, cb, uri) {
3232

3333
const stream = Boolean(res.headers['x-stream-output'])
3434
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
35-
const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0
35+
const isJson = res.headers['content-type'] &&
36+
res.headers['content-type'].indexOf('application/json') === 0
3637

3738
if (res.statusCode >= 400 || !res.statusCode) {
3839
const error = new Error(`Server responded with ${res.statusCode}`)
@@ -96,7 +97,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
9697
const port = config.port ? `:${config.port}` : ''
9798

9899
const opts = {
99-
method: files ? 'POST' : 'GET',
100+
method: 'POST',
100101
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
101102
headers: {}
102103
}
@@ -112,7 +113,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
112113
}
113114

114115
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
115-
opts.downstreamRes = stream
116116
opts.payload = stream
117117
}
118118

test/api/config.spec.js

+12-130
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,15 @@
33
/* globals apiClients */
44
'use strict'
55

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

0 commit comments

Comments
 (0)