UNPKG

umay-render

Version:

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

40 lines (39 loc) 1.67 kB
// src/umay-sdk.ts import { HttpClient } from "./http-client"; import { loadConfig } from "./config"; import { z } from "zod"; import { ConversionRequestSchema, } from "./schemas"; import { UmayError, ErrorCodes } from "./errors"; export class UmaySDK { constructor(config) { const mergedConfig = loadConfig(process.env); Object.assign(mergedConfig, config); this.config = mergedConfig; this.httpClient = new HttpClient(this.config); } async render(request) { let validatedPayload; try { validatedPayload = ConversionRequestSchema.parse(request); } catch (error) { if (error instanceof z.ZodError) { const validationErrors = error.errors .map((e) => `${e.path.join(".")}: ${e.message}`) .join("; "); console.error("SDK Input Validation Error:", error.flatten()); throw new UmayError(ErrorCodes.SDK_INVALID_INPUT, `Invalid input provided to the SDK: ${validationErrors}`, error.flatten()); } console.error("Unexpected error during SDK input validation:", error); let errorMessage = "An unexpected error occurred during SDK input processing."; if (error instanceof Error) { errorMessage = `Unexpected SDK error: ${error.message}`; } throw new UmayError(ErrorCodes.SDK_UNEXPECTED_PROCESSING_ERROR, errorMessage, error); } const endpoint = "/render"; return this.httpClient.request(endpoint, validatedPayload); } } export * from "./schemas"; export * from "./errors";