UNPKG

@estarlincito/utils

Version:

A collection of utility functions designed to simplify and speed up development tasks in JavaScript and TypeScript projects.

151 lines (150 loc) 4.08 kB
import { t } from "tyne"; const h = t.object({ alt: t.string(), height: t.number().optional(), url: t.string(), width: t.number().optional() }), i = t.object({ description: t.string(), icons: t.object({ icon: t.string(), shortcut: t.string() }).optional(), images: t.array(h), locale: t.string(), metadataBase: t.url().optional(), siteName: t.string(), title: t.string(), url: t.string() }), l = t.object({ authors: t.array(t.string()), isbn: t.string(), releaseDate: t.string(), tags: t.array(t.string()), ...i.shape }), m = t.object({ audio: t.url().optional(), authors: t.array(t.string()), modifiedTime: t.string(), publishedTime: t.string(), section: t.string(), tags: t.array(t.string()), ...i.shape }), o = t.object({ article: m, book: l, website: i }), u = (s) => t.object({ description: t.string(), icons: i.shape.icons, metadataBase: t.instanceof(URL), openGraph: t.object({ ...o.shape[s].shape, type: t.literal(s) }), title: t.string() }), j = { Article: m, Book: l, Images: h, OpenGraph: o, Returns: u, Website: i }; class S { static generate(e, a) { const d = e.images.map((r, g) => { const n = { ...r }; return g === 0 && r.height !== void 0 && (n.height = 600), g === 0 && r.width !== void 0 && (n.width = 800), n; }), { metadataBase: c, icons: b, ...p } = e; return u(a).validate({ description: e.description, icons: b ?? { icon: "/favicon.ico", shortcut: "/shortcut-icon.png" }, metadataBase: c ? new URL(c) : new URL(new URL(e.url).origin), openGraph: { ...o.shape[a].validate({ ...p, images: d }), type: a }, title: e.title }); } /** * Generates metadata specifically for an article. * * @param meta - The article metadata. * @returns - The generated metadata for the article. * * @example * const articleMeta: Article = { * title: 'Example Article Title', * description: 'Description for the article.', * url: 'https://example.com/article', * siteName: 'Example Site', * locale: 'en-US', * images: [{ url: '/assets/image1.jpg', alt: 'Article Image', width: 800, height: 600 }], * section: 'Tech News', * publishedTime: '2025-02-12T15:09:16Z', * modifiedTime: '2025-02-12T16:00:00Z', * authors: ['Author One'], * audio: new URL('/assets/audio.mp3'), * }; * const metadata = GenerateMetadata.article(articleMeta); */ static article(e) { return this.generate(e, "article"); } /** * Generates metadata specifically for a website. * * @param meta - The website metadata. * @returns - The generated metadata for the website. * * @example * const websiteMeta: Website = { * title: 'Example Website Title', * description: 'Description for the website.', * url: 'https://example.com/website', * siteName: 'Example Site', * locale: 'en-US', * images: [{ url: '/assets/image1.jpg', alt: 'Website Image', width: 800, height: 600 }], * }; * const metadata = GenerateMetadata.website(websiteMeta); */ static website(e) { return this.generate(e, "website"); } /** * Generates metadata specifically for a book. * * @param meta - The book metadata. * @returns - The generated metadata for the book. * * @example * const bookMeta: Book = { * title: 'Example Book Title', * description: 'Description for the book.', * url: 'https://example.com/book', * siteName: 'Example Site', * locale: 'en-US', * images: [{ url: '/assets/image1.jpg', alt: 'Book Cover Image', width: 800, height: 600 }], * isbn: '978-3-16-148410-0', * authors: ['Author One', 'Author Two'], * releaseDate: '2025-02-12', * tags: ['Fiction', 'Adventure'], * }; * const metadata = GenerateMetadata.book(bookMeta); */ static book(e) { return this.generate(e, "book"); } } export { S as GenerateMetadata, j as Schemas };