siren-types
Version:
A bunch of Siren types defined in TypeScript plus some useful functions
81 lines (80 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAction = exports.normalizeEmbedded = exports.normalizeEmbeddedRepresentationSubEntity = exports.normalizeEmbeddedLinkSubEntity = exports.normalize = void 0;
const functions_1 = require("./functions");
/**
* This function normalizes an existing Siren Entity.
*
* Note this doesn't normalize properties because we cannot know how to
* normalize them. So we just hope they are what we expect.
*/
function normalize(e, defaultProps) {
let props = e.properties === undefined ? undefined : defaultProps;
if (props) {
return {
class: e.class || [],
title: e.title || null,
properties: props,
entities: e.entities
? e.entities.map((x) => normalizeEmbedded(x))
: [],
actions: e.actions ? e.actions.map((x) => normalizeAction(x)) : [],
links: e.links || [],
};
}
else {
return {
class: e.class || [],
title: e.title || null,
entities: e.entities
? e.entities.map((x) => normalizeEmbedded(x))
: [],
actions: e.actions ? e.actions.map((x) => normalizeAction(x)) : [],
links: e.links || [],
};
}
}
exports.normalize = normalize;
function normalizeEmbeddedLinkSubEntity(e) {
return {
class: e.class || [],
rel: e.rel,
href: e.href,
type: e.type || "application/octet-stream",
title: e.title || null,
};
}
exports.normalizeEmbeddedLinkSubEntity = normalizeEmbeddedLinkSubEntity;
function normalizeEmbeddedRepresentationSubEntity(e) {
return {
class: e.class || [],
rel: e.rel,
title: e.title || null,
properties: e.properties,
entities: e.entities
? e.entities.map((x) => normalizeEmbedded(x))
: [],
actions: e.actions ? e.actions.map((x) => normalizeAction(x)) : [],
links: e.links || [],
};
}
exports.normalizeEmbeddedRepresentationSubEntity = normalizeEmbeddedRepresentationSubEntity;
function normalizeEmbedded(e) {
if ((0, functions_1.isEmbeddedLink)(e)) {
return normalizeEmbeddedLinkSubEntity(e);
}
return normalizeEmbeddedRepresentationSubEntity(e);
}
exports.normalizeEmbedded = normalizeEmbedded;
function normalizeAction(a) {
return {
name: a.name,
class: a.class || [],
method: a.method || "GET",
href: a.href,
title: a.title || a.name,
type: a.type || "application/x-www-form-urlencoded",
fields: a.fields || [],
};
}
exports.normalizeAction = normalizeAction;