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

Commit cc40ce5

Browse files
committed
fix: use ignoreUndefined merge-options option
1 parent 7ac6b30 commit cc40ce5

File tree

15 files changed

+42
-24
lines changed

15 files changed

+42
-24
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"ky": "^0.15.0",
8282
"ky-universal": "^0.3.0",
8383
"lru-cache": "^5.1.1",
84-
"merge-options": "^1.0.1",
84+
"merge-options": "github:achingbrain/merge-options#add-ignore-undefined-config-option",
8585
"multiaddr": "^6.0.6",
8686
"multibase": "~0.6.0",
8787
"multicodec": "~0.5.1",

src/add/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = configure(({ ky }) => {
3131
if (options.preload !== null) searchParams.set('preload', options.preload)
3232

3333
const res = await ky.post('add', {
34-
timeout: options.timeout || false,
34+
timeout: options.timeout,
3535
signal: options.signal,
3636
headers: options.headers,
3737
searchParams,

src/block/rm-async-iterator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = configure(({ ky }) => {
2424
})
2525

2626
const res = await ky.post('block/rm', {
27-
timeout: options.timeout || false,
27+
timeout: options.timeout,
2828
signal: options.signal,
2929
headers: options.headers,
3030
searchParams

src/cat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = configure(({ ky }) => {
2121
if (options.length) searchParams.set('length', options.length)
2222

2323
const res = await ky.get('cat', {
24-
timeout: options.timeout || false,
24+
timeout: options.timeout,
2525
signal: options.signal,
2626
headers: options.headers,
2727
searchParams

src/config/profiles/apply.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = configure(({ ky }) => {
88
options = options || {}
99

1010
const res = await ky.post('config/profile/apply', {
11-
timeout: options.timeout || false,
11+
timeout: options.timeout,
1212
signal: options.signal,
1313
headers: options.headers,
1414
searchParams: {

src/config/profiles/list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = configure(({ ky }) => {
99
options = options || {}
1010

1111
const res = await ky.get('config/profile/list', {
12-
timeout: options.timeout || false,
12+
timeout: options.timeout,
1313
signal: options.signal,
1414
headers: options.headers
1515
})

src/files-regular/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = configure(({ ky }) => {
3737
}
3838

3939
const res = await ky.get('get', {
40-
timeout: options.timeout || false,
40+
timeout: options.timeout,
4141
signal: options.signal,
4242
headers: options.headers,
4343
searchParams

src/files-regular/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = configure(({ ky }) => {
3232
}
3333

3434
const res = await ky.get('ls', {
35-
timeout: options.timeout || false,
35+
timeout: options.timeout,
3636
signal: options.signal,
3737
headers: options.headers,
3838
searchParams

src/files-regular/refs-local.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = configure(({ ky }) => {
1010
options = options || {}
1111

1212
const res = await ky.get('refs/local', {
13-
timeout: options.timeout || false,
13+
timeout: options.timeout,
1414
signal: options.signal,
1515
headers: options.headers
1616
})

src/files-regular/refs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = configure(({ ky }) => {
5050
}
5151

5252
const res = await ky.get('refs', {
53-
timeout: options.timeout || false,
53+
timeout: options.timeout,
5454
signal: options.signal,
5555
headers: options.headers,
5656
searchParams

src/lib/configure.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ky = require('ky-universal').default
55
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
66
const { toUri } = require('./multiaddr')
77
const errorHandler = require('./error-handler')
8+
const mergeOptions = require('merge-options')
89

910
// Set default configuration and call create function with them
1011
module.exports = create => config => {
@@ -22,17 +23,26 @@ module.exports = create => config => {
2223
config.apiAddr = config.apiAddr.startsWith('/') ? toUri(config.apiAddr) : config.apiAddr
2324
config.apiPath = config.apiPath || config['api-path'] || '/api/v0'
2425

26+
// TODO configure ky to use config.fetch when this is released:
27+
// https://github.com/sindresorhus/ky/pull/153
28+
const defaults = {
29+
prefixUrl: config.apiAddr + config.apiPath,
30+
timeout: config.timeout || 60000 * 20,
31+
headers: config.headers,
32+
hooks: {
33+
afterResponse: [errorHandler]
34+
}
35+
}
36+
const k = ky.extend(defaults)
37+
const client = ['get', 'post', 'put', 'delete', 'patch', 'head']
38+
.reduce((client, key) => {
39+
client[key] = wrap(k[key], defaults)
40+
41+
return client
42+
}, wrap(k, defaults))
43+
2544
return create({
26-
// TODO configure ky to use config.fetch when this is released:
27-
// https://github.com/sindresorhus/ky/pull/153
28-
ky: ky.extend({
29-
prefixUrl: config.apiAddr + config.apiPath,
30-
timeout: config.timeout || false,
31-
headers: config.headers,
32-
hooks: {
33-
afterResponse: [errorHandler]
34-
}
35-
}),
45+
ky: client,
3646
...config
3747
})
3848
}
@@ -57,3 +67,11 @@ function getDefaultApiAddr ({ protocol, host, port }) {
5767

5868
return `${protocol || 'http'}://${host || 'localhost'}:${port || 5001}`
5969
}
70+
71+
// returns the passed function wrapped in a function that ignores
72+
// undefined values in the passed `options` object
73+
function wrap (fn, defaults) {
74+
return (input, options) => {
75+
return fn(input, mergeOptions.call({ ignoreUndefined: true }, (defaults, options)))
76+
}
77+
}

src/pubsub/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = configure(({ ky }) => {
77
options = options || {}
88

99
const { Strings } = await ky.get('pubsub/ls', {
10-
timeout: options.timeout || false,
10+
timeout: options.timeout,
1111
signal: options.signal,
1212
headers: options.headers,
1313
searchParams: options.searchParams

src/pubsub/peers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = configure(({ ky }) => {
1515
searchParams.set('arg', topic)
1616

1717
const { Strings } = await ky.get('pubsub/peers', {
18-
timeout: options.timeout || false,
18+
timeout: options.timeout,
1919
signal: options.signal,
2020
headers: options.headers,
2121
searchParams

src/pubsub/publish.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = configure(({ ky }) => {
1212
searchParams.set('arg', topic)
1313

1414
const res = await ky.post(`pubsub/pub?${searchParams}&arg=${encodeBuffer(data)}`, {
15-
timeout: options.timeout || false,
15+
timeout: options.timeout,
1616
signal: options.signal,
1717
headers: options.headers
1818
}).text()

src/pubsub/subscribe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = configure((config) => {
3838

3939
try {
4040
res = await ky.post('pubsub/sub', {
41-
timeout: options.timeout || false,
41+
timeout: options.timeout,
4242
signal: options.signal,
4343
headers: options.headers,
4444
searchParams

0 commit comments

Comments
 (0)