UNPKG

meta-cloud-api

Version:
3 lines 6.32 kB
import {a as a$3}from'./chunk-3MO3BTBC.js';import {a as a$1}from'./chunk-R36L2XYO.js';import {a as a$2}from'./chunk-GT3XQN7Y.js';import {b as b$4}from'./chunk-O2OTHNWM.js';import {b as b$2}from'./chunk-7NQLQPLH.js';import {b as b$3}from'./chunk-UQPD6GIF.js';import {b as b$6}from'./chunk-5JGM556C.js';import {b}from'./chunk-2NJVBJUN.js';import {b as b$5}from'./chunk-XPPDOFBN.js';import {b as b$7}from'./chunk-KFPOVYEJ.js';import {c}from'./chunk-LGLRGXCE.js';import {b as b$1}from'./chunk-GIQLIMOD.js';import {a}from'./chunk-VW3LGHTJ.js';import {z}from'./chunk-4O3T25CT.js';import {Agent}from'https';var D="UTILS",M=false,W=new a(D,process.env.DEBUG==="true"||M),U=3e3,k=30,q=2e4,v=s=>{if(!process.env.CLOUD_API_ACCESS_TOKEN&&!s?.accessToken)throw W.log("Environmental variable: CLOUD_API_ACCESS_TOKEN and/or access token argument is undefined."),new Error("Missing WhatsApp access token.")},N=s=>{v(s);let e={M4D_APP_ID:s?.appId||process.env.M4D_APP_ID||"",M4D_APP_SECRET:s?.appSecret||process.env.M4D_APP_SECRET||"",WA_PHONE_NUMBER_ID:s?.phoneNumberId||Number(process.env.WA_PHONE_NUMBER_ID),WA_BUSINESS_ACCOUNT_ID:s?.businessAcctId||process.env.WA_BUSINESS_ACCOUNT_ID||"",CLOUD_API_VERSION:s?.apiVersion||process.env.CLOUD_API_VERSION||"",CLOUD_API_ACCESS_TOKEN:s?.accessToken||process.env.CLOUD_API_ACCESS_TOKEN||"",WEBHOOK_ENDPOINT:s?.webhookEndpoint||process.env.WEBHOOK_ENDPOINT||"",WEBHOOK_VERIFICATION_TOKEN:s?.webhookVerificationToken||process.env.WEBHOOK_VERIFICATION_TOKEN||"",LISTENER_PORT:s?.listenerPort||parseInt(process.env.LISTENER_PORT||"")||U,MAX_RETRIES_AFTER_WAIT:s?.maxRetriesAfterWait||parseInt(process.env.MAX_RETRIES_AFTER_WAIT||"")||k,REQUEST_TIMEOUT:s?.requestTimeout||parseInt(process.env.REQUEST_TIMEOUT||"")||q,DEBUG:s?.debug||process.env.DEBUG==="true",FLOW_API_PRIVATE_PEM:s?.privatePem||process.env.FLOW_API_PRIVATE_PEM||"",FLOW_API_PASSPHRASE:s?.passphrase||process.env.FLOW_API_PASSPHRASE||""};return W.log(`Configuration loaded for App Id ${e.M4D_APP_ID}`),e};var B="HttpsClient",$=false,L=new a(B,process.env.DEBUG==="true"||$),u=class{agent;constructor(){this.agent=new Agent({keepAlive:true});}clearSockets(){return this.agent.destroy(),true}async sendRequest(e,r,t,o,i,m){let a=`https://${e}/${r}`,g=new AbortController,n=setTimeout(()=>g.abort(),i);try{let p=await fetch(a,{method:t,headers:o,body:m,signal:g.signal});return L.log(`${t} : ${a} - ${JSON.stringify(p)}`),clearTimeout(n),new A(p)}catch(p){throw L.error(`${t} : ${a} - ${JSON.stringify(p)}`),p}}},A=class{res;respStatusCode;respHeaders;constructor(e){this.res=e,this.respStatusCode=e.status,this.respHeaders=Object.fromEntries(e.headers.entries());}statusCode(){return this.respStatusCode}headers(){return this.respHeaders}rawResponse(){return this.res}async json(){try{return await this.res.json()}catch(e){throw new Error("Failed to parse response body to JSON: "+e.message)}}};var G="REQUESTER",K=false,F=new a(G,process.env.DEBUG==="true"||K),d=class{client;accessToken;phoneNumberId;businessAcctId;apiVersion;userAgent;host;protocol="https:";constructor(e,r,t,o,i){this.client=new u,this.host="graph.facebook.com",this.apiVersion=e||"22",this.phoneNumberId=r,this.accessToken=t,this.businessAcctId=o,this.userAgent=i;}buildHeader(e,r){let t={Authorization:`Bearer ${this.accessToken}`,"User-Agent":this.userAgent};return e!=="multipart/form-data"&&(t["Content-Type"]=e),r&&Object.assign(t,r),t}buildCAPIPath(e){return `v${this.apiVersion}.0/${e}`}async sendRequest(e,r,t,o,i="application/json",m){let a=i;o instanceof FormData?a="multipart/form-data":typeof o=="string"&&o.startsWith("<?xml")&&(a="application/xml");let g=`${this.protocol.toLowerCase()}//${this.host}/${this.buildCAPIPath(r)}`;F.log(`${e} : ${g} (${a})`);try{let n=await this.client.sendRequest(this.host,this.buildCAPIPath(r),e,this.buildHeader(a,m),t,e==="POST"||e==="PUT"?o:void 0);if(!n.rawResponse().ok){let p=await n.json();throw a$3(p)?p:{name:"MetaError",message:"Unknown error occurred",error:{message:p.message||"Unknown error occurred",type:"UnknownError",code:n.statusCode(),fbtrace_id:""}}}return n}catch(n){throw a$3(n)?n:{name:"MetaError",message:n instanceof Error?n.message:"Network error occurred",error:{message:n instanceof Error?n.message:"Network error occurred",type:"NetworkError",code:500,fbtrace_id:""}}}}async getJson(e,r,t,o,i){return await(await this.sendRequest(e,r,t,o,"application/json",i)).json()}async sendFormData(e,r,t,o,i){return await(await this.sendRequest(e,r,t,o,"multipart/form-data",i)).json()}async sendUrlEncodedForm(e,r,t,o,i){let m=new URLSearchParams(o).toString();return await(await this.sendRequest(e,r,t,m,"application/x-www-form-urlencoded",i)).json()}};var V="WHATSAPP",x=false,h=new a(V,process.env.DEBUG==="true"||x),j="WA_SDK",l=class{config;requester;messages;templates;phoneNumber;qrCode;encryption;twoStepVerification;registration;media;waba;flow;businessProfile;static Enums=z;constructor(e){this.config=N(e),this.requester=new d(this.config.CLOUD_API_VERSION,this.config.WA_PHONE_NUMBER_ID,this.config.CLOUD_API_ACCESS_TOKEN,this.config.WA_BUSINESS_ACCOUNT_ID,this.userAgent()),this.messages=new b(this.config,this.requester),this.templates=new a$1(this.config,this.requester),this.phoneNumber=new b$1(this.config,this.requester),this.qrCode=new a$2(this.config,this.requester),this.encryption=new b$2(this.config,this.requester),this.twoStepVerification=new b$3(this.config,this.requester),this.registration=new b$4(this.config,this.requester),this.media=new b$5(this.config,this.requester),this.waba=new b$6(this.config,this.requester),this.flow=new c(this.config,this.requester),this.businessProfile=new b$7(this.config,this.requester),h.log("WhatsApp Node.js SDK instantiated!");}userAgent(){return `${j}/0.0.1 (Node.js ${process.version})`}updateTimeout(e){return this.config.REQUEST_TIMEOUT=e,h.log(`Updated request timeout to ${e}ms`),true}updatePhoneNumberId(e){return this.config.WA_PHONE_NUMBER_ID=e,h.log(`Updated sender phone number id to ${e}`),true}updateAccessToken(e){return this.config.CLOUD_API_ACCESS_TOKEN=e,h.log("Updated access token"),true}updateWabaId(e){return this.config.WA_BUSINESS_ACCOUNT_ID=e,h.log(`Updated business account id to ${e}`),true}}; export{N as a,l as b};//# sourceMappingURL=chunk-5O5GLWIE.js.map //# sourceMappingURL=chunk-5O5GLWIE.js.map