mtxuilib 0.1.31 → 0.1.33
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.
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MTM_COOKIE_TOKEN_NAME } from "@/consts";
|
|
2
|
+
import { useMtmapi } from "@/providers/MtmapiProvider";
|
|
3
|
+
export const useGqlFetcher = (query, options) => {
|
|
4
|
+
const backendUrl = useMtmapi((x) => x.backendUrl);
|
|
5
|
+
const url = `${backendUrl}/api.ent/gql`;
|
|
6
|
+
return async (variables) => {
|
|
7
|
+
let token = "";
|
|
8
|
+
if (typeof window == "undefined") {
|
|
9
|
+
const cookies = await import("next/headers").then((x) => x.cookies);
|
|
10
|
+
token = cookies().get(MTM_COOKIE_TOKEN_NAME)?.value || "";
|
|
11
|
+
}
|
|
12
|
+
const res = await fetch(url, {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: {
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
...(token && { Authorization: `Bearer ${token}` }),
|
|
17
|
+
},
|
|
18
|
+
body: JSON.stringify({
|
|
19
|
+
query,
|
|
20
|
+
variables,
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
const json = (await res.json());
|
|
24
|
+
if (json.errors) {
|
|
25
|
+
const { message } = json.errors[0] || {};
|
|
26
|
+
throw new Error(message || "Error…");
|
|
27
|
+
}
|
|
28
|
+
return json.data;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
2
|
+
import { cache } from "react";
|
|
3
|
+
const hasVariablesTypeGuard = (variables) => !!Object.keys(variables || {}).length;
|
|
4
|
+
const cachedServerPreFetch = cache(async (useQuery, stringifiedQueryOptions) => {
|
|
5
|
+
const queryOptions = stringifiedQueryOptions ? JSON.parse(stringifiedQueryOptions) : undefined;
|
|
6
|
+
const hasVariables = hasVariablesTypeGuard(queryOptions);
|
|
7
|
+
let variables;
|
|
8
|
+
if (hasVariables) {
|
|
9
|
+
variables = queryOptions.variables;
|
|
10
|
+
}
|
|
11
|
+
const queryClient = new QueryClient();
|
|
12
|
+
await queryClient.prefetchQuery({
|
|
13
|
+
queryKey: useQuery.getKey(variables),
|
|
14
|
+
queryFn: useQuery.fetcher(variables, {
|
|
15
|
+
next: queryOptions?.next,
|
|
16
|
+
cache: queryOptions?.cache,
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
return queryClient;
|
|
20
|
+
});
|
|
21
|
+
export const serverPreFetch = async (useQuery, queryOptions) => {
|
|
22
|
+
let stringifiedQueryOptions;
|
|
23
|
+
if (queryOptions)
|
|
24
|
+
stringifiedQueryOptions = JSON.stringify(queryOptions);
|
|
25
|
+
return cachedServerPreFetch(useQuery, stringifiedQueryOptions);
|
|
26
|
+
};
|
|
27
|
+
const cachedServerFetch = cache(async (useQuery, stringifiedQueryOptions) => {
|
|
28
|
+
const queryOptions = stringifiedQueryOptions ? JSON.parse(stringifiedQueryOptions) : undefined;
|
|
29
|
+
const hasVariables = hasVariablesTypeGuard(queryOptions);
|
|
30
|
+
let variables;
|
|
31
|
+
if (hasVariables) {
|
|
32
|
+
variables = queryOptions.variables;
|
|
33
|
+
}
|
|
34
|
+
const queryClient = new QueryClient();
|
|
35
|
+
const data = await queryClient.fetchQuery({
|
|
36
|
+
queryKey: useQuery.getKey(variables),
|
|
37
|
+
queryFn: useQuery.fetcher(variables, {
|
|
38
|
+
next: queryOptions?.next,
|
|
39
|
+
cache: queryOptions?.cache,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
return data;
|
|
43
|
+
});
|
|
44
|
+
export const serverFetch = async (useQuery, queryOptions) => {
|
|
45
|
+
let stringifiedQueryOptions;
|
|
46
|
+
if (queryOptions)
|
|
47
|
+
stringifiedQueryOptions = JSON.stringify(queryOptions);
|
|
48
|
+
return cachedServerFetch(useQuery, stringifiedQueryOptions);
|
|
49
|
+
};
|