diff options
author | mat <github@matdoes.dev> | 2022-12-24 19:38:19 -0600 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-12-24 19:38:19 -0600 |
commit | f174a3ed5fb04d038f26339c86f69ebfec175fa9 (patch) | |
tree | c13c146bf84d482f803076bae3649cc64c91ee1b | |
parent | c4ebc9ccd9332928197c72903b72dd0161e55cdb (diff) | |
download | skyblock-stats-f174a3ed5fb04d038f26339c86f69ebfec175fa9.tar.gz skyblock-stats-f174a3ed5fb04d038f26339c86f69ebfec175fa9.tar.bz2 skyblock-stats-f174a3ed5fb04d038f26339c86f69ebfec175fa9.zip |
fix server being ratelimited
-rw-r--r-- | src/donators.txt | 1 | ||||
-rw-r--r-- | src/hooks.server.ts | 17 | ||||
-rw-r--r-- | src/lib/api.ts | 13 |
3 files changed, 18 insertions, 13 deletions
diff --git a/src/donators.txt b/src/donators.txt index 7a296c8..afe5e41 100644 --- a/src/donators.txt +++ b/src/donators.txt @@ -15,3 +15,4 @@ bd082a55373e4305bee348b9060e16c9 MelonKingDe 69c843a8f8864bfaacbb16687dd169ed itsPerfect81 46a1b94536da4195ba9e551812d4e343 ToumaTheCat 5984cc0bf4cb48fdaaa2b10ecde88c33 Ayype +7da0c47581dc42b4962118f8049147b7 Ke5o diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 0d2087b..eb8de0a 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,7 +1,20 @@ -import type { Handle } from '@sveltejs/kit' +import { API_URL } from '$lib/api' +import env from '$lib/env' +import type { Handle, HandleFetch } from '@sveltejs/kit' export const handle = (async ({ event, resolve }) => { event.locals.sid = event.cookies.get('sid') const response = await resolve(event) return response -}) satisfies Handle
\ No newline at end of file +}) satisfies Handle + +export const handleFetch = (async ({ request, fetch, event }) => { + if (request.url.startsWith(API_URL)) { + const { SKYBLOCK_STATS_API_KEY } = env(event.platform) + if (SKYBLOCK_STATS_API_KEY) { + request.headers.set('key', SKYBLOCK_STATS_API_KEY) + } + } + + return fetch(request) +}) satisfies HandleFetch diff --git a/src/lib/api.ts b/src/lib/api.ts index 552607c..01960c4 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,18 +1,9 @@ -import env from './env' - // the trailing slash is required -const API_URL = 'https://skyblock-api.matdoes.dev/' +export const API_URL = 'https://skyblock-api.matdoes.dev/' // export const API_URL = 'http://localhost:8080/' export async function fetchApi(path: string, fetch: (info: RequestInfo, init?: RequestInit | undefined) => Promise<Response>, init?: RequestInit | undefined) { - const { SKYBLOCK_STATS_API_KEY } = env() - if (SKYBLOCK_STATS_API_KEY) { - init = init || {} - if (!init.headers) - init.headers = {} - init.headers['key'] = SKYBLOCK_STATS_API_KEY - } - + // the header is set in hooks.server.ts const response = await fetch(API_URL + path, init) return response }
\ No newline at end of file |