flockstr/lib/fetchers/metadata.ts
2023-10-15 08:40:36 -04:00

32 lines
789 B
TypeScript

import { z } from "zod";
import { validateUrl } from "@/lib/utils";
import { createZodFetcher } from "zod-fetch";
const fetchWithZod = createZodFetcher();
const metadataSchema = z.object({
title: z.string(),
description: z.string().optional(),
image: z.string().optional(),
creator: z.string().optional(),
type: z.string().optional(),
"theme-color": z.string().optional(),
});
const metadataSchemaResponse = z.object({
data: metadataSchema,
});
export function fetchMetadata(url: string) {
if (!validateUrl(url)) return;
return fetchWithZod(
// The schema you want to validate with
metadataSchemaResponse,
// Any parameters you would usually pass to fetch
"/api/metadata",
{
method: "POST",
body: JSON.stringify({ url }),
},
);
}