Skip to content

Commit

Permalink
Fetch 3.x (#491)
Browse files Browse the repository at this point in the history
* Update requests.js

import

* Update package.json

Changed version to 3.3.2

* Update src/Private/requests.js

* feat(same node-fetch 3.x workaround everywhere)

* styles(prettier)

---------

Co-authored-by: Kath <[email protected]>
  • Loading branch information
ChiefChippy2 and Kathund authored Feb 21, 2024
1 parent 06086e4 commit 0edfbab
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
86 changes: 81 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"author": "StavZ",
"dependencies": {
"node-fetch": "^2.6.1",
"node-fetch": "^3.3.2",
"object-path": "^0.11.8",
"prismarine-nbt": "^2.5.0",
"rss-parser": "^3.13.0"
Expand Down
6 changes: 3 additions & 3 deletions src/Private/requests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable require-jsdoc */
const requireFetch = !globalThis.fetch;
const externalFetch = require('node-fetch');
const externalFetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const fetch = requireFetch ? externalFetch : globalThis.fetch;
const BASE_URL = 'https://api.hypixel.net/v2';
const Errors = require('../Errors');
const Cache = require('./defaultCache');
Expand All @@ -14,11 +15,10 @@ class Requests {
}
async request(endpoint, options = {}) {
options.headers = { 'API-Key': this.client.key, ...options.headers };
const fetchMethod = requireFetch ? externalFetch : fetch;
/**
* @type {externalFetch.Response|Response}
*/
const res = await fetchMethod(BASE_URL + endpoint, options);
const res = await fetch(BASE_URL + endpoint, options);
if (res.status >= 500 && res.status < 528) throw new Error(Errors.ERROR_STATUSTEXT.replace(/{statustext}/, `Server Error : ${res.status} ${res.statusText}`));
const parsedRes = await res.json().catch(() => {
throw new Error(Errors.INVALID_RESPONSE_BODY);
Expand Down
4 changes: 3 additions & 1 deletion src/Private/updater.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable require-jsdoc */
/* eslint-disable no-console */
const fetch = require('node-fetch');
const requireFetch = !globalThis.fetch;
const externalFetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const fetch = requireFetch ? externalFetch : globalThis.fetch;
const Errors = require('../Errors');
class Updater {
async checkForUpdates() {
Expand Down
2 changes: 1 addition & 1 deletion src/Private/uuidCache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable require-jsdoc */
const requireFetch = !globalThis.fetch;
const externalFetch = require('node-fetch');
const externalFetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const fetch = requireFetch ? externalFetch : globalThis.fetch;
const cachedUuids = new Map();

Expand Down

0 comments on commit 0edfbab

Please sign in to comment.