(version, definitions, config = {})
| 6 | module.exports = apiGen |
| 7 | |
| 8 | function apiGen (version, definitions, config = {}) { |
| 9 | const configDefaults = { |
| 10 | httpEndpoint: 'http://127.0.0.1:8888', |
| 11 | verbose: false, |
| 12 | logger: { |
| 13 | log: (...args) => config.verbose ? console.log(...args) : null, |
| 14 | error: (...args) => config.verbose ? console.error(...args) : null |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | function applyDefaults(target, defaults) { |
| 19 | Object.keys(defaults).forEach(key => { |
| 20 | if(target[key] === undefined) { |
| 21 | target[key] = defaults[key] |
| 22 | } |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | applyDefaults(config, configDefaults) |
| 27 | applyDefaults(config.logger, configDefaults.logger) |
| 28 | |
| 29 | const api = {} |
| 30 | const {httpEndpoint} = config |
| 31 | |
| 32 | for (const apiGroup in definitions) { |
| 33 | for (const apiMethod in definitions[apiGroup]) { |
| 34 | const methodName = camelCase(apiMethod) |
| 35 | const url = `${httpEndpoint}/${version}/${apiGroup}/${apiMethod}` |
| 36 | api[methodName] = fetchMethod(methodName, url, definitions[apiGroup][apiMethod], config) |
| 37 | } |
| 38 | } |
| 39 | for(const helper in helpers.api) { |
| 40 | // Insert `api` as the first parameter to all API helpers |
| 41 | api[helper] = (...args) => helpers.api[helper](api, ...args) |
| 42 | } |
| 43 | return api |
| 44 | } |
| 45 | |
| 46 | function fetchMethod (methodName, url, definition, config) { |
| 47 | const {logger} = config |
no test coverage detected