@siren-js/client
Version:
Siren API client library
99 lines (98 loc) • 3.39 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 { Type } from 'class-transformer';
import { ArrayUnique, IsArray, IsMimeType, IsOptional, IsString, ValidateNested } from 'class-validator';
import { IsUri } from '../utils/is-uri';
import { Field } from './field';
/**
* Represents available behavior exposed by an `Entity`.
*/
export class Action {
/**
* List of strings describing the nature of the `Action` based on the current representation. Possible values are
* implementation-dependent and should be documented.
*/
class = [];
/**
* Input controls of the `Action`.
*/
fields = [];
/**
* URI of the action
*/
href;
/**
* Protocol method used when submitting the `Action`. When missing, the default is assumed to be `'GET'`.
*/
method = 'GET';
/**
* Name identifying the action to be performed. Must be unique within an `Entity`'s `actions`.
*/
name;
/**
* Descriptive text about the `Action`.
*/
title;
/**
* Encoding type indicating how `fields` are serialized when submitting the `Action`. When missing, the default is
* assumed to be `'application/x-www-form-urlencoded'`.
*/
type = 'application/x-www-form-urlencoded';
/**
* Visits this action's {@linkcode fields} followed by the action itself.
*/
async accept(visitor) {
await Promise.all(this.fields.map((field) => field.accept(visitor)));
await visitor.visitAction(this);
}
/**
* Returns the `Field` in `fields` with the given `name`, if it exists. Otherwise, returns `undefined`.
*/
getField(name) {
return this.fields.find((field) => field.name === name);
}
}
__decorate([
IsArray(),
IsString({ each: true }),
IsOptional(),
__metadata("design:type", Array)
], Action.prototype, "class", void 0);
__decorate([
IsArray(),
ValidateNested({ each: true }),
ArrayUnique((field) => field.name),
IsOptional(),
Type(() => Field),
__metadata("design:type", Array)
], Action.prototype, "fields", void 0);
__decorate([
IsUri(),
__metadata("design:type", String)
], Action.prototype, "href", void 0);
__decorate([
IsString(),
IsOptional(),
__metadata("design:type", Object)
], Action.prototype, "method", void 0);
__decorate([
IsString(),
__metadata("design:type", String)
], Action.prototype, "name", void 0);
__decorate([
IsString(),
IsOptional(),
__metadata("design:type", String)
], Action.prototype, "title", void 0);
__decorate([
IsMimeType(),
IsOptional(),
__metadata("design:type", Object)
], Action.prototype, "type", void 0);