UNPKG

apeman-service-api

Version:
82 lines (74 loc) 1.63 kB
/** * Service * @class ApApiService * @param {Object} store - Redux store * @param {Object} options - Optional settings */ 'use strict' const { ApService } = require('apeman-service-base') const apemanApiClient = require('apeman-api-client') const { get } = require('bwindow') const co = require('co') const abind = require('abind') /** @lends ApApiService */ class ApApiService extends ApService { constructor (store, options = {}) { super(store) const s = this abind(s, { proto: ApApiService.prototype }) abind(s) let { pathname = '/api' } = options s.api = apemanApiClient(s.url(pathname)) s.cache = {} } /** * Configure api * @param {string} url */ configure ({ url }) { const s = this s.api = apemanApiClient(url) } /** * Test reachability of the API * @param args * @returns {*} */ reach (...args) { const s = this return s.api.reach(...args) } /** * Use api * @param {string} name - Name of API * @returns {*} */ use (name) { const s = this return co(function * () { let cache = s.cache[ name ] if (cache) { return cache } let connected = yield s.api.connect(name) s.cache[ name ] = connected return connected }) } /** * Get url * @param {string} pathname - Url path name * @returns {string} - Url */ url (pathname) { const location = get('location') if (!location) { return `${pathname}` } const { protocol, host } = location return `${protocol}//${host}${pathname}` } } module.exports = ApApiService