vormiaqueryjs
Version:
Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application
138 lines (137 loc) • 3.83 kB
JavaScript
import { useSignal, useTask$ } from "@builder.io/qwik";
import { getGlobalVormiaClient, VormiaError } from "../core/VormiaClient.mjs";
function useVormiaQuery(options) {
const client = getGlobalVormiaClient();
const data = useSignal(null);
const error = useSignal(null);
const isLoading = useSignal(false);
const isError = useSignal(false);
const isSuccess = useSignal(false);
const fetchData = async (opts = {}) => {
var _a;
const mergedOptions = { ...options, ...opts };
const {
endpoint,
method = "GET",
params,
data: bodyData,
headers,
transform,
onSuccess,
onError,
setEncrypt = false
} = mergedOptions;
isLoading.value = true;
isError.value = false;
isSuccess.value = false;
try {
let config = {
method,
url: endpoint,
params: method === "GET" ? params : void 0,
data: method !== "GET" ? bodyData || params : void 0,
headers
};
if (setEncrypt && config.data) {
const { encryptWithPublicKey } = await import("../client/utils/encryption.mjs");
config.data = encryptWithPublicKey(config.data);
}
const response = await client.request(config);
let result = response.data;
if (setEncrypt && result) {
const { decryptWithPrivateKey } = await import("../client/utils/encryption.mjs");
result = decryptWithPrivateKey(result);
}
if (transform && (result == null ? void 0 : result.response)) {
result.response = transform(result.response);
}
data.value = result.response;
isSuccess.value = true;
if (onSuccess) {
onSuccess(result);
}
return result;
} catch (err) {
const vormiaError = err instanceof VormiaError ? err : new VormiaError(
(err == null ? void 0 : err.message) || "An unknown error occurred",
(_a = err == null ? void 0 : err.response) == null ? void 0 : _a.status
);
error.value = vormiaError;
isError.value = true;
if (onError) {
onError(vormiaError);
}
throw vormiaError;
} finally {
isLoading.value = false;
}
};
useTask$(async () => {
if (options.enabled !== false) {
await fetchData();
}
});
return {
data,
error,
isLoading,
isError,
isSuccess,
fetch: fetchData,
refetch: fetchData
};
}
function useVormiaMutation(options = {}) {
const client = getGlobalVormiaClient();
const data = useSignal(null);
const error = useSignal(null);
const isLoading = useSignal(false);
const isError = useSignal(false);
const isSuccess = useSignal(false);
const mutate = async (endpoint, values, method = "POST", config = {}) => {
var _a;
isLoading.value = true;
isError.value = false;
isSuccess.value = false;
try {
const response = await client.request({
method,
url: endpoint,
data: values,
...config
});
data.value = response.data;
isSuccess.value = true;
if (options.onSuccess) {
options.onSuccess(response.data);
}
return response.data;
} catch (err) {
const vormiaError = err instanceof VormiaError ? err : new VormiaError(
(err == null ? void 0 : err.message) || "An unknown error occurred",
(_a = err == null ? void 0 : err.response) == null ? void 0 : _a.status
);
error.value = vormiaError;
isError.value = true;
if (options.onError) {
options.onError(vormiaError);
}
throw vormiaError;
} finally {
isLoading.value = false;
}
};
return {
data,
error,
isLoading,
isError,
isSuccess,
mutate
};
}
export {
useVormiaMutation,
useVormiaQuery
};
//# sourceMappingURL=qwik.mjs.map