trc-client-core
Version:
The core of the TRC Client
31 lines (26 loc) • 1.02 kB
JavaScript
import TrcAPI from 'trc-api-js';
import PromiseErrorHandler from 'trc-client-core/src/utils/PromiseErrorHandler';
import PromiseSuccessHandler from 'trc-client-core/src/utils/PromiseSuccessHandler';
var api = TrcAPI();
// wrapper for reducing objects.
function reduceObject(obj, reducer) {
return Object.keys(obj).reduce(reducer, {});
}
//
// Append each method on the trc-api-js with the global promise handlers
// Only handles two levels deep. This might be naive.
//
// Reduce the top level
module.exports = reduceObject(api, (reduction, key) => {
// reduce second level
reduction[key] = reduceObject(api[key], (subReduction, subKey) => {
// proxy each sub method
subReduction[subKey] = function PromiseProxyApi() {
// Apply the given arguments and attach the global `then()`
return api[key][subKey].apply(null, arguments)
.then(PromiseSuccessHandler, PromiseErrorHandler);
}
return subReduction;
});
return reduction;
});