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) 3.96 kB
var C=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var T=(o,t,r,s)=>{for(var i=s>1?void 0:s?I(t,r):t,d=o.length-1,R;d>=0;d--)(R=o[d])&&(i=(s?R(t,r,i):R(i))||i);return s&&i&&C(t,r,i),i};var m=(o,t,r)=>new Promise((s,i)=>{var d=n=>{try{l(r.next(n))}catch(P){i(P)}},R=n=>{try{l(r.throw(n))}catch(P){i(P)}},l=n=>n.done?s(n.value):Promise.resolve(n.value).then(d,R);l((r=r.apply(o,t)).next())});import y from"ajv";import V from"ajv-formats";import b from"axios";import{plainToInstance as B}from"class-transformer";import{plainToInstance as D,Transform as u,Type as S}from"class-transformer";var p=class{constructor(t,r,s,i){this.id="";this.description="";this.quantity=1;this.unit="piece";this.currency="EUR";this.taxRate=0;this.id=t,this.price=r,this.description=i||"",this.unit=s||"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,r){Object.assign(this,{discount:t,discountType:r}),Object.setPrototypeOf(this,E.prototype)}};var E=class extends p{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 a=class{constructor(){this.paymentType="sepa"}};var x={sepa_mandate:a,sepa:a,swift:a,credit_card:a,cash:a};function O(o){return x[o]}var e=class{constructor(t,r){this.createdAt=new Date;this.items=[];this.id=t,this.servicePeriod=r}get netPriceSum(){return this.items.reduce((t,r)=>t+r.netPrice,0)}get taxSum(){return this.items.reduce((t,r)=>t+r.tax,0)}get grossPriceSum(){return this.items.reduce((t,r)=>t+r.grossPrice,0)}};T([S(()=>Date)],e.prototype,"createdAt",2),T([S(()=>Date)],e.prototype,"servicePeriod",2),T([u(({value:t})=>D(O(t.paymentType),t))],e.prototype,"paymentInformation",2),T([u(({value:t})=>t.map(r=>"discount"in r?D(E,r):D(p,r)))],e.prototype,"items",2);var f=class extends e{constructor(){super(...arguments);this.type="partial_invoice"}};var c=class extends e{constructor(){super(...arguments);this.type="recurring_invoice"}};var _=class extends e{constructor(){super(...arguments);this.type="invoice"}};var N=class{constructor(){this.type="cancellation_invoice"}};var h=class extends Error{constructor(t){super(t),this.name="InvalidOIFException"}};import"reflect-metadata";var g=["latest","2025.01"],L=class o{constructor(){this._version=g[1];this._invoiceTypeMap={partial_invoice:f,recurring_invoice:c,invoice:_,cancellation_invoice:N};this.ajv=new y({removeAdditional:!0}),V(this.ajv)}get isHeated(){return!!this._validateFn}static _getInstance(){return o._instance||(o._instance=new o),o._instance}_load(){return m(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 r=yield b.get(t).then(s=>s.data);this._version=r.definitions.SCHEMA_VERSION.const,this._validateFn=this.ajv.compile(r)})}_validate(t){return m(this,null,function*(){return this.isHeated||(yield this._load()),this._validateFn(t)?B(this._invoiceTypeMap[t.type],t):null})}static setVersion(t){return m(this,null,function*(){let r=o._getInstance();r._version=t,yield r._load()})}static stringify(t){return m(this,null,function*(){let r=o._getInstance(),s=JSON.parse(JSON.stringify(t));return s.version=r._version,s._version==="latest"&&(s._version=g[1]),(yield r._validate(s))||console.error(r.ajv.errorsText(r._validateFn.errors)),JSON.stringify(s)})}static parse(t){return m(this,null,function*(){let r=o._getInstance(),s=yield r._validate(JSON.parse(t));if(!s)throw new h(r.ajv.errorsText(r._validateFn.errors));return s})}};export{L as SchemaProvider,g as SchemaVersions};