UNPKG

siren-types

Version:

A bunch of Siren types defined in TypeScript plus some useful functions

59 lines (58 loc) 1.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SirenResponse = void 0; class SirenResponse { constructor(def, prev) { if (def) { this.resp = { ...(prev || {}), properties: def }; } else { this.resp = { ...(prev || {}) }; } } addProperty(key, val) { let props = this.resp.properties; if (props) { props[key] = val; } return new SirenResponse(props, this.resp); } properties(props) { return new SirenResponse(props, this.resp); } link(rel, href, rest) { const resp = { ...this.resp, links: [ ...(this.resp.links || []), { ...(rest || {}), rel: rel, href: href } ] }; return new SirenResponse(this.resp.properties, resp); } action(name, href, rest) { const resp = { ...this.resp, actions: [ ...(this.resp.actions || []), { ...(rest || {}), name: name, href: href } ] }; return new SirenResponse(this.resp.properties, resp); } asJSON() { return this.resp; } } exports.SirenResponse = SirenResponse;