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

fix: increase default timeout and respect value passed to ky.extend #1130

Merged
merged 4 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 28 additions & 10 deletions src/lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
})
}
Expand All @@ -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))
}
}
Comment on lines +73 to +77

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

const wrap = (fn, defaults) => (input, options) => fn(input, mergeOptions(defaults, options));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this sort of deeply chained single-line-arrow-function style optimises readability for computers and not humans. It'll get minified to something similar to this anyway so there's little to gain here.