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) 10.4 kB
var Q=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var n=(r,s,a,p)=>{for(var f=p>1?void 0:p?j(s,a):s,e=r.length-1,A;e>=0;e--)(A=r[e])&&(f=(p?A(s,a,f):A(f))||f);return p&&f&&Q(s,a,f),f};var T=(r,s,a)=>new Promise((p,f)=>{var e=P=>{try{D(a.next(P))}catch(_){f(_)}},A=P=>{try{D(a.throw(P))}catch(_){f(_)}},D=P=>P.done?p(P.value):Promise.resolve(P.value).then(e,A);D((a=a.apply(r,s)).next())});import{decodePDFRawStream as d,PDFArray as u,PDFDict as G,PDFDocument as w,PDFName as R,PDFStream as y}from"pdf-lib";import k from"ajv";import q from"ajv-formats";import z from"axios";import{plainToInstance as $}from"class-transformer";var I=(f=>(f.INVOICE="invoice",f.PARTIAL_INVOICE="partial_invoice",f.RECURRING_INVOICE="recurring_invoice",f.CANCELLATION="cancellation_invoice",f))(I||{});import{plainToInstance as g,Transform as v,Type as b}from"class-transformer";var K=(a=>(a.FIXED="fixed",a.PERCENTAGE="percentage",a))(K||{});var o=(m=>(m.PIECE="piece",m.FLAT_RATE="flat_rate",m.TIME_UNIT="time_unit",m.PROJECT_DAY="project_day",m.METER="m",m.CENTIMETER="cm",m.MILLIMETER="mm",m.KILOMETER="km",m.INCH="in",m.FOOT="ft",m.YARD="yd",m.MILE="mi",m.KILOGRAM="kg",m.GRAM="g",m.MILLIGRAM="mg",m.TON="t",m.POUND="lb",m.OUNCE="oz",m.LITER="L",m.MILLILITER="ml",m.GALLON="gal",m.QUART="qt",m.PINT="pt",m.CUP="cup",m.FLUID_OUNCE="fl oz",m.MILLISECOND="ms",m.SECOND="s",m.MINUTE="m",m.HOUR="h",m.DAY="d",m.WEEK="w",m.MONTH="M",m.YEAR="y",m.SQUARE_METER="m\xB2",m.SQUARE_CENTIMETER="cm\xB2",m.SQUARE_KILOMETER="km\xB2",m.SQUARE_INCH="in\xB2",m.SQUARE_FOOT="ft\xB2",m.SQUARE_YARD="yd\xB2",m.SQUARE_MILE="mi\xB2",m.CUBIC_METER="m\xB3",m.CUBIC_CENTIMETER="cm\xB3",m.CUBIC_KILOMETER="km\xB3",m.CUBIC_INCH="in\xB3",m.CUBIC_FOOT="ft\xB3",m.CUBIC_YARD="yd\xB3",m.CUBIC_MILE="mi\xB3",m.KILOMETER_PER_HOUR="km/h",m.MILE_PER_HOUR="mi/h",m.METERS_PER_SECOND="m/s",m.FEET_PER_SECOND="ft/s",m))(o||{});var H=(t=>(t.USD="USD",t.EUR="EUR",t.GBP="GBP",t.JPY="JPY",t.CHF="CHF",t.AUD="AUD",t.CAD="CAD",t.NZD="NZD",t.CNY="CNY",t.INR="INR",t.RUB="RUB",t.BRL="BRL",t.MXN="MXN",t.ZAR="ZAR",t.SEK="SEK",t.NOK="NOK",t.DKK="DKK",t.PLN="PLN",t.TRY="TRY",t.AED="AED",t.SAR="SAR",t.HKD="HKD",t.KRW="KRW",t.SGD="SGD",t.MYR="MYR",t.THB="THB",t.IDR="IDR",t.ARS="ARS",t.BTC="BTC",t.ETH="ETH",t.SOL="SOL",t.DOGE="DOGE",t.LTC="LTC",t.BCH="BCH",t.XRP="XRP",t.ADA="ADA",t.DOT="DOT",t.MATIC="MATIC",t.UNI="UNI",t.LINK="LINK",t.AVAX="AVAX",t))(H||{});var N=class{constructor(s,a,p,f){this.id="";this.description="";this.quantity=1;this.unit="piece";this.currency="EUR";this.taxRate=0;this.id=s,this.price=a,this.description=f||"",this.unit=p||"piece"}get netPrice(){return this.price*this.quantity}get tax(){return this.netPrice*(this.taxRate/100)}get grossPrice(){return this.netPrice+this.tax}addDiscount(s,a){Object.assign(this,{discount:s,discountType:a}),Object.setPrototypeOf(this,M.prototype)}};var M=class extends N{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 S=(e=>(e.SEPA_MANDATE="sepa_mandate",e.SEPA="sepa",e.SWIFT="swift",e.CREDIT_CARD="credit_card",e.CASH="cash",e))(S||{});var c=class{constructor(){this.paymentType="sepa"}};var X={sepa_mandate:c,sepa:c,swift:c,credit_card:c,cash:c};function V(r){return X[r]}var E=class{constructor(s,a){this.createdAt=new Date;this.items=[];this.id=s,this.servicePeriod=a}get netPriceSum(){return this.items.reduce((s,a)=>s+a.netPrice,0)}get taxSum(){return this.items.reduce((s,a)=>s+a.tax,0)}get grossPriceSum(){return this.items.reduce((s,a)=>s+a.grossPrice,0)}};n([b(()=>Date)],E.prototype,"createdAt",2),n([b(()=>Date)],E.prototype,"servicePeriod",2),n([v(({value:s})=>g(V(s.paymentType),s))],E.prototype,"paymentInformation",2),n([v(({value:s})=>s.map(a=>"discount"in a?g(M,a):g(N,a)))],E.prototype,"items",2);var x=class extends E{constructor(){super(...arguments);this.type="partial_invoice"}};var O=class extends E{constructor(){super(...arguments);this.type="recurring_invoice"}};var B=class extends E{constructor(){super(...arguments);this.type="invoice"}};var L=class{constructor(){this.type="cancellation_invoice"}};var h=class extends Error{constructor(s){super(s),this.name="InvalidOIFException"}};import"reflect-metadata";var Y=["latest","2025.01"],l=class r{constructor(){this._version=Y[1];this._invoiceTypeMap={partial_invoice:x,recurring_invoice:O,invoice:B,cancellation_invoice:L};this.ajv=new k({removeAdditional:!0}),q(this.ajv)}get isHeated(){return!!this._validateFn}static _getInstance(){return r._instance||(r._instance=new r),r._instance}_load(){return T(this,null,function*(){let s=`https://github.com/inbridgeio/open-invoice-format/releases/download/${this._version}/schema.json`;this._version==="latest"&&(s="https://raw.githubusercontent.com/inbridgeio/open-invoice-format/refs/heads/main/schema.json");let a=yield z.get(s).then(p=>p.data);this._version=a.definitions.SCHEMA_VERSION.const,this._validateFn=this.ajv.compile(a)})}_validate(s){return T(this,null,function*(){return this.isHeated||(yield this._load()),this._validateFn(s)?$(this._invoiceTypeMap[s.type],s):null})}static setVersion(s){return T(this,null,function*(){let a=r._getInstance();a._version=s,yield a._load()})}static stringify(s){return T(this,null,function*(){let a=r._getInstance(),p=JSON.parse(JSON.stringify(s));return p.version=a._version,p._version==="latest"&&(p._version=Y[1]),(yield a._validate(p))||console.error(a.ajv.errorsText(a._validateFn.errors)),JSON.stringify(p)})}static parse(s){return T(this,null,function*(){let a=r._getInstance(),p=yield a._validate(JSON.parse(s));if(!p)throw new h(a.ajv.errorsText(a._validateFn.errors));return p})}};var F=class extends Error{constructor(s){super(s),this.name="FileNotFoundException"}};import"reflect-metadata";function fa(r,s){return T(this,null,function*(){let a=yield w.load(s),p=yield l.stringify(r);return yield a.attach(Buffer.from(p),"inbridge-oif.json"),a.setKeywords(["OIF Invoice generated. More under https://github.com/inbridgeio/open-invoice-format","Created with OIF Integration Library"]),a.getCreationDate()||a.setCreationDate(new Date),a.save()})}function ea(r){return T(this,null,function*(){let s=yield w.load(r),p=(yield C(s)).find(({fileName:A})=>A.decodeText()==="inbridge-oif.json");if(!p)throw new F("Couldn't extract OIF attachment in the PDF");let f=p.fileSpec.lookup(R.of("EF"),G).lookup(R.of("F"),y),e=new TextDecoder().decode(d(f).decode());return l.parse(e)})}function C(r){return T(this,null,function*(){if(!r.catalog.has(R.of("Names")))return[];let s=r.catalog.lookup(R.of("Names"),G);if(!s.has(R.of("EmbeddedFiles")))return[];let a=s.lookup(R.of("EmbeddedFiles"),G);if(!a.has(R.of("Names")))return[];let p=a.lookup(R.of("Names"),u),f=[];for(let e=0,A=p.size();e<A;e+=2){let D=p.lookup(e),P=p.lookup(e+1,G);f.push({fileName:D,fileSpec:P})}return f})}var W=class{constructor(){this.paymentType="cash"}};var Z=class{constructor(){this.paymentType="credit_card"}};var J=class{constructor(){this.paymentType="sepa_mandate"}};var U=class{constructor(){this.paymentType="swift"}};var ii=(i=>(i.AF="AF",i.AL="AL",i.DZ="DZ",i.AS="AS",i.AD="AD",i.AO="AO",i.AI="AI",i.AQ="AQ",i.AG="AG",i.AR="AR",i.AM="AM",i.AW="AW",i.AU="AU",i.AT="AT",i.AZ="AZ",i.BS="BS",i.BH="BH",i.BD="BD",i.BB="BB",i.BY="BY",i.BE="BE",i.BZ="BZ",i.BJ="BJ",i.BM="BM",i.BT="BT",i.BO="BO",i.BQ="BQ",i.BA="BA",i.BW="BW",i.BV="BV",i.BR="BR",i.IO="IO",i.BN="BN",i.BG="BG",i.BF="BF",i.BI="BI",i.CV="CV",i.KH="KH",i.CM="CM",i.CA="CA",i.KY="KY",i.CF="CF",i.TD="TD",i.CL="CL",i.CN="CN",i.CX="CX",i.CC="CC",i.CO="CO",i.KM="KM",i.CD="CD",i.CG="CG",i.CK="CK",i.CR="CR",i.HR="HR",i.CU="CU",i.CW="CW",i.CY="CY",i.CZ="CZ",i.CI="CI",i.DK="DK",i.DJ="DJ",i.DM="DM",i.DO="DO",i.EC="EC",i.EG="EG",i.SV="SV",i.GQ="GQ",i.ER="ER",i.EE="EE",i.SZ="SZ",i.ET="ET",i.FK="FK",i.FO="FO",i.FJ="FJ",i.FI="FI",i.FR="FR",i.GF="GF",i.PF="PF",i.TF="TF",i.GA="GA",i.GM="GM",i.GE="GE",i.DE="DE",i.GH="GH",i.GI="GI",i.GR="GR",i.GL="GL",i.GD="GD",i.GP="GP",i.GU="GU",i.GT="GT",i.GG="GG",i.GN="GN",i.GW="GW",i.GY="GY",i.HT="HT",i.HM="HM",i.VA="VA",i.HN="HN",i.HK="HK",i.HU="HU",i.IS="IS",i.IN="IN",i.ID="ID",i.IR="IR",i.IQ="IQ",i.IE="IE",i.IM="IM",i.IL="IL",i.IT="IT",i.JM="JM",i.JP="JP",i.JE="JE",i.JO="JO",i.KZ="KZ",i.KE="KE",i.KI="KI",i.KP="KP",i.KR="KR",i.KW="KW",i.KG="KG",i.LA="LA",i.LV="LV",i.LB="LB",i.LS="LS",i.LR="LR",i.LY="LY",i.LI="LI",i.LT="LT",i.LU="LU",i.MO="MO",i.MG="MG",i.MW="MW",i.MY="MY",i.MV="MV",i.ML="ML",i.MT="MT",i.MH="MH",i.MQ="MQ",i.MR="MR",i.MU="MU",i.YT="YT",i.MX="MX",i.FM="FM",i.MD="MD",i.MC="MC",i.MN="MN",i.ME="ME",i.MS="MS",i.MA="MA",i.MZ="MZ",i.MM="MM",i.NA="NA",i.NR="NR",i.NP="NP",i.NL="NL",i.NC="NC",i.NZ="NZ",i.NI="NI",i.NE="NE",i.NG="NG",i.NU="NU",i.NF="NF",i.MP="MP",i.NO="NO",i.OM="OM",i.PK="PK",i.PW="PW",i.PS="PS",i.PA="PA",i.PG="PG",i.PY="PY",i.PE="PE",i.PH="PH",i.PN="PN",i.PL="PL",i.PT="PT",i.PR="PR",i.QA="QA",i.MK="MK",i.RO="RO",i.RU="RU",i.RW="RW",i.RE="RE",i.BL="BL",i.SH="SH",i.KN="KN",i.LC="LC",i.MF="MF",i.PM="PM",i.VC="VC",i.WS="WS",i.SM="SM",i.ST="ST",i.SA="SA",i.SN="SN",i.RS="RS",i.SC="SC",i.SL="SL",i.SG="SG",i.SX="SX",i.SK="SK",i.SI="SI",i.SB="SB",i.SO="SO",i.ZA="ZA",i.GS="GS",i.SS="SS",i.ES="ES",i.LK="LK",i.SD="SD",i.SR="SR",i.SJ="SJ",i.SE="SE",i.CH="CH",i.SY="SY",i.TW="TW",i.TJ="TJ",i.TZ="TZ",i.TH="TH",i.TL="TL",i.TG="TG",i.TK="TK",i.TO="TO",i.TT="TT",i.TN="TN",i.TR="TR",i.TM="TM",i.TC="TC",i.TV="TV",i.UG="UG",i.UA="UA",i.AE="AE",i.GB="GB",i.UM="UM",i.US="US",i.UY="UY",i.UZ="UZ",i.VU="VU",i.VE="VE",i.VN="VN",i.VG="VG",i.VI="VI",i.WF="WF",i.EH="EH",i.YE="YE",i.ZM="ZM",i.ZW="ZW",i.AX="AX",i))(ii||{});var ai=(p=>(p.ONE_OFF="one_off",p.RECURRING="recurring",p.B2B="b2b",p))(ai||{});export{E as BaseInvoice,L as CancellationInvoice,W as CashPayment,ii as CountryCode,Z as CreditCardPayment,H as Currency,B as DefaultInvoice,K as DiscountType,F as FileNotFoundException,h as InvalidOIFException,o as InvoiceItemUnit,M as InvoiceItemWithDiscount,N as InvoiceItemWithoutDiscount,I as InvoiceType,x as PartialInvoice,S as PaymentInformationType,O as RecurringInvoice,J as SEPAMandatePayment,ai as SEPAMandateType,c as SEPAPayment,U as SWIFTPayment,l as SchemaProvider,Y as SchemaVersions,fa as addOIFToPdf,ea as getOIFFromPdf,V as getPaymentInformationConstructorByType};