femto-webapp/src/api/api.ts
2025-05-03 20:18:08 +02:00

26 lines
809 B
TypeScript

import { components } from './schema.ts'
// TODO for now we just assume that the API and client are running on the same host
// i think this might change but dependes on what we do with deployment
const ApiHost = `http://${location.hostname}:5181`
console.debug('API HOST IS', ApiHost)
export async function loadPostsForAuthor(
authorId: string,
count?: number,
cursor?: string,
): Promise<components['schemas']['GetAuthorPostsResponse']> {
const url = new URL(`authors/${authorId}/posts`, ApiHost)
if (count != null) url.searchParams.set('count', count.toString())
if (cursor) url.searchParams.set('cursor', cursor)
const request = new Request(url)
const response = await fetch(request)
if (!response.ok) throw new Error(await response.text())
return await response.json()
}