UNPKG

@siren-js/core

Version:

Cross-platform library of classes for generating and parsing Siren entities

88 lines (87 loc) 3.74 kB
"use strict"; 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.Action = void 0; const class_transformer_1 = require("class-transformer"); const class_validator_1 = require("class-validator"); const Field_1 = require("./Field"); const utils_1 = require("./utils"); const IsUri_1 = require("./utils/IsUri"); /** * Represents available behavior exposed by an `Entity`. */ class Action { constructor() { /** * Protocol method used when submitting the `Action`. When missing, the default is assumed to be `'GET'`. */ this.method = 'GET'; /** * Encoding type indicating how `fields` are serialized when submitting the `Action`. When missing, the default is * assumed to be `'application/x-www-form-urlencoded'`. */ this.type = 'application/x-www-form-urlencoded'; } /** * Finds all `Field`s in this `Action` with the given `name`. Returns `undefined` if no `Field` exists with that * `name`. */ findFieldByName(name) { var _a; return (_a = this.fields) === null || _a === void 0 ? void 0 : _a.find((field) => field.name === name); } /** * Finds all `Field`s in this `Action` with the given `classes`. Returns an empty array if no `Field`s match. */ findFieldsByClass(...classes) { var _a, _b; return (_b = (_a = this.fields) === null || _a === void 0 ? void 0 : _a.filter((field) => classes.every((c) => { var _a; return (_a = field.class) === null || _a === void 0 ? void 0 : _a.includes(c); }))) !== null && _b !== void 0 ? _b : []; } static of(action) { return (0, utils_1.transformAndValidate)(this, action); } } __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsString)({ each: true }), __metadata("design:type", Array) ], Action.prototype, "class", void 0); __decorate([ (0, class_validator_1.ArrayUnique)((field) => field.name), (0, class_validator_1.IsOptional)(), (0, class_transformer_1.Type)(() => Field_1.Field), (0, class_validator_1.ValidateNested)({ each: true }), __metadata("design:type", Array) ], Action.prototype, "fields", void 0); __decorate([ (0, IsUri_1.IsUri)(), __metadata("design:type", String) ], Action.prototype, "href", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], Action.prototype, "method", void 0); __decorate([ (0, class_validator_1.IsString)(), __metadata("design:type", String) ], Action.prototype, "name", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], Action.prototype, "title", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsMimeType)(), __metadata("design:type", String) ], Action.prototype, "type", void 0); exports.Action = Action;