UNPKG

ketting

Version:

Opiniated HATEAOS / Rest client.

84 lines 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.factory = exports.JsonApiState = void 0; const base_state_1 = require("./base-state"); const util_1 = require("../http/util"); const link_1 = require("../link"); /** * Represents a resource state in the HAL format */ class JsonApiState extends base_state_1.BaseState { serializeBody() { return JSON.stringify(this.data); } clone() { return new JsonApiState(this.uri, this.data, new Headers(this.headers), new link_1.Links(this.uri, this.links)); } } exports.JsonApiState = JsonApiState; /** * Turns a HTTP response into a JsonApiState */ const factory = async (uri, response) => { const body = await response.json(); const links = util_1.parseLink(uri, response.headers.get('Link')); links.add(...parseJsonApiLinks(uri, body), ...parseJsonApiCollection(uri, body)); return new JsonApiState(uri, body, response.headers, links); }; exports.factory = factory; /** * This function takes a JSON:API object, and extracts the links property. */ function parseJsonApiLinks(contextUri, body) { const result = []; if (body.links === undefined) { return result; } for (const [rel, linkValue] of Object.entries(body.links)) { if (Array.isArray(linkValue)) { result.push(...linkValue.map(link => parseJsonApiLink(contextUri, rel, link))); } else { result.push(parseJsonApiLink(contextUri, rel, linkValue)); } } return result; } /** * Find collection members in JSON:API objects. * * A JSON:API top-level object might represent a collection that has 0 or more * members. * * Members of this collection should appear as an 'item' link to the parent. */ function parseJsonApiCollection(contextUri, body) { if (!Array.isArray(body.data)) { // Not a collection return []; } const result = []; for (const member of body.data) { if ('links' in member && 'self' in member.links) { const selfLink = parseJsonApiLink(contextUri, 'self', member.links.self); result.push({ context: contextUri, href: selfLink.href, rel: 'item' }); } } return result; } /** * This function takes a single link value from a JSON:API link object, and * returns a object of type Link */ function parseJsonApiLink(contextUri, rel, link) { return ({ context: contextUri, rel, href: typeof link === 'string' ? link : link.href, }); } //# sourceMappingURL=jsonapi.js.map