UNPKG

chromiumly

Version:

A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.

245 lines 8.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chromiumly = exports.TemplatesRoute = exports.ChromiumRoute = void 0; const gotenberg_1 = require("./gotenberg"); /** * Enum representing the available Chromium routes for conversion. * @enum {string} */ var ChromiumRoute; (function (ChromiumRoute) { ChromiumRoute["URL"] = "url"; ChromiumRoute["HTML"] = "html"; ChromiumRoute["MARKDOWN"] = "markdown"; })(ChromiumRoute || (exports.ChromiumRoute = ChromiumRoute = {})); /** * Enum representing the available routes for the PDF engine. * @enum {string} */ var PdfEngineRoute; (function (PdfEngineRoute) { PdfEngineRoute["CONVERT"] = "convert"; PdfEngineRoute["MERGE"] = "merge"; PdfEngineRoute["READ_METADATA"] = "metadata/read"; PdfEngineRoute["WRITE_METADATA"] = "metadata/write"; PdfEngineRoute["READ_BOOKMARKS"] = "bookmarks/read"; PdfEngineRoute["WRITE_BOOKMARKS"] = "bookmarks/write"; PdfEngineRoute["SPLIT"] = "split"; PdfEngineRoute["FLATTEN"] = "flatten"; PdfEngineRoute["ENCRYPT"] = "encrypt"; PdfEngineRoute["EMBED"] = "embed"; PdfEngineRoute["WATERMARK"] = "watermark"; PdfEngineRoute["STAMP"] = "stamp"; PdfEngineRoute["ROTATE"] = "rotate"; })(PdfEngineRoute || (PdfEngineRoute = {})); /** * Enum representing the available routes for LibreOffice. * @enum {string} */ var LibreOfficeRoute; (function (LibreOfficeRoute) { LibreOfficeRoute["CONVERT"] = "convert"; })(LibreOfficeRoute || (LibreOfficeRoute = {})); var SystemRoute; (function (SystemRoute) { SystemRoute["HEALTH"] = "health"; SystemRoute["VERSION"] = "version"; SystemRoute["DEBUG"] = "debug"; SystemRoute["PROMETHEUS_METRICS"] = "prometheus/metrics"; })(SystemRoute || (SystemRoute = {})); /** * Enum representing the available routes for Templates. * @enum {string} */ var TemplatesRoute; (function (TemplatesRoute) { TemplatesRoute["GENERATE"] = "generate"; })(TemplatesRoute || (exports.TemplatesRoute = TemplatesRoute = {})); /** * Class providing constants and routes for interacting with the Gotenberg service and related engines. */ class Chromiumly { /** * Default endpoint for the hosted Chromiumly API. */ static HOSTED_API_ENDPOINT = 'https://api.chromiumly.dev'; /** * The Gotenberg service endpoint. * @type {string} */ static gotenbergEndpoint = gotenberg_1.Gotenberg.endpoint; /** * The username for basic authentication with the Gotenberg service. * @type {string | undefined} */ static gotenbergApiBasicAuthUsername = gotenberg_1.Gotenberg.username; /** * The password for basic authentication with the Gotenberg service. * @type {string | undefined} */ static gotenbergApiBasicAuthPassword = gotenberg_1.Gotenberg.password; /** * The API key for X-Api-Key authentication with the Gotenberg service. * @type {string | undefined} */ static gotenbergApiKey = gotenberg_1.Gotenberg.apiKey; /** * Custom HTTP headers to be sent with each request. * @type {Record<string, string> | undefined} */ static customHttpHeaders; /** * The path for Chromium-related conversions. * @type {string} */ static CHROMIUM_CONVERT_PATH = 'forms/chromium/convert'; /** * The path for Chromium-related screenshots. * @type {string} */ static CHROMIUM_SCREENSHOT_PATH = 'forms/chromium/screenshot'; /** * The path for PDF engine-related operations. * @type {string} */ static PDF_ENGINES_PATH = 'forms/pdfengines'; /** * The path for LibreOffice-related conversions. * @type {string} */ static LIBRE_OFFICE_PATH = 'forms/libreoffice'; /** * The path for Templates-related operations. * @type {string} */ static TEMPLATES_PATH = 'templates'; /** * Routes for Chromium conversions. * @type {Object} */ static CHROMIUM_ROUTES = { url: ChromiumRoute.URL, html: ChromiumRoute.HTML, markdown: ChromiumRoute.MARKDOWN }; /** * Routes for PDF engine operations. * @type {Object} */ static PDF_ENGINE_ROUTES = { convert: PdfEngineRoute.CONVERT, merge: PdfEngineRoute.MERGE, readMetadata: PdfEngineRoute.READ_METADATA, writeMetadata: PdfEngineRoute.WRITE_METADATA, readBookmarks: PdfEngineRoute.READ_BOOKMARKS, writeBookmarks: PdfEngineRoute.WRITE_BOOKMARKS, split: PdfEngineRoute.SPLIT, flatten: PdfEngineRoute.FLATTEN, encrypt: PdfEngineRoute.ENCRYPT, embed: PdfEngineRoute.EMBED, watermark: PdfEngineRoute.WATERMARK, stamp: PdfEngineRoute.STAMP, rotate: PdfEngineRoute.ROTATE }; /** * Routes for LibreOffice conversions. * @type {Object} */ static LIBRE_OFFICE_ROUTES = { convert: LibreOfficeRoute.CONVERT }; /** * Routes for system endpoints. * @type {Object} */ static SYSTEM_ROUTES = { health: SystemRoute.HEALTH, version: SystemRoute.VERSION, debug: SystemRoute.DEBUG, prometheusMetrics: SystemRoute.PROMETHEUS_METRICS }; /** * Routes for Templates generation. * @type {Object} */ static TEMPLATES_ROUTES = { generate: TemplatesRoute.GENERATE }; /** * Configures the Gotenberg service endpoint and other optional parameters. * @param {Object} config - Configuration object. * @param {string} [config.endpoint] - The Gotenberg service endpoint. Optional when using the hosted API with an API key. * @param {string} [config.username] - The username for basic authentication. * @param {string} [config.password] - The password for basic authentication. * @param {string} [config.apiKey] - The API key for X-Api-Key authentication. * @param {Record<string, string>} [config.customHttpHeaders] - Custom HTTP headers to be sent with each request. */ static configure(config) { if (config.endpoint !== undefined) { this.gotenbergEndpoint = config.endpoint; } if (config.username !== undefined) { this.gotenbergApiBasicAuthUsername = config.username; } if (config.password !== undefined) { this.gotenbergApiBasicAuthPassword = config.password; } if (config.apiKey !== undefined) { this.gotenbergApiKey = config.apiKey; if (!this.gotenbergEndpoint && config.endpoint === undefined) { this.gotenbergEndpoint = this.HOSTED_API_ENDPOINT; } } if (config.customHttpHeaders !== undefined) { this.customHttpHeaders = config.customHttpHeaders; } } /** * Gets the Gotenberg service endpoint. * @returns {string} */ static getGotenbergEndpoint() { if (!this.gotenbergEndpoint) { const endpointFromEnv = gotenberg_1.Gotenberg.endpoint; if (endpointFromEnv) { this.gotenbergEndpoint = endpointFromEnv; } else if (this.gotenbergApiKey) { this.gotenbergEndpoint = this.HOSTED_API_ENDPOINT; } } if (!this.gotenbergEndpoint) throw new Error('Gotenberg endpoint is not set. Please ensure that the Gotenberg service endpoint is configured correctly in your environment variables or through the configure method.'); return this.gotenbergEndpoint; } /** * Gets the username for basic authentication. * @returns {string | undefined} */ static getGotenbergApiBasicAuthUsername() { return this.gotenbergApiBasicAuthUsername; } /** * Gets the password for basic authentication. * @returns {string | undefined} */ static getGotenbergApiBasicAuthPassword() { return this.gotenbergApiBasicAuthPassword; } /** * Gets the API key for X-Api-Key authentication. * @returns {string | undefined} */ static getGotenbergApiKey() { return this.gotenbergApiKey; } /** * Gets the custom HTTP headers. * @returns {Record<string, string> | undefined} */ static getCustomHttpHeaders() { return this.customHttpHeaders; } } exports.Chromiumly = Chromiumly; //# sourceMappingURL=main.config.js.map