@siren-js/client
Version:
Siren API client library
100 lines (99 loc) • 3.55 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);
};
import { Transform, Type } from 'class-transformer';
import { ArrayUnique, IsArray, IsObject, IsOptional, IsString, ValidateNested } from 'class-validator';
import { Action } from './action';
import { Link } from './link';
import { transformSubEntities } from './sub-entity';
/**
* Represents a URI-addressable resource
* @typeParam T Type of the `properties` property
*/
export class Entity {
/**
* Available behavior exposed by the `Entity`
*/
actions = [];
/**
* List of strings describing the nature of the `Entity` based on the current representation. Possible values are
* implementation-dependent and should be documented.
*/
class = [];
/**
* Related entities represented as embedded links or representations
*/
entities = [];
/**
* Navigation links that communicate ways to navigate outside the entity graph
*/
links = [];
/**
* Key-value pairs describing the state of the `Entity`
*/
properties = {};
/**
* Descriptive text about the `Entity`
*/
title;
/**
* Visits this entity's {@linkcode actions}, {@linkcode entities}, and
* {@linkcode links}, followed by the entity itself.
*/
async accept(visitor) {
await Promise.all(this.actions.map((action) => action.accept(visitor)));
await Promise.all(this.entities.map((subEntity) => subEntity.accept(visitor)));
await Promise.all(this.links.map((link) => link.accept(visitor)));
await visitor.visitEntity(this);
}
/**
* Returns the `Action` in `actions` with the given `name`, if it exists. Otherwise, returns `undefined`.
*/
getAction(name) {
return this.actions.find((action) => action.name === name);
}
}
__decorate([
IsArray(),
ValidateNested({ each: true }),
ArrayUnique((action) => action.name),
IsOptional(),
Type(() => Action),
__metadata("design:type", Array)
], Entity.prototype, "actions", void 0);
__decorate([
IsArray(),
IsString({ each: true }),
IsOptional(),
__metadata("design:type", Array)
], Entity.prototype, "class", void 0);
__decorate([
IsArray(),
ValidateNested({ each: true }),
IsOptional(),
Transform(({ value }) => transformSubEntities(value)),
__metadata("design:type", Array)
], Entity.prototype, "entities", void 0);
__decorate([
IsArray(),
ValidateNested({ each: true }),
IsOptional(),
Type(() => Link),
__metadata("design:type", Array)
], Entity.prototype, "links", void 0);
__decorate([
IsObject(),
IsOptional(),
__metadata("design:type", Object)
], Entity.prototype, "properties", void 0);
__decorate([
IsString(),
IsOptional(),
__metadata("design:type", String)
], Entity.prototype, "title", void 0);