From f0676e44d544bba91a2a25184aff51feca034c07 Mon Sep 17 00:00:00 2001 From: natxocc Date: Sat, 21 Mar 2026 21:13:55 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20src/plugins/sigpro-fetch.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/sigpro-fetch.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/plugins/sigpro-fetch.js diff --git a/src/plugins/sigpro-fetch.js b/src/plugins/sigpro-fetch.js new file mode 100644 index 0000000..9fdd9dd --- /dev/null +++ b/src/plugins/sigpro-fetch.js @@ -0,0 +1,28 @@ +/** + * SigPro Fetch Plugin + * Adds reactive data fetching to the $ namespace. + */ +$.use(($) => { + /** + * Performs a reactive fetch request. + * @param {string} url - The API endpoint. + * @param {Object} [options] - Fetch options (method, headers, etc). + * @returns {{ $data: Function, $loading: Function, $error: Function }} + */ + $.fetch = (url, options = {}) => { + const $data = $(null); + const $loading = $(true); + const $error = $(null); + + fetch(url, options) + .then(res => { + if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`); + return res.json(); + }) + .then(json => $data(json)) + .catch(err => $error(err.message)) + .finally(() => $loading(false)); + + return { $data, $loading, $error }; + }; +}); \ No newline at end of file