refactor!: improve Mail type and simplify parsing

This commit is contained in:
Julien Oculi 2024-03-29 15:58:19 +01:00
parent fdac58e5b9
commit 3eb207d18c
3 changed files with 19 additions and 22 deletions

View file

@ -1,8 +1,7 @@
import { Command } from '@cliffy/command/mod.ts'
import { format } from '../src/format.ts'
import { Contact } from '../src/contact.ts'
// import { send } from '../src/send.ts'
import { send } from '../src/transporter.ts'
import { send } from '../src/send.ts'
import { Mail } from '../types.ts'
export const cmd = new Command()
.name('send')
@ -17,13 +16,10 @@ export const cmd = new Command()
collect: true,
})
.option('-a, --attachments <file:file>', 'Attachments.', { collect: true })
.option('--content-type <type:string>', 'Mail body Content-Type.', {
default: 'text/html; charset=utf-8',
})
//.option('-t, --template <name:template>', 'HTML template from config', { default: 'basic' })
//.option('-t, --template <name:template>', 'HTML template from config', { default: 'message' })
.arguments('<to:string> <subject:string> <body:string>')
.action(
async ({ from, cc, cci, attachments, contentType }, to, subject, body) => {
async ({ from, cc, cci, attachments }, to, subject, body) => {
const fromContact: Contact = await (async () => {
if (from === '!me') {
const whoami = new Deno.Command('whoami', {
@ -45,14 +41,17 @@ export const cmd = new Command()
return Contact.fromString(from)
})()
const toContact = Contact.fromString(to)
const mail = format(fromContact, toContact, subject, body, {
cc: cc?.map(Contact.fromString) ?? [],
cci: cci?.map(Contact.fromString) ?? [],
attachments: attachments ?? [],
contentType,
})
const mail: Mail = {
from: fromContact,
to: [Contact.fromString(to)],
subject,
body,
options: {
cc: cc?.map(Contact.fromString) ?? [],
cci: cci?.map(Contact.fromString) ?? [],
attachments: attachments ?? [],
},
}
await send(mail)
},

View file

@ -3,7 +3,7 @@ import { Contact } from './contact.ts'
export function format(
from: Contact,
to: Contact | Contact[],
to: Contact[],
subject: string,
body: string,
options: Partial<Options>,
@ -13,11 +13,10 @@ export function format(
cc: [],
cci: [],
attachments: [],
contentType: 'text/html; charset=utf-8',
}
return {
to: Array.isArray(to) ? to : [to],
to,
subject,
body,
options: { ...defaultOptions, ...options },

View file

@ -2,16 +2,15 @@ import type { JSX } from 'preact'
import { Contact } from './src/contact.ts'
export type Options = {
from: Contact
cc: Contact[]
cci: Contact[]
attachments: string[]
contentType: string
}
export type Mail = {
from: Contact
to: Contact[]
subject: string
body: string | JSX.Element
body: JSX.Element
options: Options
}