|
1 | 1 | const BasicAPI = require('./basic') |
2 | | -module.exports = { |
3 | | - setup: _setup |
4 | | -} |
5 | 2 |
|
6 | 3 | // default logger if none is provided in the opts object to _setup |
7 | 4 | const defaultLog = {} |
8 | 5 | defaultLog.error = defaultLog.info = defaultLog.debug = defaultLog.warn = console.log |
9 | 6 | const defaultTimeout = 30000 |
10 | 7 |
|
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 |
20 | 10 | } |
21 | 11 |
|
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 = {} |
39 | 17 |
|
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 |
41 | 34 | } |
42 | 35 |
|
43 | 36 | // unpack nodeApi:s and pair with keys, returns BasicAPI objects |
@@ -122,26 +115,33 @@ function configureApiCache (clients, opts) { |
122 | 115 | return clients |
123 | 116 | } |
124 | 117 |
|
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 | +} |
130 | 127 |
|
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) |
147 | 147 | } |
0 commit comments