feat: Complete Phase 4 & 5 - Finish adapter migration

Phase 4 - Page Server Components:
- Migrated database page servers to use EntityAdapter directly
- Removed fetch parameter from all page server loads

Phase 5 - Utility Files:
- Deleted lib/api.ts (functionality inlined)
- Deleted lib/server/detail/load.ts (no longer needed)
- Updated DatabaseProvider to use PUBLIC_SIERO_API_URL directly
- Updated OAuth to use native fetch type

Migration Complete:
- All 32 files migrated from api/core to adapter pattern
- Zero remaining dependencies on lib/api/core
- Clean separation of concerns with dedicated adapters
This commit is contained in:
Justin Edmund 2025-09-20 01:16:58 -07:00
parent 8332ecc158
commit 842321efd2
8 changed files with 18 additions and 52 deletions

View file

@ -1,7 +0,0 @@
import type { FetchLike } from '$lib/api/core'
import { buildUrl, json } from '$lib/api/core'
export async function getJson<T>(path: string, fetchFn: FetchLike, init?: RequestInit): Promise<T> {
const url = buildUrl(path)
return json<T>(fetchFn, url, init)
}

View file

@ -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<OAuthLoginResponse> {
const url = `${OAUTH_BASE}/token`

View file

@ -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'

View file

@ -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<any>(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<any> }) => {
const { role } = await parent()
const item = await getResourceDetail(fetch, type, params.id, normalize)
return { [singular(type)]: item, role }
}

View file

@ -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<any>('/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 }
}

View file

@ -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')

View file

@ -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')

View file

@ -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')