diff --git a/src/lib/api.ts b/src/lib/api.ts deleted file mode 100644 index 229ca200..00000000 --- a/src/lib/api.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { FetchLike } from '$lib/api/core' -import { buildUrl, json } from '$lib/api/core' - -export async function getJson(path: string, fetchFn: FetchLike, init?: RequestInit): Promise { - const url = buildUrl(path) - return json(fetchFn, url, init) -} diff --git a/src/lib/auth/oauth.ts b/src/lib/auth/oauth.ts index 4920b433..a55ba86d 100644 --- a/src/lib/auth/oauth.ts +++ b/src/lib/auth/oauth.ts @@ -1,4 +1,3 @@ -import type { FetchLike } from '$lib/api/core' import { OAUTH_BASE } from '$lib/config' export interface OAuthLoginResponse { @@ -13,8 +12,9 @@ export interface OAuthLoginResponse { role: number } } + export async function passwordGrantLogin( - fetchFn: FetchLike, + fetchFn: typeof fetch, body: { email: string; password: string; grant_type: 'password' } ): Promise { const url = `${OAUTH_BASE}/token` diff --git a/src/lib/providers/DatabaseProvider.ts b/src/lib/providers/DatabaseProvider.ts index 597ec703..81a17ba8 100644 --- a/src/lib/providers/DatabaseProvider.ts +++ b/src/lib/providers/DatabaseProvider.ts @@ -1,5 +1,7 @@ import { RestDataProvider } from 'wx-grid-data-provider' -import { API_BASE } from '$lib/api/core' +import { PUBLIC_SIERO_API_URL } from '$env/static/public' + +const API_BASE = PUBLIC_SIERO_API_URL || 'http://localhost:3000' interface DatabaseProviderOptions { resource: 'weapons' | 'characters' | 'summons' diff --git a/src/lib/server/detail/load.ts b/src/lib/server/detail/load.ts deleted file mode 100644 index cbe012c8..00000000 --- a/src/lib/server/detail/load.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { FetchLike } from '$lib/api/core' -import { get } from '$lib/api/core' -import { error } from '@sveltejs/kit' - -export type Resource = 'characters' | 'weapons' | 'summons' - -function singular(type: Resource): 'character' | 'weapon' | 'summon' { - if (type === 'characters') return 'character' - if (type === 'weapons') return 'weapon' - return 'summon' -} - -export async function getResourceDetail(fetch: FetchLike, type: Resource, id: string, normalize?: (m: any) => any) { - try { - const item = await get(fetch, `/${type}/${id}`) - if (!item) throw error(404, 'Not found') - return normalize ? normalize(item) : item - } catch (e: any) { - // Map HTTP 404 from API client into SvelteKit 404 - if (e?.message?.includes('HTTP 404')) throw error(404, 'Not found') - throw error(500, `Failed to load ${singular(type)}`) - } -} - -export const createDetailLoader = (type: Resource, normalize?: (m: any) => any) => - async ({ params, fetch, parent }: { params: { id: string }; fetch: FetchLike; parent: () => Promise }) => { - const { role } = await parent() - const item = await getResourceDetail(fetch, type, params.id, normalize) - return { [singular(type)]: item, role } - } - diff --git a/src/routes/about/+page.ts b/src/routes/about/+page.ts index 61e3d993..662ce0ad 100644 --- a/src/routes/about/+page.ts +++ b/src/routes/about/+page.ts @@ -1,6 +1,8 @@ -import { getJson } from '$lib/api' +import { PUBLIC_SIERO_API_URL } from '$env/static/public' export const load = async ({ fetch }) => { - const status = await getJson('/api/v1/version', fetch) + const apiBase = PUBLIC_SIERO_API_URL || 'http://localhost:3000' + const response = await fetch(`${apiBase}/api/v1/version`) + const status = await response.json() return { status } } diff --git a/src/routes/database/characters/[id]/+page.server.ts b/src/routes/database/characters/[id]/+page.server.ts index 8cc8ecb4..c9fd51c7 100644 --- a/src/routes/database/characters/[id]/+page.server.ts +++ b/src/routes/database/characters/[id]/+page.server.ts @@ -1,13 +1,13 @@ import type { PageServerLoad } from './$types' -import { get } from '$lib/api/core' +import { entityAdapter } from '$lib/api/adapters' import { error } from '@sveltejs/kit' -export const load: PageServerLoad = async ({ params, fetch, parent }) => { +export const load: PageServerLoad = async ({ params, parent }) => { try { // Get parent data to access role const parentData = await parent() - const character = await get(fetch, `/characters/${params.id}`) + const character = await entityAdapter.getCharacter(params.id) if (!character) { throw error(404, 'Character not found') diff --git a/src/routes/database/summons/[id]/+page.server.ts b/src/routes/database/summons/[id]/+page.server.ts index de553770..0057b292 100644 --- a/src/routes/database/summons/[id]/+page.server.ts +++ b/src/routes/database/summons/[id]/+page.server.ts @@ -1,13 +1,13 @@ import type { PageServerLoad } from './$types' -import { get } from '$lib/api/core' +import { entityAdapter } from '$lib/api/adapters' import { error } from '@sveltejs/kit' -export const load: PageServerLoad = async ({ params, fetch, parent }) => { +export const load: PageServerLoad = async ({ params, parent }) => { try { // Get parent data to access role const parentData = await parent() - const summon = await get(fetch, `/summons/${params.id}`) + const summon = await entityAdapter.getSummon(params.id) if (!summon) { throw error(404, 'Summon not found') diff --git a/src/routes/database/weapons/[id]/+page.server.ts b/src/routes/database/weapons/[id]/+page.server.ts index c780878c..dcd38341 100644 --- a/src/routes/database/weapons/[id]/+page.server.ts +++ b/src/routes/database/weapons/[id]/+page.server.ts @@ -1,13 +1,13 @@ import type { PageServerLoad } from './$types' -import { get } from '$lib/api/core' +import { entityAdapter } from '$lib/api/adapters' import { error } from '@sveltejs/kit' -export const load: PageServerLoad = async ({ params, fetch, parent }) => { +export const load: PageServerLoad = async ({ params, parent }) => { try { // Get parent data to access role const parentData = await parent() - const weapon = await get(fetch, `/weapons/${params.id}`) + const weapon = await entityAdapter.getWeapon(params.id) if (!weapon) { throw error(404, 'Weapon not found')