@directus-labs/resend-operation
Version:
Resend API operation for Directus Flows. Send emails, update domains, and more.
2 lines (1 loc) • 19 kB
JavaScript
import{defineOperationApp as e}from"@directus/extensions-sdk";import{defineComponent as t,ref as n,computed as a,watch as i,resolveComponent as o,openBlock as s,createBlock as d}from"vue";const c={label:"Emails",icon:"mail",actions:{send:{label:"Send Email",description:"Send an email to a single recipient or multiple recipients.",icon:"send",method:"POST",path:"/emails",options:[{field:"from",name:"From",type:"string",meta:{width:"full",interface:"input",options:{placeholder:"Acme <sender@example.com>"},note:'Sender email address. To include a friendly name, use the format "Your Name <sender@domain.com>".',required:!0}},{field:"to",name:"To",type:"json",meta:{width:"full",interface:"tags",options:{placeholder:"Enter email address and press Enter to add."},note:"Recipient email address. For multiple addresses, enter each address separately. Max 50.",required:!0}},{field:"cc",name:"CC",type:"json",meta:{width:"half",interface:"tags",options:{placeholder:"Enter email address and press Enter to add."},note:"CC recipient email address. For multiple addresses, enter each address separately."}},{field:"bcc",name:"BCC",type:"json",meta:{width:"half",interface:"tags",options:{placeholder:"Enter email address and press Enter to add."},note:"BCC recipient email address. For multiple addresses, enter each address separately."}},{field:"subject",name:"Subject",type:"string",meta:{width:"full",interface:"input",note:"Email subject.",required:!0}},{field:"html",name:"HTML Content",type:"text",meta:{width:"full",interface:"input-code",options:{language:"html"},note:"The HTML version of the message."}},{field:"text",name:"Plain Text Content",type:"text",meta:{width:"full",interface:"input-multiline",note:"The plain text version of the message."}},{field:"scheduledAt",name:"Scheduled At",type:"timestamp",meta:{width:"full",interface:"input",note:"Schedule email to be sent later. The date should be in ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z)."}},{field:"replyTo",name:"Reply To",type:"json",meta:{width:"full",interface:"tags",options:{placeholder:"Enter email address and press Enter to add."},note:"Reply-to email address. For multiple addresses, enter each address separately."}},{field:"headers",name:"Headers",type:"json",meta:{width:"full",interface:"input-code",options:{language:"json"},note:"Custom headers to add to the email. Enter as a JSON object."}},{field:"attachments",name:"Attachments",type:"json",meta:{width:"full",interface:"input-code",options:{language:"json"},note:"Filename and content of attachments (max 40mb per email). Enter as a JSON array of objects with filename, content, path, and content_type properties."}},{field:"tags",name:"Tags",type:"json",meta:{width:"full",interface:"input-code",options:{language:"json"},note:"Custom data passed in key/value pairs. Enter as a JSON array of objects with name and value properties."}}],handler:async(e,t)=>e.fetchRequest("/emails","POST",{from:t.from,to:t.to,subject:t.subject,html:t.html,text:t.text,cc:t.cc,bcc:t.bcc,reply_to:t.replyTo,scheduled_at:t.scheduledAt,headers:t.headers,attachments:t.attachments,tags:t.tags})},sendBatch:{label:"Send Batch Email",description:"Send up to 100 emails in a single API call.",icon:"send",method:"POST",path:"/emails/batch",options:[{field:"batchEmails",name:"Batch Emails",type:"json",meta:{width:"full",interface:"input-code",options:{language:"json"},note:"An array of email objects. Each object should contain from, to, subject, and either html or text fields. The cc, bcc, reply_to, and headers fields are optional. Attachments, tags, and scheduled_at are not supported for batch emails."}}],handler:async(e,t)=>{const{batchEmails:n}=t;return e.fetchRequest("/emails/batch","POST",n.map((e=>({from:e.from,to:e.to,subject:e.subject,html:e.html,text:e.text,cc:e.cc,bcc:e.bcc,reply_to:e.replyTo,headers:e.headers}))))}},retrieve:{label:"Retrieve Email",description:"Retrieve an email by ID.",icon:"search",method:"GET",path:"/emails/:email_id",options:[{field:"emailId",name:"Email ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the email to retrieve.",required:!0}}],handler:async(e,t)=>{const{emailId:n}=t;return e.fetchRequest(`/emails/${n}`,"GET")}},update:{label:"Update Email",description:"Update an email by ID.",icon:"edit",method:"PATCH",path:"/emails/:email_id",options:[{field:"emailId",name:"Email ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the email to update.",required:!0}},{field:"scheduledAt",name:"Scheduled At",type:"timestamp",meta:{width:"full",interface:"datetime",note:"Schedule email to be sent later. The date should be in ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z)."}}],handler:async(e,t)=>{const{emailId:n,scheduledAt:a}=t;return e.fetchRequest(`/emails/${n}`,"PATCH",{scheduled_at:a})}},cancel:{label:"Cancel Email",description:"Cancel an email by ID.",icon:"cancel",method:"POST",path:"/emails/:email_id/cancel",options:[{field:"emailId",name:"Email ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the email to cancel.",required:!0}}],handler:async(e,t)=>{const{emailId:n}=t;return e.fetchRequest(`/emails/${n}/cancel`,"POST")}}}},l={label:"Domains",icon:"domain",actions:{retrieve:{label:"Retrieve Domain",description:"Retrieve a single domain for the authenticated user.",icon:"search",method:"GET",path:"/domains/:id",options:[{field:"id",name:"Domain ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the domain you want to retrieve.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/domains/${n}`,"GET")}},verify:{label:"Verify Domain",description:"Verify an existing domain.",icon:"check_circle",method:"POST",path:"/domains/:id/verify",options:[{field:"id",name:"Domain ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the domain you want to verify.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/domains/${n}/verify`,"POST")}},update:{label:"Update Domain",description:"Update an existing domain.",icon:"edit",method:"PATCH",path:"/domains/:id",options:[{field:"id",name:"Domain ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the domain you want to update.",required:!0}},{field:"clickTracking",name:"Click Tracking",type:"boolean",meta:{width:"half",interface:"boolean",note:"Track clicks within the body of each HTML email."}},{field:"openTracking",name:"Open Tracking",type:"boolean",meta:{width:"half",interface:"boolean",note:"Track the open rate of each email."}},{field:"tls",name:"TLS",type:"string",meta:{width:"full",interface:"select-dropdown",options:{choices:[{text:"Opportunistic",value:"opportunistic"},{text:"Enforced",value:"enforced"}]},note:'Choose TLS setting. Default is "opportunistic".'}}],handler:async(e,t)=>{const{id:n,clickTracking:a,openTracking:i,tls:o}=t;return e.fetchRequest(`/domains/${n}`,"PATCH",{click_tracking:a,open_tracking:i,tls:o})}},list:{label:"List Domains",description:"Retrieve a list of domains for the authenticated user.",icon:"list",method:"GET",path:"/domains",options:[],handler:async e=>e.fetchRequest("/domains","GET")},delete:{label:"Delete Domain",description:"Remove an existing domain.",icon:"delete",method:"DELETE",path:"/domains/:id",options:[{field:"id",name:"Domain ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the domain you want to delete.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/domains/${n}`,"DELETE")}}}},r={label:"API Keys",icon:"key",actions:{create:{label:"Create API Key",description:"Add a new API key to authenticate communications with Resend.",icon:"add",method:"POST",path:"/api-keys",options:[{field:"name",name:"API Key Name",type:"string",meta:{width:"full",interface:"input",note:"The name for the new API key.",required:!0}},{field:"permission",name:"Permission",type:"string",meta:{width:"full",interface:"select-dropdown",options:{choices:[{text:"Full Access",value:"full_access"},{text:"Sending Access",value:"sending_access"}]},note:"The level of access for the API key."}},{field:"domainId",name:"Domain ID",type:"string",meta:{width:"full",interface:"input",note:"Restrict the API key to send emails only from a specific domain. Only used when permission is set to sending_access."}}],handler:async(e,t)=>{const{name:n,permission:a,domainId:i}=t,o={name:n,permission:a,domain_id:i};return e.fetchRequest("/api-keys","POST",o)}},list:{label:"List API Keys",description:"Retrieve a list of API keys for the authenticated user.",icon:"list",method:"GET",path:"/api-keys",options:[],handler:async e=>e.fetchRequest("/api-keys","GET")},delete:{label:"Delete API Key",description:"Remove an existing API key.",icon:"delete",method:"DELETE",path:"/api-keys/:id",options:[{field:"id",name:"API Key ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the API key you want to delete.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/api-keys/${n}`,"DELETE")}}}},m={label:"Audiences",icon:"group",actions:{create:{label:"Create Audience",description:"Create a new audience.",icon:"add",method:"POST",path:"/audiences",options:[{field:"name",name:"Audience Name",type:"string",meta:{width:"full",interface:"input",note:"The name for the new audience.",required:!0}}],handler:async(e,t)=>{const{name:n}=t;return e.fetchRequest("/audiences","POST",{name:n})}},retrieve:{label:"Retrieve Audience",description:"Retrieve a single audience.",icon:"search",method:"GET",path:"/audiences/:id",options:[{field:"id",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience you want to retrieve.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/audiences/${n}`,"GET")}},delete:{label:"Delete Audience",description:"Remove an existing audience.",icon:"delete",method:"DELETE",path:"/audiences/:id",options:[{field:"id",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience you want to delete.",required:!0}}],handler:async(e,t)=>{const{id:n}=t;return e.fetchRequest(`/audiences/${n}`,"DELETE")}},list:{label:"List Audiences",description:"Retrieve a list of audiences.",icon:"list",method:"GET",path:"/audiences",options:[],handler:async e=>e.fetchRequest("/audiences","GET")}}},u={label:"Contacts",icon:"person",actions:{create:{label:"Create Contact",description:"Create a contact inside an audience.",icon:"add",method:"POST",path:"/audiences/:audienceId/contacts",options:[{field:"email",name:"Email",type:"string",meta:{width:"full",interface:"input",note:"The email address of the contact.",required:!0}},{field:"audienceId",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience to add the contact to.",required:!0}},{field:"firstName",name:"First Name",type:"string",meta:{width:"half",interface:"input",note:"The first name of the contact."}},{field:"lastName",name:"Last Name",type:"string",meta:{width:"half",interface:"input",note:"The last name of the contact."}},{field:"unsubscribed",name:"Unsubscribed",type:"boolean",meta:{width:"full",interface:"boolean",note:"The subscription status of the contact."}}],handler:async(e,t)=>{const{audienceId:n,email:a,firstName:i,lastName:o,unsubscribed:s}=t;return e.fetchRequest(`/audiences/${n}/contacts`,"POST",{email:a,first_name:i,last_name:o,unsubscribed:s})}},retrieve:{label:"Retrieve Contact",description:"Retrieve a single contact from an audience.",icon:"search",method:"GET",path:"/audiences/:audienceId/contacts/:id",options:[{field:"audienceId",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience the contact belongs to.",required:!0}},{field:"id",name:"Contact ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the contact you want to retrieve.",required:!0}}],handler:async(e,t)=>{const{audienceId:n,id:a}=t;return e.fetchRequest(`/audiences/${n}/contacts/${a}`,"GET")}},update:{label:"Update Contact",description:"Update an existing contact.",icon:"edit",method:"PATCH",path:"/audiences/:audienceId/contacts/:id",options:[{field:"audienceId",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience the contact belongs to.",required:!0}},{field:"id",name:"Contact ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the contact you want to update.",required:!0}},{field:"firstName",name:"First Name",type:"string",meta:{width:"half",interface:"input",note:"The updated first name of the contact."}},{field:"lastName",name:"Last Name",type:"string",meta:{width:"half",interface:"input",note:"The updated last name of the contact."}},{field:"unsubscribed",name:"Unsubscribed",type:"boolean",meta:{width:"full",interface:"boolean",note:"The updated subscription status of the contact."}}],handler:async(e,t)=>{const{audienceId:n,id:a,firstName:i,lastName:o,unsubscribed:s}=t;return e.fetchRequest(`/audiences/${n}/contacts/${a}`,"PATCH",{first_name:i,last_name:o,unsubscribed:s})}},delete:{label:"Delete Contact",description:"Remove an existing contact from an audience.",icon:"delete",method:"DELETE",path:"/audiences/:audienceId/contacts/:id",options:[{field:"audienceId",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience the contact belongs to.",required:!0}},{field:"id",name:"Contact ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the contact you want to delete."}},{field:"email",name:"Contact Email",type:"string",meta:{width:"full",interface:"input",note:"The email of the contact you want to delete."}}],handler:async(e,t)=>{const{audienceId:n,id:a,email:i}=t,o=a?`/audiences/${n}/contacts/${a}`:`/audiences/${n}/contacts?email=${encodeURIComponent(i)}`;return e.fetchRequest(o,"DELETE")}},list:{label:"List Contacts",description:"Show all contacts from an audience.",icon:"list",method:"GET",path:"/audiences/:audienceId/contacts",options:[{field:"audienceId",name:"Audience ID",type:"string",meta:{width:"full",interface:"input",note:"The ID of the audience to list contacts from.",required:!0}}],handler:async(e,t)=>{const{audienceId:n}=t;return e.fetchRequest(`/audiences/${n}/contacts`,"GET")}}}};var h=e({id:"resend-operation",name:"Resend",icon:"mail",description:"Use the Resend API to send an email",overview:({endpoint:e,action:t})=>[{label:"Endpoint",text:e},{label:"Action",text:t}],options:((e,t)=>{const n=e.__vccOpts||e;for(const[e,a]of t)n[e]=a;return n})(t({__name:"options",props:{value:{default:()=>({apiKey:"",endpoint:"",action:""})}},emits:["input"],setup(e,{emit:t}){const h={emails:c,domains:l,apiKeys:r,audiences:m,contacts:u},p=e,f=t,y=n(p.value),T=a((()=>y.value.endpoint||"")),g=a((()=>y.value.action||"")),b=[{field:"info",type:"alias",meta:{width:"full",interface:"presentation-notice",options:{icon:!1,text:'<svg style="vertical-align:middle" xmlns="http://www.w3.org/2000/svg" width="112" height="37" fill="none" viewBox="0 0 1500 489"><path fill="var(--theme--primary)" d="M25.41 393.747h63.522v-99.519h53.36l49.971 99.519h70.299l-58.441-114.341c30.914-14.822 50.818-45.313 50.818-83.427 0-60.135-43.619-98.673-108.836-98.673H25.409v296.441ZM141.02 150.242c30.068 0 49.125 17.787 49.125 45.737 0 27.527-19.057 45.313-49.125 45.313H88.932v-91.05h52.089ZM388.86 396.288c58.018 0 95.708-33.879 102.484-76.227h-58.865c-5.081 20.327-22.021 30.914-46.16 30.914-30.067 0-49.971-18.633-51.242-49.548v-2.964h157.961c1.27-6.776 1.694-13.975 1.694-20.328-.847-63.946-44.89-104.601-110.107-104.601-67.334 0-111.8 44.043-111.8 111.801 0 67.334 43.619 110.953 116.035 110.953Zm-52.512-136.786C339.736 233.669 359.216 218 385.049 218c27.103 0 45.313 14.822 49.548 41.502h-98.249Zm179.198 61.829c2.541 46.584 43.619 74.957 104.177 74.957 58.442 0 99.096-27.526 99.096-70.722 0-49.125-41.501-60.559-91.473-65.641-31.338-3.811-49.971-5.505-49.971-22.021 0-13.975 15.245-22.445 38.537-22.445 24.139 0 40.655 10.587 42.349 27.95h57.17c-2.964-44.466-43.195-70.298-101.636-70.298-55.901-.424-93.591 27.95-93.591 71.145 0 44.89 39.384 56.324 90.203 62.253 35.149 4.658 49.971 5.929 49.971 23.715 0 15.246-15.245 23.292-40.231 23.292-29.221 0-45.737-13.128-47.854-32.185h-56.747Zm342.293 74.957c58.018 0 95.708-33.879 102.484-76.227h-58.865c-5.082 20.327-22.021 30.914-46.16 30.914-30.068 0-49.972-18.633-51.242-49.548v-2.964h157.961c1.27-6.776 1.694-13.975 1.694-20.328-.847-63.946-44.89-104.601-110.107-104.601-67.334 0-111.801 44.043-111.801 111.801 0 67.334 43.62 110.953 116.036 110.953Zm-52.513-136.786C808.714 233.669 828.195 218 854.027 218c27.104 0 45.314 14.822 49.548 41.502h-98.249Zm315.944-85.968c-33.45 0-53.36 12.705-67.33 29.644l-5.51-27.103h-52.088v217.672h59.708V282.37c0-37.69 17.79-59.711 48.7-59.711 30.07 0 44.05 19.48 44.05 56.323v114.765h59.71V273.054c0-74.534-40.23-99.52-87.24-99.52Zm282 29.644c-13.98-17.786-35.58-29.644-65.64-29.644-58.02 0-100.79 43.619-100.79 110.954 0 69.028 42.77 111.8 101.21 111.8 31.34 0 52.51-13.975 66.91-32.608l5.93 30.067h52.09V97.307h-59.71v105.871Zm-52.09 144.409c-32.61 0-53.36-25.409-53.36-62.676s20.75-62.676 53.36-62.676 52.93 25.833 52.93 63.1c0 36.843-20.32 62.252-52.93 62.252Z"/></svg><span style="padding-left: 16px">Email API for developers. Learn more and get started at <a href="https://resend.com?ref=directus_marketplace" target="_blank">resend.com</a>.</span>'}}},{field:"apiKey",name:"API Key",type:"string",meta:{interface:"input",note:"The API key for your Resend account. You can find this in the Resend dashboard under [Account > API Keys.](https://resend.com/api-keys)",options:{masked:!0}}},{field:"endpoint",name:"Endpoint",type:"string",meta:{width:"half",interface:"select-dropdown",note:"The endpoint to call. Check the [Resend API documentation](https://resend.com/docs/api-reference) for more information.",options:{choices:[{text:"Emails",value:"emails",icon:"mail"},{text:"Domains",value:"domains",icon:"domain"},{text:"API Keys",value:"apiKeys",icon:"key"},{text:"Audiences",value:"audiences",icon:"group"},{text:"Contacts",value:"contacts",icon:"person"}]}}},{field:"action",name:"Action",type:"string",meta:{width:"half",note:"The action to perform via the API.",interface:"select-dropdown",options:{choices:a((()=>{const e=h[T.value];return e?Object.entries(e.actions).map((([e,t])=>({text:t.label,value:e,icon:t.icon}))):[]}))}}}],I=a((()=>{var e;if(!T.value||!g.value)return[];const t=h[T.value];return t&&(null==(e=t.actions[g.value])?void 0:e.options)||[]})),v=a((()=>[...b,...I.value]));return i(y,(e=>{console.log("Form values changed:",e),f("input",e)}),{deep:!0}),i((()=>p.value),(e=>{null!==e&&JSON.stringify(e)!==JSON.stringify(y.value)&&(y.value={...e})}),{deep:!0}),(e,t)=>{const n=o("v-form");return s(),d(n,{modelValue:y.value,"onUpdate:modelValue":t[0]||(t[0]=e=>y.value=e),fields:v.value,"primary-key":"+"},null,8,["modelValue","fields"])}}}),[["__file","options.vue"]])});export{h as default};