From 3eb207d18cfc0385a64a102784fca3b5f689e259 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Fri, 29 Mar 2024 15:58:19 +0100 Subject: [PATCH] refactor!: improve `Mail` type and simplify parsing --- cli/send.ts | 31 +++++++++++++++---------------- src/format.ts | 5 ++--- types.ts | 5 ++--- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/cli/send.ts b/cli/send.ts index cbef06b..4a4bf68 100644 --- a/cli/send.ts +++ b/cli/send.ts @@ -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 ', 'Attachments.', { collect: true }) - .option('--content-type ', 'Mail body Content-Type.', { - default: 'text/html; charset=utf-8', - }) - //.option('-t, --template ', 'HTML template from config', { default: 'basic' }) + //.option('-t, --template ', 'HTML template from config', { default: 'message' }) .arguments(' ') .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) }, diff --git a/src/format.ts b/src/format.ts index 8d64046..339b5de 100644 --- a/src/format.ts +++ b/src/format.ts @@ -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, @@ -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 }, diff --git a/types.ts b/types.ts index ac6075e..89dad7a 100644 --- a/types.ts +++ b/types.ts @@ -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 }