UNPKG

@inbridge/oif-ts

Version:

TypeScript integration for the Open Invoice Format (OIF) schema. Easily create OIF-compliant PDFs and parse/validate JSON.

2 lines (1 loc) 5.26 kB
var y=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var T=(e,t,o,r)=>{for(var i=r>1?void 0:r?M(t,o):t,s=e.length-1,m;s>=0;s--)(m=e[s])&&(i=(r?m(t,o,i):m(i))||i);return r&&i&&y(t,o,i),i};var n=(e,t,o)=>new Promise((r,i)=>{var s=p=>{try{R(o.next(p))}catch(g){i(g)}},m=p=>{try{R(o.throw(p))}catch(g){i(g)}},R=p=>p.done?r(p.value):Promise.resolve(p.value).then(s,m);R((o=o.apply(e,t)).next())});import{decodePDFRawStream as K,PDFArray as j,PDFDict as S,PDFDocument as L,PDFName as d,PDFStream as G}from"pdf-lib";import V from"ajv";import v from"ajv-formats";import B from"axios";import{plainToInstance as H}from"class-transformer";import{plainToInstance as O,Transform as x,Type as F}from"class-transformer";var E=class{constructor(t,o,r,i){this.id="";this.description="";this.quantity=1;this.unit="piece";this.currency="EUR";this.taxRate=0;this.id=t,this.price=o,this.description=i||"",this.unit=r||"piece"}get netPrice(){return this.price*this.quantity}get tax(){return this.netPrice*(this.taxRate/100)}get grossPrice(){return this.netPrice+this.tax}addDiscount(t,o){Object.assign(this,{discount:t,discountType:o}),Object.setPrototypeOf(this,f.prototype)}};var f=class extends E{constructor(){super(...arguments);this.discount=0;this.discountType="fixed";this.discountReason=""}get discountAmount(){return this.discountType==="percentage"?this.price*this.quantity*(this.discount/100):this.discount}get netPrice(){return this.price*this.quantity-this.discountAmount}};import"reflect-metadata";var c=class{constructor(){this.paymentType="sepa"}};var b={sepa_mandate:c,sepa:c,swift:c,credit_card:c,cash:c};function I(e){return b[e]}var a=class{constructor(t,o){this.createdAt=new Date;this.items=[];this.id=t,this.servicePeriod=o}get netPriceSum(){return this.items.reduce((t,o)=>t+o.netPrice,0)}get taxSum(){return this.items.reduce((t,o)=>t+o.tax,0)}get grossPriceSum(){return this.items.reduce((t,o)=>t+o.grossPrice,0)}};T([F(()=>Date)],a.prototype,"createdAt",2),T([F(()=>Date)],a.prototype,"servicePeriod",2),T([x(({value:t})=>O(I(t.paymentType),t))],a.prototype,"paymentInformation",2),T([x(({value:t})=>t.map(o=>"discount"in o?O(f,o):O(E,o)))],a.prototype,"items",2);var l=class extends a{constructor(){super(...arguments);this.type="partial_invoice"}};var u=class extends a{constructor(){super(...arguments);this.type="recurring_invoice"}};var D=class extends a{constructor(){super(...arguments);this.type="invoice"}};var P=class{constructor(){this.type="cancellation_invoice"}};var N=class extends Error{constructor(t){super(t),this.name="InvalidOIFException"}};import"reflect-metadata";var C=["latest","2025.01"],A=class e{constructor(){this._version=C[1];this._invoiceTypeMap={partial_invoice:l,recurring_invoice:u,invoice:D,cancellation_invoice:P};this.ajv=new V({removeAdditional:!0}),v(this.ajv)}get isHeated(){return!!this._validateFn}static _getInstance(){return e._instance||(e._instance=new e),e._instance}_load(){return n(this,null,function*(){let t=`https://github.com/inbridgeio/open-invoice-format/releases/download/${this._version}/schema.json`;this._version==="latest"&&(t="https://raw.githubusercontent.com/inbridgeio/open-invoice-format/refs/heads/main/schema.json");let o=yield B.get(t).then(r=>r.data);this._version=o.definitions.SCHEMA_VERSION.const,this._validateFn=this.ajv.compile(o)})}_validate(t){return n(this,null,function*(){return this.isHeated||(yield this._load()),this._validateFn(t)?H(this._invoiceTypeMap[t.type],t):null})}static setVersion(t){return n(this,null,function*(){let o=e._getInstance();o._version=t,yield o._load()})}static stringify(t){return n(this,null,function*(){let o=e._getInstance(),r=JSON.parse(JSON.stringify(t));return r.version=o._version,r._version==="latest"&&(r._version=C[1]),(yield o._validate(r))||console.error(o.ajv.errorsText(o._validateFn.errors)),JSON.stringify(r)})}static parse(t){return n(this,null,function*(){let o=e._getInstance(),r=yield o._validate(JSON.parse(t));if(!r)throw new N(o.ajv.errorsText(o._validateFn.errors));return r})}};var _=class extends Error{constructor(t){super(t),this.name="FileNotFoundException"}};import"reflect-metadata";function $t(e,t){return n(this,null,function*(){let o=yield L.load(t),r=yield A.stringify(e);return yield o.attach(Buffer.from(r),"inbridge-oif.json"),o.setKeywords(["OIF Invoice generated. More under https://github.com/inbridgeio/open-invoice-format","Created with OIF Integration Library"]),o.getCreationDate()||o.setCreationDate(new Date),o.save()})}function Ut(e){return n(this,null,function*(){let t=yield L.load(e),r=(yield Y(t)).find(({fileName:m})=>m.decodeText()==="inbridge-oif.json");if(!r)throw new _("Couldn't extract OIF attachment in the PDF");let i=r.fileSpec.lookup(d.of("EF"),S).lookup(d.of("F"),G),s=new TextDecoder().decode(K(i).decode());return A.parse(s)})}function Y(e){return n(this,null,function*(){if(!e.catalog.has(d.of("Names")))return[];let t=e.catalog.lookup(d.of("Names"),S);if(!t.has(d.of("EmbeddedFiles")))return[];let o=t.lookup(d.of("EmbeddedFiles"),S);if(!o.has(d.of("Names")))return[];let r=o.lookup(d.of("Names"),j),i=[];for(let s=0,m=r.size();s<m;s+=2){let R=r.lookup(s),p=r.lookup(s+1,S);i.push({fileName:R,fileSpec:p})}return i})}export{$t as addOIFToPdf,Ut as getOIFFromPdf};