@siren-js/core
Version:
Cross-platform library of classes for generating and parsing Siren entities
86 lines (85 loc) • 3.86 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddedEntity = void 0;
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const Action_1 = require("./Action");
const Link_1 = require("./Link");
const utils_1 = require("./utils");
/**
* Represents a URI-addressable resource
*/
class EmbeddedEntity {
/**
* Finds an `Action` in this `Entity` with the given `name`. Returns `undefined` if no `Action` exists with that
* `name`.
*/
findActionByName(name) {
var _a;
return (_a = this.actions) === null || _a === void 0 ? void 0 : _a.find((action) => action.name === name);
}
/**
* Finds all sub-entities in this `Entity` with the given `rels`. Returns an empty array if no sub-entities match.
*/
findEntitiesByRel(...rels) {
var _a, _b;
return (_b = (_a = this.entities) === null || _a === void 0 ? void 0 : _a.filter((entity) => rels.every((rel) => entity.rel.includes(rel)))) !== null && _b !== void 0 ? _b : [];
}
/**
* Finds all `Link`s in this `Entity` with the given `rels`. Returns an empty array if no `Link`s match.
*/
findLinksByRel(...rels) {
var _a, _b;
return (_b = (_a = this.links) === null || _a === void 0 ? void 0 : _a.filter((link) => rels.every((rel) => link.rel.includes(rel)))) !== null && _b !== void 0 ? _b : [];
}
static of(embeddedEntity) {
return (0, utils_1.transformAndValidate)(this, embeddedEntity);
}
}
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_transformer_1.Type)(() => Action_1.Action),
(0, class_validator_1.ValidateNested)({ each: true }),
__metadata("design:type", Array)
], EmbeddedEntity.prototype, "actions", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)({ each: true }),
__metadata("design:type", Array)
], EmbeddedEntity.prototype, "class", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_transformer_1.Transform)(({ value }) => (0, utils_1.transformSubEntities)(value)),
(0, class_validator_1.ValidateNested)({ each: true }),
__metadata("design:type", Array)
], EmbeddedEntity.prototype, "entities", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_transformer_1.Type)(() => Link_1.Link),
(0, class_validator_1.ValidateNested)({ each: true }),
__metadata("design:type", Array)
], EmbeddedEntity.prototype, "links", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsObject)(),
__metadata("design:type", Object)
], EmbeddedEntity.prototype, "properties", void 0);
__decorate([
(0, class_validator_1.IsString)({ each: true }),
__metadata("design:type", Array)
], EmbeddedEntity.prototype, "rel", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], EmbeddedEntity.prototype, "title", void 0);
exports.EmbeddedEntity = EmbeddedEntity;