@dxc-technology/halstack-client
Advanced tools
Comparing version
956
main.js
@@ -1,532 +0,494 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('axios')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'axios'], factory) : | ||
(global = global || self, factory(global.halSDK = {}, global.axios)); | ||
}(this, (function (exports, axios) { 'use strict'; | ||
import axios from 'axios'; | ||
axios = axios && Object.prototype.hasOwnProperty.call(axios, 'default') ? axios['default'] : axios; | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
enumerableOnly && (symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
})), keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
function ownKeys(e, r) { | ||
var t = Object.keys(e); | ||
if (Object.getOwnPropertySymbols) { | ||
var o = Object.getOwnPropertySymbols(e); | ||
r && (o = o.filter(function (r) { | ||
return Object.getOwnPropertyDescriptor(e, r).enumerable; | ||
})), t.push.apply(t, o); | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = null != arguments[i] ? arguments[i] : {}; | ||
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
return target; | ||
return t; | ||
} | ||
function _objectSpread2(e) { | ||
for (var r = 1; r < arguments.length; r++) { | ||
var t = null != arguments[r] ? arguments[r] : {}; | ||
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { | ||
_defineProperty(e, r, t[r]); | ||
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { | ||
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); | ||
}); | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
return e; | ||
} | ||
function _toPrimitive(t, r) { | ||
if ("object" != typeof t || !t) return t; | ||
var e = t[Symbol.toPrimitive]; | ||
if (void 0 !== e) { | ||
var i = e.call(t, r || "default"); | ||
if ("object" != typeof i) return i; | ||
throw new TypeError("@@toPrimitive must return a primitive value."); | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
return ("string" === r ? String : Number)(t); | ||
} | ||
function _toPropertyKey(t) { | ||
var i = _toPrimitive(t, "string"); | ||
return "symbol" == typeof i ? i : i + ""; | ||
} | ||
function _defineProperty(obj, key, value) { | ||
key = _toPropertyKey(key); | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
var _excluded = ["rel"]; | ||
var getPropertiesFromSchema = function getPropertiesFromSchema() { | ||
var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var propertiesNode = schema.properties || {}; | ||
return Object.keys(propertiesNode).map(function (key) { | ||
return _objectSpread2(_objectSpread2({}, propertiesNode[key]), {}, { | ||
key: key | ||
}); | ||
var _excluded = ["rel"]; | ||
var getPropertiesFromSchema = function getPropertiesFromSchema() { | ||
var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var propertiesNode = schema.properties || {}; | ||
return Object.keys(propertiesNode).map(function (key) { | ||
return _objectSpread2(_objectSpread2({}, propertiesNode[key]), {}, { | ||
key: key | ||
}); | ||
}); | ||
}; | ||
var getRequiredFromSchema = function getRequiredFromSchema() { | ||
var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return schema.required || []; | ||
}; | ||
var addSchemaProperty = function addSchemaProperty(propertyObject, schema) { | ||
schema[propertyObject.key] = propertyObject.schema; | ||
}; | ||
var getInteractionHelpers = function getInteractionHelpers(interaction) { | ||
var getSchemaProperties = function getSchemaProperties() { | ||
return getPropertiesFromSchema(interaction.schema); | ||
}; | ||
var getRequiredFromSchema = function getRequiredFromSchema() { | ||
var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return schema.required || []; | ||
var getSchemaProperty = function getSchemaProperty(key) { | ||
return getSchemaProperties().find(function (property) { | ||
return property.key === key; | ||
}); | ||
}; | ||
var addSchemaProperty = function addSchemaProperty(propertyObject, schema) { | ||
schema[propertyObject.key] = propertyObject.schema; | ||
var hasProperty = function hasProperty(key) { | ||
return Boolean(getSchemaProperty(key)); | ||
}; | ||
var getInteractionHelpers = function getInteractionHelpers(interaction) { | ||
var getSchemaProperties = function getSchemaProperties() { | ||
return getPropertiesFromSchema(interaction.schema); | ||
}; | ||
var getSchemaProperty = function getSchemaProperty(key) { | ||
return getSchemaProperties().find(function (property) { | ||
return property.key === key; | ||
var getRequiredProperties = function getRequiredProperties() { | ||
return getRequiredFromSchema(interaction.schema); | ||
}; | ||
var isPropertyRequired = function isPropertyRequired(key) { | ||
return getRequiredProperties().includes(key); | ||
}; | ||
var addSchemaProperties = function addSchemaProperties(propertiesAray) { | ||
interaction.schema = interaction.schema || {}; | ||
interaction.schema.properties = interaction.schema.properties || {}; | ||
propertiesAray.forEach(function (prop) { | ||
return addSchemaProperty(prop, interaction.schema.properties); | ||
}); | ||
}; | ||
return { | ||
getSchemaProperties: getSchemaProperties, | ||
getSchemaProperty: getSchemaProperty, | ||
hasProperty: hasProperty, | ||
getRequiredProperties: getRequiredProperties, | ||
isPropertyRequired: isPropertyRequired, | ||
addSchemaProperties: addSchemaProperties, | ||
addSchemaProperty: addSchemaProperty | ||
}; | ||
}; | ||
var HalResource = function HalResource(existingRepresentation) { | ||
var resourceRepresentation = existingRepresentation || {}; | ||
var getTitle = function getTitle() { | ||
var _resourceRepresentati; | ||
return (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati = resourceRepresentation._options) === null || _resourceRepresentati === void 0 ? void 0 : _resourceRepresentati.title) || ""; | ||
}; | ||
var getInteractions = function getInteractions() { | ||
var _resourceRepresentati2; | ||
return ((resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati2 = resourceRepresentation._options) === null || _resourceRepresentati2 === void 0 ? void 0 : _resourceRepresentati2.links) || []).map(function (interaction) { | ||
return _objectSpread2(_objectSpread2({}, interaction), getInteractionHelpers(interaction)); | ||
}); | ||
}; | ||
var getInteraction = function getInteraction(rel) { | ||
return getInteractions().find(function (link) { | ||
return link.rel === rel; | ||
}); | ||
}; | ||
var getItems = function getItems() { | ||
var _resourceRepresentati3; | ||
var itemNode = (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati3 = resourceRepresentation._links) === null || _resourceRepresentati3 === void 0 ? void 0 : _resourceRepresentati3.item) || []; | ||
return Array.isArray(itemNode) ? itemNode : [itemNode]; | ||
}; | ||
var getItem = function getItem() { | ||
var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
return getItems()[index]; | ||
}; | ||
var getLinks = function getLinks() { | ||
var linksNode = (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._links) || {}; | ||
return Object.keys(linksNode).filter(function (key) { | ||
return key !== "item"; | ||
}).map(function (key) { | ||
return _objectSpread2(_objectSpread2({}, linksNode[key]), {}, { | ||
rel: key | ||
}); | ||
}; | ||
var hasProperty = function hasProperty(key) { | ||
return Boolean(getSchemaProperty(key)); | ||
}; | ||
var getRequiredProperties = function getRequiredProperties() { | ||
return getRequiredFromSchema(interaction.schema); | ||
}; | ||
var isPropertyRequired = function isPropertyRequired(key) { | ||
return getRequiredProperties().includes(key); | ||
}; | ||
var addSchemaProperties = function addSchemaProperties(propertiesAray) { | ||
interaction.schema = interaction.schema || {}; | ||
interaction.schema.properties = interaction.schema.properties || {}; | ||
propertiesAray.forEach(function (prop) { | ||
return addSchemaProperty(prop, interaction.schema.properties); | ||
}); | ||
}; | ||
return { | ||
getSchemaProperties: getSchemaProperties, | ||
getSchemaProperty: getSchemaProperty, | ||
hasProperty: hasProperty, | ||
getRequiredProperties: getRequiredProperties, | ||
isPropertyRequired: isPropertyRequired, | ||
addSchemaProperties: addSchemaProperties, | ||
addSchemaProperty: addSchemaProperty | ||
}; | ||
}); | ||
}; | ||
var HalResource = function HalResource(existingRepresentation) { | ||
var resourceRepresentation = existingRepresentation || {}; | ||
var getTitle = function getTitle() { | ||
var _resourceRepresentati; | ||
return (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati = resourceRepresentation._options) === null || _resourceRepresentati === void 0 ? void 0 : _resourceRepresentati.title) || ""; | ||
var getLink = function getLink(rel) { | ||
return getLinks().find(function (link) { | ||
return link.rel === rel; | ||
}); | ||
}; | ||
var getSchemaProperties = function getSchemaProperties() { | ||
return getPropertiesFromSchema(resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._options); | ||
}; | ||
var getSchemaProperty = function getSchemaProperty(key) { | ||
return getSchemaProperties().find(function (property) { | ||
return property.key === key; | ||
}); | ||
}; | ||
var getRequiredProperties = function getRequiredProperties() { | ||
return getRequiredFromSchema(resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._options); | ||
}; | ||
var isPropertyRequired = function isPropertyRequired(key) { | ||
return getRequiredProperties().includes(key); | ||
}; | ||
var getPropertyHelpers = function getPropertyHelpers(property) { | ||
var isRequired = function isRequired() { | ||
return isPropertyRequired(property); | ||
}; | ||
var getInteractions = function getInteractions() { | ||
var _resourceRepresentati2; | ||
return ((resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati2 = resourceRepresentation._options) === null || _resourceRepresentati2 === void 0 ? void 0 : _resourceRepresentati2.links) || []).map(function (interaction) { | ||
return _objectSpread2(_objectSpread2({}, interaction), getInteractionHelpers(interaction)); | ||
}); | ||
var getSchema = function getSchema() { | ||
return getSchemaProperty(property); | ||
}; | ||
var getInteraction = function getInteraction(rel) { | ||
return getInteractions().find(function (link) { | ||
return link.rel === rel; | ||
}); | ||
var existsInInteraction = function existsInInteraction(rel) { | ||
var _getInteraction; | ||
return Boolean((_getInteraction = getInteraction(rel)) === null || _getInteraction === void 0 ? void 0 : _getInteraction.hasProperty(property)); | ||
}; | ||
var getItems = function getItems() { | ||
var _resourceRepresentati3; | ||
var itemNode = (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati3 = resourceRepresentation._links) === null || _resourceRepresentati3 === void 0 ? void 0 : _resourceRepresentati3.item) || []; | ||
return Array.isArray(itemNode) ? itemNode : [itemNode]; | ||
}; | ||
var getItem = function getItem() { | ||
var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
return getItems()[index]; | ||
}; | ||
var getLinks = function getLinks() { | ||
var linksNode = (resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._links) || {}; | ||
return Object.keys(linksNode).filter(function (key) { | ||
return key !== "item"; | ||
}).map(function (key) { | ||
return _objectSpread2(_objectSpread2({}, linksNode[key]), {}, { | ||
rel: key | ||
}); | ||
}); | ||
}; | ||
var getLink = function getLink(rel) { | ||
return getLinks().find(function (link) { | ||
return link.rel === rel; | ||
}); | ||
}; | ||
var getSchemaProperties = function getSchemaProperties() { | ||
return getPropertiesFromSchema(resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._options); | ||
}; | ||
var getSchemaProperty = function getSchemaProperty(key) { | ||
return getSchemaProperties().find(function (property) { | ||
return property.key === key; | ||
}); | ||
}; | ||
var getRequiredProperties = function getRequiredProperties() { | ||
return getRequiredFromSchema(resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : resourceRepresentation._options); | ||
}; | ||
var isPropertyRequired = function isPropertyRequired(key) { | ||
return getRequiredProperties().includes(key); | ||
}; | ||
var getPropertyHelpers = function getPropertyHelpers(property) { | ||
var isRequired = function isRequired() { | ||
return isPropertyRequired(property); | ||
}; | ||
var getSchema = function getSchema() { | ||
return getSchemaProperty(property); | ||
}; | ||
var existsInInteraction = function existsInInteraction(rel) { | ||
var _getInteraction; | ||
return Boolean((_getInteraction = getInteraction(rel)) === null || _getInteraction === void 0 ? void 0 : _getInteraction.hasProperty(property)); | ||
}; | ||
return { | ||
isRequired: isRequired, | ||
getSchema: getSchema, | ||
existsInInteraction: existsInInteraction | ||
}; | ||
}; | ||
var getProperties = function getProperties() { | ||
var _resourceRepresentati4; | ||
return Object.keys((resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati4 = resourceRepresentation._options) === null || _resourceRepresentati4 === void 0 ? void 0 : _resourceRepresentati4.properties) || {}).filter(function (key) { | ||
return key !== "_embedded" && key !== "_options" && key !== "_links"; | ||
}).map(function (key) { | ||
return _objectSpread2({ | ||
key: key, | ||
value: resourceRepresentation[key] | ||
}, getPropertyHelpers(key)); | ||
}); | ||
}; | ||
var getProperty = function getProperty(key) { | ||
return getProperties().find(function (property) { | ||
return property.key === key; | ||
}); | ||
}; | ||
var requiredParam = function requiredParam(param) { | ||
var requiredParamError = new Error("Required parameter, \"".concat(param, "\" is missing.")); | ||
throw requiredParamError; | ||
}; | ||
var addLink = function addLink() { | ||
var link = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : requiredParam("href"); | ||
createLinksSection(); | ||
var rel = link.rel, | ||
linkWithoutRel = _objectWithoutProperties(link, _excluded); | ||
rel ? resourceRepresentation._links[rel] = linkWithoutRel : requiredParam("rel"); | ||
}; | ||
var addLinks = function addLinks() { | ||
var links = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
links.forEach(function (link) { | ||
addLink(link); | ||
}); | ||
}; | ||
var addItem = function addItem() { | ||
var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
item.href || requiredParam("href"); | ||
createItemsArray(); | ||
resourceRepresentation._links.item.push(item); | ||
}; | ||
var addItems = function addItems() { | ||
var itemsArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
createItemsArray(); | ||
itemsArray.forEach(function (item) { | ||
addItem(item); | ||
}); | ||
}; | ||
var addProperty = function addProperty(property) { | ||
if (property.key !== "_links" && property.key !== "_options" && property.key !== "_embedded") { | ||
property.key || requiredParam("key"); | ||
property.value || requiredParam("value"); | ||
resourceRepresentation[property.key] = property.value; | ||
} | ||
}; | ||
var addProperties = function addProperties() { | ||
var propertiesAray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
propertiesAray.forEach(function (prop) { | ||
addProperty(prop); | ||
}); | ||
}; | ||
var addOptionsProperties = function addOptionsProperties(propertiesAray) { | ||
createOptionsPropertiesSection(); | ||
propertiesAray.forEach(function (prop) { | ||
return addSchemaProperty(prop, resourceRepresentation._options.properties); | ||
}); | ||
}; | ||
var addOptionsProperty = function addOptionsProperty(propertyObject) { | ||
createOptionsPropertiesSection(); | ||
addSchemaProperty(propertyObject, resourceRepresentation._options.properties); | ||
}; | ||
var addTitle = function addTitle(title) { | ||
createOptionsSection(); | ||
resourceRepresentation._options.title = title; | ||
}; | ||
var addInteraction = function addInteraction(interaction) { | ||
createInteractionsSection(); | ||
interaction.rel || requiredParam("rel"); | ||
interaction.href || requiredParam("href"); | ||
resourceRepresentation._options.links.push(interaction); | ||
}; | ||
var addRequired = function addRequired(required) { | ||
createOptionsSection(); | ||
resourceRepresentation._options.required = required || []; | ||
}; | ||
var addOptions = function addOptions() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
createOptionsSection(); | ||
options.links.forEach(function (item) { | ||
addInteraction(item); | ||
}); | ||
addOptionsProperties(Array.isArray(options.properties) ? options.properties : [options.properties]); | ||
addTitle(options.title); | ||
addRequired(options.required); | ||
}; // *************************************************************************************** // | ||
var createOptionsSection = function createOptionsSection() { | ||
resourceRepresentation._options = resourceRepresentation._options || {}; | ||
}; | ||
var createOptionsPropertiesSection = function createOptionsPropertiesSection() { | ||
createOptionsSection(); | ||
resourceRepresentation._options.properties = resourceRepresentation._options.properties || {}; | ||
}; | ||
var createInteractionsSection = function createInteractionsSection() { | ||
createOptionsSection(); | ||
resourceRepresentation._options.links = resourceRepresentation._options.links || []; | ||
}; | ||
var createLinksSection = function createLinksSection() { | ||
resourceRepresentation._links = resourceRepresentation._links || {}; | ||
}; | ||
var createItemsArray = function createItemsArray() { | ||
createLinksSection(); | ||
resourceRepresentation._links.item = resourceRepresentation._links.item || []; | ||
}; | ||
return { | ||
resourceRepresentation: resourceRepresentation, | ||
getTitle: getTitle, | ||
getInteractions: getInteractions, | ||
getInteraction: getInteraction, | ||
getItems: getItems, | ||
getItem: getItem, | ||
getLinks: getLinks, | ||
getLink: getLink, | ||
getProperties: getProperties, | ||
getProperty: getProperty, | ||
getSchemaProperties: getSchemaProperties, | ||
getSchemaProperty: getSchemaProperty, | ||
getRequiredProperties: getRequiredProperties, | ||
addLink: addLink, | ||
addLinks: addLinks, | ||
addItem: addItem, | ||
addItems: addItems, | ||
addProperty: addProperty, | ||
addProperties: addProperties, | ||
addOptionsProperties: addOptionsProperties, | ||
addOptionsProperty: addOptionsProperty, | ||
addTitle: addTitle, | ||
addInteraction: addInteraction, | ||
addOptions: addOptions | ||
isRequired: isRequired, | ||
getSchema: getSchema, | ||
existsInInteraction: existsInInteraction | ||
}; | ||
}; | ||
// Header names | ||
var CONTENT_TYPE = "content-type"; | ||
var CONTENT_LOCATION = "content-location"; | ||
var LOCATION = "location"; // Media Types | ||
var HAL_JSON = "application/vnd.hal+json"; | ||
var HAL_JSON2 = "application/hal+json"; | ||
function getType() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
headers = _ref.headers; | ||
return headers[CONTENT_TYPE] ? headers[CONTENT_TYPE] : null; | ||
} | ||
function containsHalResource() { | ||
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
headers = _ref2.headers; | ||
return headers && headers[CONTENT_TYPE] ? headers[CONTENT_TYPE].includes(HAL_JSON) || headers[CONTENT_TYPE].includes(HAL_JSON2) ? true : headers[CONTENT_LOCATION] && headers[LOCATION] && headers[CONTENT_LOCATION] === headers[LOCATION] : false; | ||
} | ||
function HalResponse(_ref) { | ||
var body = _ref.body, | ||
headers = _ref.headers, | ||
status = _ref.status; | ||
return { | ||
headers: headers, | ||
body: body, | ||
status: status, | ||
type: getType({ | ||
headers: headers | ||
}), | ||
halResource: HalResource(body), | ||
containsHalResource: containsHalResource({ | ||
headers: headers | ||
}) | ||
}; | ||
} | ||
var globalheaders = {}; | ||
var setGlobalHeaders = function setGlobalHeaders(headers) { | ||
globalheaders = headers; | ||
var getProperties = function getProperties() { | ||
var _resourceRepresentati4; | ||
return Object.keys((resourceRepresentation === null || resourceRepresentation === void 0 ? void 0 : (_resourceRepresentati4 = resourceRepresentation._options) === null || _resourceRepresentati4 === void 0 ? void 0 : _resourceRepresentati4.properties) || {}).filter(function (key) { | ||
return key !== "_embedded" && key !== "_options" && key !== "_links"; | ||
}).map(function (key) { | ||
return _objectSpread2({ | ||
key: key, | ||
value: resourceRepresentation[key] | ||
}, getPropertyHelpers(key)); | ||
}); | ||
}; | ||
var patch = function patch(_ref) { | ||
var url = _ref.url, | ||
body = _ref.body, | ||
headers = _ref.headers; | ||
return axios({ | ||
url: url, | ||
data: body, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "patch" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
var getProperty = function getProperty(key) { | ||
return getProperties().find(function (property) { | ||
return property.key === key; | ||
}); | ||
}; | ||
var get = function get(_ref2) { | ||
var url = _ref2.url, | ||
headers = _ref2.headers; | ||
return axios({ | ||
url: url, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "get" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
var requiredParam = function requiredParam(param) { | ||
var requiredParamError = new Error("Required parameter, \"".concat(param, "\" is missing.")); | ||
throw requiredParamError; | ||
}; | ||
var addLink = function addLink() { | ||
var link = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : requiredParam("href"); | ||
createLinksSection(); | ||
var rel = link.rel, | ||
linkWithoutRel = _objectWithoutProperties(link, _excluded); | ||
rel ? resourceRepresentation._links[rel] = linkWithoutRel : requiredParam("rel"); | ||
}; | ||
var addLinks = function addLinks() { | ||
var links = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
links.forEach(function (link) { | ||
addLink(link); | ||
}); | ||
}; | ||
var post = function post(_ref3) { | ||
var url = _ref3.url, | ||
headers = _ref3.headers, | ||
body = _ref3.body; | ||
return axios({ | ||
url: url, | ||
data: body, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "post" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
var addItem = function addItem() { | ||
var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
item.href || requiredParam("href"); | ||
createItemsArray(); | ||
resourceRepresentation._links.item.push(item); | ||
}; | ||
var addItems = function addItems() { | ||
var itemsArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
createItemsArray(); | ||
itemsArray.forEach(function (item) { | ||
addItem(item); | ||
}); | ||
}; | ||
var del = function del(_ref4) { | ||
var url = _ref4.url, | ||
headers = _ref4.headers; | ||
return axios({ | ||
url: url, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "delete" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
var addProperty = function addProperty(property) { | ||
if (property.key !== "_links" && property.key !== "_options" && property.key !== "_embedded") { | ||
property.key || requiredParam("key"); | ||
property.value || property.value === 0 || property.value === "" || requiredParam("value"); | ||
resourceRepresentation[property.key] = property.value; | ||
} | ||
}; | ||
var addProperties = function addProperties() { | ||
var propertiesAray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
propertiesAray.forEach(function (prop) { | ||
addProperty(prop); | ||
}); | ||
}; | ||
var addOptionsProperties = function addOptionsProperties(propertiesAray) { | ||
createOptionsPropertiesSection(); | ||
propertiesAray.forEach(function (prop) { | ||
return addSchemaProperty(prop, resourceRepresentation._options.properties); | ||
}); | ||
}; | ||
var addOptionsProperty = function addOptionsProperty(propertyObject) { | ||
createOptionsPropertiesSection(); | ||
addSchemaProperty(propertyObject, resourceRepresentation._options.properties); | ||
}; | ||
var addTitle = function addTitle(title) { | ||
createOptionsSection(); | ||
resourceRepresentation._options.title = title; | ||
}; | ||
var addInteraction = function addInteraction(interaction) { | ||
createInteractionsSection(); | ||
interaction.rel || requiredParam("rel"); | ||
interaction.href || requiredParam("href"); | ||
resourceRepresentation._options.links.push(interaction); | ||
}; | ||
var addRequired = function addRequired(required) { | ||
createOptionsSection(); | ||
resourceRepresentation._options.required = required || []; | ||
}; | ||
var addOptions = function addOptions() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
createOptionsSection(); | ||
options.links.forEach(function (item) { | ||
addInteraction(item); | ||
}); | ||
addOptionsProperties(Array.isArray(options.properties) ? options.properties : [options.properties]); | ||
addTitle(options.title); | ||
addRequired(options.required); | ||
}; | ||
// *************************************************************************************** // | ||
var halApiCaller = { | ||
setGlobalHeaders: setGlobalHeaders, | ||
patch: patch, | ||
get: get, | ||
post: post, | ||
del: del | ||
var createOptionsSection = function createOptionsSection() { | ||
resourceRepresentation._options = resourceRepresentation._options || {}; | ||
}; | ||
var createOptionsPropertiesSection = function createOptionsPropertiesSection() { | ||
createOptionsSection(); | ||
resourceRepresentation._options.properties = resourceRepresentation._options.properties || {}; | ||
}; | ||
var createInteractionsSection = function createInteractionsSection() { | ||
createOptionsSection(); | ||
resourceRepresentation._options.links = resourceRepresentation._options.links || []; | ||
}; | ||
var createLinksSection = function createLinksSection() { | ||
resourceRepresentation._links = resourceRepresentation._links || {}; | ||
}; | ||
var createItemsArray = function createItemsArray() { | ||
createLinksSection(); | ||
resourceRepresentation._links.item = resourceRepresentation._links.item || []; | ||
}; | ||
return { | ||
resourceRepresentation: resourceRepresentation, | ||
getTitle: getTitle, | ||
getInteractions: getInteractions, | ||
getInteraction: getInteraction, | ||
getItems: getItems, | ||
getItem: getItem, | ||
getLinks: getLinks, | ||
getLink: getLink, | ||
getProperties: getProperties, | ||
getProperty: getProperty, | ||
getSchemaProperties: getSchemaProperties, | ||
getSchemaProperty: getSchemaProperty, | ||
getRequiredProperties: getRequiredProperties, | ||
addLink: addLink, | ||
addLinks: addLinks, | ||
addItem: addItem, | ||
addItems: addItems, | ||
addProperty: addProperty, | ||
addProperties: addProperties, | ||
addOptionsProperties: addOptionsProperties, | ||
addOptionsProperty: addOptionsProperty, | ||
addTitle: addTitle, | ||
addInteraction: addInteraction, | ||
addOptions: addOptions | ||
}; | ||
}; | ||
exports.HalApiCaller = halApiCaller; | ||
exports.HalResource = HalResource; | ||
exports.HalResponse = HalResponse; | ||
// Header names | ||
var CONTENT_TYPE = "content-type"; | ||
var CONTENT_LOCATION = "content-location"; | ||
var LOCATION = "location"; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
// Media Types | ||
var HAL_JSON = "application/vnd.hal+json"; | ||
var HAL_JSON2 = "application/hal+json"; | ||
function getType() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
headers = _ref.headers; | ||
return headers[CONTENT_TYPE] ? headers[CONTENT_TYPE] : null; | ||
} | ||
function containsHalResource() { | ||
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
headers = _ref2.headers; | ||
return headers && headers[CONTENT_TYPE] ? headers[CONTENT_TYPE].includes(HAL_JSON) || headers[CONTENT_TYPE].includes(HAL_JSON2) ? true : headers[CONTENT_LOCATION] && headers[LOCATION] && headers[CONTENT_LOCATION] === headers[LOCATION] : false; | ||
} | ||
}))); | ||
function HalResponse(_ref) { | ||
var body = _ref.body, | ||
headers = _ref.headers, | ||
status = _ref.status; | ||
return { | ||
headers: headers, | ||
body: body, | ||
status: status, | ||
type: getType({ | ||
headers: headers | ||
}), | ||
halResource: HalResource(body), | ||
containsHalResource: containsHalResource({ | ||
headers: headers | ||
}) | ||
}; | ||
} | ||
var globalheaders = {}; | ||
var setGlobalHeaders = function setGlobalHeaders(headers) { | ||
globalheaders = headers; | ||
}; | ||
var patch = function patch(_ref) { | ||
var url = _ref.url, | ||
body = _ref.body, | ||
headers = _ref.headers; | ||
return axios({ | ||
url: url, | ||
data: body, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "patch" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var get = function get(_ref2) { | ||
var url = _ref2.url, | ||
headers = _ref2.headers; | ||
return axios({ | ||
url: url, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "get" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var post = function post(_ref3) { | ||
var url = _ref3.url, | ||
headers = _ref3.headers, | ||
body = _ref3.body; | ||
return axios({ | ||
url: url, | ||
data: body, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "post" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var put = function put(_ref4) { | ||
var url = _ref4.url, | ||
headers = _ref4.headers, | ||
body = _ref4.body; | ||
return axios({ | ||
url: url, | ||
data: body, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "put" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var del = function del(_ref5) { | ||
var url = _ref5.url, | ||
headers = _ref5.headers; | ||
return axios({ | ||
url: url, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "delete" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: res.data, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var options = function options(_ref6) { | ||
var url = _ref6.url, | ||
headers = _ref6.headers; | ||
return axios({ | ||
url: url, | ||
headers: _objectSpread2(_objectSpread2({}, globalheaders), headers), | ||
method: "options" | ||
}).then(function (res) { | ||
return HalResponse({ | ||
body: { | ||
_options: res.data | ||
}, | ||
headers: res.headers, | ||
status: res.status | ||
}); | ||
}); | ||
}; | ||
var halApiCaller = { | ||
setGlobalHeaders: setGlobalHeaders, | ||
patch: patch, | ||
get: get, | ||
post: post, | ||
put: put, | ||
del: del, | ||
options: options | ||
}; | ||
export { halApiCaller as HalApiCaller, HalResource, HalResponse }; |
{ | ||
"name": "@dxc-technology/halstack-client", | ||
"version": "0.0.0-2135bec", | ||
"version": "0.0.0-25d27ff", | ||
"description": "DXC Technology Halstack Client", | ||
@@ -9,3 +9,4 @@ "repository": { | ||
}, | ||
"main": "./main.js", | ||
"main": "./main.cjs", | ||
"module": "./main.js", | ||
"scripts": { | ||
@@ -16,33 +17,32 @@ "bundle": "rollup -c", | ||
"build": "npm run bundle && npm run copy", | ||
"test": "jest", | ||
"test:watch": "npm test -- --watch --coverage", | ||
"test:coverage": "npm test -- --coverage --coverageReporters=cobertura", | ||
"test": "jest --collect-coverage --transformIgnorePatterns node_modules/?!axios/", | ||
"test:watch": "npm test --watch --coverage", | ||
"test:coverage": "npm test --collect-coverage --coverageReporters=cobertura", | ||
"test:coverage-dashboard": "npm test --collect-coverage" | ||
}, | ||
"author": "Jesus Suarez Ardid", | ||
"license": "ISC", | ||
"author": "DXC Halstack team", | ||
"license": "Apache v2.0", | ||
"devDependencies": { | ||
"@babel/core": "^7.2.2", | ||
"@babel/core": "7.23.2", | ||
"@babel/plugin-proposal-optional-chaining": "^7.8.3", | ||
"@babel/preset-env": "^7.2.3", | ||
"@babel/preset-env": "7.23.2", | ||
"@rollup/plugin-babel": "^6.0.4", | ||
"axios-mock-adapter": "^1.22.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^10.1.0", | ||
"babel-jest": "^23.6.0", | ||
"babel-jest": "^27.5.1", | ||
"babel-loader": "^8.0.4", | ||
"babel-polyfill": "^6.26.0", | ||
"eslint": "^5.10.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jest": "^22.4.1", | ||
"jest": "^23.6.0", | ||
"jest-junit": "^10.0.0", | ||
"eslint": "^8.41.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"jest": "^27.5.1", | ||
"jest-junit": "^13.0.0", | ||
"regenerator-runtime": "^0.13.1", | ||
"rollup": "^1.9.0", | ||
"rollup-plugin-babel": "^4.1.0", | ||
"rollup": "^4.28.0", | ||
"rollup-plugin-node-resolve": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.21.0" | ||
"axios": "^1.6.2" | ||
} | ||
} |
151
README.md
# Halstack Client for JavaScript | ||
Halstack Client for JavaScript enables developers to easily work with hypermedia-based APIs in a declarative manner. It offers a set of functionalities to facilitate the navigation between hypermedia resources and get access to their properties dynamically, based on the JSON:API and JSON Hyperschema specifications. | ||
Halstack Client for JavaScript enables developers to easily work with hypermedia-based APIs in a declarative manner. It offers a set of functionalities to facilitate the navigation between hypermedia resources and get access to their properties dynamically, based on the [HAL](https://datatracker.ietf.org/doc/html/draft-kelly-json-hal) and [JSON Hyper-Schema](https://json-schema.org/draft/2019-09/json-schema-hypermedia.html) draft specifications, as well as [DXC API guidelines](https://developer.dxc.com/apis) and HAL extensions. | ||
This SDK is released as a client-agnostic library that can be used both in browser applications (such as Vanilla JS, React or Angular applications) or backend applications (node.js applications or AWS Lambdas). | ||
This SDK is released as a client-agnostic library that can be used both in browser applications (such as Vanilla JS, React or Angular applications) or backend applications (NodeJS applications). As such, it is distributed as UMD. | ||
@@ -20,3 +20,3 @@ ## Installation | ||
HalResponse, | ||
HalApiCaller | ||
HalApiCaller, | ||
} = require("@dxc-technology/halstack-client"); | ||
@@ -32,3 +32,3 @@ ``` | ||
HalResponse, | ||
HalApiCaller | ||
HalApiCaller, | ||
} from "@dxc-technology/halstack-client"; | ||
@@ -43,33 +43,33 @@ ``` | ||
The HalResource can also be used to help developers to create a compliant HaleRource representation to be included in a Hal response, exposing functions to add different sections that tipically can be found in a resource. | ||
The `HalResource` can also be used to help developers to create a compliant `HalResource` representation to be included in a Hal response, exposing functions to add different sections that typically can be found in a resource. | ||
### HalResource Object | ||
| Name | Type | Description | | ||
| :----------------------- | :------------------------- | :---------------------------------------------------------------------------------------------------------------------------------- | | ||
| `resourceRepresentation` | `Object` | The whole resource representation. | | ||
| `getTitle` | `()=>String` | Returns the resource title. Taken from "\_options.title". | | ||
| `getInteractions` | `()=>HalInteraction[]` | Returns an array of [HalInteraction](#halinteraction-object) objects. Taken from the "\_options.links" array. | | ||
| `getInteraction` | `(String)=>HalInteraction` | Receives a string with the rel of the interaction. Returns the [HalInteraction](#halinteraction-object) object. | | ||
| `getItems` | `()=>Object[]` | Returns an array of the original item link objects. Taken from the "\_links.item" array. | | ||
| `getInteraction` | `(Number)=>Object` | Receives the index of the item. Returns the item object. | | ||
| `getLinks` | `()=>Object[]` | Returns an array of the original link objects (adding the "rel"), ignoring "item" link. Taken from the "\_links" object. | | ||
| `getLink` | `(String)=>Object` | Receives a string with the rel of the link. Returns the link object (adding the "rel"). | | ||
| `getProperties` | `()=>Object[]` | Returns an array of [HalProperty](#halproperty-object) objects. Taken from the resource root level properties. | | ||
| `getProperty` | `(String)=>Object` | Receives a string with the key of the property. Returns the [HalProperty](#halproperty-object) object. | | ||
| `getRequiredProperties` | `()=>String[]` | Returns an array with the required properties key. Taken from the "\_options.required" array. | | ||
| `isPropertyRequired` | `(String)=>Bool` | Receives the key of a property. Returns true if it exists within the "\_options.required" array. | | ||
| `getSchemaProperties` | `()=>Object[]` | Returns an array of the original schema objects for properties (adding "key"). Taken from the "\_options.properties" object. | | ||
| `getSchemaProperty` | `(String)=>Object` | Receives the key of a property. Returns the property's schema object (adding "key") if exists within "\_options.properties" object. | | ||
| `addLink` | `(Object)=>void` | Receives an object as parameter. This object can contain the usual link properties as { rel, name, href, title } where rel and href are required. The obect will be added to the "\links" section using rel as key | | ||
| `addTitle` | `(String)=>void` | Assign the parameter received to the resource title in "\_options.title | | ||
| `addItem` | `(Object)=>void` | Add the object received as parameter as a resource _link item | | ||
| `addItems` | `(Array<Object>)=>void` | Add the every object in the list received as parameter as a resource _link item. | | ||
| `addProperties` | `(Object)=>void` | Add the key:values found in the object received as parameter as resource properties. | | ||
| `addPropertiesSchema` | `(Object)=>void` | Add every object received as parameter as property under the _options.properties section| | ||
| `addInteraction` | `(Object)=>void` | Add the object received as parameter under the options.link section | | ||
| `addOptions` | `(Object)=>void` | It receives an object as parameter. The funciton will look for links, properties, title and rquired keys and add them under the options section. | | ||
| Name | Type | Description | | ||
| :----------------------- | :------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `resourceRepresentation` | `Object` | The whole resource representation. | | ||
| `getTitle` | `()=>String` | Returns the resource title. Taken from `\_options.title`. | | ||
| `getInteractions` | `()=>HalInteraction[]` | Returns an array of [HalInteraction](#halinteraction-object) objects. Taken from the `\_options.links` array. | | ||
| `getInteraction` | `(String)=>HalInteraction` | Receives a string with the `rel` of the interaction. Returns the [HalInteraction](#halinteraction-object) object. | | ||
| `getItems` | `()=>Object[]` | Returns an array of the original item link objects. Taken from the `\_links.item` array. | | ||
| `getItem` | `(Number)=>Object` | Receives the index of the item. Returns the item object. | | ||
| `getLinks` | `()=>Object[]` | Returns an array of the original link objects (adding the `rel`), ignoring `item` link. Taken from the `\_links` object. | | ||
| `getLink` | `(String)=>Object` | Receives a string with the `rel` of the link. Returns the link object (adding the `rel`). | | ||
| `getProperties` | `()=>Object[]` | Returns an array of [HalProperty](#halproperty-object) objects. Taken from the resource root level properties. | | ||
| `getProperty` | `(String)=>Object` | Receives a string with the key of the property. Returns the [HalProperty](#halproperty-object) object. | | ||
| `getRequiredProperties` | `()=>String[]` | Returns an array with the required properties key. Taken from the `\_options.required` array. | | ||
| `isPropertyRequired` | `(String)=>Bool` | Receives the key of a property. Returns true if it exists within the `\_options.required` array. | | ||
| `getSchemaProperties` | `()=>Object[]` | Returns an array of the original schema objects for properties (adding `key`). Taken from the `\_options.properties` object. | | ||
| `getSchemaProperty` | `(String)=>Object` | Receives the key of a property. Returns the property's schema object (adding `key`) if exists within `\_options.properties` object. | | ||
| `addLink` | `(Object)=>void` | Receives an object as parameter. This object can contain the usual link properties as { `rel`, `name`, `href`, `title` } where `rel` and `href` are required. The `object` will be added to the `\links` section using `rel` as key | | ||
| `addLinks` | `(Object)=>void` | Receives an object as parameter. This object can contain the usual link properties as { `rel`, `name`, `href`, `title` } where `rel` and `href` are required. The object will be added to the `\links` section using `rel` as key | | ||
| `addTitle` | `(String)=>void` | Assign the parameter received to the resource title in `\_options`.title | | ||
| `addItem` | `(Object)=>void` | Add the object received as parameter as a resource `\_link` item | | ||
| `addItems` | `(Object[])=>void` | Add every object in the list received as parameter as a resource `\_link` item. | | ||
| `addProperties` | `(Object)=>void` | Add the key:values found in the object received as parameter as resource properties. | | ||
| `addOptionsProperties` | `(Object)=>void` | Add every property received in parameter as property under the `\_options.properties` section | | ||
| `addOptionsProperty` | `(Object)=>void` | Add the property ({key: schema}) received in parameter as property under the `\_options.properties` section | | ||
| `addInteraction` | `(Object)=>void` | Add the object received as parameter (`{rel: Interaction}`) under the `options.link` section | | ||
| `addOptions` | `(Object)=>void` | It receives an object as parameter and add interactions , properties, required and title if those sections are found as object keys. | | ||
### HalInteraction Object | ||
@@ -83,13 +83,13 @@ | ||
| `isPropertyRequired` | `(String)=>Bool` | Receives the key of a property. Returns true if it exists within the "schema.required" array of the interaction. | | ||
| `getSchemaProperties` | `()=>Object[]` | Returns an array of the original schema objects for properties (adding "key"). Taken from the "\schema.properties" object of the interaction. | | ||
| `getSchemaProperty` | `(String)=>Object` | Receives the key of a property. Returns the property's schema object (adding "key") if exists within "\schema.properties" object of the interaction. | | ||
| `hasProperty` | `(String)=>Bool` | Receives the key of a property. Returns true if it exists within the "\schema.properties" object of the interaction. | | ||
| `getSchemaProperties` | `()=>Object[]` | Returns an array of the original schema objects for properties (adding `key`). Taken from the `\schema.properties` object of the interaction. | | ||
| `getSchemaProperty` | `(String)=>Object` | Receives the key of a property. Returns the property's schema object (adding `key`) if exists within `\schema.properties` object of the interaction. | | ||
| `hasProperty` | `(String)=>Bool` | Receives the key of a property. Returns true if it exists within the `\schema.properties` object of the interaction. | | ||
### HalProperty Object | ||
| Name | Type | Description | | ||
| :-------------------- | :--------------- | :---------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `isRequired` | `()=>Bool` | Returns true if the property exists within the "\_options.required" array. | | ||
| `existsInInteraction` | `(String)=>Bool` | Receives a string with the rel of an interaction. Returns true if the property exists within the "schema.properties" object of the interaction. | | ||
| `getSchema` | `()=>Object` | Returns the property's schema object (adding "key") if exists within "\_options.properties" object. | | ||
| Name | Type | Description | | ||
| :-------------------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| `isRequired` | `()=>Bool` | Returns true if the property exists within the `\_options.required` array. | | ||
| `existsInInteraction` | `(String)=>Bool` | Receives a string with the `rel` of an interaction. Returns true if the property exists within the "schema.properties" object of the interaction. | | ||
| `getSchema` | `()=>Object` | Returns the property's schema object (adding `key`) if exists within `\_options.properties` object. | | ||
@@ -102,3 +102,3 @@ ## HalResponse Factory | ||
| :-------- | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `body` | `Object` | The message-body is the data bytes transmitted assoiciated with a http response. | | ||
| `body` | `Object` | The message-body is the data bytes transmitted associated with a http response. | | ||
| `headers` | `Object` | Contains the http headers which provide information about the server and about further access to the resource identified by the Request-URI. | | ||
@@ -116,58 +116,21 @@ | `status` | `Number` | Status codes are issued by a server in response to a client's request made to the server. It includes codes from IETF Request for Comments (RFCs). | | ||
| `status` | `Number` | Https response code | | ||
| `type` | `String` | Response format. Example: application/json | | ||
| `type` | `String` | Response format. Example: application/JSON | | ||
| `halResource` | `HalResource` | [HalResource Object](#halresource-object) composed using the information provided inside the body. | | ||
| `containsHalResource` | `Bool` | True if the content of the response is a HAL resource. | | ||
| `containsHalResource` | `Bool` | True if the content of the response is a HAL resource. | | ||
## HalApiCaller Module | ||
This module is a http client which takes care of handing out parsed HalResponses. It exports the following functions: | ||
This module is a HTTP client which takes care of handing out parsed `HalResponses`. It exports the following functions: | ||
| Name | Type | Description | | ||
| :----------------- | :------------------------------ | :---------------------------------------------------------------------------------------------------------------------------- | | ||
| `setGlobalHeaders` | `function` | Receives a headers object that will be used for all http requests done with this module. | | ||
| `get` | `function:Promise(HalResponse)` | Receives a { url, headers } object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `patch` | `function:Promise(HalResponse)` | Receives a { url, body, headers } object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `post` | `function:Promise(HalResponse)` | Receives a { url, body, headers } object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `del` | `function:Promise(HalResponse)` | Receives a { url, headers } object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| Name | Type | Description | | ||
| :----------------- | :------------------------------ | :------------------------------------------------------------------------------------------------------------------------------ | | ||
| `setGlobalHeaders` | `function` | Receives a headers object that will be used for all http requests done with this module. | | ||
| `get` | `function:Promise(HalResponse)` | Receives a `{ url, headers }` object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `options` | `function:Promise(HalResponse)` | Receives a `{ url, headers }` object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `patch` | `function:Promise(HalResponse)` | Receives a `{ url, body, headers }` object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `post` | `function:Promise(HalResponse)` | Receives a `{ url, body, headers }` object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `put` | `function:Promise(HalResponse)` | Receives a `{ url, body, headers }` object as a parameter. Returns a promise and will pass a HalResponse object to the handler. | | ||
| `del` | `function:Promise(HalResponse)` | Receives a `{ url, headers }` object as a parameter. Returns a promise and will pass a `HalResponse` object to the handler. | | ||
## Develop | ||
In the first place, clone this repository. | ||
```bash | ||
git clone https://github.com/dxc-technology/dxc-halstack-client.git | ||
``` | ||
Local development is broken into two parts, related to the lib/example folders. | ||
First, within the lib folder, run rollup to watch your `src/` module and automatically recompile it into `dist/` whenever you make changes. | ||
```bash | ||
cd lib | ||
npm install | ||
npm start # runs rollup with watch flag | ||
``` | ||
The second part will be running the examples linked to the local version of your module. | ||
### React example | ||
```bash | ||
# (in another tab) | ||
cd example/front-end-react | ||
npm install | ||
npm start # runs create-react-app dev server | ||
``` | ||
Now, anytime you make a change to your library in `src/` or to the example app's `example/src`, `create-react-app` will live-reload your local dev server so you can iterate on your component in real-time. | ||
### Node example | ||
```bash | ||
# (in another tab) | ||
cd example/node | ||
npm install | ||
npm start # runs node application | ||
``` | ||
# License | ||
@@ -179,10 +142,2 @@ | ||
RESOURCE: | ||
- Is property patchable (Interaction or property) | ||
- Status Report | ||
RESPONSE: | ||
- X-Modified headers | ||
- X-Delete headers | ||
See our [issues](https://github.com/dxc-technology/halstack-client/issues) |
52744
61.15%18
-5.26%4
33.33%975
120.59%136
-25.27%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
Updated