Files
vue-journal-layer/app/composables/useJournalApi.ts
Jakub Zych 6f6792f51c feat: scaffold Journal layer with configurable post route and git-install check
- journal module registers the Journal pages at journal.routes paths
- typed anonymous-GET client, html gate, SSR x-forwarded-for forwarding
- fixture API, built-server SSR smoke and git-tag install check
2026-10-09 17:31:45 +02:00

27 lines
1.4 KiB
TypeScript

// The Journal API client for pages and components. On the server it calls
// the internal base (runtimeConfig.journal.apiBase) and forwards the visitor's
// x-forwarded-for, plus the address of the peer that reached Nuxt, so the Go
// rate limiter keeps one bucket per visitor instead of one for the Nuxt
// server. In the browser it calls the same-origin public base. The base is
// always runtime config, never request input.
import { createJournalClient, type JournalClient, type JournalFetcher } from '../utils/client.ts'
export function useJournalApi(): JournalClient {
const config = useRuntimeConfig()
if (import.meta.server) {
const base = (config.journal as { apiBase: string }).apiBase
const incoming = useRequestHeaders(['x-forwarded-for'])['x-forwarded-for']
const peer = useRequestEvent()?.node?.req?.socket?.remoteAddress
const forwarded = [incoming, peer].filter((v): v is string => typeof v === 'string' && v !== '').join(', ')
const headers: Record<string, string> = forwarded ? { 'x-forwarded-for': forwarded } : {}
const fetcher: JournalFetcher = (path, query) => $fetch(path, { baseURL: base, query, headers })
return createJournalClient(fetcher)
}
const base = (config.public.journal as { apiBase: string }).apiBase
const fetcher: JournalFetcher = (path, query) => $fetch(path, { baseURL: base, query })
return createJournalClient(fetcher)
}