BREAKING CHANGE(XInvoice): Refactor XInvoice API for XML handling and PDF export by replacing deprecated methods (addXmlString and getParsedXmlData) with fromXml and loadXml, and by introducing a new ExportFormat type for type-safe export. Update tests accordingly.

This commit is contained in:
2025-03-20 14:39:32 +00:00
parent d954fb4768
commit 9510d851af
10 changed files with 174 additions and 72 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@fin.cx/xinvoice',
version: '2.0.0',
version: '3.0.0',
description: 'A TypeScript module for creating, manipulating, and embedding XML data within PDF files specifically tailored for xinvoice packages.'
}

View File

@ -400,8 +400,8 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
* @param format Target format (e.g., 'facturx', 'xrechnung')
* @returns XML string in the specified format
*/
public async exportXml(format: string = 'facturx'): Promise<string> {
format = format.toLowerCase();
public async exportXml(format: interfaces.ExportFormat = 'facturx'): Promise<string> {
format = format.toLowerCase() as interfaces.ExportFormat;
// Generate XML based on format
switch (format) {
@ -421,11 +421,11 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
/**
* Exports the invoice to PDF format with embedded XML
* @param format Target format (e.g., 'facturx', 'zugferd')
* @returns PDF buffer with embedded XML
* @param format Target format (e.g., 'facturx', 'zugferd', 'xrechnung', 'ubl')
* @returns PDF object with embedded XML
*/
public async exportPdf(format: string = 'facturx'): Promise<Uint8Array> {
format = format.toLowerCase();
public async exportPdf(format: interfaces.ExportFormat = 'facturx'): Promise<plugins.tsclass.business.IPdf> {
format = format.toLowerCase() as interfaces.ExportFormat;
if (!this.pdf) {
throw new Error('No PDF data available. Use loadPdf() first or set the pdf property.');
@ -484,7 +484,7 @@ export class XInvoice implements plugins.tsclass.business.ILetter {
buffer: modifiedPdfBytes
};
return modifiedPdfBytes;
return this.pdf;
} catch (error) {
console.error('Error embedding XML into PDF:', error);
throw error;

View File

@ -26,6 +26,7 @@ export type {
ValidationResult,
ValidationLevel,
InvoiceFormat,
ExportFormat,
XInvoiceOptions,
IValidator
} from './interfaces.js';

View File

@ -45,6 +45,13 @@ export enum InvoiceFormat {
FATTURAPA = 'fatturapa' // FatturaPA (Italian e-invoice format)
}
/**
* Formats supported for export operations
* This is a subset of InvoiceFormat that only includes formats
* that can be generated and embedded in PDFs
*/
export type ExportFormat = 'facturx' | 'zugferd' | 'xrechnung' | 'ubl';
/**
* Describes a validation level for invoice validation
*/