@@ -5,6 +5,7 @@ const ky = require('ky-universal').default
5
5
const { isBrowser, isWebWorker } = require ( 'ipfs-utils/src/env' )
6
6
const { toUri } = require ( './multiaddr' )
7
7
const errorHandler = require ( './error-handler' )
8
+ const mergeOptions = require ( 'merge-options' )
8
9
9
10
// Set default configuration and call create function with them
10
11
module . exports = create => config => {
@@ -22,17 +23,26 @@ module.exports = create => config => {
22
23
config . apiAddr = config . apiAddr . startsWith ( '/' ) ? toUri ( config . apiAddr ) : config . apiAddr
23
24
config . apiPath = config . apiPath || config [ 'api-path' ] || '/api/v0'
24
25
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
+
25
44
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 ,
36
46
...config
37
47
} )
38
48
}
@@ -57,3 +67,11 @@ function getDefaultApiAddr ({ protocol, host, port }) {
57
67
58
68
return `${ protocol || 'http' } ://${ host || 'localhost' } :${ port || 5001 } `
59
69
}
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
+ }
0 commit comments