]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/catalog/unapi.service.ts
LP1860044 Angular catalog search result highlights
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / catalog / unapi.service.ts
1 import {Injectable, EventEmitter} from '@angular/core';
2 import {OrgService} from '@eg/core/org.service';
3
4 /*
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
7 */
8
9 const UNAPI_PATH = '/opac/extras/unapi?id=tag::U2@';
10
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
18 }
19
20 @Injectable()
21 export class UnapiService {
22
23     constructor(private org: OrgService) {}
24
25     createUrl(params: UnapiParams): string {
26         const depth = params.depth || 0;
27         const org = params.orgId ? this.org.get(params.orgId) : this.org.root();
28
29         return `${UNAPI_PATH}${params.target}/${params.id}${params.extras}/` +
30             `${org.shortname()}/${depth}&format=${params.format}`;
31     }
32
33     getAsXmlDocument(params: UnapiParams): Promise<XMLDocument> {
34         // XReq creates an XML document for us.  Seems like the right
35         // tool for the job.
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);
43                     } else {
44                         reject(`UNAPI request failed for ${url}`);
45                     }
46                 }
47             };
48             xhttp.open('GET', url, true);
49             xhttp.send();
50         });
51     }
52 }
53
54