Skip to content

Commit 403aec4

Browse files
committed
code reorganization
1 parent e3a49f3 commit 403aec4

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

connections.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
const BasicAPI = require('./basic')
2-
module.exports = {
3-
setup: _setup
4-
}
52

63
// default logger if none is provided in the opts object to _setup
74
const defaultLog = {}
85
defaultLog.error = defaultLog.info = defaultLog.debug = defaultLog.warn = console.log
96
const defaultTimeout = 30000
107

11-
/*
12-
* Check if there is a cache configured for this api
13-
*/
14-
function _getRedisConfig (apiName, cache) {
15-
if (cache && cache[ apiName ]) {
16-
return cache[ apiName ]
17-
}
18-
19-
return undefined
8+
module.exports = {
9+
setup: _setup
2010
}
2111

22-
/*
23-
* If configured to use nodeApi, i.e. api supporting KTH api standard and exposes a /_paths url
24-
* where the public URL is published.
25-
* Will download api specification from api and expose its methods internally under "/api" as paths objects
26-
*/
27-
function _getRedisClient (apiName, opts) {
28-
let cache = opts.cache ? opts.cache : {}
29-
let redis = opts.redis
30-
let log = opts.log || defaultLog
31-
try {
32-
if (cache[apiName]) {
33-
const cacheConfig = _getRedisConfig(apiName, cache)
34-
return redis(apiName, cacheConfig.redis)
35-
}
36-
} catch (err) {
37-
log.error('Error creating Redis client', err)
38-
}
12+
// populate an object with all api configurations and paths
13+
function _setup (apis, keys, opts) {
14+
if (!opts) opts = {}
15+
const endpoints = _createClients(apis, keys)
16+
const output = {}
3917

40-
return Promise.reject(false)
18+
_getPaths(endpoints, opts)
19+
.then((results) => {
20+
results.forEach((endpoint) => {
21+
if (endpoint) {
22+
output[ endpoint.key ] = endpoint
23+
}
24+
})
25+
return output
26+
})
27+
.then(clients => {
28+
return configureApiCache(clients, opts)
29+
})
30+
.catch((err) => {
31+
throw err
32+
})
33+
return output
4134
}
4235

4336
// unpack nodeApi:s and pair with keys, returns BasicAPI objects
@@ -122,26 +115,33 @@ function configureApiCache (clients, opts) {
122115
return clients
123116
}
124117

125-
// populate an object with all api configurations and paths
126-
function _setup (apis, keys, opts) {
127-
if (!opts) opts = {}
128-
const endpoints = _createClients(apis, keys)
129-
const output = {}
118+
/*
119+
* Check if there is a cache configured for this api
120+
*/
121+
function _getRedisConfig (apiName, cache) {
122+
if (cache && cache[ apiName ]) {
123+
return cache[ apiName ]
124+
}
125+
return undefined
126+
}
130127

131-
_getPaths(endpoints, opts)
132-
.then((results) => {
133-
results.forEach((endpoint) => {
134-
if (endpoint) {
135-
output[ endpoint.key ] = endpoint
136-
}
137-
})
138-
return output
139-
})
140-
.then(clients => {
141-
return configureApiCache(clients, opts)
142-
})
143-
.catch((err) => {
144-
throw err
145-
})
146-
return output
128+
/*
129+
* If configured to use nodeApi, i.e. api supporting KTH api standard and exposes a /_paths url
130+
* where the public URL is published.
131+
* Will download api specification from api and expose its methods internally under "/api" as paths objects
132+
*/
133+
function _getRedisClient (apiName, opts) {
134+
let cache = opts.cache ? opts.cache : {}
135+
let redis = opts.redis
136+
let log = opts.log || defaultLog
137+
try {
138+
if (cache[apiName]) {
139+
const cacheConfig = _getRedisConfig(apiName, cache)
140+
return redis(apiName, cacheConfig.redis)
141+
}
142+
} catch (err) {
143+
log.error('Error creating Redis client', err)
144+
}
145+
146+
return Promise.reject(false)
147147
}

0 commit comments

Comments
 (0)