diff --git a/.aegir.js b/.aegir.js index 9a75337d13..847d73a9b0 100644 --- a/.aegir.js +++ b/.aegir.js @@ -9,7 +9,7 @@ const echoServer = EchoServer.createServer() const echoServerStart = promisify(echoServer.start) const echoServerStop = promisify(echoServer.stop) module.exports = { - bundlesize: { maxSize: '245kB' }, + bundlesize: { maxSize: '246kB' }, webpack: { resolve: { mainFields: ['browser', 'main'] diff --git a/package.json b/package.json index 1c446e65f3..4c608ba114 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "ky": "^0.15.0", "ky-universal": "^0.3.0", "lru-cache": "^5.1.1", - "merge-options": "^1.0.1", + "merge-options": "^2.0.0", "multiaddr": "^6.0.6", "multibase": "~0.6.0", "multicodec": "~0.5.1", diff --git a/src/lib/configure.js b/src/lib/configure.js index a9036d1cde..8401683f9f 100644 --- a/src/lib/configure.js +++ b/src/lib/configure.js @@ -5,6 +5,7 @@ const ky = require('ky-universal').default const { isBrowser, isWebWorker } = require('ipfs-utils/src/env') const { toUri } = require('./multiaddr') const errorHandler = require('./error-handler') +const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) // Set default configuration and call create function with them module.exports = create => config => { @@ -22,17 +23,26 @@ module.exports = create => config => { config.apiAddr = config.apiAddr.startsWith('/') ? toUri(config.apiAddr) : config.apiAddr config.apiPath = config.apiPath || config['api-path'] || '/api/v0' + // TODO configure ky to use config.fetch when this is released: + // https://github.com/sindresorhus/ky/pull/153 + const defaults = { + prefixUrl: config.apiAddr + config.apiPath, + timeout: config.timeout || 60000 * 20, + headers: config.headers, + hooks: { + afterResponse: [errorHandler] + } + } + const k = ky.extend(defaults) + const client = ['get', 'post', 'put', 'delete', 'patch', 'head'] + .reduce((client, key) => { + client[key] = wrap(k[key], defaults) + + return client + }, wrap(k, defaults)) + return create({ - // TODO configure ky to use config.fetch when this is released: - // https://github.com/sindresorhus/ky/pull/153 - ky: ky.extend({ - prefixUrl: config.apiAddr + config.apiPath, - timeout: config.timeout || 60 * 1000, - headers: config.headers, - hooks: { - afterResponse: [errorHandler] - } - }), + ky: client, ...config }) } @@ -57,3 +67,11 @@ function getDefaultApiAddr ({ protocol, host, port }) { return `${protocol || 'http'}://${host || 'localhost'}:${port || 5001}` } + +// returns the passed function wrapped in a function that ignores +// undefined values in the passed `options` object +function wrap (fn, defaults) { + return (input, options) => { + return fn(input, mergeOptions(defaults, options)) + } +}