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