export type CreateApiSparqlArgs = {body: {query: string, infer?: boolean | undefined}}; export type CreateApiSparqlOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * Auckland Museum SPARQL endpoint * * POST /sparql */ export const createApiSparql = async (args: CreateApiSparqlArgs, options: CreateApiSparqlOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") const url = `${baseUrl.replace(/\/+$/, '')}/sparql${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };