vormiaqueryjs
Version:
Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application
48 lines (47 loc) • 1.14 kB
JavaScript
import { useQuery } from "@tanstack/react-query";
import { getGlobalVormiaClient } from "../client/createVormiaClient.mjs";
const useVrmQuery = (options) => {
const client = getGlobalVormiaClient();
const {
endpoint,
method = "GET",
params,
data,
headers,
transform,
enabled = true,
...queryOptions
} = options;
const queryKey = [endpoint, method, params, data];
const queryFn = async () => {
try {
const config = {
method,
url: endpoint,
params: method === "GET" ? params : void 0,
data: method !== "GET" ? data : void 0,
headers
};
const response = await client.request(config);
if (transform && typeof transform === "function") {
return {
...response,
data: transform(response.data)
};
}
return response;
} catch (error) {
throw error instanceof Error ? error : new Error("An unknown error occurred");
}
};
return useQuery({
queryKey,
queryFn,
enabled,
...queryOptions
});
};
export {
useVrmQuery
};
//# sourceMappingURL=useVrmQuery.mjs.map