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

View file

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

View file

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