2022-01-27 20:56:00 +01:00
|
|
|
import type { UpEntry } from "upend";
|
|
|
|
import type { ListingResult } from "upend/types";
|
2022-01-27 21:28:42 +01:00
|
|
|
import { fetchEntry, query as queryFn, queryOnce } from "../lib/entity";
|
2022-01-27 20:56:00 +01:00
|
|
|
|
|
|
|
export function baseSearch(query: string) {
|
|
|
|
return queryFn(() => `(matches ? ? (contains "${query}"))`);
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:28:42 +01:00
|
|
|
export function baseSearchOnce(query: string) {
|
|
|
|
return queryOnce(`(matches ? ? (contains "${query}"))`);
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:26:47 +01:00
|
|
|
export async function getObjects(
|
|
|
|
entries: UpEntry[]
|
|
|
|
): Promise<[string, string][]> {
|
2022-01-27 20:56:00 +01:00
|
|
|
const labelled = entries
|
|
|
|
.filter((e) => e.attribute == "LBL")
|
2022-01-27 21:26:47 +01:00
|
|
|
.map((e) => [e.entity, String(e.value.c)] as [string, string]);
|
2022-01-27 20:56:00 +01:00
|
|
|
const aliased = entries
|
|
|
|
.filter((e) => e.attribute === "ALIAS")
|
2022-01-27 21:26:47 +01:00
|
|
|
.map(async (aliasEntry) => {
|
|
|
|
const entry = await fetchEntry(aliasEntry.entity);
|
|
|
|
return [String(entry.value.c), String(aliasEntry.value.c)] as [
|
|
|
|
string,
|
|
|
|
string
|
|
|
|
];
|
2022-01-27 20:56:00 +01:00
|
|
|
});
|
|
|
|
return labelled.concat(await Promise.all(aliased));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createLabelled(label: string) {
|
|
|
|
const response = await fetch(`/api/obj`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
attribute: "LBL",
|
|
|
|
value: {
|
|
|
|
t: "Value",
|
|
|
|
c: label,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to create object: ${await response.text()}`);
|
|
|
|
}
|
|
|
|
const result = (await response.json()) as ListingResult;
|
|
|
|
const address = Object.values(result)[0].entity;
|
|
|
|
return address;
|
|
|
|
}
|