-
Notifications
You must be signed in to change notification settings - Fork 0
/
terms.service.ts
92 lines (79 loc) · 2.46 KB
/
terms.service.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
87
88
89
90
91
92
const axios = import("axios").then((m) => m.default);
import memoize from "lodash/memoize";
import type {
QlitCollection,
QlitCollectionRaw,
QlitName,
QlitTerm,
QlitTermRaw,
} from "./qlit.types";
/**
* Location of the thesaurus backend service.
*
* See https://github.com/gu-gridh/queerlit-terms for API and more information.
*/
const QLIT_BASE =
import.meta.env.VITE_QLIT_BASE || "https://queerlit.dh.gu.se/qlit/v1/api/";
/** Get a single term. */
async function qlitGet<T = any>(
endpoint: string,
params?: Record<string, any>,
): Promise<T> {
const response = await (await axios).get(QLIT_BASE + endpoint, { params });
return response.data;
}
/** Get multiple terms and sort alphabetically. */
async function qlitList(endpoint: string, params?: Record<string, any>) {
const data: QlitTermRaw[] = await qlitGet(endpoint, params);
const terms = data.map(processTerm);
return terms;
}
export const getTerm = memoize(async (name: QlitName) => {
const data = await qlitGet<QlitTermRaw>("term/" + name);
return processTerm(data);
});
export const getParents = memoize(async (narrower: QlitName) =>
qlitList("broader", { narrower }),
);
export const getChildren = memoize(async (broader: QlitName) =>
qlitList("narrower", { broader }),
);
export const getRelated = memoize(async (other: QlitName) =>
qlitList("related", { other }),
);
export const getRoots = memoize(async () => qlitList("roots"));
export async function searchTerms(s: string) {
return qlitList("search", { s });
}
export const getCollections = memoize(async (): Promise<QlitCollection[]> => {
const collections = await qlitGet<QlitCollectionRaw[]>("collections");
return collections.map((collection) => ({
...collection,
id: collection.uri,
label: collection.prefLabel
.replace("Tema: ", "")
.replace(/ \(HBTQI\)/i, ""),
}));
});
export const getCollection = memoize(async (name: QlitName) =>
qlitList("collections/" + name),
);
export const getLabels = memoize(async () =>
qlitGet<Readonly<Record<QlitName, string>>>("labels"),
);
function processTerm(term: QlitTermRaw): QlitTerm {
return {
id: term.uri,
label: term.prefLabel,
scheme: "https://queerlit.dh.gu.se/qlit/v1",
name: term.name,
altLabels: term.altLabels,
hiddenLabels: term.hiddenLabels,
scopeNote: term.scopeNote,
broader: term.broader,
narrower: term.narrower,
related: term.related,
exactMatch: term.exactMatch,
closeMatch: term.closeMatch,
};
}