UNPKG

simple-queries-react

Version:

Simple Queries React: Streamlined requests, powerful results. Transform your API calls into something simple, fast, and scalable with just a few lines of code.

1,226 lines (1,195 loc) 47.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var react = require('react'); let configSimpleQueries = { baseUrl: "", bearerToken: undefined, headers: undefined, APIs: undefined, }; const initSimpleQueries = (config) => { configSimpleQueries = Object.assign(Object.assign({}, configSimpleQueries), config); }; const getConfig = () => { return configSimpleQueries; }; const setBearerToken = (token, apiName = undefined) => { var _a, _b; if (token != undefined) { if (apiName) { const index = (_b = (_a = configSimpleQueries === null || configSimpleQueries === void 0 ? void 0 : configSimpleQueries.APIs) === null || _a === void 0 ? void 0 : _a.findIndex((item) => (item === null || item === void 0 ? void 0 : item.name) === apiName)) !== null && _b !== void 0 ? _b : -1; if (index > -1) { configSimpleQueries.APIs[index].bearerToken = token; } return; } configSimpleQueries = Object.assign(Object.assign({}, configSimpleQueries), { bearerToken: token }); } }; const cleanBearerToken = (apiName = undefined) => { var _a, _b; if (apiName) { const index = (_b = (_a = configSimpleQueries === null || configSimpleQueries === void 0 ? void 0 : configSimpleQueries.APIs) === null || _a === void 0 ? void 0 : _a.findIndex((item) => (item === null || item === void 0 ? void 0 : item.name) === apiName)) !== null && _b !== void 0 ? _b : -1; if (index > -1) { configSimpleQueries.APIs[index].bearerToken = undefined; } return; } configSimpleQueries = Object.assign(Object.assign({}, configSimpleQueries), { bearerToken: undefined }); }; const setHeaders = (headers, apiName = undefined) => { var _a, _b; if (headers) { if (apiName) { const index = (_b = (_a = configSimpleQueries === null || configSimpleQueries === void 0 ? void 0 : configSimpleQueries.APIs) === null || _a === void 0 ? void 0 : _a.findIndex((item) => (item === null || item === void 0 ? void 0 : item.name) === apiName)) !== null && _b !== void 0 ? _b : -1; if (index > -1) { configSimpleQueries.APIs[index].headers = headers; } return; } configSimpleQueries = Object.assign(Object.assign({}, configSimpleQueries), { headers: headers }); } }; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const buildURLFromMap = (pathMap) => { return Object.entries(pathMap) .map(([key, value]) => value !== null && value !== undefined ? `${key}/${value}` : key) .join("/"); }; const capitalizeFirstLetter = (str) => { return str.charAt(0).toUpperCase() + str.slice(1); }; const endsWithSlash = (str) => { return str.charAt(str.length - 1) === "/"; }; const flattenObject = (input, parentKey = "") => { return Object.keys(input).reduce((acc, key) => { const newKey = parentKey ? `${parentKey}${capitalizeFirstLetter(key)}` : key; if (typeof input[key] === "object" && input[key] !== null) { return Object.assign(Object.assign({}, acc), flattenObject(input[key], newKey)); } else { return Object.assign(Object.assign({}, acc), { [newKey]: input[key] }); } }, {}); }; const isURL = (str) => { const urlRegex = /^(?:http|https):\/\/\S+/i; return urlRegex.test(str); }; const removeNullValues = (obj) => { const newObj = {}; for (let key in obj) { if (obj[key] !== null && typeof obj[key] === "object") { newObj[key] = removeNullValues(obj[key]); } else if (obj[key] !== null) { newObj[key] = obj[key]; } } return newObj; }; const removeUndefinedValues = (obj) => { const newObj = {}; for (let key in obj) { if (obj[key] !== undefined && typeof obj[key] !== "undefined") { if (typeof obj[key] === "object" && obj[key] !== null) { newObj[key] = removeUndefinedValues(obj[key]); } else { newObj[key] = obj[key]; } } } return newObj; }; const fetchRequest = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, methods = "GET", apiName = undefined, onSuccess, bodyURLSearchParams } = {}) => __awaiter(void 0, void 0, void 0, function* () { var _a; const config = getConfig(); const apiConfig = (_a = config === null || config === void 0 ? void 0 : config.APIs) === null || _a === void 0 ? void 0 : _a.find((item) => (item === null || item === void 0 ? void 0 : item.name) === apiName); let configHeaders = undefined; if (apiConfig) { configHeaders = Object.assign(Object.assign({}, apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.headers), headers); } else { configHeaders = Object.assign(Object.assign({}, config === null || config === void 0 ? void 0 : config.headers), headers); } if ((config === null || config === void 0 ? void 0 : config.bearerToken) || (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken)) { const enableDefaultToken = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.enableDefaultToken) !== undefined ? apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.enableDefaultToken : true; if ((apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken) !== undefined) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken}` }); } else if (apiConfig && enableDefaultToken) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${config.bearerToken}` }); } else if (config === null || config === void 0 ? void 0 : config.bearerToken) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${config.bearerToken}` }); } } try { let fullPath = endpoint ? `${endpoint}` : ""; if (pathRest) { fullPath = `${fullPath && !endsWithSlash(fullPath) ? `${fullPath}/` : ""}${buildURLFromMap(flattenObject(pathRest))}`; } if (url) { fullPath = `${url}${fullPath ? `${endsWithSlash(url) ? "" : "/"}${fullPath}` : ""}`; } else if (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl) { fullPath = `${apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl}${fullPath ? `${endsWithSlash(apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl) ? "" : "/"}${fullPath}` : ""}`; } else if (config === null || config === void 0 ? void 0 : config.baseUrl) { fullPath = `${config === null || config === void 0 ? void 0 : config.baseUrl}${fullPath ? `${endsWithSlash(config === null || config === void 0 ? void 0 : config.baseUrl) ? "" : "/"}${fullPath}` : ""}`; } if (params) { const transformParams = removeNullValues(removeUndefinedValues(flattenObject(params))); const queryParams = new URLSearchParams(transformParams).toString(); fullPath = queryParams ? `${fullPath}?${queryParams}` : fullPath; } const response = yield fetch(fullPath, { method: methods ? methods : "GET", headers: Object.assign({ "Content-Type": "application/json" }, configHeaders), body: bodyURLSearchParams ? new URLSearchParams(bodyURLSearchParams) : JSON.stringify(body), }); if (!response.ok) { const resp = yield response.json(); throw resp; } const contentType = response.headers.get("content-type"); if (contentType && contentType.includes("application/json")) { const resp = yield response.json(); onSuccess && onSuccess(resp); return resp; } else { return yield response.text(); } } catch (err) { if (errorFn) { errorFn(err); } throw err; } }); const deleteData = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchRequest({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, methods: "DELETE", onSuccess, }); }); const getData = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchRequest({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, methods: "GET", onSuccess, }); }); const patchData = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchRequest({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, methods: "PATCH", onSuccess, }); }); const postData = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, bodyURLSearchParams } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchRequest({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, methods: "POST", onSuccess, bodyURLSearchParams, }); }); const putData = ({ url = "", endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, bodyURLSearchParams } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchRequest({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, methods: "PUT", onSuccess, bodyURLSearchParams }); }); const fetchDownloadRequest = ({ url = "", endpoint, errorFn, pathRest, params, headers, methods = "GET", apiName = undefined, fileName = ["download", "pdf"], download = true, onSuccess, } = {}) => __awaiter(void 0, void 0, void 0, function* () { var _a; const config = getConfig(); const apiConfig = (_a = config === null || config === void 0 ? void 0 : config.APIs) === null || _a === void 0 ? void 0 : _a.find((item) => (item === null || item === void 0 ? void 0 : item.name) === apiName); let configHeaders = undefined; if (apiConfig) { configHeaders = Object.assign(Object.assign({}, apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.headers), headers); } else { configHeaders = Object.assign(Object.assign({}, config === null || config === void 0 ? void 0 : config.headers), headers); } if ((config === null || config === void 0 ? void 0 : config.bearerToken) || (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken)) { const enableDefaultToken = (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.enableDefaultToken) !== undefined ? apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.enableDefaultToken : true; if ((apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken) !== undefined) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.bearerToken}` }); } else if (apiConfig && enableDefaultToken) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${config.bearerToken}` }); } else if (config === null || config === void 0 ? void 0 : config.bearerToken) { configHeaders = Object.assign(Object.assign({}, configHeaders), { Authorization: `Bearer ${config.bearerToken}` }); } } try { let fullPath = endpoint ? `${endpoint}` : ""; if (pathRest) { fullPath = `${fullPath && !endsWithSlash(fullPath) ? `${fullPath}/` : ""}${buildURLFromMap(flattenObject(pathRest))}`; } if (url) { fullPath = `${url}${fullPath ? `${endsWithSlash(url) ? "" : "/"}${fullPath}` : ""}`; } else if (apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl) { fullPath = `${apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl}${fullPath ? `${endsWithSlash(apiConfig === null || apiConfig === void 0 ? void 0 : apiConfig.baseUrl) ? "" : "/"}${fullPath}` : ""}`; } else if (config === null || config === void 0 ? void 0 : config.baseUrl) { fullPath = `${config === null || config === void 0 ? void 0 : config.baseUrl}${fullPath ? `${endsWithSlash(config === null || config === void 0 ? void 0 : config.baseUrl) ? "" : "/"}${fullPath}` : ""}`; } if (params) { const transformParams = removeNullValues(removeUndefinedValues(flattenObject(params))); const queryParams = new URLSearchParams(transformParams).toString(); fullPath = queryParams ? `${fullPath}?${queryParams}` : fullPath; } const response = yield fetch(fullPath, { method: methods ? methods : "GET", headers: Object.assign({}, configHeaders), }); if (!response.ok) { throw new Error(`Failed to fetch data: ${response.statusText}`); } const contentDispositionHeader = response.headers.get("Content-Disposition"); const match = contentDispositionHeader && contentDispositionHeader.match(/filename="(.+)"/); let fileNameDowanload = fileName.join("."); if (match) { fileNameDowanload = match[1]; } const fileExtension = fileNameDowanload.split(".").pop(); const blob = yield response.blob(); const urlDownload = window.URL.createObjectURL(blob); if (!download) { return urlDownload; } const a = document.createElement("a"); a.href = urlDownload; a.download = fileNameDowanload; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(urlDownload); onSuccess && onSuccess(response); return fileExtension; } catch (err) { if (errorFn) { errorFn(err); } throw err === null || err === void 0 ? void 0 : err.response; } }); const downloadData = ({ url = "", endpoint, errorFn, pathRest, params, headers, apiName, fileName, download, methods = "GET", onSuccess, } = {}) => __awaiter(void 0, void 0, void 0, function* () { return fetchDownloadRequest({ url, endpoint, errorFn, pathRest, params, headers, apiName, fileName, download, methods: methods, onSuccess, }); }); function useDelete(props = {}) { let propsDelete = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, onSuccess: undefined, }; const [response, setResponse] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [msgErrors, setMsgErrors] = react.useState(undefined); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, onSuccess } = props; propsDelete = { endpoint, errorFn, headers, url, apiName, onSuccess }; } else if (typeof props === "string") { if (isURL(props)) { propsDelete.url = props; } else { propsDelete.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsDelete === null || propsDelete === void 0 ? void 0 : propsDelete.errorFn) && propsDelete.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsDelete.headers = headers; } }; const request = ({ params, pathRest, body, }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield deleteData({ endpoint: propsDelete === null || propsDelete === void 0 ? void 0 : propsDelete.endpoint, params, pathRest, headers: propsDelete.headers, errorFn: handleSetErrors, url: propsDelete.url, body: body, apiName: propsDelete === null || propsDelete === void 0 ? void 0 : propsDelete.apiName, onSuccess: propsDelete === null || propsDelete === void 0 ? void 0 : propsDelete.onSuccess, }) .then((res) => { setSuccess(true); setError(false); setResponse(res); }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, body: dataBody, } = data; if (dataParams != undefined || dataPathRest != undefined || dataBody != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, body: (_c = dataBody) !== null && _c !== void 0 ? _c : undefined, }; } else { options.body = (_d = data) !== null && _d !== void 0 ? _d : undefined; } } else if (typeof data === "string" || typeof data === "number") { propsDelete.endpoint = propsDelete.endpoint ? `${propsDelete.endpoint}/${data}` : `${data}`; } request({ params: (_e = options === null || options === void 0 ? void 0 : options.params) !== null && _e !== void 0 ? _e : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, body: options === null || options === void 0 ? void 0 : options.body, }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, success, error, msgErrors }; } function useDownload(props = {}) { let propsDownload = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, defaultName: undefined, download: undefined, extension: undefined, onSuccess: undefined, }; const [response, setResponse] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [msgErrors, setMsgErrors] = react.useState(undefined); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, defaultName, download, extension, onSuccess, } = props; propsDownload = { endpoint, errorFn, headers, url, apiName, defaultName, download, extension, onSuccess, }; } else if (typeof props === "string") { if (isURL(props)) { propsDownload.url = props; } else { propsDownload.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.errorFn) && propsDownload.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsDownload.headers = headers; } }; const request = ({ params, pathRest, download, fileName, }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield downloadData({ endpoint: propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.endpoint, params, pathRest, headers: propsDownload.headers, errorFn: handleSetErrors, url: propsDownload.url, apiName: propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.apiName, fileName: fileName, download, onSuccess: propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.onSuccess, }) .then((res) => { setSuccess(true); setError(false); if (typeof res == "string") { setResponse(res); } }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, download: dataDownload, fileName: dataFileName, extension: dataExtension, } = data; if (dataParams != undefined || dataPathRest != undefined || dataDownload != undefined || dataFileName != undefined || dataExtension != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, download: dataDownload, fileName: dataFileName, extension: dataExtension, }; } else { options.params = (_c = data) !== null && _c !== void 0 ? _c : undefined; } } request({ params: (_d = options === null || options === void 0 ? void 0 : options.params) !== null && _d !== void 0 ? _d : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, download: options === null || options === void 0 ? void 0 : options.download, fileName: [ (options === null || options === void 0 ? void 0 : options.fileName) ? options === null || options === void 0 ? void 0 : options.fileName : (propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.defaultName) ? propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.defaultName : "download", (options === null || options === void 0 ? void 0 : options.extension) ? typeof options.extension == "string" ? options.extension : (_e = options.extension) === null || _e === void 0 ? void 0 : _e.customExtension : propsDownload.extension ? typeof propsDownload.extension == "string" ? propsDownload.extension : propsDownload === null || propsDownload === void 0 ? void 0 : propsDownload.extension.customExtension : "pdf", ], }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, success, error, msgErrors }; } function useGet(props = {}) { let propsGet = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, onSuccess: undefined, }; const [response, setResponse] = react.useState(undefined); const [msgErrors, setMsgErrors] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, onSuccess } = props; propsGet = { endpoint, errorFn, headers, url, apiName, onSuccess }; } else if (typeof props === "string") { if (isURL(props)) { propsGet.url = props; } else { propsGet.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsGet === null || propsGet === void 0 ? void 0 : propsGet.errorFn) && propsGet.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsGet.headers = headers; } }; const request = ({ params, pathRest, body, }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield getData({ endpoint: propsGet === null || propsGet === void 0 ? void 0 : propsGet.endpoint, params, pathRest, headers: propsGet.headers, errorFn: handleSetErrors, url: propsGet.url, body, apiName: propsGet === null || propsGet === void 0 ? void 0 : propsGet.apiName, onSuccess: propsGet === null || propsGet === void 0 ? void 0 : propsGet.onSuccess, }) .then((res) => { setSuccess(true); setError(false); setResponse(res); }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, body: dataBody, } = data; if (dataParams != undefined || dataPathRest != undefined || dataBody != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, body: (_c = dataBody) !== null && _c !== void 0 ? _c : undefined, }; } else { options.params = (_d = data) !== null && _d !== void 0 ? _d : undefined; } } else if (typeof data === "string" || typeof data === "number") { propsGet.endpoint = propsGet.endpoint ? `${propsGet.endpoint}/${data}` : `${data}`; } request({ params: (_e = options === null || options === void 0 ? void 0 : options.params) !== null && _e !== void 0 ? _e : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, body: options === null || options === void 0 ? void 0 : options.body, }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, success, error, msgErrors, }; } function usePatch(props = {}) { let propsPatch = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, onSuccess: undefined, }; const [response, setResponse] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [msgErrors, setMsgErrors] = react.useState(undefined); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, onSuccess } = props; propsPatch = { endpoint, errorFn, headers, url, apiName, onSuccess }; } else if (typeof props === "string") { if (isURL(props)) { propsPatch.url = props; } else { propsPatch.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsPatch === null || propsPatch === void 0 ? void 0 : propsPatch.errorFn) && propsPatch.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsPatch.headers = headers; } }; const request = ({ params, pathRest, body, }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield patchData({ endpoint: propsPatch === null || propsPatch === void 0 ? void 0 : propsPatch.endpoint, params, pathRest, headers: propsPatch.headers, errorFn: handleSetErrors, url: propsPatch.url, body: body, apiName: propsPatch === null || propsPatch === void 0 ? void 0 : propsPatch.apiName, onSuccess: propsPatch === null || propsPatch === void 0 ? void 0 : propsPatch.onSuccess, }) .then((res) => { setSuccess(true); setError(false); setResponse(res); }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, body: dataBody, } = data; if (dataParams != undefined || dataPathRest != undefined || dataBody != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, body: (_c = dataBody) !== null && _c !== void 0 ? _c : undefined, }; } else { options.body = (_d = data) !== null && _d !== void 0 ? _d : undefined; } } else if (typeof data === "string" || typeof data === "number") { propsPatch.endpoint = propsPatch.endpoint ? `${propsPatch.endpoint}/${data}` : `${data}`; } request({ params: (_e = options === null || options === void 0 ? void 0 : options.params) !== null && _e !== void 0 ? _e : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, body: options === null || options === void 0 ? void 0 : options.body, }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, msgErrors, success, error, }; } function usePost(props = {}) { let propsPost = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, onSuccess: undefined, bodyURLSearchParams: undefined, }; const [response, setResponse] = react.useState(undefined); const [msgErrors, setMsgErrors] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, onSuccess } = props; propsPost = { endpoint, errorFn, headers, url, apiName, onSuccess }; } else if (typeof props === "string") { if (isURL(props)) { propsPost.url = props; } else { propsPost.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsPost === null || propsPost === void 0 ? void 0 : propsPost.errorFn) && propsPost.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsPost.headers = headers; } }; const request = ({ params, pathRest, body, bodyURLSearchParams }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield postData({ endpoint: propsPost === null || propsPost === void 0 ? void 0 : propsPost.endpoint, params, pathRest, headers: propsPost.headers, errorFn: handleSetErrors, url: propsPost.url, body: body, apiName: propsPost === null || propsPost === void 0 ? void 0 : propsPost.apiName, onSuccess: propsPost === null || propsPost === void 0 ? void 0 : propsPost.onSuccess, bodyURLSearchParams: bodyURLSearchParams }) .then((res) => { setSuccess(true); setError(false); setResponse(res); }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e, _f; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, body: dataBody, bodyURLSearchParams: dataBodyURLSearchParams, } = data; if (dataParams != undefined || dataPathRest != undefined || dataBody != undefined || dataBodyURLSearchParams != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, body: (_c = dataBody) !== null && _c !== void 0 ? _c : undefined, bodyURLSearchParams: (_d = dataBodyURLSearchParams) !== null && _d !== void 0 ? _d : undefined, }; } else { options.body = (_e = data) !== null && _e !== void 0 ? _e : undefined; } } else if (typeof data === "string" || typeof data === "number") { propsPost.endpoint = propsPost.endpoint ? `${propsPost.endpoint}/${data}` : `${data}`; } request({ params: (_f = options === null || options === void 0 ? void 0 : options.params) !== null && _f !== void 0 ? _f : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, body: options === null || options === void 0 ? void 0 : options.body, bodyURLSearchParams: options === null || options === void 0 ? void 0 : options.bodyURLSearchParams, }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, success, error, msgErrors }; } function usePut(props = {}) { let propsPut = { endpoint: undefined, errorFn: undefined, headers: undefined, url: undefined, apiName: undefined, onSuccess: undefined, bodyURLSearchParams: undefined, }; const [response, setResponse] = react.useState(undefined); const [msgErrors, setMsgErrors] = react.useState(undefined); const [loading, setLoading] = react.useState(false); const [success, setSuccess] = react.useState(false); const [error, setError] = react.useState(false); if (typeof props === "object" && props !== null && !Array.isArray(props)) { const { endpoint, errorFn, headers, url, apiName, onSuccess } = props; propsPut = { endpoint, errorFn, headers, url, apiName, onSuccess }; } else if (typeof props === "string") { if (isURL(props)) { propsPut.url = props; } else { propsPut.endpoint = props; } } const getResponse = () => { return response; }; const clearResponse = () => { setResponse(undefined); }; const isLoading = () => { return loading; }; const cancelLoading = () => { setLoading(false); }; const getErrors = () => { return msgErrors; }; const handleSetErrors = (data) => { setMsgErrors(data); (propsPut === null || propsPut === void 0 ? void 0 : propsPut.errorFn) && propsPut.errorFn(data); }; const clearErrors = () => { setError(false); setMsgErrors(undefined); }; const setHeaders = (headers) => { if (headers) { propsPut.headers = headers; } }; const request = ({ params, pathRest, body, bodyURLSearchParams, }) => __awaiter(this, void 0, void 0, function* () { setLoading(true); setMsgErrors(undefined); setError(false); setSuccess(false); yield putData({ endpoint: propsPut === null || propsPut === void 0 ? void 0 : propsPut.endpoint, params, pathRest, headers: propsPut.headers, errorFn: handleSetErrors, url: propsPut.url, body: body, apiName: propsPut === null || propsPut === void 0 ? void 0 : propsPut.apiName, onSuccess: propsPut === null || propsPut === void 0 ? void 0 : propsPut.onSuccess, bodyURLSearchParams: bodyURLSearchParams, }) .then((res) => { setSuccess(true); setError(false); setResponse(res); }) .catch(() => { setSuccess(false); setError(true); setResponse(undefined); }) .finally(() => { setLoading(false); }); }); const send = (data = {}) => { var _a, _b, _c, _d, _e, _f; let options = {}; if (typeof data === "object" && props !== null && !Array.isArray(props)) { const { params: dataParams, pathRest: dataPathRest, body: dataBody, bodyURLSearchParams: dataBodyURLSearchParams, } = data; if (dataParams != undefined || dataPathRest != undefined || dataBody != undefined || dataBodyURLSearchParams != undefined) { options = { params: (_a = dataParams) !== null && _a !== void 0 ? _a : undefined, pathRest: (_b = dataPathRest) !== null && _b !== void 0 ? _b : undefined, body: (_c = dataBody) !== null && _c !== void 0 ? _c : undefined, bodyURLSearchParams: (_d = dataBodyURLSearchParams) !== null && _d !== void 0 ? _d : undefined, }; } else { options.body = (_e = data) !== null && _e !== void 0 ? _e : undefined; } } else if (typeof data === "string" || typeof data === "number") { propsPut.endpoint = propsPut.endpoint ? `${propsPut.endpoint}/${data}` : `${data}`; } request({ params: (_f = options === null || options === void 0 ? void 0 : options.params) !== null && _f !== void 0 ? _f : {}, pathRest: options === null || options === void 0 ? void 0 : options.pathRest, body: options === null || options === void 0 ? void 0 : options.body, bodyURLSearchParams: options === null || options === void 0 ? void 0 : options.bodyURLSearchParams, }); }; return { getResponse, send, clearResponse, isLoading, cancelLoading, getErrors, clearErrors, setHeaders, success, error, msgErrors, }; } exports.cleanBearerToken = cleanBearerToken; exports.deleteData = deleteData; exports.downloadData = downloadData; exports.getData = getData; exports.initSimpleQueries = initSimpleQueries; exports.patchData = patchData; exports.postData = postData; exports.putData = putData; exports.setBearerToken = setBearerToken; exports.setHeaders = setHeaders; exports.useDelete = useDelete; exports.useDownload = useDownload; exports.useGet = useGet; exports.usePatch = usePatch; exports.usePost = usePost; exports.usePut = usePut;