MagicMirror/js/fetch.js
Veeck d5e855dd6d
Use fetch instead of XMLHttpRequest in weatherprovider (#2935)
small update to the fetchData method to use the fetch helper instead of
the old XCMLHttpRequest.
Also fixes some typos :-)

Co-authored-by: veeck <michael@veeck.de>
2022-10-06 19:44:16 +02:00

21 lines
448 B
JavaScript

/**
* fetch
*
* @param {string} url to be fetched
* @param {object} options object e.g. for headers
* @class
*/
async function fetch(url, options = {}) {
const nodeVersion = process.version.match(/^v(\d+)\.*/)[1];
if (nodeVersion >= 18) {
// node version >= 18
return global.fetch(url, options);
} else {
// node version < 18
const nodefetch = require("node-fetch");
return nodefetch(url, options);
}
}
module.exports = fetch;