UNPKG

umay-render

Version:

Free, high-performance HTML to PDF and HTML to Image conversion SDK for both browser and Node.js

166 lines (165 loc) 6.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConversionRequestSchema = exports.ScreenshotOutputOptionsSchema = exports.PdfOutputOptionsSchema = exports.PageSetupOptionsSchema = void 0; // src/schemas.ts const zod_1 = require("zod"); // Opsiyonel: SDK'da puppeteer tiplerine bağımlılık istemiyorsanız, // cookie tipi için daha genel bir yapı kullanabilirsiniz veya Protocol tipini kopyalayabilirsiniz. // Şimdilik Zod'un yapısal kontrolüne güvenelim. // import type { Protocol } from 'puppeteer'; // Tekrarkullanılabilir Şema: Puppeteer Yaşam Döngüsü Olayları const LifeCycleEventSchema = zod_1.z.enum([ "load", "domcontentloaded", "networkidle0", "networkidle2", ]); // Şema: Sayfa Kurulum Seçenekleri (Render öncesi uygulanır) exports.PageSetupOptionsSchema = zod_1.z .object({ viewport: zod_1.z .object({ width: zod_1.z.number().int().positive(), height: zod_1.z.number().int().positive(), deviceScaleFactor: zod_1.z.number().positive().optional(), isMobile: zod_1.z.boolean().optional(), hasTouch: zod_1.z.boolean().optional(), isLandscape: zod_1.z.boolean().optional(), }) .optional(), emulateMediaType: zod_1.z.enum(["screen", "print"]).optional(), waitForSelector: zod_1.z.string().optional(), waitForTimeout: zod_1.z.number().int().positive().optional().default(30000), waitUntil: zod_1.z .union([LifeCycleEventSchema, zod_1.z.array(LifeCycleEventSchema)]) .optional() .default("networkidle0"), cookies: zod_1.z .array(zod_1.z.object({ name: zod_1.z.string(), value: zod_1.z.string(), url: zod_1.z.string().url().optional(), domain: zod_1.z.string().optional(), path: zod_1.z.string().optional(), expires: zod_1.z.number().optional(), // epoch time httpOnly: zod_1.z.boolean().optional(), secure: zod_1.z.boolean().optional(), sameSite: zod_1.z.enum(["Strict", "Lax", "None"]).optional(), }) // .satisfies(z.ZodType<Protocol.Network.CookieParam>) // Puppeteer bağımlılığı olmadan kullanmak zor ) .optional(), extraHTTPHeaders: zod_1.z.record(zod_1.z.string()).optional(), javascriptEnabled: zod_1.z.boolean().optional(), userAgent: zod_1.z.string().optional(), evaluateScript: zod_1.z.string().optional(), // SDK kullanıcısını güvenlik riskleri konusunda uyarın }) .strict() .optional(); // Şema: PDF Çıktı Seçenekleri (Puppeteer PDFOptions'a benzer, 'path' hariç) exports.PdfOutputOptionsSchema = zod_1.z .object({ scale: zod_1.z.number().positive().optional(), displayHeaderFooter: zod_1.z.boolean().optional(), headerTemplate: zod_1.z.string().optional(), footerTemplate: zod_1.z.string().optional(), printBackground: zod_1.z.boolean().optional(), // Varsayılan backend'de ayarlı landscape: zod_1.z.boolean().optional(), pageRanges: zod_1.z.string().optional(), format: zod_1.z .enum([ "Letter", "Legal", "Tabloid", "Ledger", "A0", "A1", "A2", "A3", "A4", "A5", "A6", ]) .optional(), // Varsayılan backend'de ayarlı width: zod_1.z.string().or(zod_1.z.number()).optional(), height: zod_1.z.string().or(zod_1.z.number()).optional(), margin: zod_1.z .object({ top: zod_1.z.string().or(zod_1.z.number()).optional(), right: zod_1.z.string().or(zod_1.z.number()).optional(), bottom: zod_1.z.string().or(zod_1.z.number()).optional(), left: zod_1.z.string().or(zod_1.z.number()).optional(), }) .optional(), // Varsayılan backend'de ayarlı preferCSSPageSize: zod_1.z.boolean().optional(), omitBackground: zod_1.z.boolean().optional(), tagged: zod_1.z.boolean().optional(), timeout: zod_1.z.number().int().nonnegative().optional(), }) .strict() .optional(); // Şema: Ekran Görüntüsü Çıktı Seçenekleri (Puppeteer ScreenshotOptions'a benzer, 'path', 'type' hariç) exports.ScreenshotOutputOptionsSchema = zod_1.z .object({ quality: zod_1.z.number().int().min(0).max(100).optional(), // Sadece jpeg/webp için fullPage: zod_1.z.boolean().optional(), // Varsayılan backend'de ayarlı clip: zod_1.z .object({ x: zod_1.z.number(), y: zod_1.z.number(), width: zod_1.z.number().positive(), height: zod_1.z.number().positive(), }) .optional(), omitBackground: zod_1.z.boolean().optional(), encoding: zod_1.z.enum(["base64", "binary"]).optional(), // API genellikle binary döner captureBeyondViewport: zod_1.z.boolean().optional(), fromSurface: zod_1.z.boolean().optional(), timeout: zod_1.z.number().int().nonnegative().optional(), }) .strict() .optional(); // --- Ana Dönüştürme İstek Şeması (Backend ile Aynı) --- exports.ConversionRequestSchema = zod_1.z .object({ // Girdi Kaynağı (en az biri gerekli) html: zod_1.z.string().optional(), url: zod_1.z.string().url().optional(), // Çıktı Formatı (Gerekli) outputFormat: zod_1.z.enum(["pdf", "png", "jpeg", "webp"]), // İsteğe Bağlı Dosya Adı filename: zod_1.z.string().optional(), // Sayfa Kurulum Seçenekleri pageSetupOptions: exports.PageSetupOptionsSchema, // Opsiyonel blok // Çıktıya Özel Seçenekler pdfOptions: exports.PdfOutputOptionsSchema, // Opsiyonel blok screenshotOptions: exports.ScreenshotOutputOptionsSchema, // Opsiyonel blok }) // Backend'deki refine kuralları SDK tarafında da uygulanabilir veya // SDK kullanıcısının doğru input sağlamasına güvenilebilir. // Şimdilik eklemeyelim, SDK'nın görevi isteği göndermek. // Backend zaten tekrar kontrol edecek. .refine((data) => data.html || data.url, { message: "SDK Input Error: Either 'html' or 'url' must be provided", path: ["html"], }) .refine((data) => !(data.html && data.url), { message: "SDK Input Error: Provide either 'html' or 'url', not both", path: ["html"], }) // Kalite kontrolünü de ekleyelim, SDK kullanıcısına yardımcı olur. .refine((data) => { if (data.outputFormat === "png" && data.screenshotOptions?.quality !== undefined) return false; return true; }, { message: "SDK Input Error: 'quality' option is not applicable for 'png' output format", path: ["screenshotOptions", "quality"], }); // --- ESKİ ŞEMALARI SİL --- // export const PdfOptionsSchema = ... // export const ImageOptionsSchema = ... // export type PdfOptions = ... // export type ImageOptions = ... // export const ViewportSchema = ...