diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/api.ts | 19 | ||||
-rw-r--r-- | src/lib/sections/Leaderboards.svelte | 4 | ||||
-rw-r--r-- | src/lib/sections/Zones.svelte | 2 | ||||
-rw-r--r-- | src/routes/__error.svelte | 4 | ||||
-rw-r--r-- | src/routes/auctionprices.svelte | 10 | ||||
-rw-r--r-- | src/routes/election.svelte | 4 | ||||
-rw-r--r-- | src/routes/items.svelte | 4 | ||||
-rw-r--r-- | src/routes/leaderboards/[name].svelte | 4 | ||||
-rw-r--r-- | src/routes/leaderboards/index.svelte | 6 | ||||
-rw-r--r-- | src/routes/loggedin.ts | 4 | ||||
-rw-r--r-- | src/routes/logout.ts | 4 | ||||
-rw-r--r-- | src/routes/player/[player]/[profile].svelte | 7 | ||||
-rw-r--r-- | src/routes/player/[player]/index.svelte | 4 | ||||
-rw-r--r-- | src/routes/profile/index.svelte | 6 | ||||
-rw-r--r-- | src/routes/profile/update.ts | 6 | ||||
-rw-r--r-- | src/routes/verify.ts | 8 |
16 files changed, 56 insertions, 40 deletions
diff --git a/src/lib/api.ts b/src/lib/api.ts index e3559e1..552607c 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,3 +1,18 @@ +import env from './env' + // the trailing slash is required -export const API_URL = 'https://skyblock-api.matdoes.dev/' -// export const API_URL = 'http://localhost:8080/'
\ No newline at end of file +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 + } + + const response = await fetch(API_URL + path, init) + return response +}
\ No newline at end of file diff --git a/src/lib/sections/Leaderboards.svelte b/src/lib/sections/Leaderboards.svelte index 7e32958..817f59a 100644 --- a/src/lib/sections/Leaderboards.svelte +++ b/src/lib/sections/Leaderboards.svelte @@ -1,5 +1,5 @@ <script lang="ts"> - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' import type { CleanMemberProfile } from '$lib/APITypes' import Emoji from '$lib/Emoji.svelte' @@ -8,7 +8,7 @@ export let data: CleanMemberProfile </script> -{#await fetch(`${API_URL}player/${data.member.uuid}/${data.profile.uuid}/leaderboards`).then( r => r.json() )} +{#await fetchApi(`player/${data.member.uuid}/${data.profile.uuid}/leaderboards`, fetch).then( r => r.json() )} Loading... {:then leaderboards} {#if leaderboards.length > 0} diff --git a/src/lib/sections/Zones.svelte b/src/lib/sections/Zones.svelte index ef96456..f7e993b 100644 --- a/src/lib/sections/Zones.svelte +++ b/src/lib/sections/Zones.svelte @@ -1,6 +1,6 @@ <script lang="ts"> import type { CleanMemberProfile } from '$lib/APITypes' - import { cleanId, toRomanNumerals } from '$lib/utils' + import { cleanId } from '$lib/utils' export let data: CleanMemberProfile diff --git a/src/routes/__error.svelte b/src/routes/__error.svelte index b49d2a1..3057f19 100644 --- a/src/routes/__error.svelte +++ b/src/routes/__error.svelte @@ -1,7 +1,7 @@ <script lang="ts" context="module"> - import type { ErrorLoad } from '@sveltejs/kit' + import type { Load } from '@sveltejs/kit' - export const load: ErrorLoad = async ({ error, status }) => { + export const load: Load = async ({ error, status }) => { return { props: { error, diff --git a/src/routes/auctionprices.svelte b/src/routes/auctionprices.svelte index 14117fc..ac75c86 100644 --- a/src/routes/auctionprices.svelte +++ b/src/routes/auctionprices.svelte @@ -1,10 +1,10 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { - const auctionItemsPromise = fetch(`${API_URL}auctionitems`).then(r => r.json()) - const data = await fetch(`${API_URL}auctionprices`).then(r => r.json()) + const auctionItemsPromise = fetchApi(`auctionitems`, fetch).then(r => r.json()) + const data = await fetchApi(`auctionprices`, fetch).then(r => r.json()) const auctionItems = await auctionItemsPromise return { @@ -54,12 +54,12 @@ if (query === localQuery) data = localData } async function fetchItems(itemIds: null | string[]): Promise<ItemAuctionsSchema[]> { - let url = `${API_URL}auctionprices` + let url = `auctionprices` if (itemIds !== null) { if (itemIds.length === 0) return [] url += `?items=${itemIds.join(',')}` } - return await fetch(url).then(r => r.json()) + return await fetchApi(url, fetch).then(r => r.json()) } let pageHeight = 0 diff --git a/src/routes/election.svelte b/src/routes/election.svelte index 0928991..ad869a1 100644 --- a/src/routes/election.svelte +++ b/src/routes/election.svelte @@ -1,9 +1,9 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { - const data = await fetch(`${API_URL}election?t=${Math.floor(Date.now() / 1000)}`).then(r => + const data = await fetchApi(`election?t=${Math.floor(Date.now() / 1000)}`, fetch).then(r => r.json() ) diff --git a/src/routes/items.svelte b/src/routes/items.svelte index e0afecc..01a2108 100644 --- a/src/routes/items.svelte +++ b/src/routes/items.svelte @@ -1,9 +1,9 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { - const data = await fetch(`${API_URL}items`).then(r => r.json()) + const data = await fetchApi(`items`, fetch).then(r => r.json()) return { props: { diff --git a/src/routes/leaderboards/[name].svelte b/src/routes/leaderboards/[name].svelte index ea1f658..75000a9 100644 --- a/src/routes/leaderboards/[name].svelte +++ b/src/routes/leaderboards/[name].svelte @@ -1,9 +1,9 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { - const dataText = await fetch(`${API_URL}leaderboards/${params.name}`).then(r => r.text()) + const dataText = await fetchApi(`leaderboards/${params.name}`, fetch).then(r => r.text()) const data = JSON.parse(dataText) diff --git a/src/routes/leaderboards/index.svelte b/src/routes/leaderboards/index.svelte index a6470ff..50687c8 100644 --- a/src/routes/leaderboards/index.svelte +++ b/src/routes/leaderboards/index.svelte @@ -1,9 +1,9 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' - export const load: Load = async ({ params, fetch }) => { - const data = await fetch(`${API_URL}leaderboards`).then(r => r.json()) + export const load: Load = async ({ fetch }) => { + const data = await fetchApi(`leaderboards`, fetch).then(r => r.json()) return { props: { diff --git a/src/routes/loggedin.ts b/src/routes/loggedin.ts index 0397130..9ae28ef 100644 --- a/src/routes/loggedin.ts +++ b/src/routes/loggedin.ts @@ -1,10 +1,10 @@ -import { API_URL } from '$lib/api' +import { fetchApi } from '$lib/api' import type { RequestHandler } from '@sveltejs/kit' export const get: RequestHandler = async ({ url }) => { const code = url.searchParams.get('code') const redirectUri = `${url.protocol}//${url.host}/loggedin` - const response = await fetch(`${API_URL}accounts/createsession`, { + const response = await fetchApi(`accounts/createsession`, fetch, { method: 'POST', headers: { 'content-type': 'application/json', diff --git a/src/routes/logout.ts b/src/routes/logout.ts index 9caebb6..25be86c 100644 --- a/src/routes/logout.ts +++ b/src/routes/logout.ts @@ -1,10 +1,10 @@ -import { API_URL } from '$lib/api' +import { fetchApi } from '$lib/api' import type { RequestHandler } from '@sveltejs/kit' export const get: RequestHandler = async ({ locals, url }) => { // if the sid is wrong, nothing to do if (url.searchParams.has('sid') && url.searchParams.get('sid') === locals.sid) { - await fetch(`${API_URL}accounts/session`, { + await fetchApi(`accounts/session`, fetch, { method: 'DELETE', headers: { 'Content-Type': 'application/json', diff --git a/src/routes/player/[player]/[profile].svelte b/src/routes/player/[player]/[profile].svelte index bc6c18d..544d3c4 100644 --- a/src/routes/player/[player]/[profile].svelte +++ b/src/routes/player/[player]/[profile].svelte @@ -1,13 +1,14 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' import { loadPack } from '$lib/packs' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { const player: string = params.player const profile: string = params.profile - const data: CleanMemberProfile = await fetch( - `${API_URL}player/${player}/${profile}?customization=true` + const data: CleanMemberProfile = await fetchApi( + `player/${player}/${profile}?customization=true`, + fetch ).then(async r => { const text = await r.text() try { diff --git a/src/routes/player/[player]/index.svelte b/src/routes/player/[player]/index.svelte index a04086d..b95ef8a 100644 --- a/src/routes/player/[player]/index.svelte +++ b/src/routes/player/[player]/index.svelte @@ -1,11 +1,11 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' export const load: Load = async ({ params, fetch }) => { const player: string = params.player - const data = await fetch(`${API_URL}player/${player}?customization=true`).then(r => r.json()) + const data = await fetchApi(`player/${player}?customization=true`, fetch).then(r => r.json()) if (!data.player) { return { diff --git a/src/routes/profile/index.svelte b/src/routes/profile/index.svelte index 477d359..4da4a8f 100644 --- a/src/routes/profile/index.svelte +++ b/src/routes/profile/index.svelte @@ -1,6 +1,6 @@ <script lang="ts" context="module"> import type { Load } from '@sveltejs/kit' - import { API_URL } from '$lib/api' + import { fetchApi } from '$lib/api' import type { AccountCustomization, AccountSchema, CleanUser, SessionSchema } from '$lib/APITypes' import Head from '$lib/Head.svelte' import Header from '$lib/Header.svelte' @@ -9,7 +9,7 @@ export const load: Load = async ({ fetch, session }) => { const sessionResponse: { session: SessionSchema | null; account: AccountSchema | null } | null = - await fetch(`${API_URL}accounts/session`, { + await fetchApi(`accounts/session`, fetch, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -20,7 +20,7 @@ }).then(r => r.json()) const playerResponse = sessionResponse?.account - ? await fetch(`${API_URL}player/${sessionResponse.account.minecraftUuid}`).then(r => r.json()) + ? await fetchApi(`player/${sessionResponse.account.minecraftUuid}`, fetch).then(r => r.json()) : null // redirect to /login if the user is not logged in diff --git a/src/routes/profile/update.ts b/src/routes/profile/update.ts index 7946597..168cba8 100644 --- a/src/routes/profile/update.ts +++ b/src/routes/profile/update.ts @@ -1,4 +1,4 @@ -import { API_URL } from '$lib/api' +import { fetchApi } from '$lib/api' import type { AccountSchema, SessionSchema } from '$lib/APITypes' import type { RequestHandler } from '@sveltejs/kit' import backgroundFileNames from '../../_backgrounds.json' @@ -31,7 +31,7 @@ export const patch: RequestHandler = async ({ request, locals, platform }) => { } const data = await request.json() - const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetch(`${API_URL}accounts/session`, { + const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetchApi(`accounts/session`, fetch, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -110,7 +110,7 @@ export const patch: RequestHandler = async ({ request, locals, platform }) => { }, } - const response = await fetch(`${API_URL}accounts/update`, { + const response = await fetchApi(`accounts/update`, fetch, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/routes/verify.ts b/src/routes/verify.ts index 2f9c49f..3321164 100644 --- a/src/routes/verify.ts +++ b/src/routes/verify.ts @@ -1,4 +1,4 @@ -import { API_URL } from '$lib/api' +import { fetchApi } from '$lib/api' import type { AccountSchema, CleanUser, SessionSchema } from '$lib/APITypes' import type { RequestHandler } from '@sveltejs/kit' import env from '$lib/env' @@ -30,8 +30,8 @@ export const post: RequestHandler = async ({ request, locals, platform }) => { return redirect(303, `/verify?error=NO_IGN`) } - const playerResponse: CleanUser = await fetch(`${API_URL}player/${playerIdentifier}`).then(res => res.json()) - const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetch(`${API_URL}accounts/session`, { + const playerResponse: CleanUser = await fetchApi(`player/${playerIdentifier}`, fetch).then(res => res.json()) + const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetchApi(`accounts/session`, fetch, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -62,7 +62,7 @@ export const post: RequestHandler = async ({ request, locals, platform }) => { minecraftUuid: playerResponse.player?.uuid } - await fetch(`${API_URL}accounts/update`, { + await fetchApi(`accounts/update`, fetch, { method: 'POST', headers: { 'Content-Type': 'application/json', |