picturarium/src/api/caption.ts
2026-05-15 14:45:06 +02:00

33 lines
842 B
TypeScript

import { z } from "zod"
import { fetchJsonSafeWith } from "../request"
import type { RequestError } from "../request"
import type { Result } from "../result"
const CAPTION_API_URL = "https://picsum-caption-worker.meatbagoverclocked.workers.dev/"
const captionResponseSchema = z.object({
description: z.string(),
})
export type CaptionResponse = z.infer<typeof captionResponseSchema>
export type GetCaptionParams = {
imageUrl: string
maxWords: number
}
export async function getCaption({
imageUrl,
maxWords,
}: GetCaptionParams): Promise<Result<CaptionResponse, RequestError>> {
return fetchJsonSafeWith(CAPTION_API_URL, (json) => captionResponseSchema.parse(json), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
imageUrl,
maxWords,
}),
})
}