This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
configure.js
96 lines (78 loc) · 2.92 KB
/
configure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict'
/* eslint-env browser */
const ky = require('ky-universal').default
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
const toUri = require('multiaddr-to-uri')
const errorHandler = require('./error-handler')
const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
const parseDuration = require('parse-duration')
// Set default configuration and call create function with them
module.exports = create => config => {
config = config || {}
if (typeof config === 'string') {
config = { apiAddr: config }
} else if (config.constructor && config.constructor.isMultiaddr) {
config = { apiAddr: config }
} else {
config = { ...config }
}
config.apiAddr = (config.apiAddr || getDefaultApiAddr(config)).toString()
config.apiAddr = config.apiAddr.startsWith('/') ? toUri(config.apiAddr) : config.apiAddr
config.apiAddr = trimEnd(config.apiAddr, '/')
const apiAddrPath = getNonRootPath(config.apiAddr)
// Use configured apiPath, or path on the end of apiAddr (if there is one) or default to /api/v0
config.apiPath = config.apiPath || config['api-path'] || apiAddrPath || '/api/v0'
config.apiPath = trimEnd(config.apiPath, '/')
// If user passed apiAddr with a path, trim it from the end (it is now apiPath)
config.apiAddr = apiAddrPath ? trimEnd(config.apiAddr, apiAddrPath) : config.apiAddr
const defaults = {
prefixUrl: config.apiAddr + config.apiPath,
timeout: parseTimeout(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({
ky: client,
...config
})
}
function getDefaultApiAddr ({ protocol, host, port }) {
if (isBrowser || isWebWorker) {
if (!protocol) {
protocol = location.protocol.startsWith('http')
? trimEnd(location.protocol, ':')
: 'http'
}
host = host || location.hostname
port = port || location.port
return `${protocol}://${host}${port ? ':' + 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) => {
if (options.timeout) options.timeout = parseTimeout(options.timeout)
return fn(input, mergeOptions(defaults, options))
}
}
function parseTimeout (value) {
return typeof value === 'string' ? parseDuration(value) : value
}
const trimEnd = (str, end) => str.endsWith(end) ? str.slice(0, -end.length) : str
// Get the path from a URL is it is not /
function getNonRootPath (url) {
if (url) {
const { pathname } = new URL(url)
return pathname === '/' ? null : pathname
}
}