UNPKG

alise-api

Version:

A wrapper around Alise internal API.

1 lines 6.52 kB
"use strict";var e=class{constructor(e){this.baseURL=e}async sendRequest(e){const{method:t,path:a,body:s,headers:n}=e,i=`${this.baseURL}/${a}`,r=await fetch(i,{method:t,body:s?new URLSearchParams(s).toString():void 0,headers:{"Content-Type":"application/x-www-form-urlencoded",...n,"User-Agent":"@raphckrman/alise-api"},redirect:"manual"});if(r.headers.get("Content-Type")?.includes("image")){const e=await r.blob();return{cookies:r.headers.get("Set-Cookie")?.split(";")??[],data:e}}const o=await r.text();return{cookies:r.headers.get("Set-Cookie")?.split(";")??[],data:o}}async get(e,t){return this.sendRequest({method:"GET",path:e,headers:t})}async post(e,t,a){return this.sendRequest({method:"POST",path:e,body:t,headers:a?.headers})}async put(e,t,a){return this.sendRequest({method:"PUT",path:e,body:t,headers:a?.headers})}async delete(e,t,a){const s=new URLSearchParams(t).toString(),n=s?`${e}?${s}`:e;return this.sendRequest({method:"DELETE",path:n,headers:a?.headers})}},t=class{constructor(e,t,a,s,n,i,r,o,l){this.establishment=e,this.firstName=t,this.lastName=a,this.email=s,this.phoneNumber=n,this.faxNumber=i,this.address=r,this.balance=o,this.estimatedAt=l}},a=class{constructor(e,t,a){this.date=e,this.lunch=t,this.dinner=a}};function s(e,t,a){const s=t.replaceAll(/[$()*+.?[\\\]^{|}]/g,"\\$&"),n=a.replaceAll(/[$()*+.?[\\\]^{|}]/g,"\\$&"),i=new RegExp(`${s}([\\s\\S]*?)(?=${n}|$)`,"g"),r=[];let o;for(;null!==(o=i.exec(e));)r.push(o[1]);return r}var n=new e("https://webparent.paiementdp.com"),i=async e=>{const{data:a}=await n.get("aliInformations.php",{Cookie:`PHPSESSID=${e}`}),i=s(a,"<nobr>","<nobr> </h1>")[0],r=s(a,'id="rsp_prenom" value="','"')[0],o=s(a,'id="rsp_nom" value="','"')[0],l=s(a,'id="rsp_email" value="','"')[0],d=s(a,'id="rsp_tel2" value="','"')[0],c=s(a,'id="rsp_tel1" value="','"')[0],p=s(a,'id="rsp_adr1" value="','"')[0]+""+s(a,'id="textfield2" value="','"')[0]+" "+s(a,'id="rsp_cp" value="','"')[0]+" "+s(a,'id="rsp_ville" value="','"')[0],h=parseFloat(s(a,"<b>"," &#128; </b>")[0].replace(",",".")),[u,m,g]=s(a,"<time>","</time>")[0].split("/").map(Number),w=new Date(g,m-1,u);return new t(i,r,o,l,d,c,p,h,w)},r=async e=>{const{data:t}=await n.get("aliConsultLog.php",{Cookie:`PHPSESSID=${e}`});return s(t,"<tr class='ligneTableau' ><td>","</tr>").map((e=>{const[t,a,n,i,r]=s(e,"<time>","</time>")[0].split(/[\s/:]/).map(Number),o=new Date(n,a-1,t,i,r);return{label:s(e,"<td>","</td>")[0],date:o}}))},o=async e=>{const{data:t}=await n.get("aliOperationsFin.php",{Cookie:`PHPSESSID=${e}`});return s(t,"<tr class='detail'>","<tr class='detail'>").map((e=>{const[t,a,n]=s(e,"<td class='detail_date'>","</td>")[0].split("/").map(Number),i=new Date(2e3+n,a-1,t),r=s(e,"<td class='detail_data'>","</td>")[0],o=s(e,"<td class='detail_debit_montant'>","</td>")[0]?.replace(",",".")??"0.0",l=s(e,"<td class='detail_credit_montant'>","</td>")[0]?.replace(",",".")??"0.0";return{label:r,date:i,amount:(isNaN(parseFloat(l))||""===l.trim()?0:parseFloat(l))- -(isNaN(parseFloat(o))||""===o.trim()?0:parseFloat(o))}})).sort(((e,t)=>new Date(e.date).getTime()-new Date(t.date).getTime()))},l=async e=>{const{data:t}=await n.get("aliReservation.php",{Cookie:`PHPSESSID=${e}`});return s(t,'<table width="100%" cellpadding=2 cellspacing=0 border=0><tr><td align=center>','" w').map((t=>{if(0===s(t+'"','<td id="','"').length)return null;const[a,n,i]=s(t+'"','<td id="','"')[0].split("-").map(Number),r=new Date(Date.UTC(a,n-1,i-1)),o=s(t,"?date=",' "')[0],l=s(t,'<a href="',' "?date').some((e=>e.includes("aliReservationCancel.php")));return new h(e,o??null,l,!!o,r)})).filter((e=>null!==e))},d=async(e,t,a=1,s=!1)=>(await n.get(s?(e=>"aliReservationCancel.php?date="+e)(t):(e=>"aliReservationDetail.php?date="+e)(t),{Cookie:`PHPSESSID=${e}`}),console.log(s),await n.post(s?"aliReservationCancel.php":"aliReservationDetail.php",s?"ref=cancel&btnOK.x=0&btnOK.y=0&valide_form=1":"CONS_QUANTITE="+a.toString()+"&restaurant=1&btnOK.x=0&btnOK.y=0&valide_form=1",{headers:{Cookie:`PHPSESSID=${e}`,"Content-Type":"application/x-www-form-urlencoded"}}),!0),c=async e=>{const{data:t}=await n.get("aliGenQrCode.php",{Cookie:`PHPSESSID=${e}`});return t},p=async e=>{const{data:t}=await n.get("aliMenu.php",{Cookie:`PHPSESSID=${e}`}),i=s(t,'<tr class="table_header">',"</tr>")[0],r=["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"],o=s(i,"<strong>","</strong>").map((e=>{const[t,a,s,n]=e.replaceAll("<br>"," ").trim().split(" ");return new Date(Date.UTC(parseInt(n,10),r.indexOf(s),parseInt(a,10)))})),l=s(t,"<tr>","</tr>"),d=e=>s(e,'<td width="10%" style="text-align: center; padding-top: 1em;">',"</td>").map((e=>e.split(/<br\s*\/?>/i).map((e=>e.replaceAll(/<\/?[^>]+(>|$)/g,"").trim())).filter((e=>0!==e.length)))),c=d(l[3]),p=d(l[4]);return o.map(((e,t)=>new a(e,c[t]??[],p[t]??[])))},h=class{constructor(e,t,a,s,n){this.token=e,this.identifier=t??null,this.booked=a,this.canBook=s,this.date=n}async book(e=1){if(!this.identifier)throw new Error("Cannot book this day: missing identifier.");if(!this.canBook)throw new Error("Booking is not allowed for this day.");if(e<=0)throw new Error("Quantity must be greater than zero.");return await d(this.token,this.identifier,e,this.booked),this.booked=!this.booked,this}},u=class{constructor(e,t){this.credentials=e,this.account=t}async bookDay(e,t=1,a=!1){return await d(this.credentials.token,e,t,a),new h(this.credentials.token,e,!a,!0)}async getBarcode(){return c(this.credentials.token)}async getBookings(){return l(this.credentials.token)}async getConnectionsHistory(){return r(this.credentials.token)}async getFinancialHistory(){return o(this.credentials.token)}async getInformations(){return i(this.credentials.token)}async getWeeklyMenu(){return p(this.credentials.token)}},m=new e("https://webparent.paiementdp.com");exports.Account=t,exports.BookingDay=h,exports.Client=u,exports.authenticateWithCredentials=async(e,t,a,s=!0,n=!1)=>{const{cookies:r}=await m.post((e=>"aliAuthentification.php?site="+e)(a),{txtLogin:e,txtMdp:t,chkKeepSession:s?"1":"0"}),o=/PHPSESSID=([\dA-Za-z]+)/g,l=[];let d;for(;null!==(d=o.exec(r.toString()));)l.push(d[1]);const c=l[l.length-1];return new u({token:c,username:s?e:null,password:s?t:null},n?void 0:await i(c))},exports.findBetween=s,exports.getAccountInformations=i,exports.getBarcode=c,exports.getBookings=l,exports.getConnectionsHistory=r,exports.getFinancialHistory=o,exports.getWeeklyMenu=p,exports.updateBook=d;//# sourceMappingURL=index.js.map