diff options
author | mat <github@matdoes.dev> | 2022-05-30 12:48:48 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-05-30 12:48:48 -0500 |
commit | f8b1876203c6d0449e597bf9d9de4bdabf55b242 (patch) | |
tree | 72c2dd7679de28c3b50d19601135d1f215f52a2e /src | |
parent | 5777b24bbf1ccad2b4c897d8c3960b892405b7c0 (diff) | |
download | skyblock-api-f8b1876203c6d0449e597bf9d9de4bdabf55b242.tar.gz skyblock-api-f8b1876203c6d0449e597bf9d9de4bdabf55b242.tar.bz2 skyblock-api-f8b1876203c6d0449e597bf9d9de4bdabf55b242.zip |
Replace node-fetch with undici and bump deps
Diffstat (limited to 'src')
-rw-r--r-- | src/constants.ts | 11 | ||||
-rw-r--r-- | src/discord.ts | 10 | ||||
-rw-r--r-- | src/mojang.ts | 17 |
3 files changed, 12 insertions, 26 deletions
diff --git a/src/constants.ts b/src/constants.ts index 8143381..769eb7d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,17 +5,13 @@ // we have to do this so we can mock the function from the tests properly import * as constants from './constants.js' -import * as nodeFetch from 'node-fetch' import NodeCache from 'node-cache' import { debug } from './index.js' import { sleep } from './util.js' import Queue from 'queue-promise' -import fetch from 'node-fetch' -import { Agent } from 'https' +import { fetch } from 'undici' +import type { Response as UndiciResponse } from 'undici/types/fetch' -const httpsAgent = new Agent({ - keepAlive: true -}) const githubApiBase = 'https://api.github.com' const owner = 'skyblockstats' @@ -34,13 +30,12 @@ const queue = new Queue({ * @param headers The extra headers * @param json The JSON body, only applicable for some types of methods */ -async function fetchGithubApi(method: string, route: string, headers?: any, json?: any): Promise<nodeFetch.Response> { +async function fetchGithubApi(method: string, route: string, headers?: any, json?: any): Promise<UndiciResponse> { try { if (debug) console.debug('fetching github api', method, route) const data = await fetch( githubApiBase + route, { - agent: () => httpsAgent, body: json ? JSON.stringify(json) : undefined, method, headers: Object.assign({ diff --git a/src/discord.ts b/src/discord.ts index 7871fa3..e6ca743 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -1,11 +1,7 @@ -import fetch from 'node-fetch' -import { Agent } from 'https' +import { fetch } from 'undici' const DISCORD_CLIENT_ID = '885347559382605916' -const httpsAgent = new Agent({ - keepAlive: true -}) export interface TokenResponse { access_token: string @@ -49,7 +45,6 @@ export async function exchangeCode(redirectUri: string, code: string): Promise<T API_ENDPOINT + '/oauth2/token', { method: 'POST', - agent: () => httpsAgent, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams(data).toString() } @@ -63,8 +58,7 @@ export async function getUser(accessToken: string): Promise<DiscordUser> { const response = await fetch( API_ENDPOINT + '/users/@me', { - headers: {'Authorization': 'Bearer ' + accessToken}, - agent: () => httpsAgent, + headers: { 'Authorization': 'Bearer ' + accessToken }, } ) return await response.json() as DiscordUser diff --git a/src/mojang.ts b/src/mojang.ts index 0ad22ab..790cc91 100644 --- a/src/mojang.ts +++ b/src/mojang.ts @@ -3,9 +3,9 @@ */ import { isUuid, sleep, undashUuid } from './util.js' -import * as nodeFetch from 'node-fetch' -import fetch from 'node-fetch' +import { fetch } from 'undici' import { Agent } from 'https' +import type { Response as UndiciResponse } from 'undici/types/fetch' // We need to create an agent to prevent memory leaks const httpsAgent = new Agent({ @@ -22,13 +22,12 @@ interface MojangApiResponse { * Get mojang api data from the session server */ export let profileFromUuid = async function profileFromUuid(uuid: string): Promise<MojangApiResponse> { - let fetchResponse: nodeFetch.Response + let fetchResponse: UndiciResponse try { fetchResponse = await fetch( - // using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here + // using mojang directly is faster than ashcon, also there seem to be no ratelimits here? `https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`, - { agent: () => httpsAgent } ) } catch { // if there's an error, wait a second and try again @@ -59,12 +58,11 @@ export let profileFromUuid = async function profileFromUuid(uuid: string): Promi export let profileFromUsername = async function profileFromUsername(username: string): Promise<MojangApiResponse> { // since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/ - let fetchResponse: nodeFetch.Response + let fetchResponse: UndiciResponse try { fetchResponse = await fetch( `https://api.mojang.com/users/profiles/minecraft/${username}`, - { agent: () => httpsAgent } ) } catch { // if there's an error, wait a second and try again @@ -76,7 +74,7 @@ export let profileFromUsername = async function profileFromUsername(username: st const rawData = await fetchResponse.text() try { data = JSON.parse(rawData) - } catch {} + } catch { } if (!data?.id) { @@ -91,12 +89,11 @@ export let profileFromUsername = async function profileFromUsername(username: st } export async function profileFromUsernameAlternative(username: string): Promise<MojangApiResponse> { - let fetchResponse: nodeFetch.Response + let fetchResponse: UndiciResponse try { fetchResponse = await fetch( `https://api.ashcon.app/mojang/v2/user/${username}`, - { agent: () => httpsAgent } ) } catch { // if there's an error, wait a second and try again |