@manta-eu/horns-client 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { SparqlReadError, type SparqlSelect } from "@manta-eu/horns";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
/**
|
|
2
|
+
import { useMantaClient } from "@manta-eu/react/providers";
|
|
3
|
+
type SparqlFetch = ReturnType<typeof useMantaClient>["sparqlFetch"];
|
|
4
|
+
/**
|
|
5
|
+
* Reads over `/sparql`, which is the endpoint that checks them.
|
|
6
|
+
*
|
|
7
|
+
* Not agents' `sparqlQuery` field: a query travelling as a GraphQL variable is not a request body, so no
|
|
8
|
+
* proxy's read-only check sees it, and a host would have to allow `/graphql` to let a Horn draw at all.
|
|
9
|
+
*
|
|
10
|
+
* Imperative rather than a hook: a `SparqlSelect` is called from inside query functions and from readers
|
|
11
|
+
* that issue one query after another, which a hook cannot serve.
|
|
12
|
+
*/
|
|
13
|
+
export declare function httpSparqlSelect(sparqlFetch: SparqlFetch): SparqlSelect;
|
|
14
|
+
/** The reader this app's pages use. The datastore rides on the fetch, from the route. */
|
|
5
15
|
export declare function useSparqlSelect(): SparqlSelect;
|
|
6
16
|
export { SparqlReadError };
|
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import { SparqlQueryDocument } from "@manta-eu/client";
|
|
2
|
+
import { SparqlReadError, sparqlResultsSchema } from "@manta-eu/horns";
|
|
4
3
|
import { useMantaClient } from "@manta-eu/react/providers";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
/** What a refused or failed read says, where the worker answered `{ error }` rather than results. */
|
|
5
|
+
async function refusalOf(response) {
|
|
6
|
+
const body = await response.json().catch(() => undefined);
|
|
7
|
+
if (typeof body === "object" && body !== null && "error" in body && typeof body.error === "string") {
|
|
8
|
+
return body.error;
|
|
9
|
+
}
|
|
10
|
+
return `The read failed (${response.status}).`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Reads over `/sparql`, which is the endpoint that checks them.
|
|
14
|
+
*
|
|
15
|
+
* Not agents' `sparqlQuery` field: a query travelling as a GraphQL variable is not a request body, so no
|
|
16
|
+
* proxy's read-only check sees it, and a host would have to allow `/graphql` to let a Horn draw at all.
|
|
17
|
+
*
|
|
18
|
+
* Imperative rather than a hook: a `SparqlSelect` is called from inside query functions and from readers
|
|
19
|
+
* that issue one query after another, which a hook cannot serve.
|
|
20
|
+
*/
|
|
21
|
+
export function httpSparqlSelect(sparqlFetch) {
|
|
8
22
|
return async (query, signal) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
23
|
+
const response = await sparqlFetch(query, signal ? { signal } : {});
|
|
24
|
+
if (!response.ok)
|
|
25
|
+
throw new SparqlReadError(await refusalOf(response));
|
|
26
|
+
const body = await response.json().catch(() => undefined);
|
|
27
|
+
const parsed = sparqlResultsSchema.safeParse(body);
|
|
28
|
+
if (!parsed.success)
|
|
29
|
+
throw new SparqlReadError("The query answered something other than results.");
|
|
30
|
+
return parsed.data;
|
|
15
31
|
};
|
|
16
32
|
}
|
|
17
|
-
/** The reader this app's pages use. The datastore rides on the
|
|
33
|
+
/** The reader this app's pages use. The datastore rides on the fetch, from the route. */
|
|
18
34
|
export function useSparqlSelect() {
|
|
19
|
-
const {
|
|
20
|
-
return useMemo(() =>
|
|
35
|
+
const { sparqlFetch } = useMantaClient();
|
|
36
|
+
return useMemo(() => httpSparqlSelect(sparqlFetch), [sparqlFetch]);
|
|
21
37
|
}
|
|
22
38
|
export { SparqlReadError };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@manta-eu/horns-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "The Manta Horns app as a library: routes, providers and the written-view runtime for a React host.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -60,15 +60,14 @@
|
|
|
60
60
|
"@manta-eu/lib": "0.1.5",
|
|
61
61
|
"@ulb-darmstadt/shacl-form": "^2.1.1-rc1",
|
|
62
62
|
"@xyflow/react": "^12.4.4",
|
|
63
|
-
"graphql-request": "^7.3.0",
|
|
64
63
|
"html-to-image": "1.11.13",
|
|
65
64
|
"ldkit": "2.9.0",
|
|
66
65
|
"lucide-react": "^0.525.0",
|
|
67
66
|
"ts-pattern": "^5.9.0",
|
|
68
67
|
"zod": "^4.5.4",
|
|
69
|
-
"@manta-eu/horns": "0.1.
|
|
70
|
-
"@manta-eu/horns-kit": "0.1.
|
|
71
|
-
"@manta-eu/horns-server": "0.1.
|
|
68
|
+
"@manta-eu/horns": "0.1.4",
|
|
69
|
+
"@manta-eu/horns-kit": "0.1.4",
|
|
70
|
+
"@manta-eu/horns-server": "0.1.4"
|
|
72
71
|
},
|
|
73
72
|
"devDependencies": {
|
|
74
73
|
"@manta-eu/ldo": "0.1.5",
|