From d21265f443556305aedac4bb5b3d36d67f22d23b Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 6 Mar 2026 20:09:14 -0500 Subject: [PATCH] refactor: centralize API URL and internal API key management for improved maintainability --- SharpbinV3.Client/src/lib/server/api.ts | 7 +++++++ SharpbinV3.Client/src/routes/[id]/+page.server.ts | 12 +++--------- SharpbinV3.Client/src/routes/raw/[id]/+server.ts | 3 +-- .../src/routes/report/[reportId]/+page.server.ts | 8 +++----- SharpbinV3.Client/src/routes/reports/+page.server.ts | 8 +++----- SharpbinV3.Client/src/routes/stats/+page.server.ts | 6 ++---- .../src/routes/user/[username]/+page.server.ts | 10 ++++------ 7 files changed, 23 insertions(+), 31 deletions(-) create mode 100644 SharpbinV3.Client/src/lib/server/api.ts diff --git a/SharpbinV3.Client/src/lib/server/api.ts b/SharpbinV3.Client/src/lib/server/api.ts new file mode 100644 index 0000000..d49ce78 --- /dev/null +++ b/SharpbinV3.Client/src/lib/server/api.ts @@ -0,0 +1,7 @@ +import { env } from '$env/dynamic/private'; + +export const apiUrl = env.VITE_API_URL ?? 'http://localhost:5050'; +export const viewInternalApiKey = + env.NODE_ENV === 'development' + ? 'cccdd42d9f2f648493e402f1da7c855ea798ad510e6bb9ea712dd7bed838e58' + : env.VIEW_INTERNAL_API_KEY ?? env.View_HMAC_Internal_API_Key ?? ''; \ No newline at end of file diff --git a/SharpbinV3.Client/src/routes/[id]/+page.server.ts b/SharpbinV3.Client/src/routes/[id]/+page.server.ts index 651b47d..2777685 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.server.ts +++ b/SharpbinV3.Client/src/routes/[id]/+page.server.ts @@ -1,15 +1,9 @@ import type { PageServerLoad } from './$types'; import type { Paste } from '$lib/types/paste'; import { getServerToken } from '$lib/utils/auth'; -import { env } from '$env/dynamic/private'; +import { apiUrl, viewInternalApiKey } from '$lib/server/api'; import { error } from '@sveltejs/kit'; -const apiUrl = process.env.VITE_API_URL || 'http://localhost:5050'; -const apiKey = - env.NODE_ENV === 'development' - ? 'cccdd42d9f2f648493e402f1da7c855ea798ad510e6bb9ea712dd7bed838e58' - : (env.VIEW_INTERNAL_API_KEY ?? env.View_HMAC_Internal_API_Key ?? ''); - export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent }) => { const { id } = params; const parentData = await parent().catch(() => null); @@ -29,7 +23,7 @@ export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent const viewed = await fetch(`${apiUrl}/api/paste/${id}/view`, { method: 'POST', headers: { - 'X-Internal-API-Key': apiKey, + 'X-Internal-API-Key': viewInternalApiKey, Authorization: token ? `Bearer ${token}` : '' } }); @@ -46,7 +40,7 @@ export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent console.error( `Failed to increment view count for paste ${id}, status code ${viewed.status}: ${await viewed.text()}` ); - if (apiKey === '') { + if (viewInternalApiKey === '') { console.error( 'The API key for internal requests is not set. Please configure it in the environment variables.' ); diff --git a/SharpbinV3.Client/src/routes/raw/[id]/+server.ts b/SharpbinV3.Client/src/routes/raw/[id]/+server.ts index bd4ec2b..7e94d95 100644 --- a/SharpbinV3.Client/src/routes/raw/[id]/+server.ts +++ b/SharpbinV3.Client/src/routes/raw/[id]/+server.ts @@ -1,6 +1,5 @@ import type { RequestHandler } from '@sveltejs/kit'; - -const apiUrl = process.env.VITE_API_URL || 'http://localhost:5050'; +import { apiUrl } from '$lib/server/api'; export const GET: RequestHandler = async ({ params }) => { const { id } = params; diff --git a/SharpbinV3.Client/src/routes/report/[reportId]/+page.server.ts b/SharpbinV3.Client/src/routes/report/[reportId]/+page.server.ts index f4c191a..e65beb6 100644 --- a/SharpbinV3.Client/src/routes/report/[reportId]/+page.server.ts +++ b/SharpbinV3.Client/src/routes/report/[reportId]/+page.server.ts @@ -1,11 +1,9 @@ import type { PageServerLoad } from './$types'; import { error } from '@sveltejs/kit'; -import { env } from '$env/dynamic/private'; +import { apiUrl } from '$lib/server/api'; import { getServerToken } from '$lib/utils/auth'; import type { Report } from '$lib/types/report'; -const API_URL = env.VITE_API_URL ?? 'http://localhost:5050'; - function decodeJwtRoles(token?: string | null): number[] { try { if (!token) return []; @@ -26,7 +24,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, params, url }) => { const roles = decodeJwtRoles(token); const canEdit = roles.includes(1) || roles.includes(255); - const res = await fetch(`${API_URL}/api/report/${params.reportId}`, { + const res = await fetch(`${apiUrl}/api/report/${params.reportId}`, { headers: { Authorization: `Bearer ${token}` } }); @@ -36,7 +34,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, params, url }) => { } const report = (await res.json()) as Report; - const optionsRes = await fetch(`${API_URL}/api/report/options`); + const optionsRes = await fetch(`${apiUrl}/api/report/options`); const options = optionsRes.ok ? await optionsRes.json() : { types: [], statuses: [] }; return { report, url: url.href, canEdit, options }; diff --git a/SharpbinV3.Client/src/routes/reports/+page.server.ts b/SharpbinV3.Client/src/routes/reports/+page.server.ts index 2ec3bb9..67a0898 100644 --- a/SharpbinV3.Client/src/routes/reports/+page.server.ts +++ b/SharpbinV3.Client/src/routes/reports/+page.server.ts @@ -1,12 +1,10 @@ import type { PageServerLoad } from './$types'; import { error } from '@sveltejs/kit'; -import { env } from '$env/dynamic/private'; +import { apiUrl } from '$lib/server/api'; import { getServerToken } from '$lib/utils/auth'; import type { ReportListResponse } from '$lib/types/report'; import { extractError } from '$lib/utils/misc'; -const API_URL = env.VITE_API_URL ?? 'http://localhost:5050'; - function decodeJwtRoles(token?: string | null): number[] { try { if (!token) return []; @@ -49,7 +47,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, url }) => { if ((target === 'pastes' || target === 'all') && pasteId) query.set('pasteId', pasteId); if ((target === 'users' || target === 'all') && userUuid) query.set('userUUID', userUuid); - const reportsRes = await fetch(`${API_URL}/api/report/${target}?${query.toString()}`, { + const reportsRes = await fetch(`${apiUrl}/api/report/${target}?${query.toString()}`, { headers: { Authorization: `Bearer ${token}` } }); @@ -60,7 +58,7 @@ export const load: PageServerLoad = async ({ fetch, cookies, url }) => { const reports = (await reportsRes.json()) as ReportListResponse; - const optionsRes = await fetch(`${API_URL}/api/report/options`); + const optionsRes = await fetch(`${apiUrl}/api/report/options`); const options = optionsRes.ok ? await optionsRes.json() : { types: [], statuses: [] }; return { diff --git a/SharpbinV3.Client/src/routes/stats/+page.server.ts b/SharpbinV3.Client/src/routes/stats/+page.server.ts index a79c077..85f075b 100644 --- a/SharpbinV3.Client/src/routes/stats/+page.server.ts +++ b/SharpbinV3.Client/src/routes/stats/+page.server.ts @@ -1,13 +1,11 @@ import type { PageServerLoad } from './$types'; -import { env } from '$env/dynamic/private'; +import { apiUrl } from '$lib/server/api'; import { error, isHttpError } from '@sveltejs/kit'; import { extractError } from '$lib/utils/misc'; -const API_URL = env.VITE_API_URL ?? 'http://localhost:5050'; - export const load: PageServerLoad = async ({ fetch }) => { try { - const res = await fetch(`${API_URL}/api/stats`); + const res = await fetch(`${apiUrl}/api/stats`); if (!res.ok) { const json = await res.json().catch(() => ({})); diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts b/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts index 42b98e7..b2def81 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts @@ -1,14 +1,12 @@ import type { PageServerLoad } from './$types'; import { error } from '@sveltejs/kit'; -import { env } from '$env/dynamic/private'; +import { apiUrl } from '$lib/server/api'; import { getServerToken } from '$lib/utils/auth'; import type { User } from '$lib/types/user'; import type { Paste } from '$lib/types/paste'; import type { Pagination } from '$lib/types/pagination'; import type { Report, ReportListResponse } from '$lib/types/report'; -const API_URL = env.VITE_API_URL ?? 'http://localhost:5050'; - function decodeJwtRoles(token?: string | null): number[] { try { if (!token) return []; @@ -40,7 +38,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent const { options } = await parent(); const jwtUuid = decodeJwtUuid(token); - const res = await fetch(`${API_URL}/api/user/${username}`, { + const res = await fetch(`${apiUrl}/api/user/${username}`, { headers: token ? { Authorization: `Bearer ${token}` } : undefined }); @@ -71,7 +69,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent } if (token && canModerate && (!reportsSubmitted || !isOwner)) { - const submittedRes = await fetch(`${API_URL}/api/user/${user.uuid}/reports/submitted`, { + const submittedRes = await fetch(`${apiUrl}/api/user/${user.uuid}/reports/submitted`, { headers: { Authorization: `Bearer ${token}` } }); if (submittedRes.ok) reportsSubmitted = await submittedRes.json(); @@ -79,7 +77,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent if (token && !isOwner) { if (canModerate) { - const r = await fetch(`${API_URL}/api/user/${user.uuid}/reports`, { + const r = await fetch(`${apiUrl}/api/user/${user.uuid}/reports`, { headers: { Authorization: `Bearer ${token}` } }); if (r.ok) reportsTarget = await r.json();