import type { HostApiResult } from '@src/types/runtime'; export interface HostApiRequest { path: string; method?: string; headers?: Record; body?: unknown; } export interface NormalizedHostApiRequest { path: string; pathname: string; method: string; headers: Record; body: unknown; url: URL; } export function normalizeRequest(request: HostApiRequest): NormalizedHostApiRequest { const path = String(request.path || '/').trim() || '/'; return { path, pathname: new URL(path, 'http://127.0.0.1').pathname, method: String(request.method || 'GET').trim().toUpperCase(), headers: request.headers || {}, body: request.body ?? null, url: new URL(path, 'http://127.0.0.1'), }; } export function parseJsonBody(body: unknown): T { if (body == null || body === '') { return {} as T; } if (typeof body === 'string') { return JSON.parse(body) as T; } return body as T; } export function ok(data: T, status = 200): HostApiResult { return { success: true, ok: true, status, json: data, data, }; } export function fail(status: number, error: string, data?: T): HostApiResult { return { success: false, ok: false, status, error, text: error, data, }; }