1 import {Injectable, EventEmitter} from '@angular/core';
2 import {OrgService} from '@eg/core/org.service';
5 TODO: Add Display Fields to UNAPI
6 https://library.biz/opac/extras/unapi?id=tag::U2@bre/1{bre.extern,holdings_xml,mra}/BR1/0&format=mods32
9 const UNAPI_PATH = '/opac/extras/unapi?id=tag::U2@';
11 interface UnapiParams {
12 target: string; // bre, ...
13 id: number | string; // 1 | 1,2,3,4,5
14 extras: string; // {holdings_xml,mra,...}
15 format: string; // mods32, marxml, ...
16 orgId?: number; // org unit ID
17 depth?: number; // org unit depth
21 export class UnapiService {
23 constructor(private org: OrgService) {}
25 createUrl(params: UnapiParams): string {
26 const depth = params.depth || 0;
27 const org = params.orgId ? this.org.get(params.orgId) : this.org.root();
29 return `${UNAPI_PATH}${params.target}/${params.id}${params.extras}/` +
30 `${org.shortname()}/${depth}&format=${params.format}`;
33 getAsXmlDocument(params: UnapiParams): Promise<XMLDocument> {
34 // XReq creates an XML document for us. Seems like the right
36 const url = this.createUrl(params);
37 return new Promise((resolve, reject) => {
38 const xhttp = new XMLHttpRequest();
39 xhttp.onreadystatechange = function() { // no () => {} !
40 if (this.readyState === 4) {
41 if (this.status === 200) {
42 resolve(xhttp.responseXML);
44 reject(`UNAPI request failed for ${url}`);
48 xhttp.open('GET', url, true);