semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
98 lines • 4.05 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.instanceOfForm = exports.knownFormPathElements = void 0;
var semantic_link_1 = require("semantic-link");
var linkRelation_1 = require("../../linkRelation");
/**
* This is a known hack to determine if a URL refers to a form. This is used
* to match a path element from URLs of the form:
* - https://schema.example.com/my/form/create
* - https://schema.example.com/my/create-form
*/
exports.knownFormPathElements = [
'form',
linkRelation_1.LinkRelation.CreateForm,
linkRelation_1.LinkRelation.EditForm,
linkRelation_1.LinkRelation.ApplyForm,
linkRelation_1.LinkRelation.SearchForm,
];
/**
* A guard to detect whether the object is a form {@link FormRepresentation}
*
* @see https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript
* @param object
* @returns whether the object is an instance on the interface
*/
function instanceOfForm(object) {
// form starts off looking like a collection with 'items'
var items = __assign({}, object).items;
if ((0, semantic_link_1.instanceOfLinkedRepresentation)(object) && Array.isArray(items)) {
var _a = __read(items, 1), first = _a[0];
// simple check that the items has a 'type' in it
if (first !== undefined && 'type' in first) {
/*
* However, there is a false match in the case of a collection that has items hydrated
* where the resource also has an attribute with 'type'. (This isn't really either an edge case either.)
*
* In this case, double check that the convention of 'form' is used in the url
*
* NOTE: this is very wrong because uris should be transparent
*
* Finally, the symptom that this solves is that collections are reloaded as singletons meaning
* that the items have id/title loaded as attributes rather than a link relations
*
* TODO: work out a better type strategy
* @see {@link LinkRelation.CreateForm}
* @see {@link LinkRelation.EditForm}
* @see {@link LinkRelation.SearchForm}
* @see {@link LinkRelation.ApplyForm}
*/
var uri = semantic_link_1.LinkUtil.getUri(object, linkRelation_1.LinkRelation.Self);
if (uri) {
try {
var path = new URL(uri).pathname;
if (path) {
var pathComponents_1 = path.split('/');
return exports.knownFormPathElements.some(function (x) { return pathComponents_1.includes(x); });
}
}
catch (e) {
// The uri isn't able to be deconstructed so fallback to treating the 'self' URL
// as a string and perform a simple contains match.
return uri.includes('form');
}
}
return false;
}
}
return false;
}
exports.instanceOfForm = instanceOfForm;
//# sourceMappingURL=instanceOfForm.js.map