41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
|
import type { UpEntry } from "upend";
|
||
|
import type { ListingResult } from "upend/types";
|
||
|
import { fetchEntry, query as queryFn } from "../lib/entity";
|
||
|
|
||
|
export function baseSearch(query: string) {
|
||
|
return queryFn(() => `(matches ? ? (contains "${query}"))`);
|
||
|
}
|
||
|
|
||
|
export async function getObjects(entries: UpEntry[]): Promise<string[]> {
|
||
|
const labelled = entries
|
||
|
.filter((e) => e.attribute == "LBL")
|
||
|
.map((e) => e.entity);
|
||
|
const aliased = entries
|
||
|
.filter((e) => e.attribute === "ALIAS")
|
||
|
.map(async (e) => {
|
||
|
const entry = await fetchEntry(e.entity);
|
||
|
return String(entry.value.c);
|
||
|
});
|
||
|
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;
|
||
|
}
|