UNPKG

alcaeus

Version:

Hydra Core hypermedia-aware client library

71 lines (70 loc) 2.37 kB
import li from 'parse-link-header'; import * as Constants from './Constants.js'; import { patchResponseBody } from './helpers/fetchToStream.js'; import { stripContentTypeParameters } from './mediaType.js'; export default class { constructor(requestedUri, xhr, parsers, jsonLdContext) { this.requestedUri = requestedUri; this.xhr = xhr; this.parsers = parsers; this.jsonLdContext = jsonLdContext; } quadStream() { const quadStream = this.parsers.import(stripContentTypeParameters(this.mediaType), patchResponseBody(this.xhr), { baseIRI: this.effectiveUri, context: this.jsonLdContext }); if (quadStream == null) { return null; } return quadStream; } get status() { return this.xhr.status; } get apiDocumentationLink() { if (this.xhr.headers.has(Constants.Headers.Link)) { const apiDocumentation = this.links[Constants.LinkRelations.apiDocumentation]; if (apiDocumentation) { return this.resolveUri(apiDocumentation.url); } } return null; } get links() { const linkHeaders = this.xhr.headers.get(Constants.Headers.Link) || ''; return li(linkHeaders) || {}; } get mediaType() { if (this.links[Constants.LinkRelations.context]) { return 'application/ld+json'; } return this.xhr.headers.get(Constants.Headers.ContentType) || ''; } get redirectUrl() { if (this.xhr.redirected) { return this.xhr.url; } return null; } get effectiveUri() { return this.redirectUrl || this.xhr.url; } get resourceUri() { return this.createdResourceUri || this.canonicalUri || this.effectiveUri; } get createdResourceUri() { const location = this.xhr.headers.get(Constants.Headers.Location); if (this.xhr.status === 201 && location !== null) { return this.resolveUri(location); } return undefined; } get canonicalUri() { const canonical = this.links[Constants.LinkRelations.canonical]; if (canonical) { return this.resolveUri(canonical.url); } return undefined; } resolveUri(uri) { return new URL(uri, this.effectiveUri).toString(); } }