refactor: centralize API URL and internal API key management for improved maintainability

This commit is contained in:
2026-03-06 20:09:14 -05:00
parent 528456c168
commit d21265f443
7 changed files with 23 additions and 31 deletions
+7
View File
@@ -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 ?? '';
@@ -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.'
);
@@ -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;
@@ -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 };
@@ -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 {
@@ -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(() => ({}));
@@ -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();