diff options
author | mat <github@matdoes.dev> | 2022-06-17 14:56:45 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-06-17 14:56:45 -0500 |
commit | a78b91ca5387fdfd1bb27b6b928154378819d9c6 (patch) | |
tree | ee74f8df7287fa4c9bb605d9b116702554e22be5 /src | |
parent | 62981dd3b864e9d4021b51ca7aaa9973c205f0bf (diff) | |
parent | 90f4b2c916cdd7f61843c7a2c7f4451d44e18367 (diff) | |
download | skyblock-stats-a78b91ca5387fdfd1bb27b6b928154378819d9c6.tar.gz skyblock-stats-a78b91ca5387fdfd1bb27b6b928154378819d9c6.tar.bz2 skyblock-stats-a78b91ca5387fdfd1bb27b6b928154378819d9c6.zip |
Merge branch 'master' into faster-heads
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Counter.svelte | 102 | ||||
-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/index.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 | 16 | ||||
-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 |
18 files changed, 66 insertions, 145 deletions
diff --git a/src/lib/Counter.svelte b/src/lib/Counter.svelte deleted file mode 100644 index 0df76f5..0000000 --- a/src/lib/Counter.svelte +++ /dev/null @@ -1,102 +0,0 @@ -<script lang="ts"> - import { spring } from 'svelte/motion'; - - let count = 0; - - const displayed_count = spring(); - $: displayed_count.set(count); - $: offset = modulo($displayed_count, 1); - - function modulo(n: number, m: number) { - // handle negative numbers - return ((n % m) + m) % m; - } -</script> - -<div class="counter"> - <button on:click={() => (count -= 1)} aria-label="Decrease the counter by one"> - <svg aria-hidden="true" viewBox="0 0 1 1"> - <path d="M0,0.5 L1,0.5" /> - </svg> - </button> - - <div class="counter-viewport"> - <div class="counter-digits" style="transform: translate(0, {100 * offset}%)"> - <strong class="hidden" aria-hidden="true">{Math.floor($displayed_count + 1)}</strong> - <strong>{Math.floor($displayed_count)}</strong> - </div> - </div> - - <button on:click={() => (count += 1)} aria-label="Increase the counter by one"> - <svg aria-hidden="true" viewBox="0 0 1 1"> - <path d="M0,0.5 L1,0.5 M0.5,0 L0.5,1" /> - </svg> - </button> -</div> - -<style> - .counter { - display: flex; - border-top: 1px solid rgba(0, 0, 0, 0.1); - border-bottom: 1px solid rgba(0, 0, 0, 0.1); - margin: 1rem 0; - } - - .counter button { - width: 2em; - padding: 0; - display: flex; - align-items: center; - justify-content: center; - border: 0; - background-color: transparent; - color: var(--text-color); - font-size: 2rem; - } - - .counter button:hover { - background-color: var(--secondary-color); - } - - svg { - width: 25%; - height: 25%; - } - - path { - vector-effect: non-scaling-stroke; - stroke-width: 2px; - stroke: var(--text-color); - } - - .counter-viewport { - width: 8em; - height: 4em; - overflow: hidden; - text-align: center; - position: relative; - } - - .counter-viewport strong { - position: absolute; - display: flex; - width: 100%; - height: 100%; - font-weight: 400; - color: var(--accent-color); - font-size: 4rem; - align-items: center; - justify-content: center; - } - - .counter-digits { - position: absolute; - width: 100%; - height: 100%; - } - - .hidden { - top: -100%; - user-select: none; - } -</style> 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/index.svelte b/src/routes/index.svelte index 0d27428..5780e7a 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -79,9 +79,9 @@ <a href="//discord.gg/KtscNUnh4f">discord.gg/KtscNUnh4f</a>. </p> <p> - Resource packs: <a href="//packshq.com">PacksHQ</a> (default), + Resource packs: <a href="//furfsky.net">Furfsky Reborn</a> (default), + <a href="//packshq.com">PacksHQ</a>, <a href="//hypixel.net/threads/2138599">Furfsky</a>, - <a href="//furfsky.net">Furfsky Reborn</a>, <a href="//hypixel.net/threads/2239953">Ectoplasm</a>, <a href="//hypixel.net/threads/3470904">RNBW</a>, <a href="//hypixel.net/threads/4174260">Hypixel+</a>, 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 caf348b..544d3c4 100644 --- a/src/routes/player/[player]/[profile].svelte +++ b/src/routes/player/[player]/[profile].svelte @@ -1,14 +1,22 @@ <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` - ).then(r => r.json()) + const data: CleanMemberProfile = await fetchApi( + `player/${player}/${profile}?customization=true`, + fetch + ).then(async r => { + const text = await r.text() + try { + return JSON.parse(text) + } catch (e) { + throw new Error(`Invalid JSON: ${text}`) + } + }) if (!data.member) { return { 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', |