@sword-health/ui-http-mapper
Version:
UI clients tool for consumption of easy to read/config endpoint maps
171 lines (126 loc) • 5.05 kB
JavaScript
var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", {
value: true
});
var defaultConfigs = {
logPrefix: '[sh-http]',
supportedMethods: ['get', 'post', 'put', 'delete', 'head', 'patch', 'options']
}; // Workaround for array based dynamic type creation
var auxDynamicMethodTypesCreation = Object.assign.apply(Object, __spreadArray([{}], defaultConfigs.supportedMethods.map(function (t) {
var _a;
return _a = {}, _a[t] = '', _a;
}), false));
var EndpointDecoderTS =
/** @class */
function () {
function EndpointDecoderTS(endpointsMap, configs) {
if (endpointsMap === void 0) {
endpointsMap = {
modules: {}
};
}
if (configs === void 0) {
configs = {};
}
this.endpointsSpec = endpointsMap;
}
EndpointDecoderTS.prototype._makeNamespacePath = function (namespace) {
var safeNameSpace = namespace.replace(/(\/{2})|(\/$)/, '');
return safeNameSpace.split('/');
};
EndpointDecoderTS.prototype._findEndpointConfigs = function (namespace, endpointsConfigsMap) {
if (endpointsConfigsMap === void 0) {
endpointsConfigsMap = {};
}
var pathSteps = this._makeNamespacePath(namespace);
if (!pathSteps.length) {
return;
}
var pathToConsider = pathSteps.join('/');
var goNextStep = function goNextStep(steps, currentConfigs, currIndex) {
if (currIndex === void 0) {
currIndex = 0;
}
var stepConfigs = currentConfigs[steps[currIndex]];
if (!stepConfigs) {
console.error("".concat(defaultConfigs.logPrefix, " Unable to find configs for '").concat(pathToConsider, "'"));
return;
} // Check if should end querying
var reachFinalStep = steps.length - 1 === currIndex; // Keep finding until reaching end of given path
if (!reachFinalStep) {
// eslint-disable-next-line consistent-return
return goNextStep(steps, stepConfigs, currIndex + 1);
} // Valid if found path is a complete one
var validFinalPath = '$$makeUrl' in stepConfigs && '$$method' in stepConfigs;
if (!validFinalPath) {
console.error("".concat(defaultConfigs.logPrefix, " Given path '").concat(pathToConsider, "' incomplete."));
return;
} // eslint-disable-next-line consistent-return
return stepConfigs;
}; // eslint-disable-next-line consistent-return
return goNextStep(pathSteps, endpointsConfigsMap);
};
Object.defineProperty(EndpointDecoderTS.prototype, "dataUpdate", {
set: function set(freshDataObject) {
this.endpointsSpec.data = freshDataObject;
},
enumerable: false,
configurable: true
});
EndpointDecoderTS.prototype.$http = function (namespace, urlData, _a) {
var _this = this;
var _b = _a === void 0 ? {
config: {}
} : _a,
requestPayload = _b.body,
requestConfigs = _b.config; // Validate namespace format
var validNameSpace = typeof namespace === 'string';
if (!validNameSpace) {
console.error("".concat(defaultConfigs.logPrefix, " Invalid namespace provided. Aborting"));
return;
} // Find requested endpoint configuration
var endpointConfigs = this._findEndpointConfigs(namespace, this.endpointsSpec.modules);
if (!endpointConfigs) {
return;
}
var method = endpointConfigs.$$method,
makeUrl = endpointConfigs.$$makeUrl,
_c = endpointConfigs.$$config,
defaultConfig = _c === void 0 ? {} : _c,
_d = endpointConfigs.$$meta,
meta = _d === void 0 ? {} : _d; // Check if what config from endpoint spec are executable and run them now
var defaultConfigExec = {};
Object.keys(defaultConfig).forEach(function (configKey) {
var configVal = defaultConfig[configKey];
var executableConfig = typeof configVal === 'function';
if (executableConfig) {
defaultConfigExec[configKey] = configVal(_this.endpointsSpec.data);
return;
}
defaultConfigExec[configKey] = configVal;
}); // Merge default configs with adhoc one, giving priority to adhoc ones
var mergedConfig = Object.assign({}, defaultConfigExec, requestConfigs); // Make endpoint url (will use default base url)
var url = makeUrl(endpointConfigs, urlData); // Appending request payload to requests that support it
var requestArgs = [mergedConfig];
if (method === 'put' || method === 'post' || method === 'patch' || method === 'delete') {
requestArgs.unshift(requestPayload);
}
return {
method: method,
url: url,
requestArgs: requestArgs,
meta: meta
};
};
return EndpointDecoderTS;
}();
exports["default"] = EndpointDecoderTS;
;