UNPKG

@kineticdata/react

Version:
123 lines (120 loc) 6.54 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateAttributeDefinition = exports.fetchAttributeDefinitions = exports.fetchAttributeDefinition = exports.deleteAttributeDefinition = exports.createAttributeDefinition = void 0; var _axios = _interopRequireDefault(require("axios")); var _helpers = require("../../helpers"); var _http = require("../http"); // The API returns the singular name of the attribute type, so we remove the "s" var responseEnvelope = function responseEnvelope(attributeType) { return attributeType.replace(/s$/, ''); }; var validateOptions = function validateOptions(functionName, requiredOptions, options) { var validAttributes = ['spaceAttributeDefinitions', 'teamAttributeDefinitions', 'userAttributeDefinitions', 'userProfileAttributeDefinitions', 'categoryAttributeDefinitions', 'kappAttributeDefinitions', 'formAttributeDefinitions']; var attributesRequiringKappSlug = ['categoryAttributeDefinitions', 'kappAttributeDefinitions']; var kappSlugMissing = attributesRequiringKappSlug.includes(options.attributeType) && !options.kappSlug; var invalidType = !validAttributes.includes(options.attributeType); var missing = requiredOptions.filter(function (requiredOption) { return !options[requiredOption]; }); if (missing.length > 0) { throw new Error("".concat(functionName, " failed! The following required options are missing: ").concat(missing)); } if (kappSlugMissing) { throw new Error("".concat(functionName, " failed! A kappSlug is required when using ").concat(options.attributeType)); } if (invalidType) { throw new Error("".concat(functionName, " failed! The provided attributeType (").concat(options.attributeType, ") is not valid")); } }; var buildEndpoint = function buildEndpoint(_ref) { var kappSlug = _ref.kappSlug, at = _ref.attributeType, an = _ref.attributeName; var attributeType = encodeURIComponent(at); var attributeName = encodeURIComponent(an); var basePath = kappSlug ? "".concat(_helpers.bundle.apiLocation(), "/kapps/").concat(kappSlug, "/").concat(attributeType) : "".concat(_helpers.bundle.apiLocation(), "/").concat(attributeType); return an ? "".concat(basePath, "/").concat(attributeName) : basePath; }; var fetchAttributeDefinitions = exports.fetchAttributeDefinitions = function fetchAttributeDefinitions() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var attributeType = options.attributeType; validateOptions('fetchAttributeDefinitions', ['attributeType'], options); return _axios["default"].get(buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { attributeDefinitions: response.data[attributeType] }; })["catch"](_http.handleErrors); }; var fetchAttributeDefinition = exports.fetchAttributeDefinition = function fetchAttributeDefinition() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var attributeType = options.attributeType; validateOptions('fetchAttributeDefinition', ['attributeType', 'attributeName'], options); return _axios["default"].get(buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { // The userProfileAttributeDefinition fetch returns a pluralized name // instead of the singular name as it should, so we want to check both // the singular and plural versions in that case to be backwards // compatible when this gets fixed on the server. // TODO Remove this check and only keep the responseEvelope once the // server side code is fixed. attributeDefinition: attributeType !== 'userProfileAttributeDefinitions' ? response.data[responseEnvelope(attributeType)] : response.data[responseEnvelope(attributeType)] || response.data[attributeType] }; })["catch"](_http.handleErrors); }; var createAttributeDefinition = exports.createAttributeDefinition = function createAttributeDefinition() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var kappSlug = options.kappSlug, attributeType = options.attributeType, attributeDefinition = options.attributeDefinition; validateOptions('createAttributeDefinition', ['attributeType', 'attributeDefinition'], options); // The API returns the singular name of the attribute type, so we remove the "s" return _axios["default"].post(buildEndpoint({ kappSlug: kappSlug, attributeType: attributeType }), attributeDefinition, { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { attributeDefinition: response.data[responseEnvelope(attributeType)] }; })["catch"](_http.handleErrors); }; var updateAttributeDefinition = exports.updateAttributeDefinition = function updateAttributeDefinition() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var attributeType = options.attributeType, attributeDefinition = options.attributeDefinition; validateOptions('updateAttributeDefinition', ['attributeType', 'attributeName'], options); // The API returns the singular name of the attribute type, so we remove the "s" return _axios["default"].put(buildEndpoint(options), attributeDefinition, { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { attributeDefinition: response.data[responseEnvelope(attributeType)] }; })["catch"](_http.handleErrors); }; var deleteAttributeDefinition = exports.deleteAttributeDefinition = function deleteAttributeDefinition() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var attributeType = options.attributeType; validateOptions('deleteAttributeDefinition', ['attributeType', 'attributeName'], options); // Build URL and fetch the space. return _axios["default"]["delete"](buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { attributeDefinition: response.data[responseEnvelope(attributeType)] }; })["catch"](_http.handleErrors); };