-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory_api.ts
86 lines (81 loc) · 2.66 KB
/
directory_api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { directoryUrl, Document, DOMParser, pathIdentifier } from "./deps.ts";
export interface DirectoryRecordInterface {
display_name: string;
given_name: string;
family_name: string;
title: string;
directory_person_type: string;
org: string;
division: string;
bio: string;
office: string;
email: string;
}
export class DirectoryRecord implements DirectoryRecordInterface {
display_name: string = "";
given_name: string = "";
family_name: string = "";
title: string = "";
directory_person_type: string = "";
org: string = "";
division: string = "";
bio: string = "";
office: string = "";
email: string = "";
}
function getElementValue(document: Document, selector: string): string {
const val = document.querySelector(selector);
if (val === null) {
return "";
}
return val.innerText;
}
export async function directoryLookup(
imss_uid: string,
): Promise<DirectoryRecord | undefined> {
const uri = `${directoryUrl}/personnel/${imss_uid}?off_campus`;
const resp = await fetch(uri, {
headers: { "content-type": "text/html" },
method: "GET",
});
if (resp.ok) {
const src = await resp.text();
const document = new DOMParser().parseFromString(src, "text/html");
let record = new DirectoryRecord();
record.display_name = getElementValue(document, ".fn");
record.given_name = getElementValue(document, ".given-name");
record.family_name = getElementValue(document, ".family-name");
record.title = getElementValue(document, ".title");
record.directory_person_type = getElementValue(document, ".person_type");
record.org = getElementValue(document, ".organization-name");
record.division = getElementValue(document, ".organization-unit");
record.bio = getElementValue(document, ".bio");
record.office = getElementValue(document, ".office");
record.email = getElementValue(document, ".email");
return record;
}
if (resp.body !== null) {
resp.body.cancel();
}
return undefined;
}
// handleDirectoryLookup proxies to the Caltech Library public directory.
// This resolves the CORS issues around accessing content there.
export async function handleDirectoryLookup(
req: Request,
options: { debug: boolean; htdocs: string },
): Promise<Response> {
const url = new URL(req.url);
const directory_id = pathIdentifier(req.url);
const record = await directoryLookup(directory_id);
if (record === undefined) {
return new Response(`{"error": "${directory_id} not found"}`, {
status: 404,
headers: { "content-type": "application/json" },
});
}
return new Response(JSON.stringify(record), {
status: 200,
headers: { "content-type": "application/json" },
});
}