UNPKG

veend-sdk

Version:

a nodejs package for veend products

2 lines (1 loc) 9.59 kB
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}Object.defineProperty(exports,"__esModule",{value:!0});var e=t(require("axios"));function i(t,e,i,n){return new(i||(i=Promise))((function(o,r){function s(t){try{l(n.next(t))}catch(t){r(t)}}function a(t){try{l(n.throw(t))}catch(t){r(t)}}function l(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,a)}l((n=n.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class n{constructor(t=!1,i,n,o="VeendHQ SDK",r="VeendHQ SDK"){this.xTag=null;const[s,a]=n.split("_");if(!s||!a)throw new Error("Invalid clientId format. Expected 'userId_accountId'.");this.isLive=t||!1,this.userId=s,this.accountId=a,this.xTag=o,this.giroDevBaseUrl="https://test.veendhq.com",this.giroProdBaseUrl="https://tx.veendhq.com",this.lendingDevBaseUrl="https://api2.veendhq.com",this.lendingProdBaseUrl="https://api.veendhq.com",this.giroClient=e.default.create({baseURL:t?this.giroProdBaseUrl:this.giroDevBaseUrl,headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","x-tag":this.xTag}}),this.lendingClient=e.default.create({baseURL:t?this.lendingProdBaseUrl:this.lendingDevBaseUrl,headers:{"x-jwt":r,"Content-Type":"application/json","x-tag":this.xTag}})}get(t,e,n){return i(this,void 0,void 0,(function*(){try{return("giro"===t?yield this.giroClient.get(e,n):yield this.lendingClient.get(e,n)).data}catch(t){this.handleError(t)}}))}post(){return i(this,arguments,void 0,(function*(t="giro",e,i,n){try{return("giro"===t?yield this.giroClient.post(e,i,n):yield this.lendingClient.post(e,i,n)).data}catch(t){this.handleError(t)}}))}patch(t,e,n,o){return i(this,void 0,void 0,(function*(){try{return("giro"===t?yield this.giroClient.patch(e,n,o):yield this.lendingClient.patch(e,n,o)).data}catch(t){this.handleError(t)}}))}handleError(t){if(t.response)throw new Error(`API Error: ${t.response.status} - ${t.response.data.message||t.message}`);throw new Error(`Network Error: ${t.message}`)}buildQuery(t){let e="?";if(i=t,0===Object.keys(i).length&&i.constructor===Object)return"";var i;for(const i in(t=>{const e=Object.assign({},t);for(let t in e)Object.prototype.hasOwnProperty.call(e,t)&&(e[t]||delete e[t]);return e})(t))e+=encodeURIComponent(i)+"="+encodeURIComponent(t[i])+"&";return e.replace(/&$/g,"")}}const o=t=>`/accounts/${t}/loansubscription`,r=t=>`/accounts/${t}/loansubscription`,s="/v1/loanproducts",a=t=>`/v1/users/${t}/loansubscriptions`;class l{constructor(t,e,i){this.apiClient=t,this.account=e,this.user=i}loanProducts(){return i(this,void 0,void 0,(function*(){try{let t=this.apiClient.buildQuery({accountId:this.account});const e=yield this.apiClient.get("giro",s+t);if(e&&e.data){let t=e.data;return t=t.filter((t=>t.isProductActive)),{status:"success",data:t}}return{status:"error",data:[]}}catch(t){throw new Error(t.message)}}))}createLoan(t){return i(this,void 0,void 0,(function*(){var e;let i=this.apiClient.buildQuery({accountId:this.account});try{const s={title:t.title,webhookUrl:t.callbackUrl,tenure:t.tenure,withdrawalFrequency:t.withdrawalFrequency},a=yield this.apiClient.post("giro",o(this.account)+i,s);if(a&&a.data){const t=a.data;yield(n=2500,new Promise((t=>setTimeout(t,n))));const o=yield this.apiClient.get("giro",r(t.account._id)+i),s=null===(e=o.data.accountNumbers)||void 0===e?void 0:e.find((t=>t.isWemaVas));return{data:{funding:{accountNumber:(null==s?void 0:s.accountNumber)||"",accountName:(null==s?void 0:s.accountName)||"",bankName:(null==s?void 0:s.bankName)||""},_id:o.data._id}||null,status:"success"}}return{data:null,status:"error"}}catch(t){throw new Error(t.message)}var n}))}getLoan(t){return i(this,void 0,void 0,(function*(){var e;let i=this.apiClient.buildQuery({accountId:this.account});try{const n=yield this.apiClient.get("giro",r(t)+i),o=null===(e=n.data.accountNumbers)||void 0===e?void 0:e.find((t=>t.isWemaVas));return Object.assign(Object.assign({},n.data),{funding:{accountNumber:(null==o?void 0:o.accountNumber)||"",accountName:(null==o?void 0:o.accountName)||"",bankName:(null==o?void 0:o.bankName)||""}})}catch(t){throw new Error(t.message)}}))}getLoans(){return i(this,void 0,void 0,(function*(){try{let t=this.apiClient.buildQuery({accountId:this.account});return(yield this.apiClient.get("giro",a(this.user)+t)).data}catch(t){throw new Error(t.message)}}))}}const c={lendLoan:"/internal/settings",loanStatistics:"/loans",loans:"/findloans",loanDetail:"/findLoans/{loanId}",loanRepaymentSchedule:"/loan/{loanId}",customers:"/prime/customers",switchPrimeRapaymentToBusinessAccount:"/settings?tab=switchPrimeRepaymentAccount",isBusinessFeature:"/settings?tab=switchBusinessFeature",lendingAccountHistory:"/v2/payments",loanRepaymentHistory:"/v2/payments",lendingAccountStatement:"/v2/payments",businessAccountStatement:"/v2/payments"};class u{constructor(t,e,i){this.apiClient=t,this.account=e}lendLoan(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account});return yield this.apiClient.patch("lending",c.lendLoan+e,t)}catch(t){throw new Error("Failed to lend loan: "+t.message)}}))}getLoanStatistics(){return i(this,void 0,void 0,(function*(){try{return yield this.apiClient.get("lending",c.loanStatistics)}catch(t){throw new Error("Failed to get loan statistics: "+t.message)}}))}getLoans(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort,limit:(null==t?void 0:t.limit)||10});return yield this.apiClient.get("lending",c.loans+e)}catch(t){throw new Error("Failed to get loans: "+t.message)}}))}getLoanDetail(t,e){return i(this,void 0,void 0,(function*(){try{let i=this.apiClient.buildQuery({accountId:this.account,populate:null==e?void 0:e.populate});const n=c.loanDetail.replace("{loanId}",t);return yield this.apiClient.get("lending",n+i)}catch(t){throw new Error("Failed to get loan details: "+t.message)}}))}getLoanRepaymentSchedule(t,e){return i(this,void 0,void 0,(function*(){try{let i=this.apiClient.buildQuery({accountId:this.account,populate:null==e?void 0:e.populate});const n=c.loanRepaymentSchedule.replace("{loanId}",t);return yield this.apiClient.get("lending",n+i)}catch(t){throw new Error("Failed to get loan repayment schedule: "+t.message)}}))}getCustomers(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,limit:(null==t?void 0:t.limit)||10,page:null==t?void 0:t.page,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort});return yield this.apiClient.get("lending",c.customers+e)}catch(t){throw new Error("Failed to get customers: "+t.message)}}))}SwitchPrimeRapaymentToBusinessAccount(t){return i(this,void 0,void 0,(function*(){try{const e={enabled:t};return yield this.apiClient.patch("lending",c.switchPrimeRapaymentToBusinessAccount,e)}catch(t){throw new Error(`Failed to toggle business feature: ${t.message}`)}}))}updateIsBusinessFeature(t){return i(this,void 0,void 0,(function*(){try{const e={enabled:t};return yield this.apiClient.patch("lending",c.isBusinessFeature,e)}catch(t){throw new Error(`Failed to toggle business feature: ${t.message}`)}}))}getLendingAccountHistory(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,account:null==t?void 0:t.account,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort,limit:(null==t?void 0:t.limit)||10});return yield this.apiClient.get("giro",c.lendingAccountHistory+e)}catch(t){throw new Error("Failed to get lending account history: "+t.message)}}))}getLoanRepaymentHistory(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,account:null==t?void 0:t.account,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort,limit:(null==t?void 0:t.limit)||10,repayment:null==t?void 0:t.repayment,onlender:null==t?void 0:t.onlender});return yield this.apiClient.get("giro",c.loanRepaymentHistory+e)}catch(t){throw new Error("Failed to get loan repayment history: "+t.message)}}))}getLendingAccountStatement(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,account:null==t?void 0:t.account,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort,limit:(null==t?void 0:t.limit)||10});return yield this.apiClient.get("giro",c.lendingAccountStatement+e)}catch(t){throw new Error("Failed to get lending account statement: "+t.message)}}))}getBusinessAccountStatement(t){return i(this,void 0,void 0,(function*(){try{let e=this.apiClient.buildQuery({accountId:this.account,account:null==t?void 0:t.account,populate:null==t?void 0:t.populate,desc:null==t?void 0:t.desc,sort:null==t?void 0:t.sort,limit:null==t?void 0:t.limit});return yield this.apiClient.get("giro",c.businessAccountStatement+e)}catch(t){throw new Error("Failed to get business account history: "+t.message)}}))}}exports.Veend=class{validateClient(t="_"){const[e,i]=t.split("_");if(!e||!i)throw new Error("Invalid clientId format. Expected 'userId_accountId'.");return[e,i]}constructor(t){const[e,i]=this.validateClient(t.clientId);this.userId=e,this.account=i,this.apiClient=new n("production"===t.env,t.developerToken,t.clientId,t.xtag||"VeendHQ SDK",t.xJwt||"VeendHQ SDK"),this.onlending=new l(this.apiClient,this.account,this.userId),this.prime=new u(this.apiClient,this.account,this.userId)}};