meta-cloud-api
Version:
TypeScript wrapper for Meta's Cloud API
4 lines (3 loc) • 13.5 kB
JavaScript
import {v,g as g$1}from'../../../chunk-KMAH6636.js';var u=class{contact={};setName(t){if(!t||typeof t!="object")throw new Error("Name must be an object");return this.contact.name=t,this}setSimpleName(t,e){if(!t||typeof t!="string")throw new Error("First name must be a non-empty string");let r={formatted_name:e?`${t} ${e}`:t,first_name:t};return e&&(r.last_name=e),this.setName(r)}setFullName(t,e,r,s,i){if(!t||typeof t!="string")throw new Error("First name must be a non-empty string");let c={formatted_name:[i,t,r,e,s].filter(Boolean).join(" "),first_name:t};return e&&(c.last_name=e),r&&(c.middle_name=r),s&&(c.suffix=s),i&&(c.prefix=i),this.setName(c)}addPhone(t,e,r){if(!t||typeof t!="string")throw new Error("Phone number must be a non-empty string");let s={phone:"PHONE_NUMBER"};return e&&(s.type=e),r&&(s.wa_id=r),this.contact.phones||(this.contact.phones=[]),this.contact.phones.push(s),this}addEmail(t,e){if(!t||typeof t!="string")throw new Error("Email must be a non-empty string");if(!t.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/))throw new Error("Invalid email format");let r={email:t};return e&&(r.type=e),this.contact.emails||(this.contact.emails=[]),this.contact.emails.push(r),this}addAddress(t){if(!t||typeof t!="object")throw new Error("Address must be an object");return this.contact.addresses||(this.contact.addresses=[]),this.contact.addresses.push(t),this}addSimpleAddress(t,e,r,s){let i={street:t,city:e,country:r};return s&&(i.type=s),this.addAddress(i)}setOrganization(t,e,r){let s={};return t&&(s.company=t),e&&(s.department=e),r&&(s.title=r),this.contact.org=s,this}addUrl(t,e){if(!t||typeof t!="string")throw new Error("URL must be a non-empty string");let r={url:t};return e&&(r.type=e),this.contact.urls||(this.contact.urls=[]),this.contact.urls.push(r),this}setBirthday(t,e,r){let s=e.toString().padStart(2,"0"),i=r.toString().padStart(2,"0");return this.contact.birthday=`${t}-${s}-${i}`,this}build(){if(!this.contact.name)throw new Error("Contact name is required");return this.contact}reset(){return this.contact={},this}},E=class{static createSimpleContact(t,e){return new u().setSimpleName(t).addPhone(e).build()}static createBusinessContact(t,e,r,s,i,o){return new u().setSimpleName(t,e).setOrganization(r,void 0,s).addPhone(i,"WORK").addEmail(o,"WORK").build()}static createPersonalContact(t,e,r,s,i){let o=new u().setSimpleName(t,e).addPhone(r,"MOBILE");return s&&o.addEmail(s,"HOME"),i&&o.setBirthday(i.year,i.month,i.day),o.build()}};function l(n){return !("name"in n)}var d=class{message={};setType(t){return this.message.type=t,this}setBody(t){return this.message.body={text:t},this}setFooter(t){return this.message.footer={text:t},this}setTextHeader(t){return this.message.header={type:"text",text:t},this}setImageHeader(t){return this.message.header={type:"image",image:{link:t}},this}addReplyButtons(t){this.message.action||(this.message.action={});let e=this.message.action;return l(e)&&(e.buttons=t.map(r=>({type:"reply",reply:{id:r.id,title:r.title}}))),this}addListSections(t){this.message.action||(this.message.action={});let e=this.message.action;return l(e)&&(e.sections=t.map(r=>({title:r.title,rows:r.rows}))),this}setListButtonText(t){this.message.action||(this.message.action={});let e=this.message.action;return l(e)&&(e.button=t),this}setCTAUrl(t,e){return this.message.action={name:"cta_url",parameters:{display_text:t,url:e}},this}build(){if(!this.message.type)throw new Error("Interactive message type is required");if(!this.message.body)throw new Error("Interactive message body is required");if(!this.message.action)throw new Error("Interactive message action is required");return this.message}reset(){return this.message={},this}},O=class{static createButtonMessage(t,e,r){let s=new d().setType("button").setBody(t).addReplyButtons(e);return r?.headerText&&s.setTextHeader(r.headerText),r?.footerText&&s.setFooter(r.footerText),s.build()}static createListMessage(t,e,r,s){let i=new d().setType("list").setBody(t).addListSections(r);s?.headerText&&i.setTextHeader(s.headerText),s?.footerText&&i.setFooter(s.footerText);let o=i.build(),c=o.action;return l(c)&&(c.button=e),o}static createCTAUrlMessage(t,e,r,s){let i=new d().setType("cta_url").setBody(t).setCTAUrl(e,r);return s?.headerText&&i.setTextHeader(s.headerText),s?.footerText&&i.setFooter(s.footerText),i.build()}};var g=class{location={};setLatitude(t){if(typeof t!="number"||t<-90||t>90)throw new Error("Latitude must be a number between -90 and 90");return this.location.latitude=t,this}setLongitude(t){if(typeof t!="number"||t<-180||t>180)throw new Error("Longitude must be a number between -180 and 180");return this.location.longitude=t,this}setCoordinates(t,e){return this.setLatitude(t).setLongitude(e)}setName(t){if(t&&typeof t!="string")throw new Error("Location name must be a string");return this.location.name=t,this}setAddress(t){if(t&&typeof t!="string")throw new Error("Location address must be a string");return this.location.address=t,this}build(){if(this.location.latitude===void 0)throw new Error("Latitude is required");if(this.location.longitude===void 0)throw new Error("Longitude is required");return this.location}reset(){return this.location={},this}},x=class{static createSimpleLocation(t,e){return new g().setCoordinates(t,e).build()}static createNamedLocation(t,e,r,s){return new g().setCoordinates(t,e).setName(r).setAddress(s).build()}static createBusinessLocation(t,e,r,s){return new g().setCoordinates(t,e).setName(r).setAddress(s).build()}};var m=class{media={};setId(t){if(!t||typeof t!="string")throw new Error("Media ID must be a non-empty string");return this.media.id=t,this}setLink(t){if(!t||typeof t!="string")throw new Error("Media URL must be a non-empty string");if(!t.match(/^https?:\/\//))throw new Error("Media URL must be a valid HTTP/HTTPS URL");return this.media.link=t,this}setCaption(t){if(t&&typeof t!="string")throw new Error("Caption must be a string");return this.media.caption=t,this}build(){if(!this.media.id&&!this.media.link)throw new Error("Either media ID or link is required");return this.media}reset(){return this.media={},this}},b=class extends m{},y=class extends m{},f=class extends m{setCaption(t){throw new Error("Audio messages do not support captions")}},w=class extends m{setFilename(t){if(!t||typeof t!="string")throw new Error("Filename must be a non-empty string");return this.media.filename=t,this}},j=class extends m{setCaption(t){throw new Error("Sticker messages do not support captions")}},T=class{static createImageWithCaption(t,e,r=false){let s=new b;return r?s.setId(t):s.setLink(t),s.setCaption(e).build()}static createVideoWithCaption(t,e,r=false){let s=new y;return r?s.setId(t):s.setLink(t),s.setCaption(e).build()}static createDocument(t,e,r,s=false){let i=new w;return s?i.setId(t):i.setLink(t),i.setFilename(e),r&&i.setCaption(r),i.build()}static createAudio(t,e=false){let r=new f;return e?r.setId(t):r.setLink(t),r.build()}static createSticker(t,e=false){let r=new j;return e?r.setId(t):r.setLink(t),r.build()}};var a=class{reaction={};setMessageId(t){if(!t||typeof t!="string")throw new Error("Message ID must be a non-empty string");return this.reaction.message_id=t,this}setEmoji(t){if(!t||typeof t!="string")throw new Error("Emoji must be a non-empty string");if(t.length>4)throw new Error("Please provide a single emoji character");return this.reaction.emoji=t,this}removeReaction(){return this.reaction.emoji="",this}build(){if(!this.reaction.message_id)throw new Error("Message ID is required");if(this.reaction.emoji===void 0)throw new Error("Emoji is required (use empty string to remove reaction)");return this.reaction}reset(){return this.reaction={},this}},A=class{static createLike(t){return new a().setMessageId(t).setEmoji("\u{1F44D}").build()}static createHeart(t){return new a().setMessageId(t).setEmoji("\u2764\uFE0F").build()}static createLaugh(t){return new a().setMessageId(t).setEmoji("\u{1F602}").build()}static createWow(t){return new a().setMessageId(t).setEmoji("\u{1F62E}").build()}static createSad(t){return new a().setMessageId(t).setEmoji("\u{1F622}").build()}static createAngry(t){return new a().setMessageId(t).setEmoji("\u{1F620}").build()}static removeReaction(t){return new a().setMessageId(t).removeReaction().build()}};function P(n){return Object.values(v).includes(n)}function _(n){return Object.values(g$1).includes(n)}var p=class{template={components:[]};setName(t){if(!t||t.trim().length===0)throw new Error("Template name cannot be empty");return this.template.name=t,this}setLanguage(t){if(!P(t))throw new Error(`Invalid language code: ${t}. Must be a valid LanguagesEnum value.`);return this.template.language={policy:"deterministic",code:t},this}addTextHeader(t){if(!Array.isArray(t))throw new Error("Parameters must be an array");let e={type:"HEADER",parameters:t.map(r=>{if(typeof r!="string")throw new Error("All parameters must be strings");return {type:"TEXT",text:r}})};return this.template.components||(this.template.components=[]),this.template.components.push(e),this}addImageHeader(t){if(!t||typeof t!="string")throw new Error("Image URL must be a non-empty string");let e={type:"HEADER",parameters:[{type:"IMAGE",image:{link:t}}]};return this.template.components||(this.template.components=[]),this.template.components.push(e),this}addBody(t){if(!Array.isArray(t))throw new Error("Parameters must be an array");let e={type:"BODY",parameters:t.map(r=>{if(typeof r!="string")throw new Error("All parameters must be strings");return {type:"TEXT",text:r}})};return this.template.components||(this.template.components=[]),this.template.components.push(e),this}addFooter(){let t={type:"FOOTER"};return this.template.components||(this.template.components=[]),this.template.components.push(t),this}addButtons(t){if(!Array.isArray(t))throw new Error("Buttons must be an array");return t.forEach(e=>{if(typeof e.index!="number"||!_(e.index))throw new Error(`Button index must be a valid ButtonPositionEnum value: ${e.index}`);let r=e.type==="quick_reply"?"QUICK_REPLY":"URL",s={type:"BUTTON",sub_type:r,index:e.index};if(e.parameters&&e.parameters.length>0){if(!Array.isArray(e.parameters))throw new Error("Button parameters must be an array");e.type==="quick_reply"?s.parameters=e.parameters.map(i=>{if(typeof i!="string")throw new Error("All button parameters must be strings");return {type:"PAYLOAD",payload:i}}):e.type==="url"&&(s.parameters=e.parameters.map(i=>{if(typeof i!="string")throw new Error("All button parameters must be strings");return {type:"TEXT",text:i}}));}this.template.components||(this.template.components=[]),this.template.components.push(s);}),this}addCurrencyParameter(t,e,r){if(!t||typeof t!="string")throw new Error("Fallback value must be a non-empty string");if(!e||typeof e!="string")throw new Error("Currency code must be a non-empty string");if(typeof r!="number"||r<0)throw new Error("Amount must be a non-negative number");return {type:"CURRENCY",currency:{fallback_value:t,code:e,amount_1000:r*1e3}}}addDateTimeParameter(t,e){if(!t||typeof t!="string")throw new Error("Fallback value must be a non-empty string");if(typeof e!="number"||e<=0)throw new Error("Timestamp must be a positive number");return {type:"DATE_TIME",date_time:{fallback_value:t,timestamp:e}}}build(){if(!this.template.name)throw new Error("Template name is required");if(!this.template.language)throw new Error("Template language is required");return this.template}reset(){return this.template={components:[]},this}},C=class{static createSimpleTextTemplate(t,e,r){if(!t||!e||!Array.isArray(r))throw new Error("Invalid parameters for simple text template");return new p().setName(t).setLanguage(e).addBody(r).build()}static createCompleteTemplate(t,e,r){if(!t||!e||!r.bodyParameters)throw new Error("Invalid parameters for complete template");let s=new p().setName(t).setLanguage(e).addBody(r.bodyParameters);return r.headerParameters?s.addTextHeader(r.headerParameters):r.headerImage&&s.addImageHeader(r.headerImage),r.buttons&&s.addButtons(r.buttons),s.build()}static createOrderConfirmationTemplate(t,e,r,s,i){if(!t||!e||!r||typeof s!="number"||!i)throw new Error("Invalid parameters for order confirmation template");return new p().setName("order_confirmation").setLanguage(t).addBody([e,r,s.toString(),i]).addButtons([{index:0,type:"quick_reply"},{index:1,type:"url",parameters:[r]}]).build()}};var h=class{textMessage={};setBody(t){if(!t||typeof t!="string")throw new Error("Message body must be a non-empty string");return this.textMessage.body=t,this}setPreviewUrl(t=true){return this.textMessage.preview_url=t,this}build(){if(!this.textMessage.body)throw new Error("Message body is required");return this.textMessage}reset(){return this.textMessage={},this}},L=class{static createSimpleText(t){return new h().setBody(t).build()}static createTextWithPreview(t){return new h().setBody(t).setPreviewUrl(true).build()}static createNotification(t,e){return new h().setBody(`*${t}*
${e}`).build()}};export{f as AudioMessageBuilder,u as ContactMessageBuilder,E as ContactMessageFactory,w as DocumentMessageBuilder,b as ImageMessageBuilder,d as InteractiveMessageBuilder,O as InteractiveMessageFactory,g as LocationMessageBuilder,x as LocationMessageFactory,T as MediaMessageFactory,a as ReactionMessageBuilder,A as ReactionMessageFactory,j as StickerMessageBuilder,p as TemplateMessageBuilder,C as TemplateMessageFactory,h as TextMessageBuilder,L as TextMessageFactory,y as VideoMessageBuilder};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map