@stackend/api
Version:
JS bindings to api.stackend.com
79 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReferenceAsString = exports.getReference = exports.constructReference = exports.parseReference = void 0;
/**
* UID of a specific type of object in a context
*/
var CommunityContext_1 = require("./CommunityContext");
/**
* Parse a reference
* @param reference
* @returns {null|Reference}
*/
function parseReference(reference) {
if (!reference) {
return null;
}
var p = reference.split('-', 4);
if (p.length !== 3) {
return null;
}
var id = parseInt(p[2]);
if (isNaN(id)) {
return null;
}
var cc = (0, CommunityContext_1.parseCommunityContext)(p[0]);
if (!cc) {
return null;
}
return {
communityContext: cc,
type: p[1],
id: id
};
}
exports.parseReference = parseReference;
/**
* Construct a reference
* @param xcapCommunityName
* @param context
* @param type
* @param id
*/
function constructReference(xcapCommunityName, context, type, id) {
var c = xcapCommunityName + ':' + context;
var cc = (0, CommunityContext_1.parseCommunityContext)(c);
if (!cc) {
throw Error('Invalid communityContext: ' + c);
}
return {
communityContext: cc,
type: type,
id: id
};
}
exports.constructReference = constructReference;
/**
* Get a reference
* @param xcapCommunityName
* @param context
* @param obj
* @returns {Reference}
*/
function getReference(xcapCommunityName, context, obj) {
return constructReference(xcapCommunityName, context, obj.__type, obj.id);
}
exports.getReference = getReference;
/**
* Get a reference as a string
* @param ref
* @returns {string|null}
*/
function getReferenceAsString(ref) {
if (!ref) {
return null;
}
return ref.communityContext.community + ':' + ref.communityContext.context + '-' + ref.type + '-' + ref.id;
}
exports.getReferenceAsString = getReferenceAsString;
//# sourceMappingURL=Reference.js.map