chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
44 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Templates = void 0;
const main_config_1 = require("../../main.config");
const templates_validators_1 = require("../validators/templates.validators");
const generator_1 = require("./generator");
class Templates extends generator_1.Generator {
constructor() {
super(main_config_1.TemplatesRoute.GENERATE);
}
async generate(request, options) {
const apiKey = main_config_1.Chromiumly.getGotenbergApiKey();
if (!apiKey) {
throw new Error('Templates requires an API key. Please configure it via Chromiumly.configure({ apiKey: "..." }).');
}
if (options?.validate) {
const validator = templates_validators_1.templateValidators[request.type];
if (validator && !validator(request.data)) {
throw new Error(`Invalid template data for type "${request.type}". Please ensure it matches the expected structure.`);
}
}
const response = await fetch(this.endpoint, {
method: 'POST',
headers: {
'X-Api-Key': apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
});
if (!response.ok) {
const body = await response.text();
const trace = response.headers.get('gotenberg-trace');
throw new Error(`Gotenberg API Error:\n` +
`Endpoint: ${this.endpoint}\n` +
`Status: ${response.status} ${response.statusText}\n` +
`Trace: ${trace || 'No trace'}\n` +
`Body: ${body}`);
}
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
}
}
exports.Templates = Templates;
//# sourceMappingURL=templates.js.map